KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > URLResource


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
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 implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.util;
16
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.Resource;
25
26 /**
27  * An implementation of {@link org.apache.hivemind.Resource}
28  * built around a string representation of a URL.
29  *
30  * @author Howard Lewis Ship
31  */

32 public class URLResource extends AbstractResource
33 {
34     private URL JavaDoc _url;
35
36     public URLResource( URL JavaDoc url )
37     {
38         super( url.toString() );
39         _url = url;
40     }
41
42     public URLResource( String JavaDoc path )
43     {
44         super( path );
45     }
46
47     public String JavaDoc toString()
48     {
49         return getPath();
50     }
51
52     protected Resource newResource( String JavaDoc path )
53     {
54         return new URLResource( path );
55     }
56
57     public URL JavaDoc getResourceURL()
58     {
59         if( _url == null )
60         {
61             try
62             {
63                 URL JavaDoc test = new URL JavaDoc( getPath() );
64                 InputStream JavaDoc stream = test.openStream();
65                 if( stream != null )
66                 {
67                     stream.close();
68                     _url = test;
69                 }
70             }
71             catch( MalformedURLException JavaDoc ex )
72             {
73                 throw new ApplicationRuntimeException( ex );
74             }
75             catch( IOException JavaDoc ex )
76             {
77                 // If the resource can't be opened,
78
// then return null.
79
}
80         }
81         return _url;
82     }
83
84     /**
85      * Always returns this location; no localization check occurs.
86      */

87     public Resource getLocalization( Locale JavaDoc locale )
88     {
89         return this;
90     }
91 }
92
Popular Tags