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;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.component.ComponentException;
021: import org.apache.avalon.framework.component.ComponentManager;
022: import org.apache.avalon.framework.component.Composable;
023: import org.apache.avalon.framework.configuration.Configurable;
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.context.Context;
027: import org.apache.avalon.framework.context.ContextException;
028: import org.apache.avalon.framework.context.Contextualizable;
029: import org.apache.avalon.framework.logger.AbstractLogEnabled;
030: import org.apache.avalon.framework.logger.LogEnabled;
031: import org.apache.cocoon.ProcessingException;
032: import org.apache.cocoon.components.url.URLFactory;
033: import org.apache.cocoon.environment.Environment;
034: import org.apache.cocoon.environment.Source;
035: import org.apache.cocoon.util.ClassUtils;
036:
037: import java.io.IOException;
038: import java.net.MalformedURLException;
039: import java.net.URL;
040: import java.util.HashMap;
041: import java.util.Iterator;
042: import java.util.Map;
043:
044: /**
045: * @deprecated The Avalon Excalibur Source Resolving is now used.
046: *
047: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
048: * @version CVS $Id: SourceHandlerImpl.java 433543 2006-08-22 06:22:54Z crossley $
049: */
050: public final class SourceHandlerImpl extends AbstractLogEnabled
051: implements Configurable, Disposable, Composable,
052: Contextualizable, SourceHandler {
053:
054: /** The component manager */
055: private ComponentManager manager;
056:
057: /** The url factory */
058: private URLFactory urlFactory;
059:
060: /** The special Source factories */
061: private Map sourceFactories;
062:
063: /** The context */
064: private Context context;
065:
066: /**
067: * Configure the SourceFactories
068: */
069: public void configure(final Configuration conf)
070: throws ConfigurationException {
071: try {
072: if (this .getLogger().isDebugEnabled()) {
073: getLogger().debug("Getting the SourceFactories");
074: }
075: HashMap factories = new HashMap();
076: Configuration[] configs = conf.getChildren("protocol");
077: SourceFactory sourceFactory = null;
078: String protocol = null;
079: for (int i = 0; i < configs.length; i++) {
080: protocol = configs[i].getAttribute("name");
081: if (factories.containsKey(protocol)) {
082: throw new ConfigurationException(
083: "SourceFactory defined twice for protocol: "
084: + protocol);
085: }
086:
087: if (this .getLogger().isDebugEnabled()) {
088: getLogger().debug(
089: "\tfor protocol: " + protocol + " "
090: + configs[i].getAttribute("class"));
091: }
092: sourceFactory = (SourceFactory) ClassUtils
093: .newInstance(configs[i].getAttribute("class"));
094: this .init(sourceFactory, configs[i]);
095: factories.put(protocol, sourceFactory);
096: }
097:
098: this .sourceFactories = java.util.Collections
099: .synchronizedMap(factories);
100: } catch (ConfigurationException e) {
101: throw e;
102: } catch (Exception e) {
103: throw new ConfigurationException(
104: "Could not get parameters because: "
105: + e.getMessage());
106: }
107: }
108:
109: /**
110: * Get the context
111: */
112: public void contextualize(Context context) throws ContextException {
113: this .context = context;
114: }
115:
116: /**
117: * Set the current <code>ComponentManager</code> instance used by this
118: * <code>Composable</code>.
119: */
120: public void compose(ComponentManager manager)
121: throws ComponentException {
122: this .manager = manager;
123: this .urlFactory = (URLFactory) this .manager
124: .lookup(URLFactory.ROLE);
125: }
126:
127: /**
128: * Dispose
129: */
130: public void dispose() {
131: this .manager.release(this .urlFactory);
132:
133: final Iterator iter = this .sourceFactories.values().iterator();
134: SourceFactory current;
135: while (iter.hasNext()) {
136: current = (SourceFactory) iter.next();
137: this .deinit(current);
138: }
139: this .sourceFactories = null;
140: }
141:
142: /**
143: * Get a <code>Source</code> object.
144: */
145: public Source getSource(Environment environment, String location)
146: throws ProcessingException, MalformedURLException,
147: IOException {
148: final int protocolEnd = location.indexOf(':');
149: if (protocolEnd != -1) {
150: final String protocol = location.substring(0, protocolEnd);
151: final SourceFactory sourceFactory = (SourceFactory) this .sourceFactories
152: .get(protocol);
153: if (sourceFactory != null) {
154: return sourceFactory.getSource(environment, location);
155: }
156: }
157:
158: // default implementation
159: Source result = new URLSource(this .urlFactory.getURL(location),
160: this .manager);
161: if (result instanceof LogEnabled) {
162: ((LogEnabled) result).enableLogging(getLogger());
163: }
164: return result;
165: }
166:
167: /**
168: * Get a <code>Source</code> object.
169: */
170: public Source getSource(Environment environment, URL base,
171: String location) throws ProcessingException,
172: MalformedURLException, IOException {
173: final String protocol = base.getProtocol();
174: final SourceFactory sourceFactory = (SourceFactory) this .sourceFactories
175: .get(protocol);
176: if (sourceFactory != null) {
177: return sourceFactory.getSource(environment, base, location);
178: }
179:
180: // default implementation
181: return new URLSource(this .urlFactory.getURL(base, location),
182: this .manager);
183: }
184:
185: /**
186: * Add a factory
187: */
188: public void addFactory(String protocol, SourceFactory factory)
189: throws ProcessingException {
190: try {
191: this .init(factory, null);
192: SourceFactory oldFactory = (SourceFactory) this .sourceFactories
193: .put(protocol, factory);
194: if (oldFactory != null) {
195: deinit(oldFactory);
196: }
197: } catch (ComponentException e) {
198: throw new ProcessingException("cannot initialize factory: "
199: + factory, e);
200: } catch (ContextException e) {
201: throw new ProcessingException("cannot initialize factory: "
202: + factory, e);
203: } catch (ConfigurationException e) {
204: throw new ProcessingException("cannot configure factory: "
205: + factory, e);
206: }
207: }
208:
209: /**
210: * Init a source factory
211: */
212: private void init(SourceFactory factory, Configuration config)
213: throws ContextException, ComponentException,
214: ConfigurationException {
215: if (factory instanceof LogEnabled) {
216: ((LogEnabled) factory).enableLogging(getLogger());
217: }
218: if (factory instanceof Contextualizable) {
219: ((Contextualizable) factory).contextualize(this .context);
220: }
221: if (factory instanceof Composable) {
222: ((Composable) factory).compose(this .manager);
223: }
224: if (config != null && factory instanceof Configurable) {
225: ((Configurable) factory).configure(config);
226: }
227: }
228:
229: /**
230: * Deinit a source factory
231: */
232: private void deinit(SourceFactory factory) {
233: if (factory instanceof Disposable) {
234: ((Disposable) factory).dispose();
235: }
236: }
237:
238: }
|