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.protocol.ApplicationRouter;
39  import ca.uhn.hl7v2.protocol.ReceivingApplication;
40  
41  /**
42   * A utility for loading <code>Application</code> and <code>ReceivingApplication</code> bindings
43   * from configuration files.  
44   *  
45   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
46   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
47   */
48  public class ApplicationLoader {
49  
50      /**
51       * <p>A convenience method for binding applications to an <code>ApplicationRouter</code>
52       * Information about bindings is read from a file at a specified URL.  Each line in the 
53       * file should have the following format (entries TAB delimited):</p>
54       * 
55       * <p>message_type &#009; trigger_event &#009; processing_id &#009; version_id &#009; app_class</p>
56       * 
57       * <p>Note that the first four fields can be the wildcard "*", which means any.</p>
58       * 
59       * <p>For example, if you write an Application called org.yourorganiztion.ADTProcessor
60       * that processes several types of ADT messages, and another called 
61       * org.yourorganization.ResultProcessor that processes result messages, you might have a 
62       * file that looks like this: </p>
63       * 
64       * <p>ADT &#009; * &#009; * &#009; * &#009; org.yourorganization.ADTProcessor<br>
65       * ORU &#009; R01 &#009; * &#009; * &#009; org.yourorganization.ResultProcessor</p>
66       * 
67       * <p>Each class listed in this file must implement either ca.uhn.hl7v2.app.Application or 
68       * ca.uhn.hl7v2.protocol.ReceivingApplication, and must have a zero-argument constructor.</p>
69       * 
70       * @param theRouter the <code>ApplicationRouter</code> on which to make the binding
71       * @param theSource a URL pointing to the bindings file 
72       */
73      public static void loadApplications(ApplicationRouter theRouter, URL theSource)
74          throws IOException, HL7Exception, ClassNotFoundException, InstantiationException, IllegalAccessException {
75          
76          if (theSource == null) {
77              throw new HL7Exception("Can't load application bindings: the given URL is null");
78          }
79          
80          BufferedReader in = new BufferedReader(new InputStreamReader(theSource.openStream()));
81          String line = null;
82          while ((line = in.readLine()) != null) {
83              //parse application registration information 
84              StringTokenizer tok = new StringTokenizer(line, "\t", false);
85              String type = null, event = null, procId = null, verId = null, className = null;
86  
87              if (tok.hasMoreTokens()) { //skip blank lines 
88                  try {
89                      type = tok.nextToken();
90                      event = tok.nextToken();
91                      procId = tok.nextToken();
92                      verId = tok.nextToken();
93                      className = tok.nextToken();
94                  }
95                  catch (NoSuchElementException ne) {
96                      throw new HL7Exception(
97                          "Can't register applications from "
98                              + theSource.toExternalForm()
99                              + ". 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                 }
105 
106                 Class<?> appClass = Class.forName(className); //may throw ClassNotFoundException 
107                 Object appObject = appClass.newInstance();
108                 ReceivingApplication app = null;
109                 if (appObject instanceof ReceivingApplication) {
110                     app = (ReceivingApplication) appObject;
111                 } else if (appObject instanceof Application) {
112                     app = new AppWrapper((Application) appObject);
113                 } else {
114                     throw new HL7Exception(
115                             "The specified class, " + appClass.getName() + 
116                             ", doesn't implement Application or ReceivingApplication.");   
117                 }
118 
119                 ApplicationRouter.AppRoutingData rd  
120                     = new AppRoutingDataImpl(type, event, procId, verId);                    
121                 theRouter.bindApplication(rd, app);
122             }
123         }
124     }    
125 }