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.url;
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.avalon.framework.parameters.ParameterException;
032: import org.apache.avalon.framework.parameters.Parameterizable;
033: import org.apache.avalon.framework.parameters.Parameters;
034: import org.apache.avalon.framework.thread.ThreadSafe;
035: import org.apache.cocoon.Constants;
036: import org.apache.cocoon.util.ClassUtils;
037:
038: import java.io.File;
039: import java.net.MalformedURLException;
040: import java.net.URL;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.Map;
044:
045: /**
046: * @deprecated by the new source resolving of avalon excalibur
047: *
048: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
049: * @version CVS $Id: URLFactoryImpl.java 433543 2006-08-22 06:22:54Z crossley $
050: */
051: public class URLFactoryImpl extends AbstractLogEnabled implements
052: ThreadSafe, Configurable, Disposable, Composable,
053: Contextualizable, URLFactory {
054:
055: /**
056: * The context
057: */
058: protected Context context;
059:
060: /**
061: * The special URL factories
062: */
063: protected Map factories;
064:
065: /** The component manager */
066: private ComponentManager manager;
067:
068: /**
069: * Create a URL from a location. This method supports specific
070: * pseudo-protocol as defined in its configuration
071: *
072: * @param location The location
073: * @return The URL pointed to by the location
074: * @exception MalformedURLException If the location is malformed
075: */
076: public URL getURL(String location) throws MalformedURLException {
077: Iterator iter = factories.keySet().iterator();
078: String protocol = null;
079: while (iter.hasNext()) {
080: protocol = (String) iter.next();
081: if (location.startsWith(protocol + "://")) {
082: return ((URLFactory) factories.get(protocol))
083: .getURL(location
084: .substring(protocol.length() + 3));
085: }
086: }
087: try {
088: if (getLogger().isDebugEnabled()) {
089: getLogger().debug("Making URL from " + location);
090: }
091: return new URL(location);
092: } catch (MalformedURLException mue) {
093: if (getLogger().isDebugEnabled()) {
094: getLogger()
095: .debug(
096: "Making URL - MalformedURLException in getURL:",
097: mue);
098: }
099:
100: org.apache.cocoon.environment.Context envContext = null;
101: try {
102: envContext = (org.apache.cocoon.environment.Context) context
103: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
104: } catch (ContextException e) {
105: getLogger().error(
106: "Making URL - ContextException in getURL", e);
107: }
108:
109: final String path = envContext.getRealPath(location);
110: if (path != null)
111: return (new File(path)).toURL();
112:
113: if (getLogger().isDebugEnabled()) {
114: getLogger().debug("Making URL a Resource:" + location);
115: }
116: URL url = envContext.getResource(location);
117: if (url != null)
118: return url;
119:
120: if (getLogger().isDebugEnabled()) {
121: getLogger().debug(
122: "Making URL a File (assuming that it is full path):"
123: + location);
124: }
125: return (new File(location)).toURL();
126: }
127: }
128:
129: public URL getURL(URL base, String location)
130: throws MalformedURLException {
131: if (base != null) {
132: if (base.getProtocol().equals("file")) {
133: File temp = new File(base.toExternalForm().substring(
134: "file:".length()), location);
135: String path = temp.getAbsolutePath();
136: // VG: M$ paths starts with drive letter
137: if (path.charAt(0) != File.separator.charAt(0)) {
138: return getURL("file:/" + path);
139: } else {
140: return getURL("file:" + path);
141: }
142: }
143:
144: return getURL(new URL(base, location).toExternalForm());
145: } else {
146: return getURL(location);
147: }
148: }
149:
150: /**
151: * Get the context
152: */
153: public void contextualize(Context context) throws ContextException {
154: if (this .context == null) {
155: this .context = context;
156: }
157: }
158:
159: /**
160: * Configure the URLFactories
161: */
162: public void configure(final Configuration conf)
163: throws ConfigurationException {
164: try {
165: getLogger().debug("Getting the URLFactories");
166: factories = new HashMap();
167: Configuration[] configs = conf.getChildren("protocol");
168: URLFactory urlFactory = null;
169: String protocol = null;
170: for (int i = 0; i < configs.length; i++) {
171: protocol = configs[i].getAttribute("name");
172: if (factories.containsKey(protocol)) {
173: throw new ConfigurationException(
174: "URLFactory defined twice for protocol: "
175: + protocol);
176: }
177: if (getLogger().isDebugEnabled()) {
178: getLogger().debug(
179: "\tfor protocol: " + protocol + " "
180: + configs[i].getAttribute("class"));
181: }
182: urlFactory = (URLFactory) ClassUtils
183: .newInstance(configs[i].getAttribute("class"));
184: this .init(urlFactory, configs[i]);
185: factories.put(protocol, urlFactory);
186: }
187: } catch (Exception e) {
188: getLogger().error("Could not get URLFactories", e);
189: throw new ConfigurationException(
190: "Could not get parameters because: "
191: + e.getMessage());
192: }
193: }
194:
195: /**
196: * Set the current <code>ComponentManager</code> instance used by this
197: * <code>Composable</code>.
198: */
199: public void compose(ComponentManager manager)
200: throws ComponentException {
201: this .manager = manager;
202: }
203:
204: /**
205: * Dispose
206: */
207: public void dispose() {
208: Iterator iter = this .factories.values().iterator();
209: URLFactory current;
210: while (iter.hasNext()) {
211: current = (URLFactory) iter.next();
212: this .deinit(current);
213: }
214: this .factories = null;
215: }
216:
217: /**
218: * Init a url factory
219: */
220: private void init(URLFactory factory, Configuration config)
221: throws ContextException, ComponentException,
222: ConfigurationException, ParameterException {
223: if (factory instanceof LogEnabled) {
224: ((LogEnabled) factory).enableLogging(getLogger());
225: }
226: if (factory instanceof Contextualizable) {
227: ((Contextualizable) factory).contextualize(this .context);
228: }
229: if (factory instanceof Composable) {
230: ((Composable) factory).compose(this .manager);
231: }
232: if (config != null && factory instanceof Configurable) {
233: ((Configurable) factory).configure(config);
234: }
235: if (config != null && factory instanceof Parameterizable) {
236: ((Parameterizable) factory).parameterize(Parameters
237: .fromConfiguration(config));
238: }
239: }
240:
241: /**
242: * Deinit a url factory
243: */
244: private void deinit(URLFactory factory) {
245: if (factory instanceof Disposable) {
246: ((Disposable) factory).dispose();
247: }
248: }
249: }
|