1 /*
2 Copyright (C) 2000 - 2007 Grid Systems, S.A.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 /*
19 * Project: KernelConfigurator
20 * Created on 24-feb-2004
21 *
22 * Copyright (c)2003 Grid Systems
23 */
24 package com.gridsystems.config.modules.jvm;
25
26 import java.util.Collection;
27
28 import com.gridsystems.config.Configurator;
29 import com.gridsystems.config.ConfiguratorModel;
30 import com.gridsystems.config.ConsoleConfiguratorView;
31 import com.gridsystems.config.SwingConfiguratorView;
32
33 /**
34 * Java Virtual Machine Configurator implementation.
35 *
36 * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
37 * @version 1.0
38 */
39 public final class JVMConfigurator extends Configurator {
40 /**
41 * The bundle to use.
42 */
43 static final String BUNDLE = "com/gridsystems/config/modules/jvm/config_jvm";
44
45 /**
46 * Singleton instance.
47 */
48 private static JVMConfigurator instance = new JVMConfigurator();
49
50 /**
51 * The model.
52 */
53 private JVMConfigModel data = new JVMConfigModel(JVMConfigModel.DEFAULT_CONFIG_PATH);
54
55 /**
56 * The swing view.
57 */
58 private JVMSwingView swingView = null;
59
60 /**
61 * The console view.
62 */
63 private JVMConsoleView consoleView = null;
64
65 /**
66 * Hidden constructor.
67 */
68 private JVMConfigurator() {
69 super(BUNDLE, "config");
70
71 setModel(data);
72 }
73
74 /**
75 * Singleton pattern implementation.
76 *
77 * @return A unique instance of this class
78 */
79 public static JVMConfigurator getInstance() {
80 return instance;
81 }
82
83 /**
84 * Sets a system property value.
85 *
86 * @param name The system property name
87 * @param value The system property value
88 */
89 public void setProperty(String name, String value) {
90 data.setProperty(name, value);
91 }
92
93 /**
94 * Gets a system property value.
95 *
96 * @param name The system property name
97 * @param def The system property default value
98 * @return The system property value
99 */
100 public String getProperty(String name, String def) {
101 return data.getProperty(name, def);
102 }
103
104 /**
105 * Gets a system property value as an int.
106 *
107 * @param name The system property name
108 * @param def The system property default value
109 * @return The int value of the system property
110 */
111 public int getIntProperty(String name, int def) {
112 String s = data.getProperty(name, null);
113 try {
114 return Integer.parseInt(s);
115 } catch (Exception e) {
116 return def;
117 }
118 }
119
120 /**
121 * Gets a system property value as a boolean.
122 *
123 * @param name The system property name
124 * @param def The system property default value
125 * @return The boolean value of the system property
126 */
127 public boolean getBoolProperty(String name, boolean def) {
128 String s = data.getProperty(name, null);
129 if (s == null) {
130 return def;
131 }
132 if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes")) {
133 return true;
134 }
135 if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("no")) {
136 return false;
137 }
138 return def;
139 }
140
141 /**
142 * Adds all flags in the specified collection to the flags set.
143 *
144 * @param flags The collection of flags to add
145 */
146 public void addFlags(Collection<String> flags) {
147 data.addFlags(flags);
148 }
149
150 /**
151 * {@inheritDoc}
152 */
153 protected SwingConfiguratorView getSwingView() {
154 if (swingView == null) {
155 swingView = new JVMSwingView(this);
156 }
157 return swingView;
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 protected ConsoleConfiguratorView getConsoleView() {
164 if (consoleView == null) {
165 consoleView = new JVMConsoleView(this);
166 }
167 return consoleView;
168 }
169
170 /**
171 * {@inheritDoc}
172 */
173 protected ConfiguratorModel createModel() {
174 // This configurator does not create new instances of the model
175 return data;
176 }
177 }