1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.fosstrak.capturingapp;
23
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Future;
31
32 import org.apache.log4j.Logger;
33 import org.fosstrak.capturingapp.util.CaptureAppWorker;
34 import org.fosstrak.capturingapp.wsdl.ArrayOfString;
35 import org.fosstrak.capturingapp.wsdl.CaptureAppPortType;
36 import org.fosstrak.capturingapp.wsdl.EmptyParms;
37
38
39
40
41
42
43
44
45
46
47
48 public class CaptureAppPortTypeImpl implements CaptureAppPortType {
49
50
51 private static ExecutorService pool = java.util.concurrent.
52 Executors.newCachedThreadPool();
53
54
55 private static Map<String, CaptureAppWorker> captureApps = new ConcurrentHashMap<String, CaptureAppWorker> ();
56
57
58 private static final Logger log = Logger.getLogger(CaptureAppPortTypeImpl.class);
59
60
61 public static final String CONFIG_FILE = "/captureapplication.properties";
62
63
64 public static final String DEFAULT_HANDLER_CLASS_NAME =
65 "org.fosstrak.capturingapp.DefaultECReportHandler";
66
67
68 private static boolean initialized = false;
69
70
71
72
73
74 public CaptureAppPortTypeImpl() throws Exception {
75 initialize();
76 }
77
78
79
80
81
82
83 @SuppressWarnings("unchecked")
84 public static Future submitToThreadPool(Runnable runnable) {
85 return pool.submit(runnable);
86
87 }
88
89
90
91
92
93
94 private static void initialize() throws Exception {
95 if (initialized) {
96 log.error("already initialized.");
97 return;
98 }
99
100 log.info("initialize capture applications");
101
102 Properties props = new Properties();
103 try {
104 props.load(
105 CaptureAppPortTypeImpl.class.getResourceAsStream(
106 CONFIG_FILE));
107
108 final int n = Integer.parseInt(props.getProperty("n"));
109
110 for (int i = 0; i<n; i++) {
111
112 final int port = Integer.parseInt(
113 props.getProperty("cap." + i + ".port", "-1"));
114
115 final String name = props.getProperty(
116 "cap." + i + ".name", "cap." + i + ".name");
117
118 final String epcis = props.getProperty(
119 "cap." + i + ".epcis", "tcp://localhost:1234");
120
121 final String changeSet = props.getProperty(
122 "cap." + i + ".changeset", null);
123
124 String handlerClzzName = props.getProperty(
125 "cap." + i + ".handler", null);
126
127 log.info(String.format("create new capture app: (%s,%d,%s)",
128 name, port, epcis));
129 captureApps.put(name, new CaptureAppWorker(
130 name,
131 new org.fosstrak.capturingapp.CaptureApp(port,
132 epcis)));
133
134 if (null == handlerClzzName) {
135 handlerClzzName = DEFAULT_HANDLER_CLASS_NAME;
136 }
137 log.info("Using handler class: " + handlerClzzName);
138
139 try {
140 Class cls = Class.forName(handlerClzzName);
141 Object obj = null;
142 if (null == changeSet) {
143 obj = cls.newInstance();
144 } else {
145 log.debug(String.format("using changeSet: %s", changeSet));
146 Constructor ctor = cls.getConstructor(String.class);
147 obj = ctor.newInstance(changeSet);
148 }
149
150 if (obj instanceof ECReportsHandler) {
151 captureApps.get(name).getCaptureApp().
152 registerHandler((ECReportsHandler)obj);
153
154 } else {
155 throw new Exception("Invalid type: " + obj.getClass());
156 }
157 } catch (Exception e) {
158 log.error("Could not create handler: " + e.getMessage());
159 }
160 }
161
162
163 for (CaptureAppWorker worker : captureApps.values()) {
164 log.info(String.format("starting capture app: (%s,%d,%s)",
165 worker.getIdentifier(),
166 worker.getCaptureApp().getPort(),
167 worker.getCaptureApp().getEpcisRepositoryURL()));
168 worker.start();
169 }
170
171 } catch (IOException e) {
172 log.error("could not load config file - aborting.");
173 e.printStackTrace();
174 initialized = false;
175 throw e;
176 }
177
178 initialized = true;
179 }
180
181
182
183
184
185
186 public org.fosstrak.capturingapp.wsdl.ArrayOfString getCaptureAppNames(EmptyParms parms) {
187 ArrayOfString aos = new ArrayOfString();
188 for (CaptureAppWorker worker : captureApps.values()) {
189 aos.getString().add(worker.getIdentifier());
190 }
191 return aos;
192 }
193
194
195
196 protected void finalize() throws Throwable {
197 log.info("finalizer called.");
198 for (CaptureAppWorker worker : captureApps.values()) {
199 worker.stop();
200 }
201 super.finalize();
202 }
203
204 }