KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > yaafi > cli > Main


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

19
20 import org.apache.avalon.framework.logger.ConsoleLogger;
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.fulcrum.yaafi.framework.container.ServiceConstants;
23 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
24 import org.apache.fulcrum.yaafi.framework.container.ServiceContainerImpl;
25 import org.apache.fulcrum.yaafi.framework.factory.ServiceManagerFactory;
26
27
28 /**
29  * An example of the embedding of a merlin kernel inside a main
30  * method. The objective of the example is to demonstrate a
31  * simple embedded scenario.
32  */

33
34 public class Main
35 {
36     /** The service manager */
37     private ServiceContainer manager;
38     
39     /** The name of the block config file */
40     private String JavaDoc componentRoleValue;
41     
42     /** The name of the component config file */
43     private String JavaDoc componentConfigValue;
44
45     /** The name of the parameters file */
46     private String JavaDoc componentParametersValue;
47
48     /** Thread for processing the shutdown notification of the JVM */
49     private Thread JavaDoc shutdownThread;
50     
51     /** Do we terminate the main thread ?! */
52     private boolean isServerMode;
53     
54     /** The logger being used */
55     private Logger logger;
56     
57     /**
58      * Constructor
59      */

60     private Main()
61     {
62         this.isServerMode = false;
63         this.componentRoleValue = ServiceConstants.COMPONENT_ROLE_VALUE;
64         this.componentConfigValue = ServiceConstants.COMPONENT_CONFIG_VALUE;
65         this.componentParametersValue = ServiceConstants.COMPONENT_PARAMETERS_VALUE;
66                 
67         this.logger = new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG );
68     }
69
70     /**
71      * The main method
72      * @param args Command line arguments
73      * @throws Exception
74      */

75     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
76     {
77        Main impl = new Main();
78        
79        // Initialize the service manager
80

81        impl.initialize();
82        
83        boolean terminateNow = ( impl.isServerMode ? false : true );
84        
85        while( terminateNow == false )
86        {
87            try
88            {
89                Thread.currentThread().sleep(1000);
90            }
91            catch (InterruptedException JavaDoc e)
92            {
93                terminateNow = true;
94            }
95        }
96        
97        impl.dispose();
98     }
99     
100     protected void initialize() throws Exception JavaDoc
101     {
102         // intialize the service manager
103

104         this.manager = ServiceManagerFactory.create(
105             new ConsoleLogger(),
106             this.componentRoleValue,
107             this.componentConfigValue,
108             this.componentParametersValue
109             );
110              
111         // initialize shutdown hoook of JVM
112

113         Shutdown JavaDoc shutdown = new Shutdown JavaDoc( this.getManager(), this.getLogger() );
114         this.shutdownThread = new Thread JavaDoc(shutdown);
115         Runtime.getRuntime().addShutdownHook(shutdownThread);
116     }
117
118     protected synchronized void dispose() throws Exception JavaDoc
119     {
120         if( this.getManager() != null )
121         {
122             this.getManager().dispose();
123         }
124     }
125
126     /**
127      * @return Returns the logger.
128      */

129     protected Logger getLogger()
130     {
131         return logger;
132     }
133
134     /////////////////////////////////////////////////////////////////////////
135
// Generated getters & setters
136
/////////////////////////////////////////////////////////////////////////
137

138     /**
139      * @return Returns the manager.
140      */

141     public ServiceContainer getManager()
142     {
143         return manager;
144     }
145     /**
146      * @param manager The manager to set.
147      */

148     public void setManager(ServiceContainerImpl manager)
149     {
150         this.manager = manager;
151     }
152     /**
153      * @return Returns the componentConfigValue.
154      */

155     public String JavaDoc getComponentConfigValue()
156     {
157         return this.componentConfigValue;
158     }
159     /**
160      * @param componentConfigValue The componentConfigValue to set.
161      */

162     public void setComponentConfigValue(String JavaDoc componentConfigValue)
163     {
164         this.componentConfigValue = componentConfigValue;
165     }
166     /**
167      * @return Returns the componentRoleValue.
168      */

169     public String JavaDoc getComponentRoleValue()
170     {
171         return this.componentRoleValue;
172     }
173     /**
174      * @param componentRoleValue The componentRoleValue to set.
175      */

176     public void setComponentRoleValue(String JavaDoc componentRoleValue)
177     {
178         this.componentRoleValue = componentRoleValue;
179     }
180 }
181
Popular Tags