KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > util > ResourceUtils


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

16 package org.apache.log4j.lf5.util;
17
18 import java.io.InputStream JavaDoc;
19 import java.net.URL JavaDoc;
20
21 /**
22  * ResourceUtils. Provide a set of convenience methods for working with
23  * Resources.
24  *
25  * @see org.apache.log4j.lf5.util.Resource
26  *
27  * @author Michael J. Sikorsky
28  * @author Robert Shaw
29  */

30
31 // Contributed by ThoughtWorks Inc.
32

33 public class ResourceUtils {
34   //--------------------------------------------------------------------------
35
// Constants:
36
//--------------------------------------------------------------------------
37

38   //--------------------------------------------------------------------------
39
// Protected Variables:
40
//--------------------------------------------------------------------------
41

42   //--------------------------------------------------------------------------
43
// Private Variables:
44
//--------------------------------------------------------------------------
45

46   //--------------------------------------------------------------------------
47
// Constructors:
48
//--------------------------------------------------------------------------
49

50   //--------------------------------------------------------------------------
51
// Public Methods:
52
//--------------------------------------------------------------------------
53

54   /**
55    * Get the InputStream for this resource. Note: to convert an InputStream
56    * into an InputReader, use: new InputStreamReader(InputStream).
57    *
58    * @param object The object to grab the Classloader from.
59    * This parameter is quite important from a
60    * visibility of resources standpoint as the
61    * hierarchy of Classloaders plays a role.
62    *
63    * @param resource The resource to load.
64    *
65    * @return If the Resource was found, the InputStream, otherwise null.
66    *
67    * @see Resource
68    * @see #getResourceAsURL(Object,Resource)
69    * @see InputStream
70    */

71   public static InputStream JavaDoc getResourceAsStream(Object JavaDoc object, Resource resource) {
72     ClassLoader JavaDoc loader = object.getClass().getClassLoader();
73
74     InputStream JavaDoc in = null;
75
76     if (loader != null) {
77       in = loader.getResourceAsStream(resource.getName());
78     } else {
79       in = ClassLoader.getSystemResourceAsStream(resource.getName());
80     }
81
82     return in;
83   }
84
85   /**
86    * Get the URL for this resource.
87    *
88    * @param object The object to grab the Classloader from.
89    * This parameter is quite important from a
90    * visibility of resources standpoint as the
91    * hierarchy of Classloaders plays a role.
92    *
93    * @param resource The resource to load.
94    *
95    * @return If the Resource was found, the URL, otherwise null.
96    *
97    * @see Resource
98    * @see #getResourceAsStream(Object,Resource)
99    */

100   public static URL JavaDoc getResourceAsURL(Object JavaDoc object, Resource resource) {
101     ClassLoader JavaDoc loader = object.getClass().getClassLoader();
102
103     URL JavaDoc url = null;
104
105     if (loader != null) {
106       url = loader.getResource(resource.getName());
107     } else {
108       url = ClassLoader.getSystemResource(resource.getName());
109     }
110
111     return (url);
112   }
113
114   //--------------------------------------------------------------------------
115
// Protected Methods:
116
//--------------------------------------------------------------------------
117

118   //--------------------------------------------------------------------------
119
// Private Methods:
120
//--------------------------------------------------------------------------
121

122   //--------------------------------------------------------------------------
123
// Nested Top-Level Classes or Interfaces:
124
//--------------------------------------------------------------------------
125

126 }
127
128
129
130
131
132
133
Popular Tags