1 17 18 package org.apache.avalon.fortress.impl.factory; 19 20 import org.apache.avalon.framework.activity.Disposable; 21 import org.apache.avalon.framework.activity.Initializable; 22 import org.apache.avalon.framework.activity.Startable; 23 import org.apache.avalon.framework.activity.Suspendable; 24 import org.apache.avalon.framework.component.Component; 25 import org.apache.avalon.framework.component.Composable; 26 import org.apache.avalon.framework.component.Recomposable; 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.Reconfigurable; 29 import org.apache.avalon.framework.context.Contextualizable; 30 import org.apache.avalon.framework.context.Recontextualizable; 31 import org.apache.avalon.framework.logger.LogEnabled; 32 import org.apache.avalon.framework.logger.Loggable; 33 import org.apache.avalon.framework.parameters.Parameterizable; 34 import org.apache.avalon.framework.parameters.Reparameterizable; 35 import org.apache.avalon.framework.service.Serviceable; 36 import org.apache.excalibur.instrument.Instrument; 37 import org.apache.excalibur.instrument.Instrumentable; 38 import org.apache.excalibur.mpool.ObjectFactory; 39 40 import java.io.Serializable ; 41 import java.util.HashSet ; 42 import java.util.Set ; 43 44 50 public abstract class AbstractObjectFactory implements ObjectFactory, Instrumentable 51 { 52 55 private static final Class [] INVALID_INTERFACES = new Class [] 56 { 57 Loggable.class, 58 LogEnabled.class, 59 Contextualizable.class, 60 Recontextualizable.class, 61 Composable.class, 62 Recomposable.class, 63 Serviceable.class, 64 Configurable.class, 65 Reconfigurable.class, 66 Parameterizable.class, 67 Reparameterizable.class, 68 Initializable.class, 69 Startable.class, 70 Suspendable.class, 71 Disposable.class, 72 Serializable .class 73 }; 74 75 79 protected final ObjectFactory m_delegateFactory; 80 81 public AbstractObjectFactory( final ObjectFactory objectFactory ) 82 { 83 if ( null == objectFactory ) 84 { 85 throw new NullPointerException ( "objectFactory" ); 86 } 87 88 m_delegateFactory = objectFactory; 89 } 90 91 94 public abstract Object newInstance() throws Exception ; 95 96 99 public final Class getCreatedClass() 100 { 101 return m_delegateFactory.getCreatedClass(); 102 } 103 104 107 public abstract void dispose( Object object ) throws Exception ; 108 109 112 public final void setInstrumentableName( final String name ) 113 { 114 if ( m_delegateFactory instanceof Instrumentable ) 115 { 116 ( (Instrumentable) m_delegateFactory ).setInstrumentableName( name ); 117 } 118 } 119 120 123 public final String getInstrumentableName() 124 { 125 if ( m_delegateFactory instanceof Instrumentable ) 126 { 127 return ( (Instrumentable) m_delegateFactory ).getInstrumentableName(); 128 } 129 130 return ""; 131 } 132 133 136 public final Instrument[] getInstruments() 137 { 138 if ( m_delegateFactory instanceof Instrumentable ) 139 { 140 return ( (Instrumentable) m_delegateFactory ).getInstruments(); 141 } 142 143 return new Instrument[]{}; 144 } 145 146 149 public final Instrumentable[] getChildInstrumentables() 150 { 151 if ( m_delegateFactory instanceof Instrumentable ) 152 { 153 return ( (Instrumentable) m_delegateFactory ).getChildInstrumentables(); 154 } 155 156 return new Instrumentable[]{}; 157 } 158 159 167 protected static Class [] guessWorkInterfaces( final Class clazz ) 168 { 169 final HashSet workInterfaces = new HashSet (); 170 171 guessWorkInterfaces( clazz, workInterfaces ); 173 174 workInterfaces.add( Component.class ); 176 177 for ( int j = 0; j < INVALID_INTERFACES.length; j++ ) 179 { 180 workInterfaces.remove(INVALID_INTERFACES[j]); 181 } 182 183 return (Class []) workInterfaces.toArray( new Class [workInterfaces.size()] ); 184 } 185 186 193 private static void guessWorkInterfaces( final Class clazz, 194 final Set workInterfaces ) 195 { 196 if ( null != clazz ) 197 { 198 addInterfaces( clazz.getInterfaces(), workInterfaces ); 199 200 guessWorkInterfaces( clazz.getSuperclass(), workInterfaces ); 201 } 202 } 203 204 211 private static void addInterfaces( final Class [] interfaces, 212 final Set workInterfaces ) 213 { 214 for ( int i = 0; i < interfaces.length; i++ ) 215 { 216 workInterfaces.add( interfaces[i] ); 217 addInterfaces(interfaces[i].getInterfaces(), workInterfaces); 218 } 219 } 220 } 221 | Popular Tags |