KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > yaafi > testcontainer > Container


1 package org.apache.fulcrum.yaafi.testcontainer;
2 /* ====================================================================
3  * The Apache Software License, Version 1.1
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Apache" and "Apache Software Foundation" and
28  * "Apache Turbine" must not be used to endorse or promote products
29  * derived from this software without prior written permission. For
30  * written permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * "Apache Turbine", nor may "Apache" appear in their name, without
34  * prior written permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 import java.io.File JavaDoc;
56
57 import org.apache.avalon.framework.activity.Disposable;
58 import org.apache.avalon.framework.activity.Initializable;
59 import org.apache.avalon.framework.component.Component;
60 import org.apache.avalon.framework.component.ComponentException;
61 import org.apache.avalon.framework.context.DefaultContext;
62 import org.apache.avalon.framework.logger.AbstractLogEnabled;
63 import org.apache.avalon.framework.logger.ConsoleLogger;
64 import org.apache.avalon.framework.logger.Logger;
65 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
66 import org.apache.fulcrum.yaafi.framework.container.ServiceContainerImpl;
67 import org.apache.fulcrum.yaafi.framework.factory.ServiceManagerFactory;
68 import org.apache.fulcrum.yaafi.service.servicemanager.ServiceManagerService;
69
70
71 /**
72  * This is a simple YAAFI based container that can be used in unit test
73  * of the fulcrum components.
74  *
75  * @author <a HREF="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
76  */

77 public class Container extends AbstractLogEnabled implements Initializable, Disposable
78 {
79     /** Key used in the context for defining the application root */
80     public static String JavaDoc COMPONENT_APP_ROOT = "componentAppRoot";
81
82     /** Alternate Merlin Friendly Key used in the context for defining the application root */
83     public static String JavaDoc URN_AVALON_HOME = "urn:avalon:home";
84
85     /** Alternate Merlin Friendly Key used in the context for defining the application root */
86     public static String JavaDoc URN_AVALON_TEMP = "urn:avalon:temp";
87
88     /** Component manager */
89     private ServiceContainer manager;
90     
91     /** Configuration file name */
92     private String JavaDoc configFileName;
93     
94     /** Role file name */
95     private String JavaDoc roleFileName;
96     
97     /** Parameters file name */
98     private String JavaDoc parametersFileName;
99     
100     
101     /**
102      * Constructor
103      */

104     public Container()
105     {
106         // org.apache.log4j.BasicConfigurator.configure();
107
this.manager = new ServiceContainerImpl();
108         this.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) );
109     }
110         
111     /**
112      * Starts up the container and initializes it.
113      *
114      * @param configFileName Name of the component configuration file
115      * @param roleFileName Name of the role configuration file
116      */

117     public void startup(String JavaDoc configFileName, String JavaDoc roleFileName, String JavaDoc parametersFileName )
118     {
119         getLogger().debug("Starting container...");
120         
121         this.configFileName = configFileName;
122         this.roleFileName = roleFileName;
123         this.parametersFileName = parametersFileName;
124         
125         File JavaDoc configFile = new File JavaDoc(configFileName);
126         
127         if (!configFile.exists())
128         {
129             throw new RuntimeException JavaDoc(
130                 "Could not initialize the container because the config file could not be found:" + configFile);
131         }
132
133         try
134         {
135             initialize();
136             getLogger().info("YaffiContainer ready.");
137         }
138         catch (Exception JavaDoc e)
139         {
140             getLogger().error("Could not initialize the container", e);
141             throw new RuntimeException JavaDoc("Could not initialize the container");
142         }
143     }
144     
145     // -------------------------------------------------------------
146
// Avalon lifecycle interfaces
147
// -------------------------------------------------------------
148
/**
149      * Initializes the container
150      *
151      * @throws Exception generic exception
152      */

153     public void initialize() throws Exception JavaDoc
154     {
155         DefaultContext context = new DefaultContext();
156         String JavaDoc absolutePath = new File JavaDoc("").getAbsolutePath();
157         context.put(COMPONENT_APP_ROOT, absolutePath);
158         context.put(URN_AVALON_HOME, new File JavaDoc( new File JavaDoc("").getAbsolutePath() ) );
159         
160         Logger logger = new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG );
161         
162         this.manager = ServiceManagerFactory.create(
163             logger,
164             this.roleFileName,
165             this.configFileName,
166             this.parametersFileName,
167             context
168             );
169     }
170
171     /**
172      * Disposes of the container and releases resources
173      */

174     public void dispose()
175     {
176         getLogger().debug("Disposing of container...");
177         this.manager.dispose();
178         getLogger().info("YaffiContainer has been disposed.");
179     }
180     /**
181      * Returns an instance of the named component
182      *
183      * @param roleName Name of the role the component fills.
184      * @throws ComponentException generic exception
185      */

186     public Object JavaDoc lookup(String JavaDoc roleName) throws ComponentException
187     {
188         try
189         {
190             return ServiceManagerService.getServiceManager().lookup(roleName);
191         }
192         catch( Exception JavaDoc e )
193         {
194             String JavaDoc msg = "Failed to lookup role " + roleName;
195             throw new ComponentException(roleName,msg,e);
196         }
197     }
198     /**
199      * Releases the component implementing the Component interface. This
200      * interface is depracted but still around in Fulcrum
201      *
202      * @param component
203      */

204     public void release(Component component)
205     {
206         ServiceManagerService.getServiceManager().release(component);
207     }
208     /**
209      * Releases the component
210      *
211      * @param component
212      */

213     public void release(Object JavaDoc component)
214     {
215         ServiceManagerService.getServiceManager().release(component);
216     }
217 }
218
Popular Tags