Coverage Report - org.wsmo.grounding.sawsdl.io.Factory
 
Classes in this File Line Coverage Branch Coverage Complexity
Factory
0%
0/43
0%
0/12
0
 
 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  0
     private Factory() {
 38  0
     } // no instances are necessary
 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  
             //use default WSMO factory
 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  
             //use default WSMO factory
 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  
  * $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  
  */