Coverage Report - ca.uhn.hl7v2.DefaultHapiContext
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultHapiContext
76%
107/139
73%
22/30
1.542
DefaultHapiContext$1
100%
3/3
N/A
1.542
 
 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 "DefaultHapiContext.java".  Description: 
 10  
 "Default implementation of HAPI configuration and factory" 
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 13  
 2001.  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  
 package ca.uhn.hl7v2;
 27  
 
 28  
 import ca.uhn.hl7v2.app.*;
 29  
 import ca.uhn.hl7v2.concurrent.DefaultExecutorService;
 30  
 import ca.uhn.hl7v2.conf.store.CodeStoreRegistry;
 31  
 import ca.uhn.hl7v2.conf.store.DefaultCodeStoreRegistry;
 32  
 import ca.uhn.hl7v2.conf.store.ProfileStore;
 33  
 import ca.uhn.hl7v2.conf.store.ProfileStoreFactory;
 34  
 import ca.uhn.hl7v2.llp.LowerLayerProtocol;
 35  
 import ca.uhn.hl7v2.llp.MinLowerLayerProtocol;
 36  
 import ca.uhn.hl7v2.model.AbstractMessage;
 37  
 import ca.uhn.hl7v2.model.Message;
 38  
 import ca.uhn.hl7v2.parser.*;
 39  
 import ca.uhn.hl7v2.util.ReflectionUtil;
 40  
 import ca.uhn.hl7v2.util.SocketFactory;
 41  
 import ca.uhn.hl7v2.util.StandardSocketFactory;
 42  
 import ca.uhn.hl7v2.validation.*;
 43  
 import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
 44  
 import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
 45  
 
 46  
 import java.io.IOException;
 47  
 import java.util.concurrent.ExecutorService;
 48  
 
 49  
 /**
 50  
  * Default implementation for {@link HapiContext}.
 51  
  * 
 52  
  * With this class you can configure HAPI and obtain all major HAPI business
 53  
  * objects that are initialized accordingly. All configuration objects already
 54  
  * have reasonable defaults.
 55  
  * <p>
 56  
  * When using Spring Framework for initializing objects, you can use the factory
 57  
  * methods like this:
 58  
  * 
 59  
  * <pre>
 60  
  * &lt;!-- Define the context --&gt;
 61  
  * &lt;bean id="hapiContext" class="ca.uhn.hl7v2.DefaultHapiContext"&gt;
 62  
  *    ...
 63  
  * &lt;/bean&gt;
 64  
  * 
 65  
  * &lt;!-- Obtain the default PipeParser instance --&gt;
 66  
  * &lt;bean id="pipeParser" factory-bean="hapiContext" factory-method="getPipeParser"/&gt;
 67  
  * ...
 68  
  * </pre>
 69  
  * 
 70  
  */
 71  96
 public class DefaultHapiContext implements HapiContext {
 72  
 
 73  
     private ExecutorService executorService;
 74  
     private ParserConfiguration parserConfiguration;
 75  
     private ValidationContext validationContext;
 76  
     private ValidationRuleBuilder validationRuleBuilder;
 77  
     private ModelClassFactory modelClassFactory;
 78  
     private ConnectionHub connectionHub;
 79  
     private LowerLayerProtocol llp;
 80  
     private SocketFactory socketFactory;
 81  
     private ProfileStore profileStore;
 82  
     private CodeStoreRegistry codeStoreRegistry;
 83  
     private PipeParser pipeParser;
 84  
     private XMLParser xmlParser;
 85  
     private GenericParser genericParser;
 86  
     private Validator<?> validator;
 87  
     private ValidationExceptionHandlerFactory<?> validationExceptionHandlerFactory;
 88  
         private ServerConfiguration serverConfiguration;
 89  
 
 90  
     public DefaultHapiContext() {
 91  5220
         this(new DefaultModelClassFactory());
 92  5220
     }
 93  
 
 94  
     public DefaultHapiContext(ExecutorService executorService) {
 95  0
         this();
 96  0
         setExecutorService(executorService);
 97  0
     }
 98  
 
 99  
     public DefaultHapiContext(ModelClassFactory modelClassFactory) {
 100  5260
         this(new ParserConfiguration(), ValidationContextFactory.defaultValidation(),
 101  
                 modelClassFactory);
 102  5260
     }
 103  
 
 104  
     public DefaultHapiContext(ValidationContext validationContext) {
 105  165
         this(new ParserConfiguration(), validationContext, new DefaultModelClassFactory());
 106  165
     }
 107  
 
 108  
     public DefaultHapiContext(ValidationRuleBuilder builder) {
 109  25
         this(new ParserConfiguration(), builder, new DefaultModelClassFactory());
 110  25
     }
 111  
 
 112  
     public DefaultHapiContext(ParserConfiguration parserConfiguration,
 113  5490
             ValidationContext validationContext, ModelClassFactory modelClassFactory) {
 114  5490
         VersionLogger.init();
 115  5490
         setParserConfiguration(parserConfiguration);
 116  5490
         setValidationContext(validationContext);
 117  5490
         setModelClassFactory(modelClassFactory);
 118  5490
         setLowerLayerProtocol(new MinLowerLayerProtocol(false));
 119  5490
         setSocketFactory(new StandardSocketFactory());
 120  5490
         setValidationExceptionHandlerFactory(new ReportingValidationExceptionHandler(true));
 121  5490
         setProfileStore(ProfileStoreFactory.getProfileStore());
 122  5490
         setCodeStoreRegistry(new DefaultCodeStoreRegistry());
 123  5490
         setServerConfiguration(new ServerConfiguration());
 124  5490
     }
 125  
 
 126  
     public DefaultHapiContext(ParserConfiguration parserConfiguration,
 127  90
             ValidationRuleBuilder builder, ModelClassFactory modelClassFactory) {
 128  90
         VersionLogger.init();
 129  90
         setParserConfiguration(parserConfiguration);
 130  90
         setValidationRuleBuilder(builder);
 131  90
         setModelClassFactory(modelClassFactory);
 132  90
         setLowerLayerProtocol(new MinLowerLayerProtocol(false));
 133  90
         setSocketFactory(new StandardSocketFactory());
 134  90
         setProfileStore(ProfileStoreFactory.getProfileStore());
 135  90
         setCodeStoreRegistry(new DefaultCodeStoreRegistry());
 136  90
         setServerConfiguration(new ServerConfiguration());
 137  90
     }
 138  
 
 139  
     public DefaultHapiContext(HapiContext context) {
 140  0
         this(context.getParserConfiguration(), context.getValidationContext(), context
 141  0
                 .getModelClassFactory());
 142  0
     }
 143  
 
 144  
     public void close() throws IOException {
 145  10
         getConnectionHub().discardAll();
 146  10
         if (DefaultExecutorService.isDefaultService(executorService)) {
 147  5
             executorService.shutdownNow();
 148  
         }
 149  10
     }
 150  
 
 151  
     public synchronized ExecutorService getExecutorService() {
 152  430
         if (executorService == null) {
 153  75
             executorService = DefaultExecutorService.getDefaultService();
 154  75
             Runtime.getRuntime().addShutdownHook(new Thread() {
 155  
 
 156  
                 @Override
 157  
                 public void run() {
 158  49
                     executorService.shutdownNow();
 159  24
                 }
 160  
 
 161  
             });
 162  
         }
 163  430
         return executorService;
 164  
     }
 165  
 
 166  
     public synchronized void setExecutorService(ExecutorService executorService) {
 167  140
         this.executorService = executorService;
 168  140
     }
 169  
 
 170  
     public ConnectionHub getConnectionHub() {
 171  110
         if (this.connectionHub == null) {
 172  75
             this.connectionHub = ConnectionHub.getNewInstance(this);
 173  
         }
 174  110
         return this.connectionHub;
 175  
     }
 176  
 
 177  
     public ParserConfiguration getParserConfiguration() {
 178  303074
         return parserConfiguration;
 179  
     }
 180  
 
 181  
     public void setParserConfiguration(ParserConfiguration configuration) {
 182  5580
         if (configuration == null)
 183  0
             throw new IllegalArgumentException("ParserConfiguration must not be null");
 184  5580
         this.parserConfiguration = configuration;
 185  5580
     }
 186  
 
 187  
     /**
 188  
      * Returns the ValidationContext. If none has been explicitly set,
 189  
      * {@link #getValidationRuleBuilder()} is called in order to to contruct a
 190  
      * context. If this is also null, the ca.uhn.hl7v2.validation.context_class
 191  
      * system property is evaluated, otherwise it returns the DefaultValidation
 192  
      * context.
 193  
      */
 194  
     public ValidationContext getValidationContext() {
 195  156979
         if (validationContext == null) {
 196  
 
 197  0
             if (getValidationRuleBuilder() != null) {
 198  0
                 setValidationContext(ValidationContextFactory
 199  0
                         .fromBuilder(getValidationRuleBuilder()));
 200  
             } else {
 201  
                 try {
 202  0
                     setValidationContext(ValidationContextFactory.getContext());
 203  0
                 } catch (HL7Exception e) {
 204  0
                     setValidationContext(ValidationContextFactory.defaultValidation());
 205  0
                 }
 206  
             }
 207  
         }
 208  156979
         return validationContext;
 209  
     }
 210  
 
 211  
     public void setValidationContext(ValidationContext context) {
 212  5765
         this.validationContext = context;
 213  5765
     }
 214  
 
 215  
     public void setValidationContext(String contextClassName) {
 216  
         try {
 217  0
             this.validationContext = ValidationContextFactory.customValidation(contextClassName);
 218  0
         } catch (HL7Exception e) {
 219  0
             throw new IllegalArgumentException(e);
 220  0
         }
 221  0
     }
 222  
 
 223  
     public ValidationRuleBuilder getValidationRuleBuilder() {
 224  0
         return validationRuleBuilder;
 225  
     }
 226  
 
 227  
     public void setValidationRuleBuilder(ValidationRuleBuilder validationRuleBuilder) {
 228  95
         this.validationRuleBuilder = validationRuleBuilder;
 229  95
         setValidationContext(ValidationContextFactory.fromBuilder(validationRuleBuilder));
 230  95
     }
 231  
 
 232  
     public void setValidationRuleBuilder(String builderClassName) {
 233  
         try {
 234  0
             setValidationRuleBuilder(ValidationContextFactory.customBuilder(builderClassName));
 235  0
         } catch (HL7Exception e) {
 236  0
             throw new IllegalArgumentException(e);
 237  0
         }
 238  0
     }
 239  
 
 240  
     public ModelClassFactory getModelClassFactory() {
 241  9443
         return modelClassFactory == null ? new DefaultModelClassFactory() : modelClassFactory;
 242  
     }
 243  
 
 244  
     public void setModelClassFactory(ModelClassFactory modelClassFactory) {
 245  5585
         this.modelClassFactory = modelClassFactory;
 246  5585
     }
 247  
     
 248  
     public ProfileStore getProfileStore() {
 249  30
         return profileStore;
 250  
     }
 251  
 
 252  
     public void setProfileStore(ProfileStore profileStore) {
 253  5580
         this.profileStore = profileStore;
 254  5580
     }
 255  
 
 256  
     public CodeStoreRegistry getCodeStoreRegistry() {
 257  735
         return codeStoreRegistry;
 258  
     }
 259  
 
 260  
     public void setCodeStoreRegistry(CodeStoreRegistry codeStoreRegistry) {
 261  5580
         this.codeStoreRegistry = codeStoreRegistry;
 262  5580
     }
 263  
     
 264  
     public ca.uhn.hl7v2.conf.check.Validator getConformanceValidator() {
 265  30
         return new ca.uhn.hl7v2.conf.check.DefaultValidator(this);
 266  
     }
 267  
 
 268  
     public synchronized PipeParser getPipeParser() {
 269  310
         if (pipeParser == null) {
 270  180
             pipeParser = new PipeParser(this);
 271  
         }
 272  310
         return pipeParser;
 273  
     }
 274  
 
 275  
     public synchronized XMLParser getXMLParser() {
 276  70
         if (xmlParser == null) {
 277  60
             xmlParser = new DefaultXMLParser(this);
 278  
         }
 279  70
         return xmlParser;
 280  
     }
 281  
 
 282  
     public synchronized GenericParser getGenericParser() {
 283  2040
         if (genericParser == null) {
 284  215
             genericParser = new GenericParser(this);
 285  
         }
 286  2040
         return genericParser;
 287  
     }
 288  
 
 289  
     @SuppressWarnings("unchecked")
 290  
     public synchronized <R> Validator<R> getMessageValidator() {
 291  11253
         if (validator == null) {
 292  1335
             validator = new DefaultValidator<R>(this);
 293  
         }
 294  11253
         return (Validator<R>) validator;
 295  
     }
 296  
 
 297  
     @SuppressWarnings("unchecked")
 298  
     public <R> ValidationExceptionHandlerFactory<R> getValidationExceptionHandlerFactory() {
 299  11238
             if (validationExceptionHandlerFactory == null) {
 300  10
                     validationExceptionHandlerFactory = new DefaultValidationExceptionHandler(this);
 301  
             }
 302  11238
         return (ValidationExceptionHandlerFactory<R>) validationExceptionHandlerFactory;
 303  
     }
 304  
 
 305  
     public <R> void setValidationExceptionHandlerFactory(
 306  
             ValidationExceptionHandlerFactory<R> factory) {
 307  5510
             if (factory == null) {
 308  0
                     throw new NullPointerException("ValidationExceptionHandlerFactory can not be null");
 309  
             }
 310  5510
         this.validationExceptionHandlerFactory = factory;
 311  5510
     }
 312  
 
 313  
     public LowerLayerProtocol getLowerLayerProtocol() {
 314  1445
         return llp;
 315  
     }
 316  
 
 317  
     public void setLowerLayerProtocol(LowerLayerProtocol llp) {
 318  5615
         this.llp = llp;
 319  5615
     }
 320  
 
 321  
     public SocketFactory getSocketFactory() {
 322  1480
         return socketFactory;
 323  
     }
 324  
 
 325  
     public void setSocketFactory(SocketFactory socketFactory) {
 326  5595
         this.socketFactory = socketFactory;
 327  5595
     }
 328  
 
 329  
     public SimpleServer newServer(int port, boolean tls) {
 330  105
         return new SimpleServer(this, port, tls);
 331  
     }
 332  
 
 333  
     public TwoPortService newServer(int port1, int port2, boolean tls) {
 334  10
         return new TwoPortService(this, port1, port2, tls);
 335  
     }
 336  
 
 337  
         public Connection newClient(String host, int port, boolean tls) throws HL7Exception {
 338  55
                 return getConnectionHub().attach(this, host, port, tls);
 339  
         }
 340  
 
 341  
         public Connection newClient(String host, int outboundPort, int inboundPort, boolean tls) throws HL7Exception {
 342  0
                 return getConnectionHub().attach(this, host, outboundPort, inboundPort, tls);
 343  
         }
 344  
 
 345  
     public Connection newLazyClient(String host, int port, boolean tls) throws HL7Exception {
 346  15
         return getConnectionHub().attachLazily(this, host, port, tls);
 347  
     }
 348  
 
 349  
     public Connection newLazyClient(String host, int outboundPort, int inboundPort, boolean tls) throws HL7Exception {
 350  0
         return getConnectionHub().attachLazily(this, host, outboundPort, inboundPort, tls);
 351  
     }
 352  
 
 353  
         public ServerConfiguration getServerConfiguration() {
 354  50
                 if (this.serverConfiguration == null) {
 355  0
                         serverConfiguration = new ServerConfiguration();
 356  
                 }
 357  50
                 return this.serverConfiguration;
 358  
         }
 359  
 
 360  
         public void setServerConfiguration(ServerConfiguration theServerConfiguration) {
 361  5580
                 if (theServerConfiguration==null) {
 362  0
                         throw new NullPointerException("Server configuration can not be null");
 363  
                 }
 364  5580
                 serverConfiguration = theServerConfiguration;
 365  5580
         }
 366  
 
 367  
     public Message newMessage(String eventType, String triggerEvent, Version version) throws HL7Exception {
 368  
         try {
 369  5
             String structure = getModelClassFactory().getMessageStructureForEvent(eventType + "_" + triggerEvent, version);
 370  5
             Class<? extends Message> messageClass = getModelClassFactory().getMessageClass(structure, version.getVersion(), false);
 371  5
             Message msg = newMessage(messageClass);
 372  5
             ((AbstractMessage) msg).initQuickstart(eventType, triggerEvent, "P");
 373  5
             return msg;
 374  0
         } catch (IOException e) {
 375  0
             throw new HL7Exception(e);
 376  
         }
 377  
     }
 378  
 
 379  
     public <T extends Message> T newMessage(Class<T> clazz) throws HL7Exception {
 380  490
         T msg = ReflectionUtil.instantiateMessage(clazz, getModelClassFactory());
 381  490
         msg.setParser(getGenericParser());
 382  490
         return msg;
 383  
     }
 384  
 
 385  
 }