KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > URLResource


1 // ========================================================================
2
// $Id: URLResource.java,v 1.9 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
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 implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15
package org.mortbay.util;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLConnection JavaDoc;
24 import java.security.Permission JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28
29 /* ------------------------------------------------------------ */
30 /** Abstract resource class.
31  *
32  * @version $Id: URLResource.java,v 1.9 2005/08/13 00:01:28 gregwilkins Exp $
33  * @author Nuno Preguiça
34  * @author Greg Wilkins (gregw)
35  */

36 public class URLResource extends Resource
37 {
38     private static Log log = LogFactory.getLog(URLResource.class);
39
40     protected URL JavaDoc _url;
41     protected String JavaDoc _urlString;
42     protected transient URLConnection JavaDoc _connection;
43     protected transient InputStream JavaDoc _in=null;
44     
45     /* ------------------------------------------------------------ */
46     protected URLResource(URL JavaDoc url, URLConnection JavaDoc connection)
47     {
48         _url = url;
49         _urlString=_url.toString();
50         _connection=connection;
51     }
52
53     /* ------------------------------------------------------------ */
54     protected synchronized boolean checkConnection()
55     {
56         if (_connection==null)
57         {
58             try{
59                 _connection=_url.openConnection();
60             }
61             catch(IOException JavaDoc e)
62             {
63                 LogSupport.ignore(log,e);
64             }
65         }
66         return _connection!=null;
67     }
68
69     /* ------------------------------------------------------------ */
70     /** Release any resources held by the resource.
71      */

72     public synchronized void release()
73     {
74         if (_in!=null)
75         {
76             try{_in.close();}catch(IOException JavaDoc e){LogSupport.ignore(log,e);}
77             _in=null;
78         }
79
80         if (_connection!=null)
81             _connection=null;
82     }
83
84     /* ------------------------------------------------------------ */
85     /**
86      * Returns true if the respresened resource exists.
87      */

88     public boolean exists()
89     {
90         try
91         {
92             synchronized(this)
93             {
94                 if (checkConnection() && _in==null )
95                     _in = _connection.getInputStream();
96             }
97         }
98         catch (IOException JavaDoc e)
99         {
100             LogSupport.ignore(log,e);
101         }
102         return _in!=null;
103     }
104
105     /* ------------------------------------------------------------ */
106     /**
107      * Returns true if the respresenetd resource is a container/directory.
108      * If the resource is not a file, resources ending with "/" are
109      * considered directories.
110      */

111     public boolean isDirectory()
112     {
113         return exists() && _url.toString().endsWith("/");
114     }
115
116
117     /* ------------------------------------------------------------ */
118     /**
119      * Returns the last modified time
120      */

121     public long lastModified()
122     {
123         if (checkConnection())
124             return _connection.getLastModified();
125         return -1;
126     }
127
128
129     /* ------------------------------------------------------------ */
130     /**
131      * Return the length of the resource
132      */

133     public long length()
134     {
135         if (checkConnection())
136             return _connection.getContentLength();
137         return -1;
138     }
139
140     /* ------------------------------------------------------------ */
141     /**
142      * Returns an URL representing the given resource
143      */

144     public URL JavaDoc getURL()
145     {
146         return _url;
147     }
148
149     /* ------------------------------------------------------------ */
150     /**
151      * Returns an File representing the given resource or NULL if this
152      * is not possible.
153      */

154     public File JavaDoc getFile()
155         throws IOException JavaDoc
156     {
157         // Try the permission hack
158
if (checkConnection())
159         {
160             Permission JavaDoc perm = _connection.getPermission();
161             if (perm instanceof java.io.FilePermission JavaDoc)
162                 return new File JavaDoc(perm.getName());
163         }
164
165         // Try the URL file arg
166
try {return new File JavaDoc(_url.getFile());}
167         catch(Exception JavaDoc e) {LogSupport.ignore(log,e);}
168
169         // Don't know the file
170
return null;
171     }
172
173     /* ------------------------------------------------------------ */
174     /**
175      * Returns the name of the resource
176      */

177     public String JavaDoc getName()
178     {
179         return _url.toExternalForm();
180     }
181
182     /* ------------------------------------------------------------ */
183     /**
184      * Returns an input stream to the resource
185      */

186     public synchronized InputStream JavaDoc getInputStream()
187         throws java.io.IOException JavaDoc
188     {
189         if (!checkConnection())
190             throw new IOException JavaDoc( "Invalid resource");
191
192         try
193         {
194             if( _in != null)
195             {
196                 InputStream JavaDoc in = _in;
197                 _in=null;
198                 return in;
199             }
200             return _connection.getInputStream();
201         }
202         finally
203         {
204             _connection=null;
205         }
206     }
207
208
209     /* ------------------------------------------------------------ */
210     /**
211      * Returns an output stream to the resource
212      */

213     public OutputStream JavaDoc getOutputStream()
214         throws java.io.IOException JavaDoc, SecurityException JavaDoc
215     {
216         throw new IOException JavaDoc( "Output not supported");
217     }
218
219     /* ------------------------------------------------------------ */
220     /**
221      * Deletes the given resource
222      */

223     public boolean delete()
224         throws SecurityException JavaDoc
225     {
226         throw new SecurityException JavaDoc( "Delete not supported");
227     }
228
229     /* ------------------------------------------------------------ */
230     /**
231      * Rename the given resource
232      */

233     public boolean renameTo( Resource dest)
234         throws SecurityException JavaDoc
235     {
236         throw new SecurityException JavaDoc( "RenameTo not supported");
237     }
238
239     /* ------------------------------------------------------------ */
240     /**
241      * Returns a list of resource names contained in the given resource
242      */

243     public String JavaDoc[] list()
244     {
245         return null;
246     }
247
248     /* ------------------------------------------------------------ */
249     /**
250      * Returns the resource contained inside the current resource with the
251      * given name
252      */

253     public Resource addPath(String JavaDoc path)
254         throws IOException JavaDoc,MalformedURLException JavaDoc
255     {
256         if (path==null)
257             return null;
258
259         path = URI.canonicalPath(path);
260
261         return newResource(URI.addPaths(_url.toExternalForm(),path));
262     }
263
264     /* ------------------------------------------------------------ */
265     public String JavaDoc toString()
266     {
267         return _urlString;
268     }
269
270     /* ------------------------------------------------------------ */
271     public int hashCode()
272     {
273         return _url.hashCode();
274     }
275     
276     /* ------------------------------------------------------------ */
277     public boolean equals( Object JavaDoc o)
278     {
279         return o instanceof URLResource &&
280             _url.equals(((URLResource)o)._url);
281     }
282
283 }
284
Popular Tags