KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > component > TurbineComponentService


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

18
19 import java.util.Iterator JavaDoc;
20 import javax.servlet.ServletConfig JavaDoc;
21
22 import org.apache.commons.configuration.BaseConfiguration;
23 import org.apache.commons.configuration.Configuration;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.stratum.component.ComponentLoader;
27 import org.apache.stratum.lifecycle.Disposable;
28 import org.apache.turbine.Turbine;
29 import org.apache.turbine.services.InitializationException;
30 import org.apache.turbine.services.TurbineBaseService;
31
32 /**
33  * An implementation of ComponentService which loads all the
34  * components given in the TurbineResources.properties File
35  *
36  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37  * @version $Id: TurbineComponentService.java,v 1.12.2.2 2004/05/20 03:05:19 seade Exp $
38  * @deprecated torque is now loaded using the AvalonComponentService
39  */

40 public class TurbineComponentService
41         extends TurbineBaseService
42         implements ComponentService
43 {
44
45     /** Logging */
46     private static Log log = LogFactory.getLog(TurbineComponentService.class);
47
48     /** Extension used for Configuration files. */
49     private static String JavaDoc CONFIG = "config";
50
51     /** Name tag used in Configurations */
52     private static String JavaDoc NAME = "name";
53
54     /** Prefix used by the Component Loader */
55     private static String JavaDoc COMPONENT = "component";
56
57     /** List of Components that was initialized */
58     private Object JavaDoc[] components = null;
59
60     /**
61      * Load all configured components and initialize them. This is
62      * a zero parameter variant which queries the Turbine Servlet
63      * for its config.
64      *
65      * @throws InitializationException Something went wrong in the init
66      * stage
67      */

68     public void init()
69             throws InitializationException
70     {
71         ServletConfig JavaDoc config = Turbine.getTurbineServletConfig();
72         Configuration loaderConf = new BaseConfiguration();
73
74         String JavaDoc[] names = getConfiguration().getStringArray(NAME);
75         
76         log.warn("The ComponentService is deprecated!");
77
78         for (int i = 0; i < names.length; i++)
79         {
80             String JavaDoc key = names[i];
81
82             loaderConf.addProperty(COMPONENT + "." + NAME, key);
83
84             String JavaDoc subProperty = COMPONENT + "." + key;
85             Configuration subConf = getConfiguration().subset(key);
86
87             for (Iterator JavaDoc it = subConf.getKeys(); it.hasNext();)
88             {
89                 String JavaDoc subKey = (String JavaDoc) it.next();
90                 Object JavaDoc subVal = subConf.getProperty(subKey);
91
92                 if (subKey.equals(CONFIG))
93                 {
94                     log.debug("Fixing up " + subVal);
95                     String JavaDoc newPath =
96                             config.getServletContext().getRealPath((String JavaDoc) subVal);
97
98                     if (newPath == null)
99                     {
100                       throw new InitializationException("Could not translate path " + subVal);
101                     }
102
103                     subVal = newPath;
104                     log.debug("Now: " + subVal);
105                 }
106
107                 loaderConf.addProperty(subProperty + "." + subKey,
108                         subVal);
109             }
110
111             log.info("Added " + key + " as a component");
112         }
113
114         try
115         {
116             ComponentLoader cl = new ComponentLoader(loaderConf);
117             components = cl.load();
118             setInit(true);
119         }
120         catch (Exception JavaDoc e)
121         {
122             log.error("Component Service failed: ", e);
123             throw new InitializationException("ComponentService failed: ", e);
124         }
125     }
126
127     /**
128      * Inits the service using servlet parameters to obtain path to the
129      * configuration file. Change relatives paths.
130      *
131      * @param config The ServletConfiguration from Turbine
132      *
133      * @throws InitializationException Something went wrong when starting up.
134      * @deprecated use init() instead.
135      */

136     public void init(ServletConfig JavaDoc config)
137             throws InitializationException
138     {
139         init();
140     }
141
142     /**
143      * Shuts the Component Service down, calls dispose on the components that
144      * implement this interface
145      *
146      */

147
148     public void shutdown()
149     {
150         if (components != null)
151         {
152             for (int i = 0; i < components.length; i++)
153             {
154                 if (components[i] instanceof Disposable)
155                 {
156                     log.debug("Disposing a " + components[i].getClass().getName() + " object");
157                     ((Disposable) components[i]).dispose();
158                 }
159                 else
160                 {
161                     log.debug("Not disposing " + components[i].getClass().getName() + ", not a Disposable Object");
162                 }
163             }
164         }
165         setInit(false);
166     }
167 }
168
Popular Tags