KickJava   Java API By Example, From Geeks To Geeks.

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


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.component.Composable;
11 import org.apache.avalon.framework.component.ComponentManager;
12 import org.apache.avalon.excalibur.monitor.FileResource;
13 import org.apache.avalon.excalibur.monitor.Monitorable;
14 import org.apache.avalon.excalibur.monitor.Resource;
15 import org.apache.avalon.excalibur.monitor.SourceResource;
16 import org.apache.avalon.excalibur.source.validity.TimeStampValidity;
17 import org.apache.avalon.excalibur.xml.Parser;
18 import org.apache.avalon.excalibur.xml.XMLConsumer;
19 import org.apache.avalon.excalibur.xml.XMLizable;
20 import org.xml.sax.ContentHandler JavaDoc;
21 import org.xml.sax.InputSource JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.ext.LexicalHandler JavaDoc;
24
25 import java.io.*;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLConnection JavaDoc;
29
30 /**
31  * Description of a source which is described by an URL.
32  *
33  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
34  * @version CVS $Revision: 1.11 $ $Date: 2002/01/08 13:43:48 $
35  */

36
37 public final class URLSource
38 implements Composable, ModifiableSource, XMLizable, Monitorable {
39
40     /** Identifier for file urls */
41     private final String JavaDoc FILE = "file:";
42
43     /** The last modification date or 0 */
44     private long lastModificationDate;
45
46     /** The content length */
47     private long contentLength;
48
49     /** The system id */
50     private String JavaDoc systemId;
51
52     /** The URL of the source */
53     private URL JavaDoc url;
54
55     /** The connection for a real URL */
56     private URLConnection JavaDoc connection;
57
58     /** Is this a file or a "real" URL */
59     private boolean isFile;
60
61     /** Are we initialized? */
62     private boolean gotInfos;
63
64     /** The ComponentManager needed for streaming */
65     private ComponentManager manager;
66
67     /**
68      * Construct a new object
69      * @param parameters This is optional
70      */

71     public URLSource(URL JavaDoc url,
72                      SourceParameters parameters)
73     throws IOException {
74         this.systemId = url.toExternalForm();
75         this.isFile = systemId.startsWith(FILE);
76         this.url = url;
77         this.gotInfos = false;
78     }
79
80     public void compose(ComponentManager manager )
81     {
82         this.manager = manager;
83     }
84
85     /**
86      * Get the last modification date and content length of the source.
87      * Any exceptions are ignored.
88      */

89     private void getInfos() {
90         if (this.gotInfos == false) {
91             if (this.isFile == true) {
92                 File file = new File(this.systemId.substring(FILE.length()));
93                 this.lastModificationDate = file.lastModified();
94                 this.contentLength = file.length();
95             } else {
96                 try {
97                     if (this.connection == null) {
98                         this.connection = this.url.openConnection();
99                         String JavaDoc userInfo = this.getUserInfo();
100                         if (this.url.getProtocol().startsWith("http") == true && userInfo != null) {
101                             this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo));
102                         }
103                     }
104                     this.lastModificationDate = this.connection.getLastModified();
105                     this.contentLength = this.connection.getContentLength();
106                 } catch (IOException ignore) {
107                     this.lastModificationDate = 0;
108                     this.contentLength = -1;
109                 }
110             }
111             this.gotInfos = true;
112         }
113     }
114
115     /**
116      * Get the last modification date of the source or 0 if it
117      * is not possible to determine the date.
118      */

119     public long getLastModified() {
120         this.getInfos();
121         return this.lastModificationDate;
122     }
123
124     /**
125      * Get the corresponding Resource object for monitoring.
126      */

127     public Resource getResource()
128     throws Exception JavaDoc {
129         this.getInfos();
130         if (this.isFile == true) {
131             return new FileResource(this.systemId.substring(FILE.length()));
132         } else {
133             return new SourceResource(this);
134         }
135     }
136
137     /**
138      * Get the content length of the source or -1 if it
139      * is not possible to determine the length.
140      */

141     public long getContentLength() {
142         this.getInfos();
143         return this.contentLength;
144     }
145
146     /**
147      * Return an <code>InputStream</code> object to read from the source.
148      *
149      * @throws ResourceNotFoundException if file not found or
150      * HTTP location does not exist.
151      * @throws IOException if I/O error occured.
152      */

153     public InputStream getInputStream()
154     throws IOException {
155         this.getInfos();
156         InputStream input = null;
157         if (this.isFile == true) {
158             input = new FileInputStream(this.systemId.substring(FILE.length()));
159         } else {
160             if (this.connection == null) {
161                 this.connection = this.url.openConnection();
162                 /* The following requires a jdk 1.3 */
163                 String JavaDoc userInfo = this.getUserInfo();
164                 if (this.url.getProtocol().startsWith("http") == true && userInfo != null) {
165                     this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo));
166                 }
167             }
168
169             input = this.connection.getInputStream();
170             this.connection = null; // make sure a new connection is created next time
171
}
172         return input;
173     }
174
175     private static boolean checkedURLClass = false;
176     private static boolean urlSupportsGetUserInfo = false;
177     private static Method JavaDoc urlGetUserInfo = null;
178     private static Object JavaDoc[] emptyParams = new Object JavaDoc[0];
179
180     /**
181      * Check if the <code>URL</code> class supports the getUserInfo()
182      * method which is introduced in jdk 1.3
183      */

184     private String JavaDoc getUserInfo() {
185         if (URLSource.checkedURLClass == true) {
186             if (URLSource.urlSupportsGetUserInfo == true) {
187                 try {
188                     return (String JavaDoc) URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams);
189                 } catch (Exception JavaDoc e){
190                     // ignore this anyway
191
}
192             }
193             return null;
194         } else {
195             // test if the url class supports the getUserInfo method
196
try {
197                 URLSource.urlGetUserInfo = URL JavaDoc.class.getMethod("getUserInfo", null);
198                 String JavaDoc ui = (String JavaDoc)URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams);
199                 URLSource.checkedURLClass = true;
200                 URLSource.urlSupportsGetUserInfo = true;
201                 return ui;
202             } catch (Exception JavaDoc e){
203             }
204             URLSource.checkedURLClass = true;
205             URLSource.urlSupportsGetUserInfo = false;
206             URLSource.urlGetUserInfo = null;
207             return null;
208         }
209     }
210
211     /**
212      * Return the unique identifer for this source
213      */

214     public String JavaDoc getSystemId() {
215         return this.systemId;
216     }
217
218     /**
219      * Get the Validity object. This can either wrap the last modification
220      * date or the expires information or...
221      * If it is currently not possible to calculate such an information
222      * <code>null</code> is returned.
223      */

224     public SourceValidity getValidity() {
225         final long lm = this.getLastModified();
226         if (lm == -1) {
227             return null;
228         } else {
229             return new TimeStampValidity(lm);
230         }
231     }
232
233     /**
234      * Refresh this object and update the last modified date
235      * and content length.
236      */

237     public void discardValidity() {
238         // reset connection
239
this.connection = null;
240         this.gotInfos = false;
241     }
242
243     /**
244      * Return a new <code>InputSource</code> object
245      *
246      * @throws IOException if I/O error occured.
247      */

248     protected InputSource JavaDoc getInputSource()
249     throws IOException
250     {
251         InputSource JavaDoc newObject = new InputSource JavaDoc(this.getInputStream());
252         newObject.setSystemId(this.systemId);
253         return newObject;
254     }
255
256     /**
257      * Stream content to a content handler or to an XMLConsumer.
258      *
259      * @throws ResourceNotFoundException if file not found or
260      * HTTP location does not exist.
261      * @throws SAXException if failed to parse source document.
262      */

263     public void toSAX(ContentHandler JavaDoc handler)
264     throws SAXException JavaDoc
265     {
266         Parser parser = null;
267         try {
268             parser = (Parser)this.manager.lookup(Parser.ROLE);
269
270             parser.parse(this.getInputSource(), handler);
271         } catch (SAXException JavaDoc e) {
272             // Preserve original exception
273
throw e;
274         } catch (Exception JavaDoc e){
275             throw new SAXException JavaDoc("Exception during processing of "
276                                           + this.systemId, e);
277         } finally {
278             if (parser != null) this.manager.release(parser);
279         }
280     }
281
282 }
283
Popular Tags