View Javadoc

1   /**
2    * $Id: Daemon.java 17 2006-10-19 15:11:35Z maldito_orco $
3    * $Revision: 17 $
4    * $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
5    *
6    * =========================================================================
7    *
8    * Copyright 2005 Tubo
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   */
22  package org.tubo.system.main;
23  
24  //log
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  //tubo
28  import org.tubo.kernel.Kernel;
29  import org.tubo.kernel.KernelFactory;
30  import org.tubo.exception.TuboConfigurationException;
31  import org.tubo.exception.TuboKernelException;
32  import org.tubo.exception.TuboException;
33  
34  /**
35   * Created: Aug 22, 2006, 6:55:09 PM
36   * Last Modification Date: $Date: 2006-10-19 12:11:35 -0300 (Thu, 19 Oct 2006) $
37   *
38   * @author maldito_orco (maldito_orco@users.sourceforge.net)
39   * @version $Revision: 17 $
40   */
41  public class Daemon {
42      public static final String RCS_ID = "$Id: Daemon.java 17 2006-10-19 15:11:35Z maldito_orco $";
43      private static Log log = LogFactory.getLog(Daemon.class);
44  
45      private String baseConfigLocation = KernelFactory.BASE_CONFIG_LOCATION;
46      private String configLocation = "tubo.xml";
47  
48      /** kernel instance */
49      private Kernel kernel = null;
50  
51      /**
52       * Default Constructor
53       */
54      public Daemon() {
55      }
56  
57      protected void startup() {
58          //Kernel kernel = null;
59          try {
60              //
61              // create factory
62              KernelFactory factory = KernelFactory.getInstance();
63              //
64              // set base config location
65              factory.setBaseConfigLocation(baseConfigLocation);
66              //
67              // set config location
68              factory.appendConfigLocation(configLocation);
69              //
70              // create kernel via factory
71              kernel = factory.createKernel();
72          } catch (TuboKernelException e) {
73              log.fatal("Tubo configuration can't be loaded",e);
74              System.exit(1);
75          }
76          //
77          // booting kernel
78          try {
79              kernel.boot();
80          } catch (TuboException e) {
81              log.fatal("Kernel can't boot",e);
82              System.exit(1);
83          }
84          //
85          // add our shutdown hook
86          Runtime.getRuntime().addShutdownHook(new Thread("Tubo shutdown thread") {
87              public void run() {
88                  System.out.println("\rTubo Shutdown Hook detected");
89                  kernel.shutdown();
90              }
91          });
92          //
93          // capture this thread until the kernel is ready to exit
94          while (kernel.isRunning()) {
95              try {
96                  synchronized (kernel) {
97                      kernel.wait();
98                  }
99              } catch (InterruptedException e) {
100                 // continue
101             }
102         }
103 
104     }
105 
106     public String getBaseConfigLocation() {
107         return baseConfigLocation;
108     }
109 
110     public void setBaseConfigLocation(String baseConfigLocation) {
111         this.baseConfigLocation = baseConfigLocation;
112     }
113 
114     public String getConfigLocation() {
115         return configLocation;
116     }
117 
118     public void setConfigLocation(String configLocation) {
119         this.configLocation = configLocation;
120     }
121 
122     protected static void processArgs(Daemon daemon, String[] args) {
123         for (int i=0; i<args.length; i++) {
124             //
125             // set Base Config Location
126             if ("--baseconfig".equals(args[i])) {
127                 i++;
128                 if (i<args.length) {
129                     daemon.setBaseConfigLocation(args[i]);
130                 }
131             }
132             //
133             // set Config Location
134             if ("--config".equals(args[i])) {
135                 i++;
136                 if (i<args.length) {
137                     daemon.setConfigLocation(args[i]);
138                 }
139             }
140 
141 
142         }
143 
144     }
145 
146 
147     /**
148      * Static entry point allowing a Kernel to be run from the command line.
149      *
150      * Once the Kernel is booted and the configuration is loaded, the process
151      * will remain running until the shutdown() method on the kernel is
152      * invoked or until the JVM exits.
153      *
154      * @param args the command line arguments
155      */
156     public static void main(String[] args) {
157         Daemon daemon = new Daemon();
158         processArgs(daemon,args);
159         daemon.startup();
160         //
161         //
162         System.out.println("KKK");
163     }
164 }