View Javadoc

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;
20  import java.util.ArrayList;
21  import java.util.List;
22  import javax.servlet.ServletContext;
23  import org.apache.commons.chain.Catalog;
24  import org.apache.commons.chain.config.ConfigParser;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  
29  /***
30   * <p>Utility methods for loading class loader and web application resources
31   * to configure a {@link Catalog}.  These methods are shared between
32   * <code>ChainListener</code> and <code>ChainServlet</code>.</p>
33   *
34   * @author Craig R. McClanahan
35   * @author Ted Husted
36   */
37  
38  final class ChainResources {
39  
40  
41      // ---------------------------------------------------------- Static Methods
42  
43  
44      /***
45       * <p>Parse the specified class loader resources.</p>
46       *
47       * @param resources Comma-delimited list of resources (or <code>null</code>)
48       * @param parser {@link ConfigParser} to use for parsing
49       */
50      static void parseClassResources(String resources,
51                                      ConfigParser parser) {
52  
53          if (resources == null) {
54              return;
55          }
56          Log log = LogFactory.getLog(ChainResources.class);
57          ClassLoader loader =
58              Thread.currentThread().getContextClassLoader();
59          if (loader == null) {
60              loader = ChainResources.class.getClassLoader();
61          }
62          String[] paths = getResourcePaths(resources);
63          String path = null;
64          try {
65              for (int i = 0; i < paths.length; i++) {
66                  path = paths[i];
67                  URL url = loader.getResource(path);
68                  if (url == null) {
69                      throw new IllegalStateException
70                          ("Missing chain config resource '" + path + "'");
71                  }
72                  if (log.isDebugEnabled()) {
73                      log.debug("Loading chain config resource '" + path + "'");
74                  }
75                  parser.parse(url);
76              }
77          } catch (Exception e) {
78              throw new RuntimeException
79                  ("Exception parsing chain config resource '" + path + "': "
80                   + e.getMessage());
81          }
82  
83      }
84  
85  
86      /***
87       * <p>Parse the specified class loader resources.</p>
88       *
89       * @param catalog {@link Catalog} we are populating
90       * @param resources Comma-delimited list of resources (or <code>null</code>)
91       * @param parser {@link ConfigParser} to use for parsing
92       *
93       * @deprecated Use the variant that does not take a catalog, on a
94       *  configuration resource containing "catalog" element(s)
95       */
96      static void parseClassResources(Catalog catalog, String resources,
97                                      ConfigParser parser) {
98  
99          if (resources == null) {
100             return;
101         }
102         Log log = LogFactory.getLog(ChainResources.class);
103         ClassLoader loader =
104             Thread.currentThread().getContextClassLoader();
105         if (loader == null) {
106             loader = ChainResources.class.getClassLoader();
107         }
108         String[] paths = getResourcePaths(resources);
109         String path = null;
110         try {
111             for (int i = 0; i < paths.length; i++) {
112                 path = paths[i];
113                 URL url = loader.getResource(path);
114                 if (url == null) {
115                     throw new IllegalStateException
116                         ("Missing chain config resource '" + path + "'");
117                 }
118                 if (log.isDebugEnabled()) {
119                     log.debug("Loading chain config resource '" + path + "'");
120                 }
121                 parser.parse(catalog, url);
122             }
123         } catch (Exception e) {
124             throw new RuntimeException
125                 ("Exception parsing chain config resource '" + path + "': "
126                  + e.getMessage());
127         }
128 
129     }
130 
131 
132     /***
133      * <p>Parse the specified web application resources.</p>
134      *
135      * @param context <code>ServletContext</code> for this web application
136      * @param resources Comma-delimited list of resources (or <code>null</code>)
137      * @param parser {@link ConfigParser} to use for parsing
138      */
139     static void parseWebResources(ServletContext context,
140                                   String resources,
141                                   ConfigParser parser) {
142 
143         if (resources == null) {
144             return;
145         }
146         Log log = LogFactory.getLog(ChainResources.class);
147         String[] paths = getResourcePaths(resources);
148         String path = null;
149         try {
150             for (int i = 0; i < paths.length; i++) {
151                 path = paths[i];
152                 URL url = context.getResource(path);
153                 if (url == null) {
154                     throw new IllegalStateException
155                         ("Missing chain config resource '" + path + "'");
156                 }
157                 if (log.isDebugEnabled()) {
158                     log.debug("Loading chain config resource '" + path + "'");
159                 }
160                 parser.parse(url);
161             }
162         } catch (Exception e) {
163             throw new RuntimeException
164                 ("Exception parsing chain config resource '" + path + "': "
165                  + e.getMessage());
166         }
167 
168     }
169 
170 
171     /***
172      * <p>Parse the specified web application resources.</p>
173      *
174      * @param catalog {@link Catalog} we are populating
175      * @param context <code>ServletContext</code> for this web application
176      * @param resources Comma-delimited list of resources (or <code>null</code>)
177      * @param parser {@link ConfigParser} to use for parsing
178      *
179      * @deprecated Use the variant that does not take a catalog, on a
180      *  configuration resource containing "catalog" element(s)
181      */
182     static void parseWebResources(Catalog catalog, ServletContext context,
183                                   String resources,
184                                   ConfigParser parser) {
185 
186         if (resources == null) {
187             return;
188         }
189         Log log = LogFactory.getLog(ChainResources.class);
190         String[] paths = getResourcePaths(resources);
191         String path = null;
192         try {
193             for (int i = 0; i < paths.length; i++) {
194                 path = paths[i];
195                 URL url = context.getResource(path);
196                 if (url == null) {
197                     throw new IllegalStateException
198                         ("Missing chain config resource '" + path + "'");
199                 }
200                 if (log.isDebugEnabled()) {
201                     log.debug("Loading chain config resource '" + path + "'");
202                 }
203                 parser.parse(catalog, url);
204             }
205         } catch (Exception e) {
206             throw new RuntimeException
207                 ("Exception parsing chain config resource '" + path + "': "
208                  + e.getMessage());
209         }
210 
211     }
212 
213 
214     /***
215      * <p> Parse the resource string into an array of paths. Empty entries will
216      * be skipped. (That is, all entries in the array are non-empty paths.)</p>
217      *
218      * @param resources A comma-delimited list of resource paths (or
219      *                  <code>null</code>).
220      *
221      * @return An array of non-empty paths. The array itself may be empty.
222      *
223      * @since Chain 1.1
224      */
225     static String[] getResourcePaths(String resources) {
226         List paths = new ArrayList();
227 
228         if (resources != null) {
229             String path;
230             int comma;
231 
232             while ((comma = resources.indexOf(',')) >= 0) {
233                 path = resources.substring(0, comma).trim();
234                 if (path.length() > 0) {
235                     paths.add(path);
236                 }
237                 resources = resources.substring(comma + 1);
238             }
239             resources = resources.trim();
240             if (resources.length() > 0) {
241                 paths.add(resources);
242             }
243         }
244 
245         return (String[]) paths.toArray(new String[0]);
246     }
247 
248 
249 }