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.protocol.ApplicationRouter;
039import ca.uhn.hl7v2.protocol.ReceivingApplication;
040
041/**
042 * A utility for loading <code>Applicaiton</code> and <code>ReceivingApplication</code> bindings 
043 * from configuration files.  
044 *  
045 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
046 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
047 */
048public class ApplicationLoader {
049
050    /**
051     * <p>A convenience method for binding applications to an <code>ApplicationRouter</code>
052     * Information about bindings is read from a file at a specified URL.  Each line in the 
053     * file should have the following format (entries TAB delimited):</p>
054     * 
055     * <p>message_type &#009; trigger_event &#009; processing_id &#009; version_id &#009; app_class</p>
056     * 
057     * <p>Note that the first four fields can be the wildcard "*", which means any.</p>
058     * 
059     * <p>For example, if you write an Application called org.yourorganiztion.ADTProcessor
060     * that processes several types of ADT messages, and another called 
061     * org.yourorganization.ResultProcessor that processes result messages, you might have a 
062     * file that looks like this: </p>
063     * 
064     * <p>ADT &#009; * &#009; * &#009; * &#009; org.yourorganization.ADTProcessor<br>
065     * ORU &#009; R01 &#009; * &#009; * &#009; org.yourorganization.ResultProcessor</p>
066     * 
067     * <p>Each class listed in this file must implement either ca.uhn.hl7v2.app.Application or 
068     * ca.uhn.hl7v2.protocol.ReceivingApplication, and must have a zero-argument constructor.</p>
069     * 
070     * @param theRouter the <code>ApplicationRouter</code> on which to make the binding
071     * @param theSource a URL pointing to the bindings file 
072     */
073    public static void loadApplications(ApplicationRouter theRouter, URL theSource)
074        throws IOException, HL7Exception, ClassNotFoundException, InstantiationException, IllegalAccessException {
075        
076        if (theSource == null) {
077            throw new HL7Exception("Can't load application bindings: the given URL is null");
078        }
079        
080        BufferedReader in = new BufferedReader(new InputStreamReader(theSource.openStream()));
081        String line = null;
082        while ((line = in.readLine()) != null) {
083            //parse application registration information 
084            StringTokenizer tok = new StringTokenizer(line, "\t", false);
085            String type = null, event = null, procId = null, verId = null, className = null;
086
087            if (tok.hasMoreTokens()) { //skip blank lines 
088                try {
089                    type = tok.nextToken();
090                    event = tok.nextToken();
091                    procId = tok.nextToken();
092                    verId = tok.nextToken();
093                    className = tok.nextToken();
094                }
095                catch (NoSuchElementException ne) {
096                    throw new HL7Exception(
097                        "Can't register applications from "
098                            + theSource.toExternalForm()
099                            + ". The line '"
100                            + line
101                            + "' is not of the form: message_type [tab] trigger_event " 
102                            + "[tab] processing ID [tab] version ID [tab] application_class. "
103                            + "*** NOTE TWO NEW FIELDS AS OF HAPI 0.5 ****. ",
104                        HL7Exception.APPLICATION_INTERNAL_ERROR);
105                }
106
107                Class<?> appClass = Class.forName(className); //may throw ClassNotFoundException 
108                Object appObject = appClass.newInstance();
109                ReceivingApplication app = null;
110                if (appObject instanceof ReceivingApplication) {
111                    app = (ReceivingApplication) 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                            HL7Exception.APPLICATION_INTERNAL_ERROR);   
119                }
120
121                ApplicationRouter.AppRoutingData rd  
122                    = new AppRoutingDataImpl(type, event, procId, verId);                    
123                theRouter.bindApplication(rd, app);
124            }
125        }
126    }    
127}