Coverage Report - org.sbpm.bpmo.factory.Factory
 
Classes in this File Line Coverage Branch Coverage Complexity
Factory
0%
0/51
0%
0/18
0
 
 1  
 /*
 2  
     wsmo4j - a WSMO API and Reference Implementation
 3  
 
 4  
     Copyright (c) 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.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
     } // no instances are necessary
 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  
             //use default BpmoFactory
 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  
             //use default Bpmo Parser 
 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  
             //use default BPMO Serializer
 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  
  * $Log$
 155  
  * Revision 1.4  2007/06/05 13:01:34  alex_simov
 156  
  * bpmo property constant added
 157  
  *
 158  
  * Revision 1.3  2007/06/05 10:45:33  alex_simov
 159  
  * fix
 160  
  *
 161  
  * Revision 1.2  2007/05/29 16:59:19  alex_simov
 162  
  * no message
 163  
  *
 164  
  * Revision 1.1  2007/05/29 16:28:57  alex_simov
 165  
  * moved
 166  
  *
 167  
  *
 168  
 **/