001: package org.apache.torque.avalon;
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.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.activity.Disposable;
027: import org.apache.avalon.framework.activity.Initializable;
028: import org.apache.avalon.framework.configuration.Configurable;
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.apache.avalon.framework.configuration.ConfigurationException;
031: import org.apache.avalon.framework.context.Context;
032: import org.apache.avalon.framework.context.ContextException;
033: import org.apache.avalon.framework.context.Contextualizable;
034: import org.apache.avalon.framework.logger.LogEnabled;
035: import org.apache.avalon.framework.logger.Logger;
036: import org.apache.avalon.framework.thread.ThreadSafe;
037: import org.apache.commons.lang.StringUtils;
038: import org.apache.torque.TorqueInstance;
039:
040: /**
041: * Avalon component for Torque.
042: *
043: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
044: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
045: * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
046: * @version $Id: TorqueComponent.java 529759 2007-04-17 20:52:01Z tv $
047: */
048: public class TorqueComponent extends TorqueInstance implements Torque,
049: LogEnabled, Configurable, Initializable, Contextualizable,
050: Disposable, ThreadSafe {
051: /** The Avalon Application Root */
052: private String appRoot = null;
053:
054: /** The Avalon Logger */
055: private Logger logger = null;
056:
057: /** The configuration file name. */
058: private String configFile = null;
059:
060: /*
061: * ========================================================================
062: *
063: * Avalon Component Interfaces
064: *
065: * ========================================================================
066: */
067:
068: /**
069: * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
070: */
071: public void enableLogging(Logger aLogger) {
072: this .logger = aLogger;
073: }
074:
075: /**
076: * Convenience method to provide the Avalon logger the way AbstractLogEnabled does.
077: */
078: public Logger getLogger() {
079: return logger;
080: }
081:
082: /**
083: * @see
084: * org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
085: */
086: public void configure(Configuration configuration)
087: throws ConfigurationException {
088: getLogger().debug("configure(" + configuration + ")");
089:
090: String configurationFile = configuration.getChild("configfile")
091: .getValue();
092:
093: if (StringUtils.isNotEmpty(appRoot)) {
094: if (configurationFile.startsWith("/")) {
095: configurationFile = configurationFile.substring(1);
096: getLogger().debug(
097: "Config File changes to " + configurationFile);
098: }
099:
100: StringBuffer sb = new StringBuffer();
101: sb.append(appRoot);
102: sb.append(File.separator);
103: sb.append(configurationFile);
104:
105: configurationFile = sb.toString();
106: }
107:
108: getLogger().debug("Config File is " + configurationFile);
109:
110: this .configFile = configurationFile;
111: }
112:
113: /**
114: * @see org.apache.avalon.framework.context.Contextualizable
115: */
116: public void contextualize(Context context) throws ContextException {
117: // check context Merlin and YAAFI style
118: try {
119: appRoot = ((File) context.get("urn:avalon:home"))
120: .getAbsolutePath();
121: } catch (ContextException ce) {
122: appRoot = null;
123: }
124:
125: if (appRoot == null) {
126: // check context old ECM style, let exception flow if not available
127: appRoot = (String) context.get("componentAppRoot");
128: }
129:
130: if (StringUtils.isNotEmpty(appRoot)) {
131: if (appRoot.endsWith("/")) {
132: appRoot = appRoot.substring(0, appRoot.length() - 1);
133: getLogger().debug(
134: "Application Root changed to " + appRoot);
135: }
136: }
137: }
138:
139: /**
140: * @see org.apache.avalon.framework.activity.Initializable#initialize()
141: */
142: public void initialize() throws Exception {
143: getLogger().debug("initialize()");
144:
145: TorqueInstance instance = org.apache.torque.Torque
146: .getInstance();
147:
148: Map mapBuilders = instance.getMapBuilders();
149:
150: if (mapBuilders != null) {
151: // Copy the registered MapBuilders and take care that they will be built again
152: for (Iterator i = mapBuilders.keySet().iterator(); i
153: .hasNext();) {
154: String className = (String) i.next();
155: registerMapBuilder(className);
156: }
157: }
158:
159: // Provide the singleton instance to the static accessor
160: org.apache.torque.Torque.setInstance(this );
161:
162: init(configFile);
163: }
164:
165: /**
166: * @see org.apache.avalon.framework.activity.Disposable#dispose()
167: */
168: public void dispose() {
169: getLogger().debug("dispose()");
170: try {
171: shutdown();
172: } catch (Exception e) {
173: getLogger().error("Error while stopping Torque", e);
174: }
175: }
176: }
|