View Javadoc

1   /*
2       wsmo4j - a WSMO API and Reference Implementation
3   
4       Copyright (c) 2007, Ontotext Lab. / SIRMA
5   
6       This library is free software; you can redistribute it and/or modify it under
7       the terms of the GNU Lesser General Public License as published by the Free
8       Software Foundation; either version 2.1 of the License, or (at your option)
9       any later version.
10      This library is distributed in the hope that it will be useful, but WITHOUT
11      ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12      FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13      details.
14      You should have received a copy of the GNU Lesser General Public License along
15      with this library; if not, write to the Free Software Foundation, Inc.,
16      59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  package org.sbpm.bpmo.factory;
20  
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.InvocationTargetException;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.sbpm.bpmo.io.BpmoParser;
27  import org.sbpm.bpmo.io.BpmoSerializer;
28  
29  
30  public class Factory {
31  
32      public static final String DEFAULT_FACTORY_IMPL =
33          "com.ontotext.sbpm.bpmo.factory.BpmoFactoryImpl";
34      public static final String DEFAULT_PARSER_IMPL =
35          "com.ontotext.sbpm.bpmo.parser.BpmoParserImpl";
36      public static final String DEFAULT_SERIALIZER_IMPL = 
37          "com.ontotext.sbpm.bpmo.serializer.BpmoSerializerImpl";
38      
39      public final static String PROVIDER_CLASS = "provider";
40      public final static String BPMO_FACTORY = "bpmo_factory";
41      
42      private Factory() {
43      } // no instances are necessary
44      
45      public static BpmoFactory createFactory(Map<String, Object> props) {
46  
47          if (null == props) {
48              props = new HashMap<String, Object>();
49          }
50  
51          if (false == props.containsKey(Factory.PROVIDER_CLASS)) {
52              //use default BpmoFactory
53              props.put(Factory.PROVIDER_CLASS, DEFAULT_FACTORY_IMPL);
54          }
55  
56          Object result = _createObject(props);
57          if (false == result instanceof BpmoFactory) {
58              throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) 
59                      + " does not implement the org.sbpm.bpmo.factory.BpmoFactory interface");
60          }
61          return (BpmoFactory)result;
62      }
63  
64      public static BpmoParser createParser(Map<String, Object> props) {
65  
66          if (null == props) {
67              props = new HashMap<String, Object>();
68          }
69  
70          if (false == props.containsKey(Factory.PROVIDER_CLASS)) {
71              //use default Bpmo Parser 
72              props.put(Factory.PROVIDER_CLASS, DEFAULT_PARSER_IMPL);
73          }
74  
75          Object result = _createObject(props);
76          if (false == result instanceof BpmoParser) {
77              throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) 
78                      + " does not implement the org.sbpm.bpmo.io.BpmoParser interface");
79          }
80          return (BpmoParser)result;
81      }
82      
83      public static BpmoSerializer createSerializer(Map<String, Object> props) {
84          if (null == props) {
85              props = new HashMap<String, Object>();
86          }
87  
88          if (false == props.containsKey(Factory.PROVIDER_CLASS)) {
89              //use default BPMO Serializer
90              props.put(Factory.PROVIDER_CLASS, DEFAULT_SERIALIZER_IMPL);
91          }
92  
93          Object result = _createObject(props);
94          if (false == result instanceof BpmoSerializer) {
95              throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) 
96                      + " does not implement the implement the org.sbpm.bpmo.io.BpmoSerializer interface");
97          }
98          return (BpmoSerializer)result;
99      }
100     
101     @SuppressWarnings("unchecked")
102     private static Object _createObject(Map properties) {
103 
104         Object result = null;
105 
106         String clazzName = (String)properties.get(Factory.PROVIDER_CLASS);
107         Class providerClass = null;
108 
109         try {
110             providerClass = Class.forName(clazzName);
111         }
112         catch (ClassNotFoundException e) {
113             try {
114                 providerClass = Class.forName(clazzName,
115                                               true,
116                                               Thread.currentThread().getContextClassLoader());
117             }
118             catch (ClassNotFoundException ne) {
119                 throw new RuntimeException("Provider's class not found in classpath..." + clazzName, ne);
120             }
121         }
122 
123         Constructor providerConstructor = null;
124 
125         try {
126             Class[] param = new Class[] {java.util.Map.class};
127             providerConstructor = providerClass.getConstructor(param);
128         }
129         catch (NoSuchMethodException nsme) {
130             throw new RuntimeException(
131                     "The provider class should have a constuctor which takes a single java.util.Map argument...",
132                     nsme);
133         }
134 
135         try {
136             result = providerConstructor.newInstance(new Object[] {properties});
137         }
138         catch (InvocationTargetException ite) {
139             throw new RuntimeException("cannot invoke the constructor!", ite);
140         }
141         catch (IllegalAccessException ile) {
142             throw new RuntimeException("cannot access the constructor!", ile);
143         }
144         catch (InstantiationException inse) {
145             throw new RuntimeException("cannot instantiate the object!", inse);
146         }
147 
148         return result;
149     }
150 
151 }
152 
153 /*
154  * $Log$
155  * Revision 1.4  2007/06/05 13:01:34  alex_simov
156  * bpmo property constant added
157  *
158  * Revision 1.3  2007/06/05 10:45:33  alex_simov
159  * fix
160  *
161  * Revision 1.2  2007/05/29 16:59:19  alex_simov
162  * no message
163  *
164  * Revision 1.1  2007/05/29 16:28:57  alex_simov
165  * moved
166  *
167  *
168 **/