001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web;
017:
018: import java.net.URL;
019: import java.util.ArrayList;
020: import java.util.List;
021: import javax.servlet.ServletContext;
022: import org.apache.commons.chain.Catalog;
023: import org.apache.commons.chain.config.ConfigParser;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: /**
028: * <p>Utility methods for loading class loader and web application resources
029: * to configure a {@link Catalog}. These methods are shared between
030: * <code>ChainListener</code> and <code>ChainServlet</code>.</p>
031: *
032: * @author Craig R. McClanahan
033: * @author Ted Husted
034: */
035:
036: final class ChainResources {
037:
038: // ---------------------------------------------------------- Static Methods
039:
040: /**
041: * <p>Parse the specified class loader resources.</p>
042: *
043: * @param resources Comma-delimited list of resources (or <code>null</code>)
044: * @param parser {@link ConfigParser} to use for parsing
045: */
046: static void parseClassResources(String resources,
047: ConfigParser parser) {
048:
049: if (resources == null) {
050: return;
051: }
052: Log log = LogFactory.getLog(ChainResources.class);
053: ClassLoader loader = Thread.currentThread()
054: .getContextClassLoader();
055: if (loader == null) {
056: loader = ChainResources.class.getClassLoader();
057: }
058: String[] paths = getResourcePaths(resources);
059: String path = null;
060: try {
061: for (int i = 0; i < paths.length; i++) {
062: path = paths[i];
063: URL url = loader.getResource(path);
064: if (url == null) {
065: throw new IllegalStateException(
066: "Missing chain config resource '" + path
067: + "'");
068: }
069: if (log.isDebugEnabled()) {
070: log.debug("Loading chain config resource '" + path
071: + "'");
072: }
073: parser.parse(url);
074: }
075: } catch (Exception e) {
076: throw new RuntimeException(
077: "Exception parsing chain config resource '" + path
078: + "': " + e.getMessage());
079: }
080:
081: }
082:
083: /**
084: * <p>Parse the specified class loader resources.</p>
085: *
086: * @param catalog {@link Catalog} we are populating
087: * @param resources Comma-delimited list of resources (or <code>null</code>)
088: * @param parser {@link ConfigParser} to use for parsing
089: *
090: * @deprecated Use the variant that does not take a catalog, on a
091: * configuration resource containing "catalog" element(s)
092: */
093: static void parseClassResources(Catalog catalog, String resources,
094: ConfigParser parser) {
095:
096: if (resources == null) {
097: return;
098: }
099: Log log = LogFactory.getLog(ChainResources.class);
100: ClassLoader loader = Thread.currentThread()
101: .getContextClassLoader();
102: if (loader == null) {
103: loader = ChainResources.class.getClassLoader();
104: }
105: String[] paths = getResourcePaths(resources);
106: String path = null;
107: try {
108: for (int i = 0; i < paths.length; i++) {
109: path = paths[i];
110: URL url = loader.getResource(path);
111: if (url == null) {
112: throw new IllegalStateException(
113: "Missing chain config resource '" + path
114: + "'");
115: }
116: if (log.isDebugEnabled()) {
117: log.debug("Loading chain config resource '" + path
118: + "'");
119: }
120: parser.parse(catalog, url);
121: }
122: } catch (Exception e) {
123: throw new RuntimeException(
124: "Exception parsing chain config resource '" + path
125: + "': " + e.getMessage());
126: }
127:
128: }
129:
130: /**
131: * <p>Parse the specified web application resources.</p>
132: *
133: * @param context <code>ServletContext</code> for this web application
134: * @param resources Comma-delimited list of resources (or <code>null</code>)
135: * @param parser {@link ConfigParser} to use for parsing
136: */
137: static void parseWebResources(ServletContext context,
138: String resources, ConfigParser parser) {
139:
140: if (resources == null) {
141: return;
142: }
143: Log log = LogFactory.getLog(ChainResources.class);
144: String[] paths = getResourcePaths(resources);
145: String path = null;
146: try {
147: for (int i = 0; i < paths.length; i++) {
148: path = paths[i];
149: URL url = context.getResource(path);
150: if (url == null) {
151: throw new IllegalStateException(
152: "Missing chain config resource '" + path
153: + "'");
154: }
155: if (log.isDebugEnabled()) {
156: log.debug("Loading chain config resource '" + path
157: + "'");
158: }
159: parser.parse(url);
160: }
161: } catch (Exception e) {
162: throw new RuntimeException(
163: "Exception parsing chain config resource '" + path
164: + "': " + e.getMessage());
165: }
166:
167: }
168:
169: /**
170: * <p>Parse the specified web application resources.</p>
171: *
172: * @param catalog {@link Catalog} we are populating
173: * @param context <code>ServletContext</code> for this web application
174: * @param resources Comma-delimited list of resources (or <code>null</code>)
175: * @param parser {@link ConfigParser} to use for parsing
176: *
177: * @deprecated Use the variant that does not take a catalog, on a
178: * configuration resource containing "catalog" element(s)
179: */
180: static void parseWebResources(Catalog catalog,
181: ServletContext context, String resources,
182: ConfigParser parser) {
183:
184: if (resources == null) {
185: return;
186: }
187: Log log = LogFactory.getLog(ChainResources.class);
188: String[] paths = getResourcePaths(resources);
189: String path = null;
190: try {
191: for (int i = 0; i < paths.length; i++) {
192: path = paths[i];
193: URL url = context.getResource(path);
194: if (url == null) {
195: throw new IllegalStateException(
196: "Missing chain config resource '" + path
197: + "'");
198: }
199: if (log.isDebugEnabled()) {
200: log.debug("Loading chain config resource '" + path
201: + "'");
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: * <p> Parse the resource string into an array of paths. Empty entries will
215: * be skipped. (That is, all entries in the array are non-empty paths.)</p>
216: *
217: * @param resources A comma-delimited list of resource paths (or
218: * <code>null</code>).
219: *
220: * @return An array of non-empty paths. The array itself may be empty.
221: *
222: * @since Chain 1.1
223: */
224: static String[] getResourcePaths(String resources) {
225: List paths = new ArrayList();
226:
227: if (resources != null) {
228: String path;
229: int comma;
230:
231: while ((comma = resources.indexOf(',')) >= 0) {
232: path = resources.substring(0, comma).trim();
233: if (path.length() > 0) {
234: paths.add(path);
235: }
236: resources = resources.substring(comma + 1);
237: }
238: resources = resources.trim();
239: if (resources.length() > 0) {
240: paths.add(resources);
241: }
242: }
243:
244: return (String[]) paths.toArray(new String[0]);
245: }
246:
247: }
|