001/*
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "ApplicationLoader.java".  Description: 
010"A utility for loading Application bindings from configuration files." 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132004.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025*/
026
027package ca.uhn.hl7v2.protocol.impl;
028
029import java.io.BufferedReader;
030import java.io.IOException;
031import java.io.InputStreamReader;
032import java.net.URL;
033import java.util.NoSuchElementException;
034import java.util.StringTokenizer;
035
036import ca.uhn.hl7v2.HL7Exception;
037import ca.uhn.hl7v2.app.Application;
038import ca.uhn.hl7v2.model.Message;
039import ca.uhn.hl7v2.protocol.ApplicationRouter;
040import ca.uhn.hl7v2.protocol.ReceivingApplication;
041
042/**
043 * A utility for loading <code>Application</code> and <code>ReceivingApplication</code> bindings
044 * from configuration files.  
045 *  
046 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
047 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
048 */
049public class ApplicationLoader {
050
051    /**
052     * <p>A convenience method for binding applications to an <code>ApplicationRouter</code>
053     * Information about bindings is read from a file at a specified URL.  Each line in the 
054     * file should have the following format (entries TAB delimited):</p>
055     * 
056     * <p>message_type &#009; trigger_event &#009; processing_id &#009; version_id &#009; app_class</p>
057     * 
058     * <p>Note that the first four fields can be the wildcard "*", which means any.</p>
059     * 
060     * <p>For example, if you write an Application called org.yourorganiztion.ADTProcessor
061     * that processes several types of ADT messages, and another called 
062     * org.yourorganization.ResultProcessor that processes result messages, you might have a 
063     * file that looks like this: </p>
064     * 
065     * <p>ADT &#009; * &#009; * &#009; * &#009; org.yourorganization.ADTProcessor<br>
066     * ORU &#009; R01 &#009; * &#009; * &#009; org.yourorganization.ResultProcessor</p>
067     * 
068     * <p>Each class listed in this file must implement either ca.uhn.hl7v2.app.Application or 
069     * ca.uhn.hl7v2.protocol.ReceivingApplication, and must have a zero-argument constructor.</p>
070     * 
071     * @param theRouter the <code>ApplicationRouter</code> on which to make the binding
072     * @param theSource a URL pointing to the bindings file 
073     */
074    public static void loadApplications(ApplicationRouter theRouter, URL theSource)
075        throws IOException, HL7Exception, ClassNotFoundException, InstantiationException, IllegalAccessException {
076        
077        if (theSource == null) {
078            throw new HL7Exception("Can't load application bindings: the given URL is null");
079        }
080        
081        BufferedReader in = new BufferedReader(new InputStreamReader(theSource.openStream()));
082        String line;
083        while ((line = in.readLine()) != null) {
084            //parse application registration information 
085            StringTokenizer tok = new StringTokenizer(line, "\t", false);
086            String type, event, procId, verId, className;
087
088            if (tok.hasMoreTokens()) { //skip blank lines 
089                try {
090                    type = tok.nextToken();
091                    event = tok.nextToken();
092                    procId = tok.nextToken();
093                    verId = tok.nextToken();
094                    className = tok.nextToken();
095                }
096                catch (NoSuchElementException ne) {
097                    throw new HL7Exception(
098                        "Can't register applications from "
099                            + theSource.toExternalForm()
100                            + ". The line '"
101                            + line
102                            + "' is not of the form: message_type [tab] trigger_event " 
103                            + "[tab] processing ID [tab] version ID [tab] application_class. "
104                            + "*** NOTE TWO NEW FIELDS AS OF HAPI 0.5 ****. ");
105                }
106
107                Class<?> appClass = Class.forName(className); //may throw ClassNotFoundException 
108                Object appObject = appClass.newInstance();
109                ReceivingApplication<? extends Message> app;
110                if (appObject instanceof ReceivingApplication) {
111                    app = (ReceivingApplication<? extends Message>) appObject;
112                } else if (appObject instanceof Application) {
113                    app = new AppWrapper((Application) appObject);
114                } else {
115                    throw new HL7Exception(
116                            "The specified class, " + appClass.getName() + 
117                            ", doesn't implement Application or ReceivingApplication.");   
118                }
119
120                ApplicationRouter.AppRoutingData rd  
121                    = new AppRoutingDataImpl(type, event, procId, verId);                    
122                theRouter.bindApplication(rd, app);
123            }
124        }
125    }    
126}