1
2
3
4 package org.sirius.server;
5
6 import java.io.BufferedReader;
7 import java.io.File;
8 import java.io.FileReader;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.io.OutputStream;
13 import java.net.MalformedURLException;
14 import java.net.URL;
15 import java.net.URLClassLoader;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18
19 import javax.xml.ws.Endpoint;
20
21 import org.apache.log4j.ConsoleAppender;
22 import org.apache.log4j.Logger;
23 import org.apache.log4j.SimpleLayout;
24
25
26
27
28 public class Starter {
29
30
31
32
33 private static final int PARAMS_COUNT = 3;
34
35
36
37
38 private static final String HOST_KEY = "-host";
39
40
41
42
43 private static final String PORT_KEY = "-port";
44
45
46
47
48 private static final String CONFIG_KEY = "-config";
49
50
51
52
53 private static final String DEFAULT_HOST = "localhost";
54
55
56
57
58 private static final String DEFAULT_PORT = "21212";
59
60
61
62
63 private static final String DEFAULT_CONFIG = ".\\modules.csv";
64
65
66
67
68 private static ArrayList<Endpoint> endpoints = new ArrayList<Endpoint>();
69
70
71
72
73 public static ArrayList<Endpoint> getEndpoints() {
74 return endpoints;
75 }
76
77
78
79
80 private static Logger logger = Logger.getLogger(Starter.class);
81
82 static {
83 logger.addAppender(new ConsoleAppender(new SimpleLayout()));
84 }
85
86
87
88
89
90
91
92 public final ArrayList<PackageOptions> readConfig(final String config)
93 throws IOException {
94
95 logger.info("Start reading configuration file: " + config);
96
97 FileReader reader = new FileReader(config);
98 BufferedReader br = new BufferedReader(reader);
99
100 String line = br.readLine();
101
102 logger.debug("Line read: " + line);
103
104 ArrayList<PackageOptions> options =
105 new ArrayList<PackageOptions>();
106
107 while ((line = br.readLine()) != null) {
108 logger.debug("Line read: " + line);
109
110 if (line.startsWith("#")) {
111 logger.debug("Commented line detected. " +
112 "Skipping...");
113 continue;
114 }
115 String[] row = line.split(",");
116 if (row.length < PARAMS_COUNT) {
117 logger.debug("Malformed line detected. " +
118 "It should contain at least 3 fields. " +
119 "Skipping...");
120 continue;
121 }
122 PackageOptions option =
123 new PackageOptions(row[0].trim(),
124 row[1].trim(), row[2].trim());
125 options.add(option);
126 }
127
128 logger.info("Complete reading configuration file: " + config);
129
130 br.close();
131 reader.close();
132 return options;
133 }
134
135
136
137
138
139
140 private String findMatchingFile(final String filter) {
141 File location = new File(filter);
142
143 for (String file : location.getParentFile().list()) {
144 if (file.matches(location.getName())) {
145 return file;
146 }
147 }
148
149 return "";
150 }
151
152
153
154
155
156
157
158
159
160 public final void startEndPoints(
161 final ArrayList<PackageOptions> options,
162 final String host,
163 final String port) throws IOException {
164 for (PackageOptions option : options) {
165 ClassLoader loader;
166
167 if (!option.get_packageLocation().equals("Local")) {
168 logger.info("Uploading binary file:"
169 + option.get_packageLocation());
170
171 String packageFile = findMatchingFile(option
172 .get_packageLocation());
173 File location = new File(packageFile);
174
175 URL[] url = {
176 location.getAbsoluteFile().toURI().toURL()
177 };
178 loader = new URLClassLoader(url, this.getClass()
179 .getClassLoader());
180 } else {
181 loader = this.getClass().getClassLoader();
182 }
183 try {
184 String endPoint = option.get_endPoint();
185 endPoint =
186 endPoint.replaceAll(
187 "\\$\\{HOST}",
188 host
189 );
190 endPoint =
191 endPoint.replaceAll(
192 "\\$\\{PORT}",
193 port
194 );
195 logger.info("Starting endpoint: " + endPoint);
196 Endpoint endpoint = Endpoint.publish(endPoint,
197 Class.forName(
198 option.get_className(),
199 true,
200 loader
201 )
202 .newInstance());
203 endpoints.add(endpoint);
204 } catch (Exception e) {
205 logger.error(
206 "Failed publishing server endpoint", e
207 );
208 } finally {
209 logger.info("Done...");
210 }
211 }
212
213 }
214
215
216
217
218
219
220
221
222 public static void main(final String[] args) throws IOException,
223 InstantiationException, IllegalAccessException,
224 ClassNotFoundException {
225 String host = DEFAULT_HOST;
226 String port = DEFAULT_PORT;
227 String config = DEFAULT_CONFIG;
228
229 logger.info("Parsing command line arguments");
230
231 HashMap<String, String> params = new HashMap<String, String>();
232
233 for (int i = 0; i < (2 * (args.length / 2)); i += 2) {
234 if (i < args.length - 1) {
235 params.put(args[i], args[i + 1]);
236 }
237 }
238
239 if (params.containsKey(HOST_KEY)) {
240 host = params.get(HOST_KEY);
241 }
242 if (params.containsKey(PORT_KEY)) {
243 port = params.get(PORT_KEY);
244 }
245 if (params.containsKey(CONFIG_KEY)) {
246 config = params.get(CONFIG_KEY);
247 }
248
249 logger.info("The following parameters were specified:");
250 logger.info("Endpoint host name: " + host);
251 logger.info("Endpoint port number: " + port);
252 logger.info("Configuration file: " + config);
253
254 Starter starter = new Starter();
255
256 logger.info("Reading configuration file...");
257 ArrayList<PackageOptions> options = starter.readConfig(config);
258
259 logger.info("Starting interanl endpoint...");
260 try {
261 endpoints.add(Endpoint.publish("http://" + host + ":" + port
262 + "/internal", new Internal()));
263 } catch (Exception e) {
264 logger.error("Failed to start interanl endpoint", e);
265 }
266 logger.info("Done...");
267
268 logger.info("Starting endpoints...");
269 starter.startEndPoints(options, host, port);
270
271 }
272
273 }