001    /**
002    The 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. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "ValidationContextFactory.java".  Description: 
010    "Source of ValidationContext" 
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
013    2004.  All Rights Reserved. 
014    
015    Contributor(s): ______________________________________. 
016    
017    Alternatively, the contents of this file may be used under the terms of the 
018    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
019    applicable instead of those above.  If you wish to allow use of your version of this 
020    file only under the terms of the GPL and not to allow others to use your version 
021    of this file under the MPL, indicate your decision by deleting  the provisions above 
022    and replace  them with the notice and other provisions required by the GPL License.  
023    If you do not delete the provisions above, a recipient may use your version of 
024    this file under either the MPL or the GPL. 
025    */
026    package ca.uhn.hl7v2.validation.impl;
027    
028    import ca.uhn.hl7v2.validation.ValidationContext;
029    import ca.uhn.hl7v2.validation.ValidationException;
030    
031    /**
032     * <p>Source of <code>ValidationContext</code>.  </p>
033     * 
034     * <p>The <code>ValidationContext</code> returned by <code>getContext()</code>
035     * is determined by the system property "ca.uhn.hl7v2.validation.context_class".
036     * This factory defines two inner classes that can be used: DefaultValidation 
037     * and NoValidation.  You can also create your own context, setting whatever rules
038     * you want in its constructor, and reference it instead (it must have a zero-arg
039     * constructor).  If this property is not set, DefaultValidation is used. </p>
040     * 
041     * <p>Also note that the contexts provided here extend <code>ValidationContextImpl</code>, 
042     * so rule bindings can be added or removed programmatically from the starting set. </p>  
043     * 
044     * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
045     * @version $Revision: 1.1 $ updated on $Date: 2007/02/19 02:24:40 $ by $Author: jamesagnew $
046     */
047    public class ValidationContextFactory {
048    
049        private static ValidationContext ourContext;
050        public static final String CONTEXT_PROPERTY = "ca.uhn.hl7v2.validation.context_class";
051        
052        /**
053         * Returns a singleton <code>ValidationContext</code>, creating it if necessary.
054         * 
055         * @return <code>ValidationContext</code> 
056         */
057        public synchronized static ValidationContext getContext() throws ValidationException {
058            if (ourContext == null) {
059                String contextClassName = System.getProperty(CONTEXT_PROPERTY);            
060                if (contextClassName == null) {
061                    ourContext = new DefaultValidation();
062                } else {
063                    Class c;
064                    try {
065                        c = Class.forName(contextClassName);
066                        ourContext = (ValidationContext) c.newInstance();
067                    } catch (Exception e) {
068                        throw new ValidationException(e);
069                    }
070                }
071            }
072            return ourContext;
073        }
074    
075    }