| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.wsmo.grounding.sawsdl.io; |
| 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 | |
|
| 27 | |
public class Factory { |
| 28 | |
|
| 29 | |
public static final String DEFAULT_PARSER_IMPL = |
| 30 | |
"com.ontotext.wsmo4j.grounding.sawsdl.io.GroundingParserImpl"; |
| 31 | |
public static final String DEFAULT_SERIALIZER_IMPL = |
| 32 | |
"com.ontotext.wsmo4j.grounding.sawsdl.io.GroundingSerializerImpl"; |
| 33 | |
|
| 34 | |
public final static String PROVIDER_CLASS = "provider"; |
| 35 | |
public final static String ENCODING = "encoding"; |
| 36 | |
|
| 37 | 0 | private Factory() { |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public static GroundingParser createParser(Map<String, Object> props) { |
| 41 | |
|
| 42 | 0 | if (null == props) { |
| 43 | 0 | props = new HashMap<String, Object>(); |
| 44 | |
} |
| 45 | |
|
| 46 | 0 | if (false == props.containsKey(Factory.PROVIDER_CLASS)) { |
| 47 | |
|
| 48 | 0 | props.put(Factory.PROVIDER_CLASS, DEFAULT_PARSER_IMPL); |
| 49 | |
} |
| 50 | |
|
| 51 | 0 | Object result = _createObject(props); |
| 52 | 0 | if (false == result instanceof GroundingParser) { |
| 53 | 0 | throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) |
| 54 | |
+ " does not implement the org.wsmo.grounding.sawsdl.io.GroundingParser interface"); |
| 55 | |
} |
| 56 | 0 | return (GroundingParser)result; |
| 57 | |
} |
| 58 | |
|
| 59 | |
public static GroundingSerializer createSerializer(Map<String, Object> props) { |
| 60 | 0 | if (null == props) { |
| 61 | 0 | props = new HashMap<String, Object>(); |
| 62 | |
} |
| 63 | |
|
| 64 | 0 | if (false == props.containsKey(Factory.PROVIDER_CLASS)) { |
| 65 | |
|
| 66 | 0 | props.put(Factory.PROVIDER_CLASS, DEFAULT_SERIALIZER_IMPL); |
| 67 | |
} |
| 68 | |
|
| 69 | 0 | Object result = _createObject(props); |
| 70 | 0 | if (false == result instanceof GroundingSerializer) { |
| 71 | 0 | throw new RuntimeException(props.get(Factory.PROVIDER_CLASS) |
| 72 | |
+ " does not implement the org.wsmo.grounding.sawsdl.io.GroundingSerializer interface"); |
| 73 | |
} |
| 74 | 0 | return (GroundingSerializer)result; |
| 75 | |
} |
| 76 | |
|
| 77 | |
@SuppressWarnings("unchecked") |
| 78 | |
private static Object _createObject(Map properties) { |
| 79 | |
|
| 80 | 0 | Object result = null; |
| 81 | |
|
| 82 | 0 | String clazzName = (String)properties.get(Factory.PROVIDER_CLASS); |
| 83 | 0 | Class providerClass = null; |
| 84 | |
|
| 85 | |
try { |
| 86 | 0 | providerClass = Class.forName(clazzName); |
| 87 | |
} |
| 88 | 0 | catch (ClassNotFoundException e) { |
| 89 | |
try { |
| 90 | 0 | providerClass = Class.forName(clazzName, |
| 91 | |
true, |
| 92 | |
Thread.currentThread().getContextClassLoader()); |
| 93 | |
} |
| 94 | 0 | catch (ClassNotFoundException ne) { |
| 95 | 0 | throw new RuntimeException("Provider's class not found in classpath..."+clazzName, ne); |
| 96 | 0 | } |
| 97 | 0 | } |
| 98 | |
|
| 99 | 0 | Constructor providerConstructor = null; |
| 100 | |
|
| 101 | |
try { |
| 102 | 0 | Class[] param = new Class[] {java.util.Map.class}; |
| 103 | 0 | providerConstructor = providerClass.getConstructor(param); |
| 104 | |
} |
| 105 | 0 | catch (NoSuchMethodException nsme) { |
| 106 | 0 | throw new RuntimeException( |
| 107 | |
"The provider class should have a constuctor which takes a single java.util.Map argument...", |
| 108 | |
nsme); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
try { |
| 112 | 0 | result = providerConstructor.newInstance(new Object[] {properties}); |
| 113 | |
} |
| 114 | 0 | catch (InvocationTargetException ite) { |
| 115 | 0 | throw new RuntimeException("cannot invoke the constructor!", ite); |
| 116 | |
} |
| 117 | 0 | catch (IllegalAccessException ile) { |
| 118 | 0 | throw new RuntimeException("cannot access the constructor!", ile); |
| 119 | |
} |
| 120 | 0 | catch (InstantiationException inse) { |
| 121 | 0 | throw new RuntimeException("cannot instantiate the object!", inse); |
| 122 | 0 | } |
| 123 | |
|
| 124 | 0 | return result; |
| 125 | |
} |
| 126 | |
|
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|