View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.validator;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.juddi.datatype.CategoryBag;
21  import org.apache.juddi.util.Config;
22  import org.apache.juddi.util.Loader;
23  
24  /***
25   * Implementation of Factory pattern used to create an implementation of
26   * the org.apache.juddi.validation.Validation interface.
27   *
28   * The name of the Validation implementation to create is passed to the
29   * getValidation method.  If a null value is passed then the default
30   * Validation implementation "org.apache.juddi.validation.DefaultValidation" 
31   * is created.
32   *
33   * @author Steve Viens (sviens@apache.org)
34   */
35  public class ValidatorFactory
36  {
37    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(ValidatorFactory.class);
39  
40    // Validation property key & default implementation
41    private static final String IMPL_KEY = "juddi.validation";
42    private static final String DEFAULT_IMPL = "org.apache.juddi.validation.DefaultValidation";
43  
44    // the shared Validation instance
45    private static Validator validation = null;
46  
47    /***
48     * Returns a new instance of a ValidationFactory.
49     *
50     * @return Validation
51     */
52    public static Validator getValidation()
53    {
54      if (validation == null)
55        validation = createValidation();
56      return validation;
57    }
58  
59    /***
60     * Returns a new instance of a Validation.
61     *
62     * @return Validation
63     */
64    private static synchronized Validator createValidation()
65    {
66      if (validation != null)
67        return validation;
68  
69      // grab class name of the Validation implementation to create
70      String className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
71  
72      // write the Validation implementation name to the log
73      log.debug("Validation Implementation = " + className);
74  
75      Class implClass = null;
76      try
77      {
78        // Use Loader to locate & load the Validation implementation
79        implClass = Loader.getClassForName(className);
80      }
81      catch(ClassNotFoundException e)
82      {
83        log.error("The specified Validation class '" + className +
84          "' was not found in classpath.");
85        log.error(e);
86      }
87  
88      try
89      {
90        // try to instantiate the Validation implementation
91        validation = (Validator)implClass.newInstance();
92      }
93      catch(Exception e)
94      {
95        log.error("Exception while attempting to instantiate the " +
96          "implementation of Validation: " + implClass.getName() +
97          "\n" + e.getMessage());
98        log.error(e);
99      }
100 
101     return validation;
102   }
103 
104 
105   /****************************************************************************/
106   /****************************** TEST DRIVER *********************************/
107   /****************************************************************************/
108 
109 
110   public static void main(String[] args)
111     throws Exception
112   {
113       Validator validation = ValidatorFactory.getValidation();
114       if (validation != null)
115       {
116         System.out.println("Got a Validation instance: "+validation.getClass().getName());
117 
118         if (validation.validate(new CategoryBag()))
119           System.out.println("The objct was successfully validated.");
120         else
121           System.out.println("Sorry validation failed.");
122       }
123       else
124         System.out.println("Couldn't get a Validation instance.");
125   }
126 }