KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStream JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLConnection JavaDoc;
24
25 import org.apache.excalibur.source.Source;
26 import org.apache.excalibur.source.SourceException;
27 import org.apache.excalibur.source.SourceNotFoundException;
28 import org.apache.excalibur.source.SourceUtil;
29 import org.apache.excalibur.source.SourceValidity;
30 import org.apache.excalibur.source.impl.validity.NOPValidity;
31
32 /**
33  * Description of a source which is described by the resource protocol
34  * which gets a resource from the classloader.
35  *
36  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
37  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:24 $
38  */

39 public final class ResourceSource
40     extends AbstractSource
41     implements Source
42 {
43     /** Location of the resource */
44     private URL JavaDoc m_location;
45     private String JavaDoc m_mimeType;
46
47     public ResourceSource( final String JavaDoc systemId ) throws MalformedURLException JavaDoc
48     {
49         final int pos = SourceUtil.indexOfSchemeColon(systemId);
50         if (pos == -1 || ! systemId.startsWith("://", pos))
51         {
52             throw new MalformedURLException JavaDoc("Invalid format for ResourceSource : " + systemId);
53         }
54         
55         setSystemId(systemId);
56         m_location = getClassLoader().getResource(systemId.substring( pos + 3 ));
57         setScheme(systemId.substring(0, pos));
58     }
59     
60     public boolean exists()
61     {
62         return m_location != null;
63     }
64     
65     protected void checkInfos()
66     {
67         super.checkInfos();
68
69         URLConnection JavaDoc connection = null;
70         try
71         {
72             connection = m_location.openConnection();
73             setLastModified(connection.getLastModified());
74             setContentLength(connection.getContentLength());
75             m_mimeType = connection.getContentType();
76         }
77         catch(IOException JavaDoc ioe)
78         {
79             setLastModified(0);
80             setContentLength(-1);
81             m_mimeType = null;
82         }
83     }
84     
85     public String JavaDoc getMimeType()
86     {
87         return m_mimeType;
88     }
89
90     /**
91      * Return an <code>InputStream</code> object to read from the source.
92      */

93     public InputStream JavaDoc getInputStream()
94         throws IOException JavaDoc, SourceException
95     {
96
97         InputStream JavaDoc in = m_location.openStream();
98         if ( in == null )
99           throw new SourceNotFoundException( "Source '"+m_location+"' was not found" );
100         return in;
101     }
102
103     /**
104      * Returns {@link NOPValidity#SHARED_INSTANCE} since a resource doesn't change.
105      *
106      */

107     public SourceValidity getValidity()
108     {
109         // we are always valid
110
return NOPValidity.SHARED_INSTANCE;
111     }
112     
113     protected ClassLoader JavaDoc getClassLoader() {
114         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
115         if( loader == null )
116         {
117             loader = getClass().getClassLoader();
118         }
119         
120         return loader;
121     }
122 }
123
Popular Tags