1 /** 2 * ServingXML 3 * 4 * Copyright (C) 2006 Daniel Parker 5 * daniel.parker@servingxml.com 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 **/ 20 21 package com.servingxml.util; 22 23 import java.lang.reflect.Constructor; 24 25 /** 26 * 27 * 28 * @author Daniel A. Parker (daniel.parker@servingxml.com) 29 */ 30 31 public class InstanceFactory { 32 33 public static final Class[] VOID_ARG_TYPES = {}; 34 public static final Object[] VOID_ARG_VALUES = {}; 35 36 private final Class<?> instanceClass; 37 InstanceFactory(String instanceClassName)38 public InstanceFactory(String instanceClassName) { 39 40 try { 41 this.instanceClass = Thread.currentThread().getContextClassLoader().loadClass(instanceClassName); 42 } catch (java.lang.ClassNotFoundException e){ 43 String s = MessageFormatter.getInstance().getMessage(ServingXmlMessages.CLASS_NOT_FOUND, 44 instanceClassName); 45 throw new ServingXmlException(s,e); 46 } 47 } 48 hasConstructor(Class[] argTypes)49 public boolean hasConstructor(Class[] argTypes) { 50 51 if (instanceClass == null) { 52 throw new ServingXmlException("Instance class is null."); 53 } 54 Constructor[] ctors = instanceClass.getConstructors(); 55 boolean found = false; 56 for (int i = 0; !found && i < ctors.length; ++i) { 57 Constructor ctor = ctors[i]; 58 Class<?>[] types = ctor.getParameterTypes(); 59 if (types.length == argTypes.length) { 60 boolean match = true; 61 for (int j = 0; match && j < types.length; ++j) { 62 if (!types[j].isAssignableFrom(argTypes[j])) { 63 match = false; 64 } 65 } 66 found = match; 67 } 68 } 69 return found; 70 } 71 InstanceFactory(String instanceClassName, Class<?> baseClass)72 public InstanceFactory(String instanceClassName, Class<?> baseClass) { 73 74 try { 75 this.instanceClass = Thread.currentThread().getContextClassLoader().loadClass(instanceClassName); 76 if (!makerOf(baseClass)) { 77 String msg = "Class " + instanceClassName + " must implement " + baseClass.getName() + " interface."; 78 throw new ServingXmlException(msg); 79 } 80 } catch (java.lang.ClassNotFoundException e){ 81 String s = MessageFormatter.getInstance().getMessage(ServingXmlMessages.CLASS_NOT_FOUND, 82 instanceClassName); 83 throw new ServingXmlException(s,e); 84 } 85 } 86 InstanceFactory(Class<?> instanceClass)87 public InstanceFactory(Class<?> instanceClass) { 88 this.instanceClass = instanceClass; 89 } 90 InstanceFactory(Class<?> instanceClass, Class<?> baseClass)91 public InstanceFactory(Class<?> instanceClass, Class<?> baseClass) { 92 this.instanceClass = instanceClass; 93 if (!makerOf(baseClass)) { 94 String msg = "Class " + instanceClass.getName() + " must implement " + baseClass.getName() + " interface."; 95 throw new ServingXmlException(msg); 96 } 97 } 98 makerOf(Class<?> cls)99 public boolean makerOf(Class<?> cls) { 100 return cls.isAssignableFrom(instanceClass); 101 } 102 createInstance()103 public Object createInstance() { 104 return createInstance(VOID_ARG_TYPES,VOID_ARG_VALUES); 105 } 106 createInstance(Class[] argTypes, Object[] args)107 public Object createInstance(Class[] argTypes, Object[] args) { 108 109 try { 110 if (instanceClass == null) { 111 throw new ServingXmlException("Instance class cannot be null."); 112 } 113 Constructor ctor = instanceClass.getConstructor(argTypes); 114 return ctor.newInstance(args); 115 } catch (java.lang.NoSuchMethodException e) { 116 throw new ServingXmlException(e.getMessage(),e); 117 } catch (java.lang.InstantiationException e) { 118 throw new ServingXmlException(e.getMessage(),e); 119 } catch (java.lang.IllegalAccessException e) { 120 throw new ServingXmlException(e.getMessage(),e); 121 } catch (java.lang.reflect.InvocationTargetException e) { 122 throw new ServingXmlException(e.getMessage(),e); 123 } 124 } 125 } 126 127