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.util.Iterator;
023: import java.util.List;
024: import java.util.Vector;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.apache.avalon.excalibur.component.DefaultRoleManager;
030: import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
031: import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
032: import org.apache.avalon.excalibur.logger.LoggerManager;
033: import org.apache.avalon.framework.activity.Disposable;
034: import org.apache.avalon.framework.activity.Initializable;
035: import org.apache.avalon.framework.component.Component;
036: import org.apache.avalon.framework.component.ComponentException;
037: import org.apache.avalon.framework.configuration.Configuration;
038: import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
039: import org.apache.avalon.framework.context.DefaultContext;
040: import org.apache.avalon.framework.logger.Logger;
041:
042: import org.apache.turbine.Turbine;
043: import org.apache.turbine.services.InitializationException;
044: import org.apache.turbine.services.TurbineBaseService;
045:
046: /**
047: * An implementation of AvalonComponentService which loads all the
048: * components given in the TurbineResources.properties File.
049: * <p>
050: * For component which require the location of the application or
051: * context root, there are two ways to get it.
052: * <ol>
053: * <li>
054: * Implement the Contextualizable interface. The full path to the
055: * correct OS directory can be found under the ComponentAppRoot key.
056: * </li>
057: * <li>
058: * The system property "applicationRoot" is also set to the full path
059: * of the correct OS directory.
060: * </li>
061: * </ol>
062: * If you want to initialize Torque by using the AvalonComponentService, you
063: * must activate Torque at initialization time by specifying
064: *
065: * services.AvalonComponentService.lookup = org.apache.torque.avalon.Torque
066: *
067: * in your TurbineResources.properties.
068: *
069: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
070: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
071: * @version $Id: TurbineAvalonComponentService.java 534527 2007-05-02 16:10:59Z tv $
072: */
073: public class TurbineAvalonComponentService extends TurbineBaseService
074: implements AvalonComponentService, Initializable, Disposable {
075: /** Logging */
076: private static Log log = LogFactory
077: .getLog(TurbineAvalonComponentService.class);
078:
079: /** Component manager */
080: private ExcaliburComponentManager manager = null;
081:
082: // -------------------------------------------------------------
083: // Service initialization
084: // -------------------------------------------------------------
085:
086: /**
087: * Load all configured components and initialize them. This is
088: * a zero parameter variant which queries the Turbine Servlet
089: * for its config.
090: *
091: * @throws InitializationException Something went wrong in the init
092: * stage
093: */
094: public void init() throws InitializationException {
095: try {
096: initialize();
097:
098: setInit(true);
099: } catch (Exception e) {
100: throw new InitializationException("init failed", e);
101: }
102: }
103:
104: /**
105: * Shuts the Component Service down, calls dispose on the components that
106: * implement this interface
107: *
108: */
109: public void shutdown() {
110: dispose();
111: setInit(false);
112: }
113:
114: // -------------------------------------------------------------
115: // Avalon lifecycle interfaces
116: // -------------------------------------------------------------
117:
118: /**
119: * Initializes the container
120: *
121: * @throws Exception generic exception
122: */
123: public void initialize() throws Exception {
124: org.apache.commons.configuration.Configuration conf = getConfiguration();
125:
126: // get the filenames and expand them relative to webapp root
127: String sysConfigFilename = Turbine.getRealPath(conf.getString(
128: COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE));
129: String roleConfigFilename = Turbine.getRealPath(conf.getString(
130: COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE));
131:
132: log.debug("Config File: " + sysConfigFilename);
133: log.debug("Role File: " + roleConfigFilename);
134:
135: // process configuration files
136:
137: DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
138: Configuration sysConfig = builder
139: .buildFromFile(sysConfigFilename);
140: Configuration roleConfig = builder
141: .buildFromFile(roleConfigFilename);
142:
143: // Create the LoggerManager for Log4J
144: LoggerManager lm = new Log4JLoggerManager();
145:
146: // Setup the RoleManager
147: DefaultRoleManager roles = new DefaultRoleManager();
148:
149: Logger logger = lm.getLoggerForCategory(AVALON_LOG_CATEGORY);
150:
151: roles.enableLogging(logger);
152: roles.configure(roleConfig);
153:
154: // Setup ECM
155: manager = new ExcaliburComponentManager();
156:
157: manager.setLoggerManager(lm);
158: manager.enableLogging(logger);
159:
160: DefaultContext context = new DefaultContext();
161: String realPath = Turbine.getRealPath("/");
162:
163: context
164: .put(AvalonComponentService.COMPONENT_APP_ROOT,
165: realPath);
166: System.setProperty("applicationRoot", realPath);
167:
168: log.debug("Application Root is " + realPath);
169:
170: manager.contextualize(context);
171: manager.setRoleManager(roles);
172: manager.configure(sysConfig);
173:
174: // Init ECM!!!!
175: manager.initialize();
176:
177: List lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY,
178: new Vector());
179:
180: for (Iterator it = lookupComponents.iterator(); it.hasNext();) {
181: String component = (String) it.next();
182: try {
183: Component c = manager.lookup(component);
184: log.info("Lookup for Component " + component
185: + " successful");
186: manager.release(c);
187: } catch (Exception e) {
188: log.error("Lookup for Component " + component
189: + " failed!");
190: }
191: }
192: }
193:
194: /**
195: * Disposes of the container and releases resources
196: */
197: public void dispose() {
198: manager.dispose();
199: }
200:
201: /**
202: * Returns an instance of the named component
203: *
204: * @param roleName Name of the role the component fills.
205: * @return an instance of the named component
206: * @throws ComponentException generic exception
207: */
208: public Component lookup(String roleName) throws ComponentException {
209: return manager.lookup(roleName);
210: }
211:
212: /**
213: * Releases the component
214: *
215: * @param component the component to release
216: */
217: public void release(Component component) {
218: manager.release(component);
219: }
220:
221: }
|