1 package org.apache.ojb.broker.util.factory; 2 3 17 18 import org.apache.commons.lang.SystemUtils; 19 import org.apache.ojb.broker.PersistenceBrokerException; 20 import org.apache.ojb.broker.util.ClassHelper; 21 import org.apache.ojb.broker.util.configuration.Configurable; 22 import org.apache.ojb.broker.util.configuration.Configuration; 23 import org.apache.ojb.broker.util.configuration.ConfigurationException; 24 import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator; 25 import org.apache.ojb.broker.util.interceptor.InterceptorFactory; 26 import org.apache.ojb.broker.util.logging.Logger; 27 import org.apache.ojb.broker.util.logging.LoggerFactory; 28 29 40 public abstract class ConfigurableFactory implements Configurable 41 { 42 private Logger log = LoggerFactory.getLogger(this.getClass()); 43 44 47 private Class classToServe = null; 48 49 50 54 public ConfigurableFactory() 55 { 56 OjbConfigurator.getInstance().configure(this); 57 } 58 59 64 protected abstract String getConfigurationKey(); 65 66 71 public void configure(Configuration pConfig) throws ConfigurationException 72 { 73 if (getConfigurationKey() == null) 74 { 75 getLogger().error("ConfigurableFactory configuration key is 'null'"); 76 throw new PersistenceBrokerException("ConfigurableFactory configuration key is 'null'"); 77 } 78 Class clazz = pConfig.getClass(getConfigurationKey(), null); 79 if (clazz == null) 80 { 81 getLogger().error("ConfigurableFactory configuration key class for key'" + getConfigurationKey() + "' does not exist."); 82 throw new PersistenceBrokerException( 83 "ConfigurableFactory configuration key class for key'" + getConfigurationKey() + "' does not exist."); 84 } 85 this.setClassToServe(clazz); 86 } 87 88 93 public Object createNewInstance(Class [] types, Object [] args) 94 { 95 try 96 { 97 Object result; 98 if (types != null) 100 { 101 result = ClassHelper.newInstance(getClassToServe(), types, args, true); 102 } 103 else 104 { 105 result = ClassHelper.newInstance(getClassToServe(), true); 106 } 107 result = InterceptorFactory.getInstance().createInterceptorFor(result); 109 return result; 110 111 } 112 catch (InstantiationException e) 113 { 114 getLogger().error("ConfigurableFactory can't instantiate class " + 115 getClassToServe() + buildArgumentString(types, args), e); 116 throw new PersistenceBrokerException(e); 117 } 118 catch (IllegalAccessException e) 119 { 120 getLogger().error("ConfigurableFactory can't access constructor for class " + 121 getClassToServe() + buildArgumentString(types, args), e); 122 throw new PersistenceBrokerException(e); 123 } 124 catch (Exception e) 125 { 126 getLogger().error("ConfigurableFactory instantiation failed for class " + 127 getClassToServe() + buildArgumentString(types, args), e); 128 throw new PersistenceBrokerException(e); 129 } 130 } 131 132 protected String buildArgumentString(Class [] types, Object [] args) 133 { 134 StringBuffer buf = new StringBuffer (); 135 String eol = SystemUtils.LINE_SEPARATOR; 136 buf.append(eol + "* Factory types: "); 137 if (types != null) 138 { 139 for (int i = 0; i < types.length; i++) 140 { 141 Class type = types[i]; 142 buf.append(eol + (i + 1) + " - Type: " + (type != null ? type.getName() : null)); 143 } 144 } 145 else 146 buf.append(eol + "none"); 147 148 buf.append(eol + "* Factory arguments: "); 149 if (args != null) 150 { 151 for (int i = 0; i < args.length; i++) 152 { 153 Object obj = args[i]; 154 buf.append(eol + (i + 1) + " - Argument: " + obj); 155 } 156 } 157 else 158 buf.append(eol + "none"); 159 return buf.toString(); 160 } 161 162 167 public Object createNewInstance() 168 { 169 return createNewInstance((Class ) null, (Object ) null); 170 } 171 172 173 178 public Object createNewInstance(Class type, Object arg) 179 { 180 if (type != null) 181 return createNewInstance(new Class []{type}, new Object []{arg}); 182 else 183 return createNewInstance((Class []) null, (Object []) null); 184 } 185 186 190 public Class getClassToServe() 191 { 192 return classToServe; 193 } 194 195 204 public void setClassToServe(Class classToServe) 205 { 206 this.classToServe = classToServe; 207 } 208 209 212 protected Logger getLogger() 213 { 214 return log; 215 } 216 } 217 | Popular Tags |