Coverage Report - ca.uhn.hl7v2.DefaultHapiContext
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultHapiContext
79%
89/112
75%
18/24
1.463
DefaultHapiContext$1
33%
1/3
N/A
1.463
 
 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 java.util.concurrent.ExecutorService;
 29  
 
 30  
 import ca.uhn.hl7v2.app.Connection;
 31  
 import ca.uhn.hl7v2.app.ConnectionHub;
 32  
 import ca.uhn.hl7v2.app.SimpleServer;
 33  
 import ca.uhn.hl7v2.app.TwoPortService;
 34  
 import ca.uhn.hl7v2.concurrent.DefaultExecutorService;
 35  
 import ca.uhn.hl7v2.conf.store.CodeStoreRegistry;
 36  
 import ca.uhn.hl7v2.conf.store.DefaultCodeStoreRegistry;
 37  
 import ca.uhn.hl7v2.conf.store.ProfileStore;
 38  
 import ca.uhn.hl7v2.conf.store.ProfileStoreFactory;
 39  
 import ca.uhn.hl7v2.llp.LowerLayerProtocol;
 40  
 import ca.uhn.hl7v2.llp.MinLowerLayerProtocol;
 41  
 import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
 42  
 import ca.uhn.hl7v2.parser.DefaultXMLParser;
 43  
 import ca.uhn.hl7v2.parser.GenericParser;
 44  
 import ca.uhn.hl7v2.parser.ModelClassFactory;
 45  
 import ca.uhn.hl7v2.parser.ParserConfiguration;
 46  
 import ca.uhn.hl7v2.parser.PipeParser;
 47  
 import ca.uhn.hl7v2.parser.XMLParser;
 48  
 import ca.uhn.hl7v2.util.SocketFactory;
 49  
 import ca.uhn.hl7v2.util.StandardSocketFactory;
 50  
 import ca.uhn.hl7v2.validation.DefaultValidationExceptionHandler;
 51  
 import ca.uhn.hl7v2.validation.DefaultValidator;
 52  
 import ca.uhn.hl7v2.validation.ReportingValidationExceptionHandler;
 53  
 import ca.uhn.hl7v2.validation.ValidationContext;
 54  
 import ca.uhn.hl7v2.validation.ValidationExceptionHandlerFactory;
 55  
 import ca.uhn.hl7v2.validation.Validator;
 56  
 import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
 57  
 import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
 58  
 
 59  
 /**
 60  
  * Default implementation for {@link HapiContext}.
 61  
  * 
 62  
  * With this class you can configure HAPI and obtain all major HAPI business
 63  
  * objects that are initialized accordingly. All configuration objects already
 64  
  * have reasonable defaults.
 65  
  * <p>
 66  
  * When using Spring Framework for initializing objects, you can use the factory
 67  
  * methods like this:
 68  
  * 
 69  
  * <pre>
 70  
  * &lt;!-- Define the context --&gt;
 71  
  * &lt;bean id="hapiContext" class="ca.uhn.hl7v2.DefaultHapiContext"&gt;
 72  
  *    ...
 73  
  * &lt;/bean&gt;
 74  
  * 
 75  
  * &lt;!-- Obtain the default PipeParser instance --&gt;
 76  
  * &lt;bean id="pipeParser" factory-bean="hapiContext" factory-method="getPipeParser"/&gt;
 77  
  * ...
 78  
  * </pre>
 79  
  * 
 80  
  */
 81  7
 public class DefaultHapiContext implements HapiContext {
 82  
 
 83  
     private ExecutorService executorService;
 84  
     private ParserConfiguration parserConfiguration;
 85  
     private ValidationContext validationContext;
 86  
     private ValidationRuleBuilder validationRuleBuilder;
 87  
     private ModelClassFactory modelClassFactory;
 88  
     private ConnectionHub connectionHub;
 89  
     private LowerLayerProtocol llp;
 90  
     private SocketFactory socketFactory;
 91  
     private ProfileStore profileStore;
 92  
     private CodeStoreRegistry codeStoreRegistry;
 93  
     private PipeParser pipeParser;
 94  
     private XMLParser xmlParser;
 95  
     private GenericParser genericParser;
 96  
     private Validator<?> validator;
 97  
     private ValidationExceptionHandlerFactory<?> validationExceptionHandlerFactory;
 98  
 
 99  
     public DefaultHapiContext() {
 100  427
         this(new DefaultModelClassFactory());
 101  427
     }
 102  
 
 103  
     public DefaultHapiContext(ExecutorService executorService) {
 104  0
         this();
 105  0
         setExecutorService(executorService);
 106  0
     }
 107  
 
 108  
     public DefaultHapiContext(ModelClassFactory modelClassFactory) {
 109  435
         this(new ParserConfiguration(), ValidationContextFactory.defaultValidation(),
 110  
                 modelClassFactory);
 111  435
     }
 112  
 
 113  
     public DefaultHapiContext(ValidationContext validationContext) {
 114  9
         this(new ParserConfiguration(), validationContext, new DefaultModelClassFactory());
 115  9
     }
 116  
 
 117  
     public DefaultHapiContext(ValidationRuleBuilder builder) {
 118  4
         this(new ParserConfiguration(), builder, new DefaultModelClassFactory());
 119  4
     }
 120  
 
 121  
     public DefaultHapiContext(ParserConfiguration parserConfiguration,
 122  456
             ValidationContext validationContext, ModelClassFactory modelClassFactory) {
 123  456
         VersionLogger.init();
 124  456
         setParserConfiguration(parserConfiguration);
 125  456
         setValidationContext(validationContext);
 126  456
         setModelClassFactory(modelClassFactory);
 127  456
         setLowerLayerProtocol(new MinLowerLayerProtocol());
 128  456
         setSocketFactory(new StandardSocketFactory());
 129  456
         setValidationExceptionHandlerFactory(new ReportingValidationExceptionHandler(true));
 130  456
         setProfileStore(ProfileStoreFactory.getProfileStore());
 131  456
         setCodeStoreRegistry(new DefaultCodeStoreRegistry());
 132  456
     }
 133  
 
 134  
     public DefaultHapiContext(ParserConfiguration parserConfiguration,
 135  13
             ValidationRuleBuilder builder, ModelClassFactory modelClassFactory) {
 136  13
         VersionLogger.init();
 137  13
         setParserConfiguration(parserConfiguration);
 138  13
         setValidationRuleBuilder(builder);
 139  13
         setModelClassFactory(modelClassFactory);
 140  13
         setLowerLayerProtocol(new MinLowerLayerProtocol());
 141  13
         setSocketFactory(new StandardSocketFactory());
 142  13
         setProfileStore(ProfileStoreFactory.getProfileStore());
 143  13
         setCodeStoreRegistry(new DefaultCodeStoreRegistry());
 144  13
     }
 145  
 
 146  
     public DefaultHapiContext(HapiContext context) {
 147  3
         this(context.getParserConfiguration(), context.getValidationContext(), context
 148  
                 .getModelClassFactory());
 149  3
     }
 150  
 
 151  
     public synchronized ExecutorService getExecutorService() {
 152  67
         if (executorService == null) {
 153  9
             executorService = DefaultExecutorService.getDefaultService();
 154  9
             Runtime.getRuntime().addShutdownHook(new Thread() {
 155  
 
 156  
                 @Override
 157  
                 public void run() {
 158  0
                     executorService.shutdownNow();
 159  0
                 }
 160  
 
 161  
             });
 162  
         }
 163  67
         return executorService;
 164  
     }
 165  
 
 166  
     public synchronized void setExecutorService(ExecutorService executorService) {
 167  20
         this.executorService = executorService;
 168  20
     }
 169  
 
 170  
     public ConnectionHub getConnectionHub() {
 171  12
         if (this.connectionHub == null) {
 172  9
             this.connectionHub = ConnectionHub.getNewInstance(this);
 173  
         }
 174  12
         return this.connectionHub;
 175  
     }
 176  
 
 177  
     public ParserConfiguration getParserConfiguration() {
 178  10806
         return parserConfiguration;
 179  
     }
 180  
 
 181  
     public void setParserConfiguration(ParserConfiguration configuration) {
 182  469
         if (configuration == null)
 183  0
             throw new IllegalArgumentException("ParserConfiguration must not be null");
 184  469
         this.parserConfiguration = configuration;
 185  469
     }
 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  4540
         if (validationContext == null) {
 196  
 
 197  0
             if (getValidationRuleBuilder() != null) {
 198  0
                 setValidationContext(ValidationContextFactory
 199  
                         .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  4540
         return validationContext;
 209  
     }
 210  
 
 211  
     public void setValidationContext(ValidationContext context) {
 212  502
         this.validationContext = context;
 213  502
     }
 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  14
         this.validationRuleBuilder = validationRuleBuilder;
 229  14
         setValidationContext(ValidationContextFactory.fromBuilder(validationRuleBuilder));
 230  14
     }
 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  1570
         return modelClassFactory == null ? new DefaultModelClassFactory() : modelClassFactory;
 242  
     }
 243  
 
 244  
     public void setModelClassFactory(ModelClassFactory modelClassFactory) {
 245  469
         this.modelClassFactory = modelClassFactory;
 246  469
     }
 247  
     
 248  
     public ProfileStore getProfileStore() {
 249  6
         return profileStore;
 250  
     }
 251  
 
 252  
     public void setProfileStore(ProfileStore profileStore) {
 253  469
         this.profileStore = profileStore;
 254  469
     }
 255  
 
 256  
     public CodeStoreRegistry getCodeStoreRegistry() {
 257  147
         return codeStoreRegistry;
 258  
     }
 259  
 
 260  
     public void setCodeStoreRegistry(CodeStoreRegistry codeStoreRegistry) {
 261  469
         this.codeStoreRegistry = codeStoreRegistry;
 262  469
     }
 263  
     
 264  
     public ca.uhn.hl7v2.conf.check.Validator getConformanceValidator() {
 265  6
         return new ca.uhn.hl7v2.conf.check.DefaultValidator(this);
 266  
     }
 267  
 
 268  
     public synchronized PipeParser getPipeParser() {
 269  52
         if (pipeParser == null) {
 270  27
             pipeParser = new PipeParser(this);
 271  
         }
 272  52
         return pipeParser;
 273  
     }
 274  
 
 275  
     public synchronized XMLParser getXMLParser() {
 276  9
         if (xmlParser == null) {
 277  7
             xmlParser = new DefaultXMLParser(this);
 278  
         }
 279  9
         return xmlParser;
 280  
     }
 281  
 
 282  
     public synchronized GenericParser getGenericParser() {
 283  271
         if (genericParser == null) {
 284  15
             genericParser = new GenericParser(this);
 285  
         }
 286  271
         return genericParser;
 287  
     }
 288  
 
 289  
     @SuppressWarnings("unchecked")
 290  
     public synchronized <R> Validator<R> getMessageValidator() {
 291  2002
         if (validator == null) {
 292  184
             validator = new DefaultValidator<R>(this);
 293  
         }
 294  2002
         return (Validator<R>) validator;
 295  
     }
 296  
 
 297  
     @SuppressWarnings("unchecked")
 298  
     public <R> ValidationExceptionHandlerFactory<R> getValidationExceptionHandlerFactory() {
 299  1999
             if (validationExceptionHandlerFactory == null) {
 300  2
                     validationExceptionHandlerFactory = new DefaultValidationExceptionHandler(this);
 301  
             }
 302  1999
         return (ValidationExceptionHandlerFactory<R>) validationExceptionHandlerFactory;
 303  
     }
 304  
 
 305  
     public <R> void setValidationExceptionHandlerFactory(
 306  
             ValidationExceptionHandlerFactory<R> factory) {
 307  458
             if (factory == null) {
 308  0
                     throw new NullPointerException("ValidationExceptionHandlerFactory can not be null");
 309  
             }
 310  458
         this.validationExceptionHandlerFactory = factory;
 311  458
     }
 312  
 
 313  
     public LowerLayerProtocol getLowerLayerProtocol() {
 314  268
         return llp;
 315  
     }
 316  
 
 317  
     public void setLowerLayerProtocol(LowerLayerProtocol llp) {
 318  470
         this.llp = llp;
 319  470
     }
 320  
 
 321  
     public SocketFactory getSocketFactory() {
 322  276
         return socketFactory;
 323  
     }
 324  
 
 325  
     public void setSocketFactory(SocketFactory socketFactory) {
 326  471
         this.socketFactory = socketFactory;
 327  471
     }
 328  
 
 329  
     public SimpleServer newServer(int port, boolean tls) {
 330  9
         return new SimpleServer(this, port, tls);
 331  
     }
 332  
 
 333  
     public TwoPortService newServer(int port1, int port2, boolean tls) {
 334  2
         return new TwoPortService(this, port1, port2, tls);
 335  
     }
 336  
 
 337  
         public Connection newClient(String host, int port, boolean tls) throws HL7Exception {
 338  6
                 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  
 }