1
2
3
4
5
6
7
8
9
10
11
12
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
29
30
31
32
33 public class Basher
34 {
35
36
37
38 private static Registry _registry;
39
40
41
42
43 private static AtomicBoolean _shutdownInitiated = new AtomicBoolean(false);
44
45
46
47
48
49
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
62
63
64
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
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
103
104
105
106
107
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 }