001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.servers;
005:
006: import org.apache.commons.lang.StringUtils;
007:
008: import java.io.File;
009: import java.io.IOException;
010: import java.lang.reflect.Method;
011: import java.util.ArrayList;
012: import java.util.Enumeration;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Properties;
016: import java.util.prefs.BackingStoreException;
017: import java.util.prefs.Preferences;
018: import java.util.regex.Matcher;
019: import java.util.regex.Pattern;
020:
021: public class ServerInfo {
022: private String m_name;
023: private String m_label;
024: private String m_displayName;
025: private String m_startupTrigger;
026: private String m_applicationPath;
027: private List m_env;
028:
029: public ServerInfo(Properties props, Properties env) {
030: this (props.getProperty("name"), props.getProperty("label"),
031: props.getProperty("display.name"), props
032: .getProperty("startup.trigger"), props
033: .getProperty("application.path"));
034: setProperties(env);
035: }
036:
037: public ServerInfo(String name, String label, String displayName,
038: String startupTrigger, String applicationPath) {
039: m_name = name;
040: m_label = label;
041: m_displayName = displayName;
042: m_startupTrigger = startupTrigger;
043: m_applicationPath = applicationPath;
044: m_env = new ArrayList();
045: }
046:
047: public ServerInfo(ServerInfo info) {
048: m_name = info.getName();
049: m_label = info.getLabel();
050: m_displayName = info.getDisplayName();
051: m_startupTrigger = info.getStartupTrigger();
052: m_applicationPath = info.getApplicationPath();
053: m_env = info.cloneProperties();
054: }
055:
056: public String getName() {
057: return m_name;
058: }
059:
060: public String getLabel() {
061: return m_label;
062: }
063:
064: public String getDisplayName() {
065: return m_displayName;
066: }
067:
068: public String getStartupTrigger() {
069: return m_startupTrigger;
070: }
071:
072: public String getApplicationPath() {
073: return m_applicationPath;
074: }
075:
076: public List getProperties() {
077: return m_env;
078: }
079:
080: public void setProperties(Properties env) {
081: Enumeration props = env.propertyNames();
082: String key;
083: String value;
084:
085: while (props.hasMoreElements()) {
086: key = (String) props.nextElement();
087: value = env.getProperty(key);
088:
089: addProperty(key, value);
090: }
091: }
092:
093: public Properties toProperties() {
094: Properties props = new Properties();
095: int propCount = propertyCount();
096: ServerProperty property;
097:
098: for (int i = 0; i < propCount; i++) {
099: property = (ServerProperty) m_env.get(i);
100: props.put(property.getName(), property.getValue());
101: }
102:
103: return props;
104: }
105:
106: public String[] toEnvironment() {
107: ArrayList list = new ArrayList();
108: Iterator iter = m_env.iterator();
109: ServerProperty prop;
110:
111: while (iter.hasNext()) {
112: prop = (ServerProperty) iter.next();
113: list.add(prop.getName() + "=" + prop.getValue());
114: }
115:
116: return (String[]) list.toArray(new String[list.size()]);
117: }
118:
119: public void storeEnvironment(Preferences prefs) {
120: int propCount = propertyCount();
121: ServerProperty property;
122: String value;
123:
124: for (int i = 0; i < propCount; i++) {
125: property = (ServerProperty) m_env.get(i);
126: if ((value = property.getValue()) != null) {
127: prefs.put(property.getName(), value);
128: }
129: }
130: }
131:
132: public void loadEnvironment(Preferences prefs) {
133: String[] keys;
134:
135: try {
136: keys = prefs.keys();
137: } catch (BackingStoreException bse) {
138: return;
139: }
140:
141: String key;
142: ServerProperty prop;
143:
144: for (int i = 0; i < keys.length; i++) {
145: key = keys[i];
146: prop = getProperty(key);
147:
148: if (prop != null) {
149: addProperty(key, prefs.get(key, prop.getValue()));
150: }
151: }
152: }
153:
154: private String getenv(String key) {
155: try {
156: Method m = System.class.getMethod("getenv",
157: new Class[] { String.class });
158:
159: if (m != null) {
160: return (String) m.invoke(null, new Object[] { key });
161: }
162: } catch (Throwable t) {/**/
163: }
164:
165: return null;
166: }
167:
168: public void addProperty(String name, String value) {
169: ServerProperty prop = getProperty(name);
170:
171: if (value == null || value.length() == 0) {
172: value = getenv(name);
173: } else {
174: Pattern pattern = Pattern.compile("\\$\\{(.*)\\}(.*)");
175: String[] comps = value.split(",");
176:
177: for (int i = 0; i < comps.length; i++) {
178: value = comps[i];
179:
180: if (value.indexOf('$') != -1) {
181: Matcher matcher = pattern.matcher(value);
182:
183: if (matcher.matches()) {
184: String var = matcher.group(1);
185:
186: if ((value = getenv(var)) == null) {
187: value = System.getProperty(var);
188: }
189:
190: if (value != null) {
191: if ((value = value + matcher.group(2)) != null) {
192: String fileSep = System
193: .getProperty("file.separator");
194: File file = new File(
195: value = StringUtils.replace(
196: value, "/", fileSep));
197:
198: if (file.exists()) {
199: try {
200: value = file.getCanonicalPath();
201: } catch (IOException ioe) {
202: value = file.getAbsolutePath();
203: }
204: break;
205: }
206: }
207: }
208: }
209: } else {
210: break;
211: }
212: }
213: }
214:
215: if (value == null) {
216: value = "";
217: }
218:
219: if (prop != null) {
220: prop.setValue(value);
221: } else {
222: m_env.add(prop = new ServerProperty(name, value));
223: }
224: }
225:
226: protected List cloneProperties() {
227: ArrayList list = new ArrayList();
228: int count = m_env.size();
229: ServerProperty prop;
230:
231: for (int i = 0; i < count; i++) {
232: prop = (ServerProperty) m_env.get(i);
233: list
234: .add(new ServerProperty(prop.getName(), prop
235: .getValue()));
236: }
237:
238: return list;
239: }
240:
241: public ServerProperty getProperty(String name) {
242: int count = m_env.size();
243: ServerProperty prop;
244:
245: for (int i = 0; i < count; i++) {
246: prop = (ServerProperty) m_env.get(i);
247: if (prop.getName().equals(name)) {
248: return prop;
249: }
250: }
251:
252: return null;
253: }
254:
255: public void setProperty(String name, String value) {
256: ServerProperty prop = getProperty(name);
257:
258: if (prop != null) {
259: prop.setValue(value);
260: }
261: }
262:
263: public int propertyCount() {
264: return m_env.size();
265: }
266:
267: public String toString() {
268: return getDisplayName();
269: }
270:
271: public String[] validateProperties() {
272: ArrayList list = new ArrayList();
273: int count = m_env.size();
274: ServerProperty prop;
275: String value;
276:
277: for (int i = 0; i < count; i++) {
278: prop = (ServerProperty) m_env.get(i);
279: value = prop.getValue();
280:
281: if (value == null || value.trim().length() == 0) {
282: list.add("Value for '" + prop.getName()
283: + "' cannot be empty.");
284: } else {
285: File file = new File(value);
286:
287: if (!file.exists()) {
288: list.add(prop.getValue() + " does not exist.");
289: }
290: }
291: }
292:
293: return list.size() > 0 ? (String[]) list.toArray(new String[0])
294: : null;
295: }
296: }
|