KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > source > impl > URLSourceFactory


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.source.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.excalibur.source.Source;
27 import org.apache.excalibur.source.SourceFactory;
28
29 /**
30  * A factory for a {@link URL} wrapper
31  *
32  * @avalon.component
33  * @avalon.service type=SourceFactory
34  * @x-avalon.info name=url-source
35  * @x-avalon.lifestyle type=singleton
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version $Id: URLSourceFactory.java,v 1.4 2004/02/28 11:47:24 cziegeler Exp $
39  */

40 public class URLSourceFactory extends AbstractLogEnabled implements SourceFactory, ThreadSafe
41 {
42
43     /**
44      * Create an URL-based source. This class actually creates an {@link URLSource}, but if another
45      * implementation is needed, subclasses can override this method.
46      */

47     protected Source createURLSource(URL JavaDoc url, Map JavaDoc parameters) throws MalformedURLException JavaDoc, IOException JavaDoc
48     {
49         URLSource result = new URLSource();
50         result.init(url, parameters);
51         return result;
52     }
53
54     /**
55      * Create an file-based source. This class actually creates an {@link FileSource}, but if another
56      * implementation is needed, subclasses can override this method.
57      */

58     protected Source createFileSource(String JavaDoc uri) throws MalformedURLException JavaDoc, IOException JavaDoc
59     {
60         return new FileSource(uri);
61     }
62
63     /**
64      * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
65      */

66     public Source getSource(String JavaDoc uri, Map JavaDoc parameters) throws MalformedURLException JavaDoc, IOException JavaDoc
67     {
68         if (getLogger().isDebugEnabled())
69         {
70             final String JavaDoc message = "Creating source object for " + uri;
71             getLogger().debug(message);
72         }
73
74         // First check if it's a file
75
if (uri.startsWith("file:"))
76         {
77             // Yes : return a file source
78
return createFileSource(uri);
79         }
80         else
81         {
82             // Not a "file:" : create an URLSource
83
// First try to create the URL
84
URL JavaDoc url;
85             try
86             {
87                 url = new URL JavaDoc(uri);
88             }
89             catch (MalformedURLException JavaDoc mue)
90             {
91                 // Maybe a file name containing a ':' ?
92
if (getLogger().isDebugEnabled())
93                 {
94                     this.getLogger().debug("URL " + uri + " is malformed. Assuming it's a file path.", mue);
95                 }
96                 return createFileSource(uri);
97             }
98
99             return createURLSource(url, parameters);
100         }
101     }
102
103     /**
104      * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
105      */

106     public void release(Source source)
107     {
108         // do nothing here
109
}
110 }
111
Popular Tags