001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.util;
027:
028: import org.apache.avalon.framework.configuration.Configurable;
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.apache.avalon.framework.container.ContainerUtil;
031: import org.apache.avalon.framework.context.Context;
032: import org.apache.avalon.framework.logger.ConsoleLogger;
033: import org.apache.avalon.framework.logger.Logger;
034: import org.apache.avalon.framework.parameters.Parameterizable;
035: import org.apache.avalon.framework.parameters.Parameters;
036: import org.apache.avalon.framework.service.ServiceManager;
037: import org.jicarilla.container.Resolver;
038: import org.jicarilla.container.integration.avalon.JicarillaBasedAvalonContext;
039: import org.jicarilla.container.integration.avalon.JicarillaBasedAvalonServiceManager;
040:
041: /**
042: * Utility methods that encapsulate reflection magic and heuristics to deal
043: * with Avalon-Framework components.
044: *
045: * @author <a href="mail at leosimons dot com">Leo Simons</a>
046: * @version $Id: Type1Util.java,v 1.2 2004/02/29 18:08:10 lsimons Exp $
047: */
048: public class Type1Util {
049: public final static ConsoleLogger FallbackLogger = new ConsoleLogger(
050: ConsoleLogger.LEVEL_WARN);
051:
052: public static void startAvalonLifecycle(final Object instance,
053: final Resolver container) throws InstantiationException {
054: try {
055: ContainerUtil.enableLogging(instance, getLogger(container,
056: instance.getClass().getName()));
057: ContainerUtil
058: .contextualize(instance, getContext(container));
059: ContainerUtil.service(instance,
060: getServiceManager(container));
061:
062: if (instance instanceof Configurable
063: || instance instanceof Parameterizable) {
064: final Configuration configuration = ConfigurationUtil
065: .getConfiguration(container, instance
066: .getClass().getName());
067: ContainerUtil.configure(instance, configuration);
068: ContainerUtil.parameterize(instance, Parameters
069: .fromConfiguration(configuration));
070: }
071:
072: ContainerUtil.initialize(instance);
073: ContainerUtil.start(instance);
074: } catch (Exception e) {
075: final InstantiationException ex = new InstantiationException();
076: ex.initCause(e);
077: throw ex;
078: }
079: }
080:
081: public static Logger getLogger(final Resolver container,
082: final String fallbackLoggerName) {
083: Logger logger = null;
084:
085: logger = (Logger) container.get(Logger.class);
086:
087: if (logger == null)
088: logger = FallbackLogger.getChildLogger(fallbackLoggerName);
089:
090: return logger;
091: }
092:
093: public static Context getContext(final Resolver container) {
094: return new JicarillaBasedAvalonContext(container);
095: }
096:
097: public static ServiceManager getServiceManager(
098: final Resolver container) {
099: return new JicarillaBasedAvalonServiceManager(container);
100: }
101: }
|