0
|
1 package nabble.model;
|
|
2
|
|
3 import fschmidt.util.java.BasicRMIClientSocketFactory;
|
|
4 import fschmidt.util.java.BasicRMIServerSocketFactory;
|
|
5 import luan.LuanException;
|
|
6 import luan.Luan;
|
|
7 import luan.LuanTable;
|
|
8 import org.slf4j.Logger;
|
|
9 import org.slf4j.LoggerFactory;
|
|
10
|
|
11 import java.io.IOException;
|
|
12 import java.net.InetAddress;
|
|
13 import java.net.ServerSocket;
|
|
14 import java.rmi.Remote;
|
|
15 import java.rmi.RemoteException;
|
|
16 import java.rmi.registry.LocateRegistry;
|
|
17 import java.rmi.registry.Registry;
|
|
18 import java.rmi.server.RMIClientSocketFactory;
|
|
19 import java.rmi.server.RMIServerSocketFactory;
|
|
20 import java.rmi.server.UnicastRemoteObject;
|
|
21 import java.util.ArrayList;
|
|
22 import java.util.HashSet;
|
|
23 import java.util.List;
|
|
24 import java.util.Map;
|
|
25 import java.util.Set;
|
|
26
|
|
27
|
|
28 public final class Init {
|
|
29
|
|
30 private static final Logger logger = LoggerFactory.getLogger(Init.class);
|
|
31
|
|
32 private static final Map initMap;
|
|
33
|
|
34 private static final Thread shutdownThread = new Thread(new Runnable(){public void run() {
|
|
35 logger.info("begin shutdown");
|
|
36 Batch.shutdown();
|
|
37 logger.info("batch shutdown done");
|
|
38 for( Runnable r : shutdownHooks ) {
|
|
39 r.run();
|
|
40 }
|
|
41 logger.info("shutdownHooks done");
|
|
42 if( modelHomeStarted ) {
|
|
43 Executors.shutdown();
|
|
44 logger.info("Executors shutdown done");
|
|
45 }
|
|
46 if( luceneStarted ) {
|
|
47 Lucene.shutdown();
|
|
48 logger.info("Lucene shutdown done");
|
|
49 }
|
|
50 if( dailyNumberStarted ) {
|
|
51 DailyNumber.shutdown();
|
|
52 }
|
|
53 logger.info("end shutdown");
|
|
54 cachingfilter.CachingFilter.shutdown();
|
|
55 logger.info("CachingFilter shutdown done");
|
|
56 }});
|
|
57
|
|
58
|
|
59 static volatile boolean luceneStarted = false;
|
|
60 static volatile boolean modelHomeStarted = false;
|
|
61 static volatile boolean dailyNumberStarted = false;
|
|
62 static {
|
|
63 setCustomProperties();
|
|
64
|
|
65 // ClassLoader cl = Init.class.getClassLoader();
|
|
66 // interp.setClassLoader(cl);
|
|
67 // Thread.currentThread().setContextClassLoader(cl);
|
|
68 try {
|
|
69 Luan luan = new Luan();
|
|
70 LuanTable mod = (LuanTable)luan.eval("return require 'file:conf/Init.luan'");
|
|
71 initMap = mod.asMap();
|
|
72 Runtime.getRuntime().addShutdownHook(shutdownThread);
|
|
73 } catch(LuanException e) {
|
|
74 logger.error(e.getLuanStackTraceString());
|
|
75 System.exit(-1);
|
|
76 throw new RuntimeException(); // for compiler
|
|
77 } catch(Throwable e) {
|
|
78 logger.error("",e);
|
|
79 System.exit(-1);
|
|
80 throw new RuntimeException(); // for compiler
|
|
81 }
|
|
82 }
|
|
83
|
|
84 private static List<Runnable> shutdownHooks = new ArrayList<Runnable>();
|
|
85
|
|
86 public static void addShutdownHook(Runnable r) {
|
|
87 shutdownHooks.add(r);
|
|
88 }
|
|
89
|
|
90 private static void setCustomProperties() {
|
|
91 // InetAddress cache should last 30 seconds only.
|
|
92 System.setProperty("networkaddress.cache.ttl", "30");
|
|
93 }
|
|
94
|
|
95 public static Object get(String var) {
|
|
96 return initMap.get(var);
|
|
97 }
|
|
98
|
|
99 public static Integer getInteger(String var) {
|
|
100 Number n = (Number)get(var);
|
|
101 if( n==null )
|
|
102 return null;
|
|
103 int i = n.intValue();
|
|
104 if( i != n.doubleValue() )
|
|
105 throw new RuntimeException("init var '"+var+"' isn't an integer");
|
|
106 return i;
|
|
107 }
|
|
108
|
|
109 public static Float getFloat(String var) {
|
|
110 Number n = (Number)get(var);
|
|
111 return n==null ? null : n.floatValue();
|
|
112 }
|
|
113
|
|
114 public static Set getSet(String var) {
|
|
115 return (Set)get(var);
|
|
116 }
|
|
117
|
|
118 public static <T> T get(String var,T defaultVal) {
|
|
119 if( defaultVal instanceof Integer ) {
|
|
120 @SuppressWarnings("unchecked")
|
|
121 T obj = (T)getInteger(var);
|
|
122 return obj!=null ? obj : defaultVal;
|
|
123 }
|
|
124 if( defaultVal instanceof Float ) {
|
|
125 @SuppressWarnings("unchecked")
|
|
126 T obj = (T)getFloat(var);
|
|
127 return obj!=null ? obj : defaultVal;
|
|
128 }
|
|
129 if( defaultVal instanceof Set ) {
|
|
130 @SuppressWarnings("unchecked")
|
|
131 T obj = (T)getSet(var);
|
|
132 return obj!=null ? obj : defaultVal;
|
|
133 }
|
|
134 if( defaultVal != null ) {
|
|
135 Class defaultCls = defaultVal.getClass();
|
|
136 if( defaultCls.isArray() && !defaultCls.getComponentType().isPrimitive() ) {
|
|
137 Object obj = get(var);
|
|
138 if( obj instanceof LuanTable ) {
|
|
139 LuanTable t = (LuanTable)obj;
|
|
140 if( t.isList() ) {
|
|
141 @SuppressWarnings("unchecked")
|
|
142 T rtn = (T)t.asList().toArray((Object[])defaultVal);
|
|
143 return rtn;
|
|
144 }
|
|
145 }
|
|
146 }
|
|
147 }
|
|
148 @SuppressWarnings("unchecked")
|
|
149 T obj = (T)get(var);
|
|
150 return obj!=null ? obj : defaultVal;
|
|
151 }
|
|
152
|
|
153 public static final boolean hasDaemons = nabble.utils.Jetty.isJetty;
|
|
154
|
|
155 public static final String tempDir = (String)Init.get("local_dir")+"temp/"; // General purpose folder
|
|
156
|
|
157 public static final int quotedLinesToHide = Init.get("quotedLinesToHide",10);
|
|
158
|
|
159
|
|
160 private static final RMIClientSocketFactory rmiClientSocketFactory = new BasicRMIClientSocketFactory();
|
|
161 private static RMIServerSocketFactory rmiServerSocketFactory;
|
|
162 public static final String localRmiServer = (String)get("localRmiServer");
|
|
163 private static Registry registry;
|
|
164
|
|
165 static {
|
|
166 if( localRmiServer!=null && Init.hasDaemons ) {
|
|
167 try {
|
|
168 String[] a = localRmiServer.split(":");
|
|
169 String rmiHost = a[0];
|
|
170 String rmiPort = a[1];
|
|
171 System.setProperty("java.rmi.server.hostname", rmiHost);
|
|
172 rmiServerSocketFactory = new InternalRMIServerSocketFactory(InetAddress.getByName(rmiHost));
|
|
173 logger.info("Starting rmi server on port "+rmiPort);
|
|
174 registry = LocateRegistry.createRegistry(Integer.parseInt(rmiPort), rmiClientSocketFactory, rmiServerSocketFactory);
|
|
175 nabble.model.export.ImportServerImpl.bind();
|
|
176 } catch (Exception e) {
|
|
177 logger.error("",e);
|
|
178 System.exit(-1);
|
|
179 }
|
|
180 }
|
|
181 }
|
|
182
|
|
183 public static void rmiBind(String name,Remote obj) {
|
|
184 try {
|
|
185 registry.bind(name,rmiExport(obj));
|
|
186 } catch (Exception e) {
|
|
187 logger.error("rmiBind failed, exiting",e);
|
|
188 System.exit(-1);
|
|
189 }
|
|
190 }
|
|
191
|
|
192 public static <T extends Remote> T rmiExport(T obj) throws RemoteException {
|
|
193 @SuppressWarnings("unchecked")
|
|
194 T t = (T)UnicastRemoteObject.exportObject( obj,0, rmiClientSocketFactory, rmiServerSocketFactory);
|
|
195 return t;
|
|
196 }
|
|
197
|
|
198 private static class InternalRMIServerSocketFactory extends BasicRMIServerSocketFactory {
|
|
199 private final InetAddress host;
|
|
200
|
|
201 InternalRMIServerSocketFactory(InetAddress host) {
|
|
202 this.host = host;
|
|
203 }
|
|
204
|
|
205 public ServerSocket createServerSocket(int port) throws IOException {
|
|
206 return new ServerSocket(port, 0, host);
|
|
207 }
|
|
208 }
|
|
209
|
|
210 public static void nop() {}
|
|
211
|
|
212 private Init() {} // never
|
|
213 }
|