001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source.impl;
018:
019: import java.util.Arrays;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Set;
023:
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.activity.Initializable;
026: import org.apache.avalon.framework.configuration.Configurable;
027: import org.apache.avalon.framework.configuration.Configuration;
028: import org.apache.avalon.framework.configuration.ConfigurationException;
029: import org.apache.avalon.framework.container.ContainerUtil;
030: import org.apache.avalon.framework.context.Context;
031: import org.apache.avalon.framework.context.ContextException;
032: import org.apache.avalon.framework.context.Contextualizable;
033: import org.apache.avalon.framework.logger.AbstractLogEnabled;
034: import org.apache.avalon.framework.parameters.Parameters;
035: import org.apache.avalon.framework.service.ServiceManager;
036: import org.apache.avalon.framework.service.Serviceable;
037: import org.apache.avalon.framework.thread.ThreadSafe;
038: import org.apache.cocoon.components.source.SourceDescriptor;
039: import org.apache.cocoon.components.source.SourceInspector;
040: import org.apache.cocoon.components.source.helpers.SourceProperty;
041: import org.apache.excalibur.source.Source;
042: import org.apache.excalibur.source.SourceException;
043: import org.apache.excalibur.source.SourceValidity;
044: import org.apache.excalibur.source.impl.validity.AggregatedValidity;
045:
046: /**
047: * This source descriptor acts as container for a set of source inspectors/descriptors.
048: *
049: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
050: * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
051: * @version CVS $Id: SourceDescriptorManager.java 433543 2006-08-22 06:22:54Z crossley $
052: */
053: public final class SourceDescriptorManager extends AbstractLogEnabled
054: implements SourceDescriptor, Contextualizable, Serviceable,
055: Configurable, Initializable, Disposable, ThreadSafe {
056:
057: // the registered inspectors
058: private Set m_inspectors;
059:
060: private Context m_context;
061: private ServiceManager m_manager;
062: private Configuration m_configuration;
063:
064: // ---------------------------------------------------- lifecycle
065:
066: public SourceDescriptorManager() {
067: }
068:
069: public void contextualize(Context context) throws ContextException {
070: m_context = context;
071: }
072:
073: public void service(ServiceManager manager) {
074: m_manager = manager;
075: }
076:
077: public void configure(Configuration configuration)
078: throws ConfigurationException {
079: m_configuration = configuration;
080: }
081:
082: public void initialize() throws Exception {
083: m_inspectors = new HashSet();
084: final ClassLoader classloader = Thread.currentThread()
085: .getContextClassLoader();
086: final Configuration[] children = m_configuration.getChildren();
087:
088: for (int i = 0; i < children.length; i++) {
089: String className = children[i].getAttribute("class", "");
090: SourceInspector inspector;
091: try {
092: final Class inspectorClass = classloader
093: .loadClass(className);
094: inspector = (SourceInspector) inspectorClass
095: .newInstance();
096: } catch (InstantiationException ie) {
097: throw new ConfigurationException(
098: "Could not instantiate class " + className, ie);
099: } catch (ClassNotFoundException cnfe) {
100: throw new ConfigurationException(
101: "Could not load class " + className, cnfe);
102: } catch (IllegalAccessException iae) {
103: throw new ConfigurationException(
104: "Could not load class " + className, iae);
105: }
106: ContainerUtil.enableLogging(inspector, getLogger());
107: ContainerUtil.contextualize(inspector, m_context);
108: ContainerUtil.service(inspector, m_manager);
109: ContainerUtil.configure(inspector, children[i]);
110: ContainerUtil.parameterize(inspector, Parameters
111: .fromConfiguration(children[i]));
112: ContainerUtil.initialize(inspector);
113:
114: m_inspectors.add(inspector);
115: }
116: // done with these
117: m_configuration = null;
118: m_context = null;
119: m_manager = null;
120: }
121:
122: public void dispose() {
123: Iterator iter = m_inspectors.iterator();
124: while (iter.hasNext()) {
125: ContainerUtil.dispose(iter.next());
126: }
127: m_inspectors = null;
128: }
129:
130: // ---------------------------------------------------- SourceDescriptor implementation
131:
132: /**
133: * Loops over the registered inspectors until it finds the property.
134: */
135: public SourceProperty getSourceProperty(Source source,
136: String namespace, String name) throws SourceException {
137:
138: final Iterator inspectors = m_inspectors.iterator();
139: while (inspectors.hasNext()) {
140: SourceInspector inspector = (SourceInspector) inspectors
141: .next();
142: SourceProperty property = inspector.getSourceProperty(
143: source, namespace, name);
144: if (property != null) {
145: return property;
146: }
147: }
148: return null;
149: }
150:
151: /**
152: * Aggregate all properties of all registered inspectors.
153: */
154: public SourceProperty[] getSourceProperties(Source source)
155: throws SourceException {
156: final Set result = new HashSet();
157: SourceInspector inspector;
158: SourceProperty[] properties;
159: final Iterator inspectors = m_inspectors.iterator();
160: while (inspectors.hasNext()) {
161: inspector = (SourceInspector) inspectors.next();
162: properties = inspector.getSourceProperties(source);
163: if (properties != null) {
164: result.addAll(Arrays.asList(properties));
165: }
166: }
167: return (SourceProperty[]) result
168: .toArray(new SourceProperty[result.size()]);
169: }
170:
171: /**
172: * Check if there is an inspector that handles properties of
173: * the given type.
174: */
175: public boolean handlesProperty(String namespace, String name) {
176: SourceInspector inspector;
177: final Iterator inspectors = m_inspectors.iterator();
178: while (inspectors.hasNext()) {
179: inspector = (SourceInspector) inspectors.next();
180: if (inspector.handlesProperty(namespace, name)) {
181: return true;
182: }
183: }
184: return false;
185: }
186:
187: /**
188: * Loops over the registered descriptors and delegates the call.
189: */
190: public void removeSourceProperty(Source source, String ns,
191: String name) throws SourceException {
192: SourceInspector inspector;
193: final Iterator inspectors = m_inspectors.iterator();
194: while (inspectors.hasNext()) {
195: inspector = (SourceInspector) inspectors.next();
196: if (inspector instanceof SourceDescriptor) {
197: ((SourceDescriptor) inspector).removeSourceProperty(
198: source, ns, name);
199: }
200: }
201: }
202:
203: /**
204: * Loops over the registered descriptors and calls delegates the call.
205: */
206: public void setSourceProperty(Source source, SourceProperty property)
207: throws SourceException {
208: SourceInspector inspector;
209: final Iterator inspectors = m_inspectors.iterator();
210: while (inspectors.hasNext()) {
211: inspector = (SourceInspector) inspectors.next();
212: if (inspector instanceof SourceDescriptor) {
213: ((SourceDescriptor) inspector).setSourceProperty(
214: source, property);
215: }
216: }
217: }
218:
219: /**
220: * Returns an aggregate validity describing the validity of all the properties.
221: */
222: public SourceValidity getValidity(Source source) {
223: AggregatedValidity validity = new AggregatedValidity();
224: SourceInspector inspector;
225: final Iterator inspectors = m_inspectors.iterator();
226: while (inspectors.hasNext()) {
227: inspector = (SourceInspector) inspectors.next();
228: SourceValidity sv = inspector.getValidity(source);
229: if (sv == null) {
230: return null;
231: }
232: validity.add(sv);
233: }
234: return validity;
235: }
236: }
|