001 /*
002 The 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.
004 You may obtain a copy of the License at http://www.mozilla.org/MPL/
005 Software distributed under the License is distributed on an "AS IS" basis,
006 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007 specific language governing rights and limitations under the License.
008
009 The Original Code is "ApplicationLoader.java". Description:
010 "A utility for loading Application bindings from configuration files."
011
012 The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 2004. All Rights Reserved.
014
015 Contributor(s): ______________________________________.
016
017 Alternatively, the contents of this file may be used under the terms of the
018 GNU General Public License (the "GPL"), in which case the provisions of the GPL are
019 applicable instead of those above. If you wish to allow use of your version of this
020 file only under the terms of the GPL and not to allow others to use your version
021 of this file under the MPL, indicate your decision by deleting the provisions above
022 and replace them with the notice and other provisions required by the GPL License.
023 If you do not delete the provisions above, a recipient may use your version of
024 this file under either the MPL or the GPL.
025 */
026
027 package ca.uhn.hl7v2.protocol.impl;
028
029 import java.io.BufferedReader;
030 import java.io.IOException;
031 import java.io.InputStreamReader;
032 import java.net.URL;
033 import java.util.NoSuchElementException;
034 import java.util.StringTokenizer;
035
036 import ca.uhn.hl7v2.HL7Exception;
037 import ca.uhn.hl7v2.app.Application;
038 import ca.uhn.hl7v2.protocol.ApplicationRouter;
039 import 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 */
048 public 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 	 trigger_event 	 processing_id 	 version_id 	 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 	 * 	 * 	 * 	 org.yourorganization.ADTProcessor<br>
065 * ORU 	 R01 	 * 	 * 	 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 }