KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > legacy > CatalogConfiguratorPlugIn


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
17 package org.apache.struts.chain.legacy;
18
19
20 import java.net.URL JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23
24 import org.apache.struts.action.ActionServlet;
25 import org.apache.struts.action.PlugIn;
26 import org.apache.struts.chain.Constants;
27 import org.apache.struts.config.ModuleConfig;
28
29 import org.apache.commons.chain.Catalog;
30 import org.apache.commons.chain.CatalogFactory;
31 import org.apache.commons.chain.config.ConfigParser;
32 import org.apache.commons.chain.impl.CatalogBase;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37
38 /**
39  * <p><strong>CatalogConfiguratorPlugIn</strong> parses the configuration
40  * resource specified by the <code>path</code> or <code>resource</code>
41  * property to configure the default {@link Catalog} registered with the
42  * {@link CatalogFactory} for this application.</p>
43  * <ul>
44  * <li><strong>path</strong> - Context-relative path to a web application
45  * resource that contains chain definitions.</li>
46  * <li><strong>resource</strong> - Absolute resource path to a class loader
47  * resource that should be in a JAR file under <code>/WEB-INF/lib</code>,
48  * or a separate file under <code>/WEB-INF/classes</code>.</li>
49  * </ul>
50  *
51  */

52
53 public class CatalogConfiguratorPlugIn implements PlugIn {
54
55
56     // ------------------------------------------------------ Instance Variables
57

58
59     private static Log log = LogFactory.getLog(CatalogConfiguratorPlugIn.class);
60     private String JavaDoc path = null;
61     private String JavaDoc resource = null;
62
63
64     // -------------------------------------------------------------- Properties
65

66
67     /**
68      * <p>Return the context-relative resource path to load from.
69      */

70     public String JavaDoc getPath() {
71         return (this.path);
72     }
73
74
75     /**
76      * <p>Set the context-relative resource path to load from.
77      *
78      * @param path The new context relative resource path
79      */

80     public void setPath(String JavaDoc path) {
81         this.path = path;
82     }
83
84
85     /**
86      * <p>Return the classloader resource path to load from.
87      */

88     public String JavaDoc getResource() {
89         return (this.resource);
90     }
91
92
93     /**
94      * <p>Set the classloader resource path to load from.
95      *
96      * @param resource The new classloader resource path
97      */

98     public void setResource(String JavaDoc resource) {
99         this.resource = resource;
100     }
101
102
103     // ---------------------------------------------------------- PlugIn Methods
104

105
106     /**
107      * <p>Clean up at web application shutdown.</p>
108      */

109     public void destroy() {
110
111         CatalogFactory.clear();
112
113     }
114
115
116     /**
117      * <p>Parse the configuration documents specified by the <code>path</code>
118      * or <code>resource</code> property to configure the default
119      * {@link Catalog} that is registered in the {@link CatalogFactory}
120      * instance for this application.</p>
121      */

122     public void init(ActionServlet servlet, ModuleConfig config)
123         throws ServletException JavaDoc {
124
125         // Parse the configuration file specified by path or resource
126
try {
127             ConfigParser parser = new ConfigParser();
128             URL JavaDoc configResource = null;
129             if (path != null) {
130                 log.info("Loading context relative resources from '" +
131                          path + "'");
132                 configResource =
133                     servlet.getServletContext().getResource(path);
134             } else if (resource != null) {
135                 log.info("Loading classloader resources from '" +
136                          resource + "'");
137                 ClassLoader JavaDoc loader =
138                     Thread.currentThread().getContextClassLoader();
139                 if (loader == null) {
140                     loader = this.getClass().getClassLoader();
141                 }
142                 configResource = loader.getResource(resource);
143             }
144             parser.parse(configResource);
145         } catch (Exception JavaDoc e) {
146             log.error("Exception loading resources", e);
147             throw new ServletException JavaDoc(e);
148         }
149
150     }
151
152
153
154 }
155
Popular Tags