KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > tools > FortressBean


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.tools;
19
20 import java.lang.reflect.Method JavaDoc;
21
22 import org.apache.avalon.fortress.impl.DefaultContainer;
23 import org.apache.avalon.fortress.impl.DefaultContainerManager;
24 import org.apache.avalon.fortress.util.FortressConfig;
25 import org.apache.avalon.framework.activity.Disposable;
26 import org.apache.avalon.framework.activity.Initializable;
27 import org.apache.avalon.framework.container.ContainerUtil;
28 import org.apache.avalon.framework.logger.ConsoleLogger;
29 import org.apache.avalon.framework.logger.LogEnabled;
30 import org.apache.avalon.framework.logger.Logger;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.Serviceable;
34
35 /**
36  * Bean for making it easier to run Fortress, for example as Ant task.
37  *
38  * @author <a HREF="mailto:dev@avalon.apache.org">The Avalon Team</a>
39  * @version CVS $Revision: 1.5 $ $Date: 2004/02/28 15:16:27 $
40  */

41 public class FortressBean implements Initializable, LogEnabled, Serviceable, Disposable {
42
43     private final FortressConfig config = new FortressConfig();
44     private Logger logger = null;
45     private DefaultContainerManager cm;
46     private DefaultContainer container;
47     private ServiceManager sm;
48
49     private String JavaDoc lookupComponentRole = null;
50     private String JavaDoc invokeMethod = null;
51
52     private boolean systemExitOnDispose = true;
53
54     /**
55      * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
56      */

57     public void enableLogging(Logger logger) {
58         this.logger = logger;
59     }
60
61     protected final Logger getLogger() {
62         if (this.logger == null) this.logger = new ConsoleLogger();
63         return this.logger;
64     }
65
66     /**
67      * @see org.apache.avalon.framework.activity.Initializable#initialize()
68      */

69     public void initialize() throws Exception JavaDoc {
70         //only initialize if we do not already have a servicemanager passed in from outside
71
if (this.sm == null) {
72             if (Thread.currentThread().getContextClassLoader() == null) {
73                 if (this.getClass().getClassLoader() != null) {
74                     ClassLoader JavaDoc cl = this.getClass().getClassLoader();
75                     config.setContextClassLoader(cl);
76                     Thread.currentThread().setContextClassLoader(cl);
77                 } else {
78                     getLogger().warn("context classloader not set and class classloader is null!");
79                 }
80             }
81             // Get the root container initialized
82
this.cm = new DefaultContainerManager(config.getContext());
83             ContainerUtil.initialize(cm);
84
85             this.container = (DefaultContainer) cm.getContainer();
86             this.sm = container.getServiceManager();
87         }
88     }
89
90     public void run() throws Exception JavaDoc {
91         Object JavaDoc component = getServiceManager().lookup(lookupComponentRole);
92         Method JavaDoc method = component.getClass().getMethod(invokeMethod, null);
93         method.invoke(component, null);
94     }
95
96     /**
97      * Implementation execute() method for Ant compatability.
98      */

99     public void execute() {
100         try {
101             initialize();
102             try {
103                 run();
104             } catch (Exception JavaDoc e) {
105                 getLogger().error("error while running", e);
106             }
107         } catch (Exception JavaDoc e) {
108             getLogger().error("error while initializing", e);
109         }
110         dispose();
111     }
112
113     /**
114      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
115      */

116     public void service(ServiceManager sm) throws ServiceException {
117         if (this.sm == null) this.sm = sm;
118     }
119
120     /**
121      * @see org.apache.avalon.framework.activity.Disposable#dispose()
122      */

123     public void dispose() {
124         // Properly clean up when we are done
125
org.apache.avalon.framework.container.ContainerUtil.dispose( cm );
126         
127         //system exit, in case we were running some GUI and some thread is still active
128
if (this.systemExitOnDispose) {
129             Thread.yield();
130             try {
131                 Thread.sleep(100);
132             } catch (InterruptedException JavaDoc e) {
133                 //ignore
134
}
135             System.exit(0);
136         }
137     }
138
139     protected ServiceManager getServiceManager() {
140         return this.sm;
141     }
142
143     /**
144      * The container implementation has to be a subclass of
145      * <code>org.apache.avalon.fortress.impl.DefaultContainer</code>.
146      *
147      * @param containerClass fully qualified class name of the container implementation class.
148      */

149     public void setContainerClass(String JavaDoc containerClass) throws Exception JavaDoc {
150         config.setContextClassLoader(getClass().getClassLoader());
151         config.setContainerClass(containerClass);
152     }
153
154     public void setContainerConfiguration(String JavaDoc containerConfiguration) {
155         config.setContainerConfiguration(containerConfiguration);
156     }
157
158     public void setContextDirectory(String JavaDoc contextDirectory) {
159         config.setContextDirectory(contextDirectory);
160     }
161
162     public void setInstrumentManagerConfiguration(String JavaDoc instrumentManagerConfiguration) {
163         config.setInstrumentManagerConfiguration(instrumentManagerConfiguration);
164     }
165
166     public void setLoggerManagerConfiguration(String JavaDoc loggerManagerConfiguration) {
167         config.setLoggerManagerConfiguration(loggerManagerConfiguration);
168     }
169
170     public void setRoleManagerConfiguration(String JavaDoc roleManagerConfiguration) {
171         config.setRoleManagerConfiguration(roleManagerConfiguration);
172     }
173
174     public void setWorkDirectory(String JavaDoc workDirectory) {
175         config.setWorkDirectory(workDirectory);
176     }
177
178     public void setInvokeMethod(String JavaDoc invokeMethod) {
179         this.invokeMethod = invokeMethod;
180     }
181
182     public void setLookupComponentRole(String JavaDoc lookupComponentRole) {
183         this.lookupComponentRole = lookupComponentRole;
184     }
185
186     /**
187      * Should we call System.exit(0) after we are finished with processing.
188      * Useful if the components have a GUI and there are some threads running that
189      * do not allow the JVM to exit.
190      */

191     public void setSystemExitOnDispose(boolean systemExitOnDispose) {
192         this.systemExitOnDispose = systemExitOnDispose;
193     }
194
195 }
196
Popular Tags