1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }
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
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
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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168