KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > SourceHandlerImpl


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;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.component.ComponentException;
20 import org.apache.avalon.framework.component.ComponentManager;
21 import org.apache.avalon.framework.component.Composable;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.context.Context;
26 import org.apache.avalon.framework.context.ContextException;
27 import org.apache.avalon.framework.context.Contextualizable;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29 import org.apache.avalon.framework.logger.LogEnabled;
30 import org.apache.cocoon.ProcessingException;
31 import org.apache.cocoon.components.url.URLFactory;
32 import org.apache.cocoon.environment.Environment;
33 import org.apache.cocoon.environment.Source;
34 import org.apache.cocoon.util.ClassUtils;
35
36 import java.io.IOException JavaDoc;
37 import java.net.MalformedURLException JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /**
44  * @deprecated The Avalon Excalibur Source Resolving is now used.
45  *
46  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
47  * @version CVS $Id: SourceHandlerImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
48  */

49 public final class SourceHandlerImpl extends AbstractLogEnabled
50     implements Configurable, Disposable, Composable, Contextualizable, SourceHandler {
51
52     /** The component manager */
53     private ComponentManager manager;
54
55     /** The url factory */
56     private URLFactory urlFactory;
57
58     /** The special Source factories */
59     private Map JavaDoc sourceFactories;
60
61     /** The context */
62     private Context context;
63
64     /**
65      * Configure the SourceFactories
66      */

67     public void configure(final Configuration conf)
68     throws ConfigurationException {
69         try {
70             if (this.getLogger().isDebugEnabled()) {
71                 getLogger().debug("Getting the SourceFactories");
72             }
73             HashMap JavaDoc factories = new HashMap JavaDoc();
74             Configuration[] configs = conf.getChildren("protocol");
75             SourceFactory sourceFactory = null;
76             String JavaDoc protocol = null;
77             for (int i = 0; i < configs.length; i++) {
78                 protocol = configs[i].getAttribute("name");
79                 if (factories.containsKey(protocol)) {
80                     throw new ConfigurationException("SourceFactory defined twice for protocol: " + protocol);
81                 }
82
83                 if (this.getLogger().isDebugEnabled()) {
84                     getLogger().debug("\tfor protocol: " + protocol + " " + configs[i].getAttribute("class"));
85                 }
86                 sourceFactory = (SourceFactory) ClassUtils.newInstance(configs[i].getAttribute("class"));
87                 this.init(sourceFactory, configs[i]);
88                 factories.put(protocol, sourceFactory);
89             }
90
91             this.sourceFactories = java.util.Collections.synchronizedMap(factories);
92         } catch (ConfigurationException e) {
93             throw e;
94         } catch (Exception JavaDoc e) {
95             throw new ConfigurationException("Could not get parameters because: " +
96                                            e.getMessage());
97         }
98     }
99
100     /**
101      * Get the context
102      */

103     public void contextualize(Context context)
104     throws ContextException {
105         this.context = context;
106     }
107
108     /**
109      * Set the current <code>ComponentManager</code> instance used by this
110      * <code>Composable</code>.
111      */

112     public void compose(ComponentManager manager)
113     throws ComponentException {
114         this.manager = manager;
115         this.urlFactory = (URLFactory)this.manager.lookup(URLFactory.ROLE);
116     }
117
118     /**
119      * Dispose
120      */

121     public void dispose() {
122         this.manager.release(this.urlFactory);
123
124         final Iterator JavaDoc iter = this.sourceFactories.values().iterator();
125         SourceFactory current;
126         while (iter.hasNext()) {
127             current = (SourceFactory) iter.next();
128             this.deinit(current);
129         }
130         this.sourceFactories = null;
131     }
132
133     /**
134      * Get a <code>Source</code> object.
135      */

136     public Source getSource(Environment environment, String JavaDoc location)
137     throws ProcessingException, MalformedURLException JavaDoc, IOException JavaDoc {
138         final int protocolEnd = location.indexOf(':');
139         if (protocolEnd != -1) {
140             final String JavaDoc protocol = location.substring(0, protocolEnd);
141             final SourceFactory sourceFactory = (SourceFactory)this.sourceFactories.get(protocol);
142             if (sourceFactory != null) {
143                 return sourceFactory.getSource(environment, location);
144             }
145         }
146
147         // default implementation
148
Source result = new URLSource(this.urlFactory.getURL(location), this.manager);
149         if (result instanceof LogEnabled) {
150             ((LogEnabled)result).enableLogging(getLogger());
151         }
152         return result;
153     }
154
155     /**
156      * Get a <code>Source</code> object.
157      */

158     public Source getSource(Environment environment, URL JavaDoc base, String JavaDoc location)
159     throws ProcessingException, MalformedURLException JavaDoc, IOException JavaDoc {
160         final String JavaDoc protocol = base.getProtocol();
161         final SourceFactory sourceFactory = (SourceFactory)this.sourceFactories.get(protocol);
162         if (sourceFactory != null) {
163             return sourceFactory.getSource(environment, base, location);
164         }
165
166         // default implementation
167
return new URLSource(this.urlFactory.getURL(base, location), this.manager);
168     }
169
170     /**
171      * Add a factory
172      */

173     public void addFactory(String JavaDoc protocol, SourceFactory factory)
174     throws ProcessingException {
175         try {
176             this.init(factory, null);
177             SourceFactory oldFactory = (SourceFactory)this.sourceFactories.put(protocol, factory);
178             if (oldFactory != null) {
179                 deinit(oldFactory);
180             }
181         } catch (ComponentException e) {
182             throw new ProcessingException("cannot initialize factory: " + factory, e);
183         } catch (ContextException e) {
184             throw new ProcessingException("cannot initialize factory: " + factory, e);
185         } catch (ConfigurationException e) {
186             throw new ProcessingException("cannot configure factory: " + factory, e);
187         }
188     }
189
190     /**
191      * Init a source factory
192      */

193     private void init(SourceFactory factory, Configuration config)
194     throws ContextException, ComponentException, ConfigurationException {
195         if (factory instanceof LogEnabled) {
196             ((LogEnabled) factory).enableLogging(getLogger());
197         }
198         if (factory instanceof Contextualizable) {
199             ((Contextualizable) factory).contextualize (this.context);
200         }
201         if (factory instanceof Composable) {
202             ((Composable) factory).compose(this.manager);
203         }
204         if (config != null && factory instanceof Configurable) {
205             ((Configurable) factory).configure(config);
206         }
207     }
208
209     /**
210      * Deinit a source factory
211      */

212     private void deinit(SourceFactory factory) {
213         if (factory instanceof Disposable) {
214             ((Disposable) factory).dispose();
215         }
216     }
217
218 }
219
Popular Tags