View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package net.sourceforge.basher;
16  
17  import java.util.Iterator;
18  import java.util.Timer;
19  import java.util.TimerTask;
20  import java.util.concurrent.atomic.AtomicBoolean;
21  
22  import bsh.EvalError;
23  import bsh.Interpreter;
24  import org.ops4j.gaderian.Registry;
25  import org.ops4j.gaderian.impl.RegistryBuilder;
26  
27  /**
28   * Main entry point to the Basher when using the command line mode or for applications not using Gaderian.
29   *
30   * @author Johan Lindquist
31   * @version 1.0
32   */
33  public class Basher
34  {
35      /**
36       * Basher Gaderian registry instance
37       */
38      private static Registry _registry;
39  
40      /**
41       * Flag indicating whether or not a shutdown has been initiated
42       */
43      private static AtomicBoolean _shutdownInitiated = new AtomicBoolean(false);
44  
45      /**
46       * Sets the registry this Basher instance should use.
47       *
48       * @param registry The Gaderian registry to use.
49       * @throws IllegalArgumentException If the registry has already been set.
50       */
51      public static void setRegistry(final Registry registry)
52      {
53          if (_registry != null)
54          {
55              throw new IllegalStateException("Basher has already been initialized");
56          }
57          _registry = registry;
58      }
59  
60      /**
61       * Retrieves the registry used by Basher
62       *
63       * @return The registry in use
64       * @throws IllegalStateException If the registry has not been set.
65       */
66      public static Registry getRegistry()
67      {
68          if (_registry == null)
69          {
70              throw new IllegalStateException("Basher has not been initialized");
71          }
72          if (_shutdownInitiated.get())
73          {
74              throw new IllegalStateException("Basher shutdown has been initiated");
75          }
76          return _registry;
77      }
78  
79      /**
80       * Convenience method to be used when a thread cleanup of the registry should happen.
81       */
82      public static void fireCleanUpThread()
83      {
84          if (_shutdownInitiated.get())
85          {
86              return;
87          }
88          try
89          {
90              if (_registry != null)
91              {
92                  _registry.cleanupThread();
93              }
94          }
95          catch (Throwable e)
96          {
97              e.printStackTrace();
98          }
99      }
100 
101     /**
102      * Retrieves a service from the Basher (Gaderian) registry.  This is only to be used by
103      * application that do not themselves use Gaderian.
104      *
105      * @param service The class of the service to retrieve.
106      * @return An instance of the service.
107      * @throws Exception If the registry fails to intialize or the service does not exist.
108      */
109     public static <T> T getService(final Class<T> service) throws Exception
110     {
111         if (_registry == null)
112         {
113             _registry = RegistryBuilder.constructDefaultRegistry();
114         }
115         return (T)_registry.getService(service);
116     }
117 
118 }