1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.util.Arrays ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Set ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.container.ContainerUtil; 29 import org.apache.avalon.framework.context.Context; 30 import org.apache.avalon.framework.context.ContextException; 31 import org.apache.avalon.framework.context.Contextualizable; 32 import org.apache.avalon.framework.logger.AbstractLogEnabled; 33 import org.apache.avalon.framework.parameters.Parameters; 34 import org.apache.avalon.framework.service.ServiceManager; 35 import org.apache.avalon.framework.service.Serviceable; 36 import org.apache.avalon.framework.thread.ThreadSafe; 37 import org.apache.cocoon.components.source.SourceDescriptor; 38 import org.apache.cocoon.components.source.SourceInspector; 39 import org.apache.cocoon.components.source.helpers.SourceProperty; 40 import org.apache.excalibur.source.Source; 41 import org.apache.excalibur.source.SourceException; 42 import org.apache.excalibur.source.SourceValidity; 43 import org.apache.excalibur.source.impl.validity.AggregatedValidity; 44 45 52 public final class SourceDescriptorManager extends AbstractLogEnabled 53 implements SourceDescriptor, Contextualizable, Serviceable, 54 Configurable, Initializable, Disposable, ThreadSafe { 55 56 private Set m_inspectors; 58 59 private Context m_context; 60 private ServiceManager m_manager; 61 private Configuration m_configuration; 62 63 64 66 public SourceDescriptorManager() { 67 } 68 69 public void contextualize(Context context) throws ContextException { 70 m_context = context; 71 } 72 73 public void service(ServiceManager manager) { 74 m_manager = manager; 75 } 76 77 public void configure(Configuration configuration) throws ConfigurationException { 78 m_configuration = configuration; 79 } 80 81 public void initialize() throws Exception { 82 m_inspectors = new HashSet (); 83 final ClassLoader classloader = Thread.currentThread().getContextClassLoader(); 84 final Configuration[] children = m_configuration.getChildren(); 85 86 for (int i = 0; i < children.length; i++) { 87 String className = children[i].getAttribute("class",""); 88 SourceInspector inspector; 89 try { 90 final Class inspectorClass = classloader.loadClass(className); 91 inspector = (SourceInspector) inspectorClass.newInstance(); 92 } catch (InstantiationException ie) { 93 throw new ConfigurationException( 94 "Could not instantiate class "+className, ie); 95 } catch (ClassNotFoundException cnfe) { 96 throw new ConfigurationException( 97 "Could not load class "+className, cnfe); 98 } catch (IllegalAccessException iae) { 99 throw new ConfigurationException( 100 "Could not load class "+className, iae); 101 } 102 ContainerUtil.enableLogging(inspector,getLogger()); 103 ContainerUtil.contextualize(inspector,m_context); 104 ContainerUtil.service(inspector,m_manager); 105 ContainerUtil.configure(inspector,children[i]); 106 ContainerUtil.parameterize(inspector, 107 Parameters.fromConfiguration(children[i])); 108 ContainerUtil.initialize(inspector); 109 110 m_inspectors.add(inspector); 111 } 112 m_configuration = null; 114 m_context = null; 115 m_manager = null; 116 } 117 118 public void dispose() { 119 Iterator iter = m_inspectors.iterator(); 120 while(iter.hasNext()) { 121 ContainerUtil.dispose(iter.next()); 122 } 123 m_inspectors = null; 124 } 125 126 127 129 132 public SourceProperty getSourceProperty(Source source, String namespace, String name) 133 throws SourceException { 134 135 final Iterator inspectors = m_inspectors.iterator(); 136 while (inspectors.hasNext()) { 137 SourceInspector inspector = (SourceInspector) inspectors.next(); 138 SourceProperty property = inspector.getSourceProperty(source,namespace,name); 139 if (property != null) { 140 return property; 141 } 142 } 143 return null; 144 } 145 146 149 public SourceProperty[] getSourceProperties(Source source) throws SourceException { 150 final Set result = new HashSet (); 151 SourceInspector inspector; 152 SourceProperty[] properties; 153 final Iterator inspectors = m_inspectors.iterator(); 154 while (inspectors.hasNext()) { 155 inspector = (SourceInspector) inspectors.next(); 156 properties = inspector.getSourceProperties(source); 157 if (properties != null) { 158 result.addAll(Arrays.asList(properties)); 159 } 160 } 161 return (SourceProperty[]) result.toArray(new SourceProperty[result.size()]); 162 } 163 164 168 public boolean handlesProperty(String namespace, String name) { 169 SourceInspector inspector; 170 final Iterator inspectors = m_inspectors.iterator(); 171 while(inspectors.hasNext()) { 172 inspector = (SourceInspector) inspectors.next(); 173 if (inspector.handlesProperty(namespace,name)) { 174 return true; 175 } 176 } 177 return false; 178 } 179 180 183 public void removeSourceProperty(Source source, String ns, String name) throws SourceException { 184 SourceInspector inspector; 185 final Iterator inspectors = m_inspectors.iterator(); 186 while (inspectors.hasNext()) { 187 inspector = (SourceInspector) inspectors.next(); 188 if (inspector instanceof SourceDescriptor) { 189 ((SourceDescriptor) inspector).removeSourceProperty(source,ns,name); 190 } 191 } 192 } 193 194 197 public void setSourceProperty(Source source, SourceProperty property) throws SourceException { 198 SourceInspector inspector; 199 final Iterator inspectors = m_inspectors.iterator(); 200 while (inspectors.hasNext()) { 201 inspector = (SourceInspector) inspectors.next(); 202 if (inspector instanceof SourceDescriptor) { 203 ((SourceDescriptor) inspector).setSourceProperty(source,property); 204 } 205 } 206 } 207 208 211 public SourceValidity getValidity(Source source) { 212 AggregatedValidity validity = new AggregatedValidity(); 213 SourceInspector inspector; 214 final Iterator inspectors = m_inspectors.iterator(); 215 while (inspectors.hasNext()) { 216 inspector = (SourceInspector) inspectors.next(); 217 SourceValidity sv = inspector.getValidity(source); 218 if (sv == null) { 219 return null; 220 } 221 validity.add(sv); 222 } 223 return validity; 224 } 225 } 226 227 | Popular Tags |