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 "HapiContextSupport.java".  Description: 
10  "Abstract base class for subclasses supporting the access to a HapiContext." 
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.HL7Service;
33  import ca.uhn.hl7v2.conf.store.CodeStoreRegistry;
34  import ca.uhn.hl7v2.conf.store.ProfileStore;
35  import ca.uhn.hl7v2.llp.LowerLayerProtocol;
36  import ca.uhn.hl7v2.parser.GenericParser;
37  import ca.uhn.hl7v2.parser.ModelClassFactory;
38  import ca.uhn.hl7v2.parser.ParserConfiguration;
39  import ca.uhn.hl7v2.parser.PipeParser;
40  import ca.uhn.hl7v2.parser.XMLParser;
41  import ca.uhn.hl7v2.util.SocketFactory;
42  import ca.uhn.hl7v2.validation.ValidationContext;
43  import ca.uhn.hl7v2.validation.ValidationExceptionHandlerFactory;
44  import ca.uhn.hl7v2.validation.Validator;
45  import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
46  
47  /**
48   * Abstract base class for subclasses supporting the access to a HapiContext.
49   * 
50   * @author Christian Ohr
51   */
52  public abstract class HapiContextSupport {
53  
54  	private HapiContext context;
55  
56  	public HapiContextSupport(HapiContext context) {
57  		super();
58  		this.context = context;
59  	}
60  
61  	/**
62  	 * Returns a unmodifiable instance of HapiContext
63  	 */
64  	public final HapiContext getHapiContext() {
65  		return unmodifiableContext(context);
66  	}
67  
68  	/**
69  	 * Only for internal purposes: associate a new {@link HapiContext} with an existing
70  	 * service object.
71  	 * 
72  	 * @param context new {@link HapiContext}
73  	 */
74  	protected void setHapiContext(HapiContext context) {
75  		this.context = context;
76  	}
77  
78  	/**
79  	 * Supports the intention that HapiContext instances should not be altered e.g. from within a
80  	 * Parser instance, but only explicitly from where the HapiContext instance is actually owned.
81  	 * 
82  	 * @param context context to be made unmodifiable
83  	 * @return an unmodifiable HapiContext
84  	 */
85  	private static HapiContext unmodifiableContext(HapiContext context) {
86  		return new UnmodifiableHapiContext(context);
87  	}
88  
89  	private static class UnmodifiableHapiContext implements HapiContext {
90  
91  		private HapiContext context;
92  		
93  		public UnmodifiableHapiContext(HapiContext context) {
94  			this.context = context;
95  		}
96  
97  		public CodeStoreRegistry getCodeStoreRegistry() {
98              return context.getCodeStoreRegistry();
99          }
100 
101 		public ca.uhn.hl7v2.conf.check.Validator getConformanceValidator() {
102             return context.getConformanceValidator();
103         }
104 
105 		public ConnectionHub getConnectionHub() {
106 			return context.getConnectionHub();
107 		}
108 
109 		public ExecutorService getExecutorService() {
110 			return context.getExecutorService();
111 		}
112 
113 		public GenericParser getGenericParser() {
114 			return context.getGenericParser();
115 		}
116 
117 		public LowerLayerProtocol getLowerLayerProtocol() {
118 			return context.getLowerLayerProtocol();
119 		}
120 
121 		public <R> Validator<R> getMessageValidator() {
122 			return context.getMessageValidator();
123 		}
124 
125 		public ModelClassFactory getModelClassFactory() {
126 			return context.getModelClassFactory();
127 		}
128 
129 		public ParserConfiguration getParserConfiguration() {
130 			return context.getParserConfiguration();
131 		}
132 
133 		public PipeParser getPipeParser() {
134 			return context.getPipeParser();
135 		}
136 
137 		public ProfileStore getProfileStore() {
138             return context.getProfileStore();
139         }
140 
141 		public SocketFactory getSocketFactory() {
142 			return context.getSocketFactory();
143 		}
144 
145 		public ValidationContext getValidationContext() {
146 			return context.getValidationContext();
147 		}
148 
149 		public <R> ValidationExceptionHandlerFactory<R> getValidationExceptionHandlerFactory() {
150 			return context.getValidationExceptionHandlerFactory();
151 		}
152 
153 		public ValidationRuleBuilder getValidationRuleBuilder() {
154 			return context.getValidationRuleBuilder();
155 		}
156 
157 		public XMLParser getXMLParser() {
158 			return context.getXMLParser();
159 		}
160 
161 		public Connection newClient(String host, int port, boolean tls) throws HL7Exception {
162 			return context.newClient(host, port, tls);
163 		}
164 
165         public Connection newClient(String host, int outboundPort, int inboundPort, boolean tls) throws HL7Exception {
166 			return context.newClient(host, outboundPort, inboundPort, tls);
167 		}
168 
169 		public HL7Service newServer(int port, boolean tls) {
170 			return context.newServer(port, tls);
171 		}
172 
173         public HL7Service newServer(int inboundPort, int outboundPort, boolean tls) {
174 			return context.newServer(inboundPort, outboundPort, tls);
175 		}
176 
177         public void setCodeStoreRegistry(CodeStoreRegistry store) {
178             throw new UnsupportedOperationException("Read-only instance");
179         }
180 
181         public void setExecutorService(ExecutorService executorService) {
182 			throw new UnsupportedOperationException("Read-only instance");
183 		}
184         
185         public void setLowerLayerProtocol(LowerLayerProtocol llp) {
186 			throw new UnsupportedOperationException("Read-only instance");
187 		}
188 
189 		public void setModelClassFactory(ModelClassFactory modelClassFactory) {
190 			throw new UnsupportedOperationException("Read-only instance");
191 		}
192 
193 		public void setParserConfiguration(ParserConfiguration configuration) {
194 			throw new UnsupportedOperationException("Read-only instance");
195 		}
196 
197 		public void setProfileStore(ProfileStore store) {
198             throw new UnsupportedOperationException("Read-only instance");
199         }
200 
201 		public void setSocketFactory(SocketFactory socketFactory) {
202 			throw new UnsupportedOperationException("Read-only instance");
203 		}
204 
205 		public void setValidationContext(String contextClassName) {
206 			throw new UnsupportedOperationException("Read-only instance");
207 		}
208 
209 		public void setValidationContext(ValidationContext context) {
210 			throw new UnsupportedOperationException("Read-only instance");
211 		}
212 
213 		public <R> void setValidationExceptionHandlerFactory(
214 				ValidationExceptionHandlerFactory<R> factory) {
215 			context.setValidationExceptionHandlerFactory(factory);
216 		}
217 
218 		public void setValidationRuleBuilder(String builderClassName) {
219 			throw new UnsupportedOperationException("Read-only instance");
220 		}
221 
222 		public void setValidationRuleBuilder(ValidationRuleBuilder ruleBuilder) {
223 			throw new UnsupportedOperationException("Read-only instance");
224 		}
225 		
226 		
227 	}
228 
229 }