KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > SourceDescriptorManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.source.impl;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
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 /**
46  * This source descriptor acts as container for a set of source inspectors/descriptors.
47  *
48  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
49  * @author <a HREF="mailto:unico@apache.org">Unico Hommes</a>
50  * @version CVS $Id: SourceDescriptorManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
51  */

52 public final class SourceDescriptorManager extends AbstractLogEnabled
53 implements SourceDescriptor, Contextualizable, Serviceable,
54 Configurable, Initializable, Disposable, ThreadSafe {
55     
56     // the registered inspectors
57
private Set JavaDoc m_inspectors;
58     
59     private Context m_context;
60     private ServiceManager m_manager;
61     private Configuration m_configuration;
62     
63     
64     // ---------------------------------------------------- lifecycle
65

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 JavaDoc {
82         m_inspectors = new HashSet JavaDoc();
83         final ClassLoader JavaDoc classloader = Thread.currentThread().getContextClassLoader();
84         final Configuration[] children = m_configuration.getChildren();
85         
86         for (int i = 0; i < children.length; i++) {
87             String JavaDoc className = children[i].getAttribute("class","");
88             SourceInspector inspector;
89             try {
90                 final Class JavaDoc inspectorClass = classloader.loadClass(className);
91                 inspector = (SourceInspector) inspectorClass.newInstance();
92             } catch (InstantiationException JavaDoc ie) {
93                 throw new ConfigurationException(
94                     "Could not instantiate class "+className, ie);
95             } catch (ClassNotFoundException JavaDoc cnfe) {
96                 throw new ConfigurationException(
97                     "Could not load class "+className, cnfe);
98             } catch (IllegalAccessException JavaDoc 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         // done with these
113
m_configuration = null;
114         m_context = null;
115         m_manager = null;
116     }
117     
118     public void dispose() {
119         Iterator JavaDoc iter = m_inspectors.iterator();
120         while(iter.hasNext()) {
121             ContainerUtil.dispose(iter.next());
122         }
123         m_inspectors = null;
124     }
125     
126     
127     // ---------------------------------------------------- SourceDescriptor implementation
128

129     /**
130      * Loops over the registered inspectors until it finds the property.
131      */

132     public SourceProperty getSourceProperty(Source source, String JavaDoc namespace, String JavaDoc name)
133             throws SourceException {
134
135         final Iterator JavaDoc 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     /**
147      * Aggregate all properties of all registered inspectors.
148      */

149     public SourceProperty[] getSourceProperties(Source source) throws SourceException {
150         final Set JavaDoc result = new HashSet JavaDoc();
151         SourceInspector inspector;
152         SourceProperty[] properties;
153         final Iterator JavaDoc 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     /**
165      * Check if there is an inspector that handles properties of
166      * the given type.
167      */

168     public boolean handlesProperty(String JavaDoc namespace, String JavaDoc name) {
169         SourceInspector inspector;
170         final Iterator JavaDoc 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     /**
181      * Loops over the registered descriptors and delegates the call.
182      */

183     public void removeSourceProperty(Source source, String JavaDoc ns, String JavaDoc name) throws SourceException {
184         SourceInspector inspector;
185         final Iterator JavaDoc 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     /**
195      * Loops over the registered descriptors and calls delegates the call.
196      */

197     public void setSourceProperty(Source source, SourceProperty property) throws SourceException {
198         SourceInspector inspector;
199         final Iterator JavaDoc 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     /**
209      * Returns an aggregate validity describing the validity of all the properties.
210      */

211     public SourceValidity getValidity(Source source) {
212         AggregatedValidity validity = new AggregatedValidity();
213         SourceInspector inspector;
214         final Iterator JavaDoc 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