1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38 private static Log log = LogFactory.getLog(ValidatorFactory.class);
39
40
41 private static final String IMPL_KEY = "juddi.validation";
42 private static final String DEFAULT_IMPL = "org.apache.juddi.validation.DefaultValidation";
43
44
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
70 String className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
71
72
73 log.debug("Validation Implementation = " + className);
74
75 Class implClass = null;
76 try
77 {
78
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
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 }