KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > url > URLFactoryImpl


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.url;
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.avalon.framework.parameters.ParameterException;
31 import org.apache.avalon.framework.parameters.Parameterizable;
32 import org.apache.avalon.framework.parameters.Parameters;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34 import org.apache.cocoon.Constants;
35 import org.apache.cocoon.util.ClassUtils;
36
37 import java.io.File JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44
45 /**
46  * @deprecated by the new source resolving of avalon excalibur
47  *
48  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
49  * @version CVS $Id: URLFactoryImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
50  */

51 public class URLFactoryImpl extends AbstractLogEnabled
52     implements ThreadSafe, Configurable, Disposable, Composable, Contextualizable, URLFactory {
53
54     /**
55      * The context
56      */

57     protected Context context;
58
59     /**
60      * The special URL factories
61      */

62     protected Map JavaDoc factories;
63
64     /** The component manager */
65     private ComponentManager manager;
66
67     /**
68      * Create a URL from a location. This method supports specific
69      * pseudo-protocol as defined in its configuration
70      *
71      * @param location The location
72      * @return The URL pointed to by the location
73      * @exception MalformedURLException If the location is malformed
74      */

75     public URL JavaDoc getURL(String JavaDoc location) throws MalformedURLException JavaDoc {
76         Iterator JavaDoc iter = factories.keySet().iterator();
77         String JavaDoc protocol = null;
78         while (iter.hasNext()) {
79             protocol = (String JavaDoc)iter.next();
80             if (location.startsWith(protocol + "://")) {
81                 return ((URLFactory)factories.get(protocol)).getURL(location.substring(protocol.length() + 3));
82             }
83         }
84         try {
85             if (getLogger().isDebugEnabled()) {
86                 getLogger().debug("Making URL from " + location);
87             }
88             return new URL JavaDoc(location);
89         } catch (MalformedURLException JavaDoc mue) {
90             if (getLogger().isDebugEnabled()) {
91                 getLogger().debug("Making URL - MalformedURLException in getURL:" , mue);
92             }
93
94             org.apache.cocoon.environment.Context envContext = null;
95             try {
96                 envContext = (org.apache.cocoon.environment.Context)
97                                 context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
98             } catch (ContextException e){
99                 getLogger().error("Making URL - ContextException in getURL",e);
100             }
101
102             final String JavaDoc path = envContext.getRealPath(location);
103             if (path != null)
104                 return (new File JavaDoc(path)).toURL();
105
106             if (getLogger().isDebugEnabled()) {
107                 getLogger().debug("Making URL a Resource:" + location);
108             }
109             URL JavaDoc url = envContext.getResource(location);
110             if(url != null)
111                 return url;
112
113             if (getLogger().isDebugEnabled()) {
114                 getLogger().debug("Making URL a File (assuming that it is full path):" + location);
115             }
116             return (new File JavaDoc(location)).toURL();
117         }
118     }
119
120     public URL JavaDoc getURL(URL JavaDoc base, String JavaDoc location) throws MalformedURLException JavaDoc {
121         if ( base != null ) {
122             if (base.getProtocol().equals("file")) {
123                 File JavaDoc temp = new File JavaDoc(base.toExternalForm().substring("file:".length()), location);
124                 String JavaDoc path = temp.getAbsolutePath();
125                 // VG: M$ paths starts with drive letter
126
if (path.charAt(0) != File.separator.charAt(0)) {
127                     return getURL("file:/" + path);
128                 } else {
129                     return getURL("file:" + path);
130                 }
131             }
132
133             return getURL(new URL JavaDoc(base, location).toExternalForm());
134         } else {
135             return getURL(location);
136         }
137     }
138
139     /**
140      * Get the context
141      */

142     public void contextualize(Context context) throws ContextException {
143         if (this.context == null) {
144             this.context = context;
145         }
146     }
147
148     /**
149      * Configure the URLFactories
150      */

151     public void configure(final Configuration conf)
152     throws ConfigurationException {
153         try {
154             getLogger().debug("Getting the URLFactories");
155             factories = new HashMap JavaDoc();
156             Configuration[] configs = conf.getChildren("protocol");
157             URLFactory urlFactory = null;
158             String JavaDoc protocol = null;
159             for (int i = 0; i < configs.length; i++) {
160                 protocol = configs[i].getAttribute("name");
161                 if (factories.containsKey(protocol)) {
162                     throw new ConfigurationException("URLFactory defined twice for protocol: " + protocol);
163                 }
164                 if (getLogger().isDebugEnabled()) {
165                     getLogger().debug("\tfor protocol: " + protocol + " " + configs[i].getAttribute("class"));
166                 }
167                 urlFactory = (URLFactory) ClassUtils.newInstance(configs[i].getAttribute("class"));
168                 this.init(urlFactory, configs[i]);
169                 factories.put(protocol, urlFactory);
170             }
171         } catch (Exception JavaDoc e) {
172             getLogger().error("Could not get URLFactories", e);
173             throw new ConfigurationException("Could not get parameters because: " +
174                                            e.getMessage());
175         }
176     }
177
178     /**
179      * Set the current <code>ComponentManager</code> instance used by this
180      * <code>Composable</code>.
181      */

182     public void compose(ComponentManager manager)
183     throws ComponentException {
184         this.manager = manager;
185     }
186
187     /**
188      * Dispose
189      */

190     public void dispose() {
191         Iterator JavaDoc iter = this.factories.values().iterator();
192         URLFactory current;
193         while (iter.hasNext()) {
194             current = (URLFactory) iter.next();
195             this.deinit(current);
196         }
197         this.factories = null;
198     }
199
200     /**
201      * Init a url factory
202      */

203     private void init(URLFactory factory, Configuration config)
204     throws ContextException, ComponentException, ConfigurationException, ParameterException {
205         if (factory instanceof LogEnabled) {
206             ((LogEnabled) factory).enableLogging(getLogger());
207         }
208         if (factory instanceof Contextualizable) {
209             ((Contextualizable) factory).contextualize (this.context);
210         }
211         if (factory instanceof Composable) {
212             ((Composable) factory).compose(this.manager);
213         }
214         if (config != null && factory instanceof Configurable) {
215             ((Configurable) factory).configure(config);
216         }
217         if (config != null && factory instanceof Parameterizable) {
218             ((Parameterizable) factory).parameterize(Parameters.fromConfiguration(config));
219         }
220     }
221
222     /**
223      * Deinit a url factory
224      */

225     private void deinit(URLFactory factory) {
226         if (factory instanceof Disposable) {
227             ((Disposable) factory).dispose();
228         }
229     }
230 }
231
Popular Tags