| 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 | 0 | private Factory() { |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
public static BpmoFactory createFactory(Map<String, Object> props) { |
| 46 | |
|
| 47 | 0 | if (null == props) { |
| 48 | 0 | props = new HashMap<String, Object>(); |
| 49 | |
} |
| 50 | |
|
| 51 | 0 | if (false == props.containsKey(Factory.PROVIDER_CLASS)) { |
| 52 | |
|
| 53 | 0 | props.put(Factory.PROVIDER_CLASS, DEFAULT_FACTORY_IMPL); |
| 54 | |
} |
| 55 | |
|
| 56 | 0 | Object result = _createObject(props); |
| 57 | 0 | if (false == result instanceof BpmoFactory) { |
| 58 | 0 | throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) |
| 59 | |
+ " does not implement the org.sbpm.bpmo.factory.BpmoFactory interface"); |
| 60 | |
} |
| 61 | 0 | return (BpmoFactory)result; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public static BpmoParser createParser(Map<String, Object> props) { |
| 65 | |
|
| 66 | 0 | if (null == props) { |
| 67 | 0 | props = new HashMap<String, Object>(); |
| 68 | |
} |
| 69 | |
|
| 70 | 0 | if (false == props.containsKey(Factory.PROVIDER_CLASS)) { |
| 71 | |
|
| 72 | 0 | props.put(Factory.PROVIDER_CLASS, DEFAULT_PARSER_IMPL); |
| 73 | |
} |
| 74 | |
|
| 75 | 0 | Object result = _createObject(props); |
| 76 | 0 | if (false == result instanceof BpmoParser) { |
| 77 | 0 | throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) |
| 78 | |
+ " does not implement the org.sbpm.bpmo.io.BpmoParser interface"); |
| 79 | |
} |
| 80 | 0 | return (BpmoParser)result; |
| 81 | |
} |
| 82 | |
|
| 83 | |
public static BpmoSerializer createSerializer(Map<String, Object> props) { |
| 84 | 0 | if (null == props) { |
| 85 | 0 | props = new HashMap<String, Object>(); |
| 86 | |
} |
| 87 | |
|
| 88 | 0 | if (false == props.containsKey(Factory.PROVIDER_CLASS)) { |
| 89 | |
|
| 90 | 0 | props.put(Factory.PROVIDER_CLASS, DEFAULT_SERIALIZER_IMPL); |
| 91 | |
} |
| 92 | |
|
| 93 | 0 | Object result = _createObject(props); |
| 94 | 0 | if (false == result instanceof BpmoSerializer) { |
| 95 | 0 | throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) |
| 96 | |
+ " does not implement the implement the org.sbpm.bpmo.io.BpmoSerializer interface"); |
| 97 | |
} |
| 98 | 0 | return (BpmoSerializer)result; |
| 99 | |
} |
| 100 | |
|
| 101 | |
@SuppressWarnings("unchecked") |
| 102 | |
private static Object _createObject(Map properties) { |
| 103 | |
|
| 104 | 0 | Object result = null; |
| 105 | |
|
| 106 | 0 | String clazzName = (String)properties.get(Factory.PROVIDER_CLASS); |
| 107 | 0 | Class providerClass = null; |
| 108 | |
|
| 109 | |
try { |
| 110 | 0 | providerClass = Class.forName(clazzName); |
| 111 | |
} |
| 112 | 0 | catch (ClassNotFoundException e) { |
| 113 | |
try { |
| 114 | 0 | providerClass = Class.forName(clazzName, |
| 115 | |
true, |
| 116 | |
Thread.currentThread().getContextClassLoader()); |
| 117 | |
} |
| 118 | 0 | catch (ClassNotFoundException ne) { |
| 119 | 0 | throw new RuntimeException("Provider's class not found in classpath..." + clazzName, ne); |
| 120 | 0 | } |
| 121 | 0 | } |
| 122 | |
|
| 123 | 0 | Constructor providerConstructor = null; |
| 124 | |
|
| 125 | |
try { |
| 126 | 0 | Class[] param = new Class[] {java.util.Map.class}; |
| 127 | 0 | providerConstructor = providerClass.getConstructor(param); |
| 128 | |
} |
| 129 | 0 | catch (NoSuchMethodException nsme) { |
| 130 | 0 | throw new RuntimeException( |
| 131 | |
"The provider class should have a constuctor which takes a single java.util.Map argument...", |
| 132 | |
nsme); |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
try { |
| 136 | 0 | result = providerConstructor.newInstance(new Object[] {properties}); |
| 137 | |
} |
| 138 | 0 | catch (InvocationTargetException ite) { |
| 139 | 0 | throw new RuntimeException("cannot invoke the constructor!", ite); |
| 140 | |
} |
| 141 | 0 | catch (IllegalAccessException ile) { |
| 142 | 0 | throw new RuntimeException("cannot access the constructor!", ile); |
| 143 | |
} |
| 144 | 0 | catch (InstantiationException inse) { |
| 145 | 0 | throw new RuntimeException("cannot instantiate the object!", inse); |
| 146 | 0 | } |
| 147 | |
|
| 148 | 0 | return result; |
| 149 | |
} |
| 150 | |
|
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|