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