View Javadoc
1   /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "ApplicationLoader.java".  Description: 
10  "A utility for loading Application bindings from configuration files." 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2004.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25  */
26  
27  package ca.uhn.hl7v2.protocol.impl;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.InputStreamReader;
32  import java.net.URL;
33  import java.util.NoSuchElementException;
34  import java.util.StringTokenizer;
35  
36  import ca.uhn.hl7v2.HL7Exception;
37  import ca.uhn.hl7v2.app.Application;
38  import ca.uhn.hl7v2.model.Message;
39  import ca.uhn.hl7v2.protocol.ApplicationRouter;
40  import ca.uhn.hl7v2.protocol.ReceivingApplication;
41  
42  /**
43   * A utility for loading <code>Application</code> and <code>ReceivingApplication</code> bindings
44   * from configuration files.  
45   *  
46   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
47   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
48   */
49  public class ApplicationLoader {
50  
51      /**
52       * <p>A convenience method for binding applications to an <code>ApplicationRouter</code>
53       * Information about bindings is read from a file at a specified URL.  Each line in the 
54       * file should have the following format (entries TAB delimited):</p>
55       * 
56       * <p>message_type &#009; trigger_event &#009; processing_id &#009; version_id &#009; app_class</p>
57       * 
58       * <p>Note that the first four fields can be the wildcard "*", which means any.</p>
59       * 
60       * <p>For example, if you write an Application called org.yourorganiztion.ADTProcessor
61       * that processes several types of ADT messages, and another called 
62       * org.yourorganization.ResultProcessor that processes result messages, you might have a 
63       * file that looks like this: </p>
64       * 
65       * <p>ADT &#009; * &#009; * &#009; * &#009; org.yourorganization.ADTProcessor<br>
66       * ORU &#009; R01 &#009; * &#009; * &#009; org.yourorganization.ResultProcessor</p>
67       * 
68       * <p>Each class listed in this file must implement either ca.uhn.hl7v2.app.Application or 
69       * ca.uhn.hl7v2.protocol.ReceivingApplication, and must have a zero-argument constructor.</p>
70       * 
71       * @param theRouter the <code>ApplicationRouter</code> on which to make the binding
72       * @param theSource a URL pointing to the bindings file 
73       */
74      public static void loadApplications(ApplicationRouter theRouter, URL theSource)
75          throws IOException, HL7Exception, ClassNotFoundException, InstantiationException, IllegalAccessException {
76          
77          if (theSource == null) {
78              throw new HL7Exception("Can't load application bindings: the given URL is null");
79          }
80          
81          BufferedReader in = new BufferedReader(new InputStreamReader(theSource.openStream()));
82          String line;
83          while ((line = in.readLine()) != null) {
84              //parse application registration information 
85              StringTokenizer tok = new StringTokenizer(line, "\t", false);
86              String type, event, procId, verId, className;
87  
88              if (tok.hasMoreTokens()) { //skip blank lines 
89                  try {
90                      type = tok.nextToken();
91                      event = tok.nextToken();
92                      procId = tok.nextToken();
93                      verId = tok.nextToken();
94                      className = tok.nextToken();
95                  }
96                  catch (NoSuchElementException ne) {
97                      throw new HL7Exception(
98                          "Can't register applications from "
99                              + 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 }