001/**
002The 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. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "HapiContextSupport.java".  Description: 
010"Abstract base class for subclasses supporting the access to a HapiContext." 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132001.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the  "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025 */
026package ca.uhn.hl7v2;
027
028import java.io.IOException;
029import java.util.concurrent.ExecutorService;
030
031import ca.uhn.hl7v2.app.Connection;
032import ca.uhn.hl7v2.app.ConnectionHub;
033import ca.uhn.hl7v2.app.HL7Service;
034import ca.uhn.hl7v2.app.ServerConfiguration;
035import ca.uhn.hl7v2.conf.store.CodeStoreRegistry;
036import ca.uhn.hl7v2.conf.store.ProfileStore;
037import ca.uhn.hl7v2.llp.LowerLayerProtocol;
038import ca.uhn.hl7v2.parser.*;
039import ca.uhn.hl7v2.util.SocketFactory;
040import ca.uhn.hl7v2.validation.ValidationContext;
041import ca.uhn.hl7v2.validation.ValidationExceptionHandlerFactory;
042import ca.uhn.hl7v2.validation.Validator;
043import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
044
045/**
046 * Abstract base class for subclasses supporting the access to a HapiContext.
047 * 
048 * @author Christian Ohr
049 */
050public abstract class HapiContextSupport {
051
052        private HapiContext context;
053
054        public HapiContextSupport(HapiContext context) {
055                super();
056                this.context = context;
057        }
058
059        /**
060         * Returns a unmodifiable instance of HapiContext
061         */
062        public final HapiContext getHapiContext() {
063                return unmodifiableContext(context);
064        }
065
066        /**
067         * Only for internal purposes: associate a new {@link HapiContext} with an existing
068         * service object.
069         * 
070         * @param context new {@link HapiContext}
071         */
072        protected void setHapiContext(HapiContext context) {
073                this.context = context;
074        }
075
076        /**
077         * Supports the intention that HapiContext instances should not be altered e.g. from within a
078         * Parser instance, but only explicitly from where the HapiContext instance is actually owned.
079         * 
080         * @param context context to be made unmodifiable
081         * @return an unmodifiable HapiContext
082         */
083        private static HapiContext unmodifiableContext(HapiContext context) {
084                return new UnmodifiableHapiContext(context);
085        }
086
087        private static class UnmodifiableHapiContext implements HapiContext {
088
089                private HapiContext context;
090                
091                public UnmodifiableHapiContext(HapiContext context) {
092                        this.context = context;
093                }
094
095                public CodeStoreRegistry getCodeStoreRegistry() {
096            return context.getCodeStoreRegistry();
097        }
098
099                public ca.uhn.hl7v2.conf.check.Validator getConformanceValidator() {
100            return context.getConformanceValidator();
101        }
102
103                public ConnectionHub getConnectionHub() {
104                        return context.getConnectionHub();
105                }
106
107                public ExecutorService getExecutorService() {
108                        return context.getExecutorService();
109                }
110
111                public GenericParser getGenericParser() {
112                        return context.getGenericParser();
113                }
114
115                public LowerLayerProtocol getLowerLayerProtocol() {
116                        return context.getLowerLayerProtocol();
117                }
118
119                public <R> Validator<R> getMessageValidator() {
120                        return context.getMessageValidator();
121                }
122
123                public ModelClassFactory getModelClassFactory() {
124                        return context.getModelClassFactory();
125                }
126
127                public ParserConfiguration getParserConfiguration() {
128                        return context.getParserConfiguration();
129                }
130
131                public PipeParser getPipeParser() {
132                        return context.getPipeParser();
133                }
134
135                public ProfileStore getProfileStore() {
136            return context.getProfileStore();
137        }
138
139                public SocketFactory getSocketFactory() {
140                        return context.getSocketFactory();
141                }
142
143                public ValidationContext getValidationContext() {
144                        return context.getValidationContext();
145                }
146
147                public <R> ValidationExceptionHandlerFactory<R> getValidationExceptionHandlerFactory() {
148                        return context.getValidationExceptionHandlerFactory();
149                }
150
151                public ValidationRuleBuilder getValidationRuleBuilder() {
152                        return context.getValidationRuleBuilder();
153                }
154
155                public XMLParser getXMLParser() {
156                        return context.getXMLParser();
157                }
158
159                public Connection newClient(String host, int port, boolean tls) throws HL7Exception {
160                        return context.newClient(host, port, tls);
161                }
162
163        public Connection newClient(String host, int outboundPort, int inboundPort, boolean tls) throws HL7Exception {
164                        return context.newClient(host, outboundPort, inboundPort, tls);
165                }
166
167        public Connection newLazyClient(String host, int port, boolean tls) throws HL7Exception {
168            return context.newLazyClient(host, port, tls);
169        }
170
171        public Connection newLazyClient(String host, int outboundPort, int inboundPort, boolean tls) throws HL7Exception {
172            return context.newLazyClient(host, outboundPort, inboundPort, tls);
173        }
174
175        public HL7Service newServer(int port, boolean tls) {
176                        return context.newServer(port, tls);
177                }
178
179        public HL7Service newServer(int inboundPort, int outboundPort, boolean tls) {
180                        return context.newServer(inboundPort, outboundPort, tls);
181                }
182
183        public void setCodeStoreRegistry(CodeStoreRegistry store) {
184            throw new UnsupportedOperationException("Read-only instance");
185        }
186
187        public void setExecutorService(ExecutorService executorService) {
188                        throw new UnsupportedOperationException("Read-only instance");
189                }
190        
191        public void setLowerLayerProtocol(LowerLayerProtocol llp) {
192                        throw new UnsupportedOperationException("Read-only instance");
193                }
194
195                public void setModelClassFactory(ModelClassFactory modelClassFactory) {
196                        throw new UnsupportedOperationException("Read-only instance");
197                }
198
199                public void setParserConfiguration(ParserConfiguration configuration) {
200                        throw new UnsupportedOperationException("Read-only instance");
201                }
202
203                public void setProfileStore(ProfileStore store) {
204            throw new UnsupportedOperationException("Read-only instance");
205        }
206
207                public void setSocketFactory(SocketFactory socketFactory) {
208                        throw new UnsupportedOperationException("Read-only instance");
209                }
210
211                public void setValidationContext(String contextClassName) {
212                        throw new UnsupportedOperationException("Read-only instance");
213                }
214
215                public void setValidationContext(ValidationContext context) {
216                        throw new UnsupportedOperationException("Read-only instance");
217                }
218
219                public <R> void setValidationExceptionHandlerFactory(
220                                ValidationExceptionHandlerFactory<R> factory) {
221                        context.setValidationExceptionHandlerFactory(factory);
222                }
223
224                public void setValidationRuleBuilder(String builderClassName) {
225                        throw new UnsupportedOperationException("Read-only instance");
226                }
227
228                public void setValidationRuleBuilder(ValidationRuleBuilder ruleBuilder) {
229                        throw new UnsupportedOperationException("Read-only instance");
230                }
231
232                public ServerConfiguration getServerConfiguration() {
233                        return context.getServerConfiguration();
234                }
235
236                public void setServerConfiguration(ServerConfiguration theServerConfiguration) {
237                        throw new UnsupportedOperationException("Read-only instance");
238                }
239
240        public void close() throws IOException {
241            context.close();
242        }
243    }
244
245}