KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > ChainResources


1 /*
2  * Copyright 1999-2004 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.commons.chain.web;
17
18
19 import java.net.URL JavaDoc;
20 import javax.servlet.ServletContext JavaDoc;
21 import org.apache.commons.chain.Catalog;
22 import org.apache.commons.chain.config.ConfigParser;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26
27 /**
28  * <p>Utility methods for loading class loader and web application resources
29  * to configure a {@link Catalog}. These methods are shared between
30  * <code>ChainListener</code> and <code>ChainServlet</code>.</p>
31  *
32  * @author Craig R. McClanahan
33  * @author Ted Husted
34  */

35
36 final class ChainResources {
37
38
39     // -------------------------------------------------------- Static Variables
40

41
42     /**
43      * <p>The <code>Log</code> instance for this class.</p>
44      */

45     private static final Log log = LogFactory.getLog(ChainResources.class);
46
47
48     // ---------------------------------------------------------- Static Methods
49

50
51     /**
52      * <p>Parse the specified class loader resources.</p>
53      *
54      * @param resources Comma-delimited list of resources (or <code>null</code>)
55      * @param parser {@link ConfigParser} to use for parsing
56      */

57     static void parseClassResources(String JavaDoc resources,
58                                     ConfigParser parser) {
59
60         if (resources == null) {
61             return;
62         }
63         ClassLoader JavaDoc loader =
64             Thread.currentThread().getContextClassLoader();
65         if (loader == null) {
66             loader = ChainResources.class.getClassLoader();
67         }
68         String JavaDoc path = null;
69         try {
70             while (true) {
71                 int comma = resources.indexOf(",");
72                 if (comma < 0) {
73                     path = resources.trim();
74                     resources = "";
75                 } else {
76                     path = resources.substring(0, comma);
77                     resources = resources.substring(comma + 1);
78                 }
79                 if (path.length() < 1) {
80                     break;
81                 }
82                 URL JavaDoc url = loader.getResource(path);
83                 if (url == null) {
84                     throw new IllegalStateException JavaDoc
85                         ("Missing chain config resource '" + path + "'");
86                 }
87                 if (log.isDebugEnabled()) {
88                     log.debug("Loading chain config resource '" + path + "'");
89                 }
90                 parser.parse(url);
91             }
92         } catch (Exception JavaDoc e) {
93             throw new RuntimeException JavaDoc
94                 ("Exception parsing chain config resource '" + path + "': "
95                  + e.getMessage());
96         }
97
98     }
99
100
101     /**
102      * <p>Parse the specified class loader resources.</p>
103      *
104      * @param catalog {@link Catalog} we are populating
105      * @param resources Comma-delimited list of resources (or <code>null</code>)
106      * @param parser {@link ConfigParser} to use for parsing
107      *
108      * @deprecated Use the variant that does not take a catalog, on a
109      * configuration resource containing "catalog" element(s)
110      */

111     static void parseClassResources(Catalog catalog, String JavaDoc resources,
112                                     ConfigParser parser) {
113
114         if (resources == null) {
115             return;
116         }
117         ClassLoader JavaDoc loader =
118             Thread.currentThread().getContextClassLoader();
119         if (loader == null) {
120             loader = ChainResources.class.getClassLoader();
121         }
122         String JavaDoc path = null;
123         try {
124             while (true) {
125                 int comma = resources.indexOf(",");
126                 if (comma < 0) {
127                     path = resources.trim();
128                     resources = "";
129                 } else {
130                     path = resources.substring(0, comma);
131                     resources = resources.substring(comma + 1);
132                 }
133                 if (path.length() < 1) {
134                     break;
135                 }
136                 URL JavaDoc url = loader.getResource(path);
137                 if (url == null) {
138                     throw new IllegalStateException JavaDoc
139                         ("Missing chain config resource '" + path + "'");
140                 }
141                 if (log.isDebugEnabled()) {
142                     log.debug("Loading chain config resource '" + path + "'");
143                 }
144                 parser.parse(catalog, url);
145             }
146         } catch (Exception JavaDoc e) {
147             throw new RuntimeException JavaDoc
148                 ("Exception parsing chain config resource '" + path + "': "
149                  + e.getMessage());
150         }
151
152     }
153
154
155     /**
156      * <p>Parse the specified web application resources.</p>
157      *
158      * @param context <code>ServletContext</code> for this web application
159      * @param resources Comma-delimited list of resources (or <code>null</code>)
160      * @param parser {@link ConfigParser} to use for parsing
161      */

162     static void parseWebResources(ServletContext JavaDoc context,
163                                   String JavaDoc resources,
164                                   ConfigParser parser) {
165
166         if (resources == null) {
167             return;
168         }
169         String JavaDoc path = null;
170         try {
171             while (true) {
172                 int comma = resources.indexOf(",");
173                 if (comma < 0) {
174                     path = resources.trim();
175                     resources = "";
176                 } else {
177                     path = resources.substring(0, comma);
178                     resources = resources.substring(comma + 1);
179                 }
180                 if (path.length() < 1) {
181                     break;
182                 }
183                 URL JavaDoc url = context.getResource(path);
184                 if (url == null) {
185                     throw new IllegalStateException JavaDoc
186                         ("Missing chain config resource '" + path + "'");
187                 }
188                 if (log.isDebugEnabled()) {
189                     log.debug("Loading chain config resource '" + path + "'");
190                 }
191                 parser.parse(url);
192             }
193         } catch (Exception JavaDoc e) {
194             throw new RuntimeException JavaDoc
195                 ("Exception parsing chain config resource '" + path + "': "
196                  + e.getMessage());
197         }
198
199     }
200
201
202     /**
203      * <p>Parse the specified web application resources.</p>
204      *
205      * @param catalog {@link Catalog} we are populating
206      * @param context <code>ServletContext</code> for this web application
207      * @param resources Comma-delimited list of resources (or <code>null</code>)
208      * @param parser {@link ConfigParser} to use for parsing
209      *
210      * @deprecated Use the variant that does not take a catalog, on a
211      * configuration resource containing "catalog" element(s)
212      */

213     static void parseWebResources(Catalog catalog, ServletContext JavaDoc context,
214                                   String JavaDoc resources,
215                                   ConfigParser parser) {
216
217         if (resources == null) {
218             return;
219         }
220         String JavaDoc path = null;
221         try {
222             while (true) {
223                 int comma = resources.indexOf(",");
224                 if (comma < 0) {
225                     path = resources.trim();
226                     resources = "";
227                 } else {
228                     path = resources.substring(0, comma);
229                     resources = resources.substring(comma + 1);
230                 }
231                 if (path.length() < 1) {
232                     break;
233                 }
234                 URL JavaDoc url = context.getResource(path);
235                 if (url == null) {
236                     throw new IllegalStateException JavaDoc
237                         ("Missing chain config resource '" + path + "'");
238                 }
239                 if (log.isDebugEnabled()) {
240                     log.debug("Loading chain config resource '" + path + "'");
241                 }
242                 parser.parse(catalog, url);
243             }
244         } catch (Exception JavaDoc e) {
245             throw new RuntimeException JavaDoc
246                 ("Exception parsing chain config resource '" + path + "': "
247                  + e.getMessage());
248         }
249
250     }
251
252
253 }
254
Popular Tags