KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > source > SourceResolverImpl


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.source;
9
10 import org.apache.avalon.framework.activity.Disposable;
11 import org.apache.avalon.framework.component.Component;
12 import org.apache.avalon.framework.component.ComponentException;
13 import org.apache.avalon.framework.component.ComponentManager;
14 import org.apache.avalon.framework.component.ComponentSelector;
15 import org.apache.avalon.framework.component.Composable;
16 import org.apache.avalon.framework.configuration.Configurable;
17 import org.apache.avalon.framework.configuration.Configuration;
18 import org.apache.avalon.framework.configuration.ConfigurationException;
19 import org.apache.avalon.framework.context.Context;
20 import org.apache.avalon.framework.context.ContextException;
21 import org.apache.avalon.framework.context.Contextualizable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.logger.LogEnabled;
24 import org.apache.avalon.framework.thread.ThreadSafe;
25 import org.apache.avalon.excalibur.pool.Recyclable;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31
32
33 /**
34  * Base interface for resolving a source by system identifiers.
35  * Instead of using the java.net.URL classes which prevent you
36  * to add your own custom protocols in a server environment,
37  * you should use this resolver for all URLs.
38  *
39  * The resolver creates for each source a <code>Source</code>
40  * object, which could then be asked for an <code>InputStream</code>
41  * etc.
42  *
43  * When the <code>Source</code> object is no longer needed
44  * it must be released using the resolver. This is very similar like
45  * looking up components from a <code>ComponentManager</code>
46  * and releasing them.
47  *
48  * It looks for the base URL in the <code>Context</code> object with
49  * the "container.rootDir" entry. If the entry does not exist, it is
50  * populated with the system property "user.dir".
51  *
52  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
53  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
54  * @version $Id: SourceResolverImpl.java,v 1.13 2002/01/25 21:12:56 bloritsch Exp $
55  */

56 public class SourceResolverImpl
57 extends AbstractLogEnabled
58 implements Composable,
59            Contextualizable,
60            Disposable,
61            SourceResolver,
62            ThreadSafe
63 {
64
65     /** The component manager */
66     protected ComponentManager m_manager;
67
68     /** The special Source factories */
69     protected ComponentSelector m_factorySelector;
70
71     /** The context */
72     protected Context m_context;
73
74     /**
75      * The base URL
76      */

77     protected URL JavaDoc m_baseURL;
78
79     /**
80      * Get the context
81      */

82     public void contextualize(Context context)
83     throws ContextException
84     {
85         m_context = context;
86
87         try
88         {
89             m_baseURL = ((File JavaDoc) m_context.get("container.rootDir")).toURL();
90         }
91         catch (ContextException ce)
92         {
93             // set the base URL to the current directory
94
try
95             {
96                 m_baseURL = new File JavaDoc(System.getProperty("user.dir")).toURL();
97                 if ( this.getLogger().isDebugEnabled() )
98                 {
99                     this.getLogger().debug("SourceResolver: Using base URL: " + m_baseURL);
100                 }
101             }
102             catch (MalformedURLException JavaDoc mue)
103             {
104                 throw new ContextException("Malformed URL for user.dir, and no container.rootDir exists", mue);
105             }
106         }
107         catch (MalformedURLException JavaDoc mue)
108         {
109             throw new ContextException("Malformed URL for container.rootDir", mue);
110         }
111     }
112
113     /**
114      * Set the current <code>ComponentManager</code> instance used by this
115      * <code>Composable</code>.
116      */

117     public void compose(ComponentManager manager)
118     throws ComponentException
119     {
120         m_manager = manager;
121         m_factorySelector = (ComponentSelector)m_manager.lookup(SourceFactory.ROLE + "Selector");
122     }
123
124     /**
125      * Dispose
126      */

127     public void dispose()
128     {
129         if (m_manager != null)
130         {
131             m_manager.release(m_factorySelector);
132             m_factorySelector = null;
133         }
134     }
135
136     /**
137      * Get a <code>Source</code> object.
138      */

139     public Source resolve(String JavaDoc location)
140     throws MalformedURLException JavaDoc, IOException JavaDoc, ComponentException
141     {
142         return this.resolve(m_baseURL, location, null);
143     }
144
145     /**
146      * Get a <code>Source</code> object.
147      */

148     public Source resolve(String JavaDoc location,
149                           SourceParameters parameters)
150     throws MalformedURLException JavaDoc, IOException JavaDoc, ComponentException
151     {
152         return this.resolve( m_baseURL, location, parameters );
153     }
154
155     /**
156      * Get a <code>Source</code> object.
157      */

158     public Source resolve(URL JavaDoc base, String JavaDoc location)
159     throws MalformedURLException JavaDoc, IOException JavaDoc, ComponentException
160     {
161         return this.resolve(base, location, null);
162     }
163
164
165     /**
166      * Get a <code>Source</code> object.
167      */

168     public Source resolve(URL JavaDoc base,
169                           String JavaDoc location,
170                           SourceParameters parameters)
171     throws MalformedURLException JavaDoc, IOException JavaDoc, ComponentException
172     {
173         if ( this.getLogger().isDebugEnabled() ) {
174             this.getLogger().debug("Resolving '"+location+"' in context '" + base + "'");
175         }
176         if (location == null) throw new MalformedURLException JavaDoc("Invalid System ID");
177
178         // first step: create systemID
179
String JavaDoc systemID;
180         if (base == null) base = m_baseURL;
181
182         if (location.length() == 0) {
183             systemID = base.toExternalForm();
184         } else if (location.indexOf(":") > 1)
185         {
186             systemID = location;
187         }
188         else if (location.charAt(0) == '/')
189         {
190             systemID = new StringBuffer JavaDoc(base.getProtocol())
191                            .append(":").append(location).toString();
192         // windows: absolute paths can start with drive letter
193
}
194         else if (location.length() > 1 && location.charAt(1) == ':')
195         {
196             systemID = new StringBuffer JavaDoc(base.getProtocol())
197                            .append(":/").append(location).toString();
198         }
199         else
200         {
201             if (base.getProtocol().equals("file") == true)
202             {
203                 File JavaDoc temp = new File JavaDoc(base.toExternalForm().substring("file:".length()), location);
204                 String JavaDoc path = temp.getAbsolutePath();
205                 // windows paths starts with drive letter
206
if (path.charAt(0) != File.separator.charAt(0))
207                 {
208                     systemID = "file:/" + path;
209                 }
210                 else
211                 {
212                     systemID = "file:" + path;
213                 }
214             }
215             else
216             {
217                 systemID = new URL JavaDoc(base, location).toExternalForm();
218             }
219         }
220         if ( this.getLogger().isDebugEnabled() )
221         {
222             this.getLogger().debug("Resolved to systemID '"+systemID+"'");
223         }
224
225         Source source = null;
226         // search for a SourceFactory implementing the protocol
227
final int protocolPos = systemID.indexOf(':');
228         if ( protocolPos != -1 )
229         {
230             final String JavaDoc protocol = systemID.substring(0, protocolPos);
231             if ( m_factorySelector.hasComponent(protocol) )
232             {
233                 SourceFactory factory = null;
234                 try
235                 {
236                     factory = ( SourceFactory )m_factorySelector.select( protocol );
237                     source = factory.getSource( systemID, parameters );
238                 }
239                 finally
240                 {
241                     m_factorySelector.release( factory );
242                 }
243             }
244         }
245
246         if ( null == source )
247         {
248             // no factory found, so usual url handling stuff...
249
try
250             {
251                 if (this.getLogger().isDebugEnabled() == true)
252                 {
253                     getLogger().debug("Making URL from " + systemID);
254                 }
255                 source = new URLSource(new URL JavaDoc(systemID), parameters);
256             }
257             catch (MalformedURLException JavaDoc mue)
258             {
259                 if ( this.getLogger().isDebugEnabled() )
260                 {
261                     getLogger().debug("Making URL - MalformedURLException in getURL:" , mue);
262                     getLogger().debug("Making URL a File (assuming that it is full path):" + systemID);
263                 }
264                 source = new URLSource((new File JavaDoc(systemID)).toURL(), parameters);
265             }
266         }
267         if (source instanceof LogEnabled)
268         {
269             ((LogEnabled) source).enableLogging(getLogger());
270         }
271         try
272         {
273             if (source instanceof Contextualizable)
274             {
275                 ((Contextualizable) source).contextualize (m_context);
276             }
277         }
278         catch (ContextException ce)
279         {
280             throw new ComponentException("ContextException occured during source resolving.", ce);
281         }
282
283         if (source instanceof Composable)
284         {
285             ((Composable) source).compose(m_manager);
286         }
287         return source;
288     }
289
290     /**
291      * Releases a resolved resource
292      */

293     public void release( Source source )
294     {
295         if ( source == null) return;
296         if ( source instanceof Recyclable )
297         {
298             ((Recyclable)source).recycle();
299         }
300         if ( source instanceof Disposable )
301         {
302             ((Disposable) source).dispose();
303         }
304     }
305
306 }
307
Popular Tags