KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > servers > ServerInfo


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.servers;
5
6 import org.apache.commons.lang.StringUtils;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Properties JavaDoc;
16 import java.util.prefs.BackingStoreException JavaDoc;
17 import java.util.prefs.Preferences JavaDoc;
18 import java.util.regex.Matcher JavaDoc;
19 import java.util.regex.Pattern JavaDoc;
20
21 public class ServerInfo {
22   private String JavaDoc m_name;
23   private String JavaDoc m_label;
24   private String JavaDoc m_displayName;
25   private String JavaDoc m_startupTrigger;
26   private String JavaDoc m_applicationPath;
27   private List JavaDoc m_env;
28
29   public ServerInfo(Properties JavaDoc props, Properties JavaDoc env) {
30     this(props.getProperty("name"),
31          props.getProperty("label"),
32          props.getProperty("display.name"),
33          props.getProperty("startup.trigger"),
34          props.getProperty("application.path"));
35     setProperties(env);
36   }
37   
38   public ServerInfo(String JavaDoc name,
39                     String JavaDoc label,
40                     String JavaDoc displayName,
41                     String JavaDoc startupTrigger,
42                     String JavaDoc applicationPath)
43   {
44     m_name = name;
45     m_label = label;
46     m_displayName = displayName;
47     m_startupTrigger = startupTrigger;
48     m_applicationPath = applicationPath;
49     m_env = new ArrayList JavaDoc();
50   }
51
52   public ServerInfo(ServerInfo info) {
53     m_name = info.getName();
54     m_label = info.getLabel();
55     m_displayName = info.getDisplayName();
56     m_startupTrigger = info.getStartupTrigger();
57     m_applicationPath = info.getApplicationPath();
58     m_env = info.cloneProperties();
59   }
60   
61   public String JavaDoc getName() {
62     return m_name;
63   }
64   
65   public String JavaDoc getLabel() {
66     return m_label;
67   }
68   
69   public String JavaDoc getDisplayName() {
70     return m_displayName;
71   }
72   
73   public String JavaDoc getStartupTrigger() {
74     return m_startupTrigger;
75   }
76   
77   public String JavaDoc getApplicationPath() {
78     return m_applicationPath;
79   }
80   
81   public List JavaDoc getProperties() {
82     return m_env;
83   }
84   
85   public void setProperties(Properties JavaDoc env) {
86     Enumeration JavaDoc props = env.propertyNames();
87     String JavaDoc key;
88     String JavaDoc value;
89     
90     while(props.hasMoreElements()) {
91       key = (String JavaDoc)props.nextElement();
92       value = env.getProperty(key);
93       
94       addProperty(key, value);
95     }
96   }
97   
98   public Properties JavaDoc toProperties() {
99     Properties JavaDoc props = new Properties JavaDoc();
100     int propCount = propertyCount();
101     ServerProperty property;
102     
103     for(int i = 0; i < propCount; i++) {
104       property = (ServerProperty)m_env.get(i);
105       props.put(property.getName(), property.getValue());
106     }
107     
108     return props;
109   }
110   
111   public String JavaDoc[] toEnvironment() {
112     ArrayList JavaDoc list = new ArrayList JavaDoc();
113     Iterator JavaDoc iter = m_env.iterator();
114     ServerProperty prop;
115     
116     while(iter.hasNext()) {
117       prop = (ServerProperty)iter.next();
118       list.add(prop.getName()+"="+prop.getValue());
119     }
120     
121     return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
122   }
123   
124   public void storeEnvironment(Preferences JavaDoc prefs) {
125     int propCount = propertyCount();
126     ServerProperty property;
127     String JavaDoc value;
128     
129     for(int i = 0; i < propCount; i++) {
130       property = (ServerProperty)m_env.get(i);
131       if((value = property.getValue()) != null) {
132         prefs.put(property.getName(), value);
133       }
134     }
135   }
136   
137   public void loadEnvironment(Preferences JavaDoc prefs) {
138     String JavaDoc[] keys;
139     
140     try {
141       keys = prefs.keys();
142     } catch(BackingStoreException JavaDoc bse) {
143       return;
144     }
145     
146     String JavaDoc key;
147     ServerProperty prop;
148      
149     for(int i = 0; i < keys.length; i++) {
150       key = keys[i];
151       prop = getProperty(key);
152       
153       if(prop != null) {
154         addProperty(key, prefs.get(key, prop.getValue()));
155       }
156     }
157   }
158   
159   private String JavaDoc getenv(String JavaDoc key) {
160     try {
161       Method JavaDoc m = System JavaDoc.class.getMethod("getenv", new Class JavaDoc[] {String JavaDoc.class});
162     
163       if(m != null) {
164         return (String JavaDoc)m.invoke(null, new Object JavaDoc[]{key});
165       }
166     } catch(Throwable JavaDoc t) {/**/}
167
168     return null;
169   }
170   
171   public void addProperty(String JavaDoc name, String JavaDoc value) {
172     ServerProperty prop = getProperty(name);
173     
174     if(value == null || value.length() == 0) {
175       value = getenv(name);
176     }
177     else {
178       Pattern JavaDoc pattern = Pattern.compile("\\$\\{(.*)\\}(.*)");
179       String JavaDoc[] comps = value.split(",");
180       
181       for(int i = 0; i < comps.length; i++) {
182         value = comps[i];
183         
184         if(value.indexOf('$') != -1) {
185           Matcher JavaDoc matcher = pattern.matcher(value);
186           
187           if(matcher.matches()) {
188             String JavaDoc var = matcher.group(1);
189             
190             if((value = getenv(var)) == null) {
191               value = System.getProperty(var);
192             }
193             
194             if(value != null) {
195               if((value = value+matcher.group(2)) != null) {
196                 String JavaDoc fileSep = System.getProperty("file.separator");
197                 File JavaDoc file = new File JavaDoc(value = StringUtils.replace(value, "/", fileSep));
198
199                 if(file.exists()) {
200                   try {
201                     value = file.getCanonicalPath();
202                   } catch(IOException JavaDoc ioe) {
203                     value = file.getAbsolutePath();
204                   }
205                   break;
206                 }
207               }
208             }
209           }
210         }
211         else {
212           break;
213         }
214       }
215     }
216     
217     if(value == null) {
218       value = "";
219     }
220     
221     if(prop != null) {
222       prop.setValue(value);
223     }
224     else {
225       m_env.add(prop = new ServerProperty(name, value));
226     }
227   }
228
229   protected List JavaDoc cloneProperties() {
230     ArrayList JavaDoc list = new ArrayList JavaDoc();
231     int count = m_env.size();
232     ServerProperty prop;
233     
234     for(int i = 0; i < count; i++) {
235       prop = (ServerProperty)m_env.get(i);
236       list.add(new ServerProperty(prop.getName(), prop.getValue()));
237     }
238     
239     return list;
240   }
241
242   public ServerProperty getProperty(String JavaDoc name) {
243     int count = m_env.size();
244     ServerProperty prop;
245     
246     for(int i = 0; i < count; i++) {
247       prop = (ServerProperty)m_env.get(i);
248       if(prop.getName().equals(name)) {
249         return prop;
250       }
251     }
252     
253     return null;
254   }
255   
256   public void setProperty(String JavaDoc name, String JavaDoc value) {
257     ServerProperty prop = getProperty(name);
258     
259     if(prop != null) {
260       prop.setValue(value);
261     }
262   }
263   
264   public int propertyCount() {
265     return m_env.size();
266   }
267   
268   public String JavaDoc toString() {
269     return getDisplayName();
270   }
271   
272   public String JavaDoc[] validateProperties() {
273     ArrayList JavaDoc list = new ArrayList JavaDoc();
274     int count = m_env.size();
275     ServerProperty prop;
276     String JavaDoc value;
277     
278     for(int i = 0; i < count; i++) {
279       prop = (ServerProperty)m_env.get(i);
280       value = prop.getValue();
281       
282       if(value == null || value.trim().length() == 0) {
283         list.add("Value for '"+prop.getName()+"' cannot be empty.");
284       }
285       else {
286         File JavaDoc file = new File JavaDoc(value);
287
288         if(!file.exists()) {
289           list.add(prop.getValue()+" does not exist.");
290         }
291       }
292     }
293
294     return list.size() > 0 ? (String JavaDoc[])list.toArray(new String JavaDoc[0]) : null;
295   }
296 }
297
Popular Tags