1 16 package org.apache.cocoon.components.elementprocessor.impl; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.component.Component; 24 import org.apache.avalon.framework.logger.AbstractLogEnabled; 25 26 import org.apache.cocoon.components.elementprocessor.CannotCreateElementProcessorException; 27 import org.apache.cocoon.components.elementprocessor.ElementProcessor; 28 import org.apache.cocoon.components.elementprocessor.ElementProcessorFactory; 29 30 37 public abstract class AbstractElementProcessorFactory 38 extends AbstractLogEnabled implements ElementProcessorFactory, Component 39 { 40 41 private Map _element_processor_map; 50 51 54 55 protected AbstractElementProcessorFactory() { 56 _element_processor_map = new HashMap (); 57 } 58 59 70 71 public ElementProcessor createElementProcessor(final String name) 72 throws CannotCreateElementProcessorException { 73 Object progenitor = lookupElementProcessorProgenitor(name); 74 75 if (progenitor == null) { 76 CannotCreateElementProcessorException exception = 77 new CannotCreateElementProcessorException( 78 "Cannot find progenitor for that name"); 79 exception.setElementName(name); 80 throw exception; 81 } 82 ElementProcessor processor = null; 83 84 try { 85 processor = doCreateElementProcessor(progenitor); 86 } catch (CannotCreateElementProcessorException e) { 87 e.setElementName(name); 88 throw e; 89 } 90 return processor; 91 } 92 93 104 105 protected void addElementProcessorProgenitor(final String name, 106 final Object progenitor) 107 { 108 if ((name == null) || (name.equals(""))) { 109 throw new IllegalArgumentException ( 110 "Cannot use null or empty name as a key"); 111 } 112 if (progenitor == null) { 113 throw new IllegalArgumentException ( 114 "Cannot add null progenitor to the map"); 115 } 116 if (_element_processor_map.put(name, progenitor) != null) { 117 throw new IllegalArgumentException ( 118 name + " is already in use in the map"); 119 } 120 } 121 122 132 133 protected Object lookupElementProcessorProgenitor(final String name) 134 { 135 Object obj = _element_processor_map.get(name); 136 if (obj == null && !name.equals("*")) { 137 obj = lookupElementProcessorProgenitor("*"); 138 } 139 return obj; 140 } 141 142 156 157 protected abstract ElementProcessor doCreateElementProcessor( 158 final Object progenitor) throws CannotCreateElementProcessorException; 159 160 172 173 protected ElementProcessor createNewElementProcessorInstance( 174 final Class progenitor) 175 throws CannotCreateElementProcessorException { 176 ElementProcessor rval = null; 177 178 try { 179 rval = (ElementProcessor)progenitor.newInstance(); 180 if (rval instanceof AbstractLogEnabled) { 181 ((AbstractLogEnabled)rval).enableLogging(getLogger()); 182 } 183 } catch (ExceptionInInitializerError e) { 184 throw new CannotCreateElementProcessorException( 185 "an exception (" + e + ") occurred in initializing the associated ElementProcessor class"); 186 } catch (SecurityException e) { 187 throw new CannotCreateElementProcessorException( 188 "a security exception was caught while creating the associated ElementProcessor"); 189 } catch (InstantiationException e) { 190 throw new CannotCreateElementProcessorException( 191 "associated ElementProcessor is an interface or abstract class or has no zero-parameter constructor"); 192 } catch (IllegalAccessException e) { 193 throw new CannotCreateElementProcessorException( 194 "cannot access ElementProcessor class or its zero-parameter constructor"); 195 } catch (ClassCastException e) { 196 throw new CannotCreateElementProcessorException( 197 "object created does not implement ElementProcessor"); 198 } catch (Exception e) { 199 throw new CannotCreateElementProcessorException( 200 "exception (" + e 201 + ") occured while creating new instance of ElementProcessor"); 202 } 203 if (rval == null) { 204 throw new CannotCreateElementProcessorException( 205 "somehow generated a null ElementProcessor"); 206 } 207 return rval; 208 } 209 210 223 224 protected ElementProcessor constructElementProcessor( 225 final Constructor progenitor) 226 throws CannotCreateElementProcessorException { 227 ElementProcessor rval = null; 228 229 try { 230 rval = (ElementProcessor) progenitor.newInstance(new Object [0]); 231 if (rval instanceof AbstractLogEnabled) { 232 ((AbstractLogEnabled)rval).enableLogging(getLogger()); 233 } 234 } catch (ExceptionInInitializerError e) { 235 throw new CannotCreateElementProcessorException( 236 "an exception (" + e 237 + ")occurred in initializing the associated ElementProcessor class"); 238 } catch (IllegalArgumentException e) { 239 throw new CannotCreateElementProcessorException( 240 "the ElementProcessor constructor apparently needs parameters"); 241 } catch (InstantiationException e) { 242 throw new CannotCreateElementProcessorException( 243 "associated ElementProcessor is an interface or abstract class"); 244 } catch (IllegalAccessException e) { 245 throw new CannotCreateElementProcessorException( 246 "cannot access ElementProcessor class or its zero-parameter constructor"); 247 } catch (InvocationTargetException e) { 248 throw new CannotCreateElementProcessorException( 249 "ElementProcessor constructor threw an exception [" 250 + e.toString() + "]"); 251 } catch (ClassCastException e) { 252 throw new CannotCreateElementProcessorException( 253 "object created does not implement ElementProcessor"); 254 } 255 if (rval == null) { 256 throw new CannotCreateElementProcessorException( 257 "somehow generated a null ElementProcessor"); 258 } 259 return rval; 260 } 261 262 } | Popular Tags |