1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.gridsystems.config.modules.jvm;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.PrintWriter;
32 import java.text.MessageFormat;
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.MissingResourceException;
37 import java.util.Properties;
38 import java.util.ResourceBundle;
39
40 import com.gridsystems.config.ConfiguratorModel;
41
42
43
44
45
46
47
48 public class JVMConfigModel implements ConfiguratorModel {
49
50
51
52
53 public static final String DEFAULT_HEAP_MAX = "256";
54
55
56
57
58 public static final String DEFAULT_HEAP_MIN = "128";
59
60
61
62
63 public static final String DEFAULT_STACK_SIZE = "";
64
65
66
67
68 public static final String DEFAULT_USE_PROXY = "false";
69
70
71
72
73 public static final String DEFAULT_PROXY_HOST = "";
74
75
76
77
78 public static final String DEFAULT_PROXY_PORT = "";
79
80
81
82
83 public static final String DEFAULT_DEBUG_MODE = "false";
84
85
86
87
88 public static final String DEFAULT_DEBUG_PORT = "8000";
89
90
91
92
93 public static final String DEFAULT_DEBUG_SUSPEND = "true";
94
95
96
97
98 public static final String DEFAULT_CONFIG_PATH = "../conf/jvm.properties";
99
100
101
102
103 private Properties values;
104
105
106
107
108 private Properties props;
109
110
111
112
113
114 private HashSet<String> extraFlags;
115
116
117
118
119 private HashSet<String> flags;
120
121
122
123
124 private final String configFilePath;
125
126
127
128
129
130 public JVMConfigModel(String configFilePath) {
131 this.configFilePath = configFilePath;
132 values = new Properties();
133 props = new Properties();
134 extraFlags = new HashSet<String>();
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public void setProperty(String key, String value) {
159 this.props.setProperty(key, value);
160 }
161
162
163
164
165
166
167
168 public void setValue(String key, Object value) {
169 this.values.setProperty(key, value.toString());
170 }
171
172
173
174
175
176
177
178
179 public String getValue(String key, String def) {
180 return this.values.getProperty(key, def);
181 }
182
183
184
185
186
187
188
189
190 public String getProperty(String key, String def) {
191 return this.props.getProperty(key, def);
192 }
193
194
195
196
197
198
199 public void addFlags(Collection<String> flags) {
200 this.extraFlags.addAll(flags);
201 }
202
203
204
205
206
207
208 public String getMinHeap() {
209 return this.getValue("heap.min", DEFAULT_HEAP_MIN);
210 }
211
212
213
214
215
216
217 public String getMaxHeap() {
218 return this.getValue("heap.max", DEFAULT_HEAP_MAX);
219 }
220
221
222
223
224
225
226 public String getStackSize() {
227 return this.getValue("stack.size", DEFAULT_STACK_SIZE);
228 }
229
230
231
232
233
234
235 public String getUseProxy() {
236 return this.getValue("proxy.use", DEFAULT_USE_PROXY);
237 }
238
239
240
241
242
243
244 public String getProxyHost() {
245 return this.getValue("proxy.host", DEFAULT_PROXY_HOST);
246 }
247
248
249
250
251
252
253 public String getProxyPort() {
254 return this.getValue("proxy.port", DEFAULT_PROXY_PORT);
255 }
256
257
258
259
260
261
262 public String getDebugEnabled() {
263 return this.getValue("debug.enable", DEFAULT_DEBUG_MODE);
264 }
265
266
267
268
269
270
271 public String getDebugPort() {
272 return this.getValue("debug.port", DEFAULT_DEBUG_PORT);
273 }
274
275
276
277
278
279
280 public String getDebugSuspend() {
281 return this.getValue("debug.suspend", DEFAULT_DEBUG_SUSPEND);
282 }
283
284
285
286
287
288
289
290
291 public void copyModel(JVMConfigModel src) {
292 this.values = (Properties)src.values.clone();
293 this.props = (Properties)src.props.clone();
294 }
295
296
297
298
299 public void load() {
300 File f = new File(this.configFilePath);
301 if (f.exists()) {
302 FileInputStream fis = null;
303 try {
304 fis = new FileInputStream(f);
305 Properties p = new Properties();
306 p.load(fis);
307
308 this.props = extractProperties(p, "system.");
309 this.extraFlags = extractSet(p, "flags.");
310 this.values = p;
311 } catch (Exception e) {
312 e.printStackTrace();
313 } finally {
314 try { fis.close(); } catch (Exception ignore) { }
315 }
316 }
317 }
318
319
320
321
322 public void store() throws IOException {
323 File f = new File(this.configFilePath);
324 f.getParentFile().mkdir();
325 Properties p = new Properties();
326 embed(p, "", values);
327 embed(p, "system.", props);
328 embed(p, "flags.", extraFlags);
329
330 FileOutputStream fos = null;
331 try {
332 fos = new FileOutputStream(f);
333 p.store(fos, "JVM Properties");
334 } catch (Exception e) {
335 e.printStackTrace();
336 } finally {
337 try { fos.close(); } catch (Exception ignore) { }
338 }
339 }
340
341
342
343
344 public void apply() throws IOException {
345 buildFlags();
346 writeBat();
347 writeSh();
348 }
349
350
351
352
353
354
355 private void writeBat() throws IOException {
356 File f = new File("setenv.bat");
357 PrintWriter out = null;
358 try {
359 out = new PrintWriter(new FileWriter(f));
360 out.println("@echo off");
361 out.println("set JAVA_OPTS=" + getJavaOpts());
362 } finally {
363 if (out != null) {
364 out.close();
365 }
366 }
367 }
368
369
370
371
372
373
374 private void writeSh() throws IOException {
375 File f = new File("setenv.sh");
376 PrintWriter out = null;
377 try {
378 out = new PrintWriter(new FileWriter(f));
379 out.println("#!/bin/sh");
380 out.println("JAVA_OPTS=\"" + getJavaOpts() + "\"");
381 out.println("export JAVA_OPTS");
382 } finally {
383 if (out != null) {
384 out.close();
385 }
386 }
387 }
388
389
390
391
392
393
394 private String getJavaOpts() {
395 StringBuffer sb = new StringBuffer();
396 boolean first = true;
397 for (Iterator it = flags.iterator(); it.hasNext();) {
398 String flag = (String)it.next();
399 if (first) {
400 first = false;
401 } else {
402 sb.append(' ');
403 }
404 sb.append(flag);
405 }
406 return sb.toString();
407 }
408
409
410
411
412 private void buildFlags() {
413 flags = new HashSet<String>();
414 flags.addAll(extraFlags);
415
416 for (Iterator it = props.keySet().iterator(); it.hasNext();) {
417 String key = (String)it.next();
418 String value = props.getProperty(key);
419
420 if (value.indexOf(" ") >= 0) {
421 flags.add("-D" + key + "=\"" + value + "\"");
422 } else {
423 flags.add("-D" + key + "=" + value);
424 }
425 }
426
427 String value = formatValue("heap.min", "-Xms{0}M");
428 if (value != null) {
429 flags.add(value);
430 }
431
432 value = formatValue("heap.max", "-Xmx{0}M");
433 if (value != null) {
434 flags.add(value);
435 }
436
437 value = formatValue("stack.size", "-Xss{0}M");
438 if (value != null) {
439 flags.add(value);
440 }
441
442 value = values.getProperty("debug.enable");
443 if ("true".equals(value)) {
444 flags.add("-Xdebug");
445 String port = values.getProperty("debug.port");
446 boolean suspend = "true".equals(values.getProperty("debug.suspend"));
447 flags.add("-Xrunjdwp:transport=dt_socket,address=" + port
448 + ",server=y,suspend=" + (suspend ? "y" : "n"));
449 flags.add("-Djpda.port=" + port);
450 }
451
452 value = values.getProperty("proxy.use");
453 if ("true".equals(value)) {
454 flags.add("-DproxySet=true");
455 flags.add("-DproxyHost=" + values.getProperty("proxy.host"));
456 flags.add("-DproxyPort=" + values.getProperty("proxy.port"));
457 }
458 }
459
460
461
462
463
464
465
466
467 private String formatValue(String key, String format) {
468 String value = values.getProperty(key);
469 if (value == null) {
470 return null;
471 }
472 value = value.trim();
473 if (value.equals("")) {
474 return null;
475 }
476
477 return MessageFormat.format(format, new Object[] { value });
478 }
479
480
481
482
483
484
485
486
487 private void embed(Properties dest, String prefix, Properties src) {
488 for (Iterator it = src.keySet().iterator(); it.hasNext();) {
489 String key = (String)it.next();
490 String value = src.getProperty(key);
491 dest.setProperty(prefix + key, value);
492 }
493 }
494
495
496
497
498
499
500
501
502 private void embed(Properties dest, String prefix, HashSet src) {
503 int index = 0;
504 for (Iterator it = src.iterator(); it.hasNext();) {
505 String item = (String)it.next();
506 dest.setProperty(prefix + "item" + index, item);
507 index += 1;
508 }
509 }
510
511
512
513
514
515
516
517
518
519 private Properties extractProperties(Properties src, String prefix) {
520 Properties dest = new Properties();
521
522 for (Iterator it = src.keySet().iterator(); it.hasNext();) {
523 String key = (String)it.next();
524 if (key.startsWith(prefix)) {
525 String value = src.getProperty(key);
526 String destKey = key.substring(prefix.length());
527 dest.setProperty(destKey, value);
528 it.remove();
529 }
530 }
531
532 return dest;
533 }
534
535
536
537
538
539
540
541
542
543 private HashSet<String> extractSet(Properties src, String prefix) {
544 HashSet<String> dest = new HashSet<String>();
545
546 for (Iterator it = src.keySet().iterator(); it.hasNext();) {
547 String key = (String)it.next();
548 if (key.startsWith(prefix)) {
549 String value = src.getProperty(key);
550 dest.add(value);
551 it.remove();
552 }
553 }
554
555 return dest;
556 }
557
558
559
560
561
562
563 public void validate() throws IOException {
564 IOException ioe = null;
565
566 int minValue;
567 try {
568 minValue = getMemoryValue(getMinHeap());
569 } catch (Exception e) {
570 Object[] params = new Object[] {getMinHeap()};
571 ioe = new IOException(getString("errors.restoreHeadMin", params));
572 minValue = Integer.parseInt(DEFAULT_HEAP_MIN);
573 setValue("heap.min", DEFAULT_HEAP_MIN);
574 }
575 int maxValue;
576 try {
577 maxValue = getMemoryValue(getMaxHeap());
578 } catch (Exception e) {
579 Object[] params = new Object[] {getMinHeap()};
580 ioe = new IOException(getString("errors.restoreHeadMax", params));
581 maxValue = Integer.parseInt(DEFAULT_HEAP_MAX);
582 setValue("heap.max", DEFAULT_HEAP_MAX);
583 }
584
585 if (minValue > maxValue) {
586 if (ioe == null) {
587 ioe = new IOException(getString("errors.minGreaterMax"));
588 }
589 }
590 if (ioe != null) {
591 throw ioe;
592 }
593 }
594
595
596
597
598
599
600
601
602 public String getString(String key) {
603 ResourceBundle bundle = JVMConfigurator.getInstance().getBundle();
604 if (bundle == null) {
605 throw new NullPointerException("Bundle not set");
606 }
607
608 try {
609 return bundle.getString(key);
610 } catch (MissingResourceException e) {
611 return null;
612 }
613 }
614
615
616
617
618
619
620
621
622
623
624 public String getString(String key, Object[] params) {
625 ResourceBundle bundle = JVMConfigurator.getInstance().getBundle();
626 if (bundle == null) {
627 throw new NullPointerException("Bundle not set");
628 }
629
630 try {
631 String pattern = bundle.getString(key);
632 return MessageFormat.format(pattern, params);
633 } catch (MissingResourceException e) {
634 return key + "#not found";
635 }
636 }
637
638
639
640
641
642
643
644
645 private int getMemoryValue(String memory) {
646 if (memory == null || memory.equals("")) {
647
648 return 0;
649 }
650
651 return Integer.parseInt(memory);
652 }
653 }