001 package org.apache.fulcrum.testcontainer;
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
024 import org.apache.avalon.framework.component.Component;
025 import org.apache.avalon.framework.component.ComponentException;
026 import org.apache.avalon.framework.logger.AbstractLogEnabled;
027 import org.apache.avalon.framework.logger.ConsoleLogger;
028 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
029 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
030 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
031
032 /**
033 * This is a simple YAAFI based container that can be used in unit test
034 * of the fulcrum components.
035 *
036 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
037 */
038 public class YAAFIContainer extends AbstractLogEnabled implements Container
039 {
040 /** The YAAFI configuration */
041 private ServiceContainerConfiguration config;
042
043 /** Component manager */
044 private ServiceContainer manager;
045
046 /**
047 * Constructor
048 */
049 public YAAFIContainer()
050 {
051 // org.apache.log4j.BasicConfigurator.configure();
052 this.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) );
053 this.config = new ServiceContainerConfiguration();
054 }
055
056 /**
057 * Starts up the container and initializes it.
058 *
059 * @param configFileName Name of the component configuration file
060 * @param roleFileName Name of the role configuration file
061 */
062 public void startup(
063 String configFileName,
064 String roleFileName,
065 String parametersFileName )
066 {
067 getLogger().debug("Starting container...");
068
069 this.config.setComponentConfigurationLocation( configFileName );
070 this.config.setComponentRolesLocation( roleFileName );
071 this.config.setParametersLocation( parametersFileName );
072 this.config.setLogger( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) );
073
074 File configFile = new File(configFileName);
075
076 if (!configFile.exists())
077 {
078 throw new RuntimeException(
079 "Could not initialize the container because the config file could not be found:" + configFile);
080 }
081
082 try
083 {
084 initialize();
085 getLogger().info("YaffiContainer ready.");
086 }
087 catch (Exception e)
088 {
089 getLogger().error("Could not initialize the container", e);
090 throw new RuntimeException("Could not initialize the container");
091 }
092 }
093
094 // -------------------------------------------------------------
095 // Avalon lifecycle interfaces
096 // -------------------------------------------------------------
097
098 /**
099 * Initializes the container
100 *
101 * @throws Exception generic exception
102 */
103 public void initialize() throws Exception
104 {
105 this.manager = ServiceContainerFactory.create(
106 this.config
107 );
108 }
109
110 /**
111 * Disposes of the container and releases resources
112 */
113 public void dispose()
114 {
115 getLogger().debug("Disposing of container...");
116 if( this.manager != null )
117 {
118 this.manager.dispose();
119 }
120 getLogger().info("YaffiContainer has been disposed.");
121 }
122
123 /**
124 * Returns an instance of the named component
125 *
126 * @param roleName Name of the role the component fills.
127 * @throws ComponentException generic exception
128 */
129 public Object lookup(String roleName) throws ComponentException
130 {
131 try
132 {
133 return this.manager.lookup(roleName);
134 }
135 catch( Exception e )
136 {
137 String msg = "Failed to lookup role " + roleName;
138 throw new ComponentException(roleName,msg,e);
139 }
140 }
141
142 /**
143 * Releases the component implementing the Component interface. This
144 * interface is deprecated but still around in Fulcrum
145 *
146 * @param component
147 */
148 public void release(Component component)
149 {
150 this.manager.release(component);
151 }
152
153 /**
154 * Releases the component
155 *
156 * @param component
157 */
158 public void release(Object component)
159 {
160 this.manager.release(component);
161 }
162 }