001: package org.apache.turbine.services.avaloncomponent;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.io.IOException;
024:
025: import org.apache.avalon.framework.activity.Disposable;
026: import org.apache.avalon.framework.activity.Initializable;
027: import org.apache.avalon.framework.component.Component;
028: import org.apache.avalon.framework.component.ComponentException;
029: import org.apache.avalon.framework.logger.Log4JLogger;
030: import org.apache.avalon.framework.service.ServiceException;
031: import org.apache.commons.configuration.Configuration;
032: import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
033: import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
034: import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
035: import org.apache.log4j.Logger;
036: import org.apache.turbine.Turbine;
037: import org.apache.turbine.services.InitializationException;
038: import org.apache.turbine.services.TurbineBaseService;
039:
040: /**
041: * An implementation of Turbine service initializing the YAAFI container
042: *
043: * @author <a href="mailto:siegfried.goescfl@it20one.at">Siegfried Goeschl</a>
044: * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
045: * @version $Id$
046: */
047: public class ACSYaafiComponentService extends TurbineBaseService
048: implements AvalonComponentService, Initializable, Disposable {
049: /** The publically visible name of the service */
050: public static final String SERVICE_NAME = "YaafiComponentService";
051:
052: /** property to lookup the container configuration file */
053: public static final String CONTAINER_CONFIGURATION_KEY = "containerConfiguration";
054:
055: /** the default value for the container configuration file */
056: public static final String CONTAINER_CONFIGURATION_VALUE = "/WEB-INF/conf/containerConfiguration.xml";
057:
058: /** property to lookup the properties file */
059: public static final String COMPONENT_PARAMETERS_KEY = "parameters";
060:
061: /** the default value for the parameter file */
062: public static final String COMPONENT_PARAMETERS_VALUE = "/WEB-INF/conf/parameters.properties";
063:
064: /** YAFFI container */
065: private ServiceContainer container;
066:
067: /** our Log4J logger */
068: private Logger logger;
069:
070: // -------------------------------------------------------------
071: // Service initialization
072: // -------------------------------------------------------------
073:
074: public ACSYaafiComponentService() {
075: this .logger = Logger.getLogger(ACSYaafiComponentService.class);
076: }
077:
078: /**
079: * Load all configured components and initialize them. This is a zero
080: * parameter variant which queries the Turbine Servlet for its config.
081: *
082: * @throws InitializationException Something went wrong in the init stage
083: */
084: public void init() throws InitializationException {
085: try {
086: this .logger
087: .info("Initializing ACSYaafiComponentService ...");
088: initialize();
089: setInit(true);
090: } catch (Exception e) {
091: this .logger.error(
092: "Exception caught initializing service: ", e);
093: throw new InitializationException(
094: "Initializing ACSYaafiComponentService failed", e);
095: }
096: }
097:
098: /**
099: * Shuts the Component Service down, calls dispose on the components that
100: * implement this interface
101: */
102: public void shutdown() {
103: this .logger.info("Disposing ACSYaafiComponentService ...");
104: dispose();
105: setInit(false);
106: }
107:
108: // -------------------------------------------------------------
109: // Avalon lifecycle interfaces
110: // -------------------------------------------------------------
111:
112: /**
113: * Initializes the container
114: *
115: * @throws Exception generic exception
116: */
117: public void initialize() throws Exception {
118: // get the configuration from the base class
119: Configuration conf = this .getConfiguration();
120:
121: // determine the home directory
122: String homePath = Turbine.getRealPath("/");
123: if (homePath == null) {
124: homePath = Turbine.getApplicationRoot();
125: }
126: File home = new File(homePath);
127:
128: this .logger.info("Using the following home : "
129: + home.getAbsolutePath());
130:
131: // create the configuration for YAAFI
132: ServiceContainerConfiguration config = this
133: .createServiceContainerConfiguration(conf, home
134: .getAbsolutePath());
135:
136: try {
137: this .container = ServiceContainerFactory.create(config);
138: } catch (Throwable t) {
139: throw new Exception("Initializing YAAFI failed", t);
140: }
141: }
142:
143: /**
144: * Disposes of the container and releases resources
145: */
146: public void dispose() {
147: if (this .container != null) {
148: this .container.dispose();
149: this .container = null;
150: }
151: }
152:
153: /**
154: * Returns an instance of the named component
155: *
156: * @param path Name of the role the component fills.
157: * @return an instance of the named component
158: * @throws Exception generic exception
159: */
160: public Component lookup(String path) throws ComponentException {
161: try {
162: return (Component) this .container.lookup(path);
163: } catch (ServiceException e) {
164: throw new ComponentException(e.getKey(), e.getMessage(), e);
165: }
166: }
167:
168: /**
169: * Releases the component
170: *
171: * @param component the component to release
172: */
173: public void release(Component component) {
174: this .container.release(component);
175: }
176:
177: /**
178: * Create a ServiceContainerConfiguration based on the Turbine configuration
179: *
180: * @param conf the Turbine configuration
181: * @param appRoot the absolute path to the application root directory
182: * @return the YAAFI configuration
183: * @throws IOException creating the YAAFI configuration failed
184: */
185: protected ServiceContainerConfiguration createServiceContainerConfiguration(
186: Configuration conf, String appRoot) throws IOException {
187: ServiceContainerConfiguration result = new ServiceContainerConfiguration();
188:
189: result.setLogger(this .createAvalonLogger(AVALON_LOG_CATEGORY));
190: result.setApplicationRootDir(appRoot);
191:
192: // are we using a "containerConfiguration.xml" ?!
193:
194: if (conf.containsKey(CONTAINER_CONFIGURATION_KEY)) {
195: // determine the container configuration file
196:
197: String containerConfiguration = conf
198: .getString(CONTAINER_CONFIGURATION_KEY);
199:
200: result.loadContainerConfiguration(containerConfiguration);
201: } else if (conf.containsKey(COMPONENT_ROLE_KEY)) {
202: // determine the location of the role configuration file
203:
204: String roleConfigurationFileName = conf.getString(
205: COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE);
206:
207: // determine the location of component configuration file
208:
209: String componentConfigurationFileName = conf.getString(
210: COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE);
211:
212: // determine the location of parameters file
213:
214: String parametersFileName = conf.getString(
215: COMPONENT_PARAMETERS_KEY,
216: COMPONENT_PARAMETERS_VALUE);
217:
218: result.setComponentRolesLocation(roleConfigurationFileName);
219: result
220: .setComponentConfigurationLocation(componentConfigurationFileName);
221: result.setParametersLocation(parametersFileName);
222: } else {
223: // determine the container configuration file
224:
225: String containerConfiguration = conf.getString(
226: CONTAINER_CONFIGURATION_KEY,
227: CONTAINER_CONFIGURATION_VALUE);
228:
229: result.loadContainerConfiguration(containerConfiguration);
230: }
231:
232: return result;
233: }
234:
235: /**
236: * Create the Avalon logger to be passed to YAAFI
237: *
238: * @param name the name of the logger
239: * @return an Avalon Logger
240: */
241: protected org.apache.avalon.framework.logger.Logger createAvalonLogger(
242: String name) {
243: return new Log4JLogger(Logger.getLogger(name));
244: }
245: }
|