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.util.concurrent.ExecutorService;
029
030import ca.uhn.hl7v2.app.Connection;
031import ca.uhn.hl7v2.app.ConnectionHub;
032import ca.uhn.hl7v2.app.HL7Service;
033import ca.uhn.hl7v2.conf.store.CodeStoreRegistry;
034import ca.uhn.hl7v2.conf.store.ProfileStore;
035import ca.uhn.hl7v2.llp.LowerLayerProtocol;
036import ca.uhn.hl7v2.parser.GenericParser;
037import ca.uhn.hl7v2.parser.ModelClassFactory;
038import ca.uhn.hl7v2.parser.ParserConfiguration;
039import ca.uhn.hl7v2.parser.PipeParser;
040import ca.uhn.hl7v2.parser.XMLParser;
041import ca.uhn.hl7v2.util.SocketFactory;
042import ca.uhn.hl7v2.validation.ValidationContext;
043import ca.uhn.hl7v2.validation.ValidationExceptionHandlerFactory;
044import ca.uhn.hl7v2.validation.Validator;
045import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
046
047/**
048 * Abstract base class for subclasses supporting the access to a HapiContext.
049 * 
050 * @author Christian Ohr
051 */
052public abstract class HapiContextSupport {
053
054        private HapiContext context;
055
056        public HapiContextSupport(HapiContext context) {
057                super();
058                this.context = context;
059        }
060
061        /**
062         * Returns a unmodifiable instance of HapiContext
063         */
064        public final HapiContext getHapiContext() {
065                return unmodifiableContext(context);
066        }
067
068        /**
069         * Only for internal purposes: associate a new {@link HapiContext} with an existing
070         * service object.
071         * 
072         * @param context new {@link HapiContext}
073         */
074        protected void setHapiContext(HapiContext context) {
075                this.context = context;
076        }
077
078        /**
079         * Supports the intention that HapiContext instances should not be altered e.g. from within a
080         * Parser instance, but only explicitly from where the HapiContext instance is actually owned.
081         * 
082         * @param context context to be made unmodifiable
083         * @return an unmodifiable HapiContext
084         */
085        private static HapiContext unmodifiableContext(HapiContext context) {
086                return new UnmodifiableHapiContext(context);
087        }
088
089        private static class UnmodifiableHapiContext implements HapiContext {
090
091                private HapiContext context;
092                
093                public UnmodifiableHapiContext(HapiContext context) {
094                        this.context = context;
095                }
096
097                public CodeStoreRegistry getCodeStoreRegistry() {
098            return context.getCodeStoreRegistry();
099        }
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}