View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-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.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      } // no instances are necessary
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              //use default WSMO factory
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              //use default WSMO factory
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  * $Log$
131  * Revision 1.2  2007/04/25 16:36:54  alex_simov
132  * no message
133  *
134  * Revision 1.1  2007/04/24 15:32:06  alex_simov
135  * generic parser/serializer factory added
136  *
137  */