1 4 package com.tc.servers; 5 6 import org.apache.commons.lang.StringUtils; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.lang.reflect.Method ; 11 import java.util.ArrayList ; 12 import java.util.Enumeration ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Properties ; 16 import java.util.prefs.BackingStoreException ; 17 import java.util.prefs.Preferences ; 18 import java.util.regex.Matcher ; 19 import java.util.regex.Pattern ; 20 21 public class ServerInfo { 22 private String m_name; 23 private String m_label; 24 private String m_displayName; 25 private String m_startupTrigger; 26 private String m_applicationPath; 27 private List m_env; 28 29 public ServerInfo(Properties props, Properties 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 name, 39 String label, 40 String displayName, 41 String startupTrigger, 42 String 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 (); 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 getName() { 62 return m_name; 63 } 64 65 public String getLabel() { 66 return m_label; 67 } 68 69 public String getDisplayName() { 70 return m_displayName; 71 } 72 73 public String getStartupTrigger() { 74 return m_startupTrigger; 75 } 76 77 public String getApplicationPath() { 78 return m_applicationPath; 79 } 80 81 public List getProperties() { 82 return m_env; 83 } 84 85 public void setProperties(Properties env) { 86 Enumeration props = env.propertyNames(); 87 String key; 88 String value; 89 90 while(props.hasMoreElements()) { 91 key = (String )props.nextElement(); 92 value = env.getProperty(key); 93 94 addProperty(key, value); 95 } 96 } 97 98 public Properties toProperties() { 99 Properties props = new Properties (); 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 [] toEnvironment() { 112 ArrayList list = new ArrayList (); 113 Iterator 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 [])list.toArray(new String [list.size()]); 122 } 123 124 public void storeEnvironment(Preferences prefs) { 125 int propCount = propertyCount(); 126 ServerProperty property; 127 String 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 prefs) { 138 String [] keys; 139 140 try { 141 keys = prefs.keys(); 142 } catch(BackingStoreException bse) { 143 return; 144 } 145 146 String 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 getenv(String key) { 160 try { 161 Method m = System .class.getMethod("getenv", new Class [] {String .class}); 162 163 if(m != null) { 164 return (String )m.invoke(null, new Object []{key}); 165 } 166 } catch(Throwable t) {} 167 168 return null; 169 } 170 171 public void addProperty(String name, String value) { 172 ServerProperty prop = getProperty(name); 173 174 if(value == null || value.length() == 0) { 175 value = getenv(name); 176 } 177 else { 178 Pattern pattern = Pattern.compile("\\$\\{(.*)\\}(.*)"); 179 String [] 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 matcher = pattern.matcher(value); 186 187 if(matcher.matches()) { 188 String 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 fileSep = System.getProperty("file.separator"); 197 File file = new File (value = StringUtils.replace(value, "/", fileSep)); 198 199 if(file.exists()) { 200 try { 201 value = file.getCanonicalPath(); 202 } catch(IOException 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 cloneProperties() { 230 ArrayList list = new ArrayList (); 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 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 name, String 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 toString() { 269 return getDisplayName(); 270 } 271 272 public String [] validateProperties() { 273 ArrayList list = new ArrayList (); 274 int count = m_env.size(); 275 ServerProperty prop; 276 String 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 file = new File (value); 287 288 if(!file.exists()) { 289 list.add(prop.getValue()+" does not exist."); 290 } 291 } 292 } 293 294 return list.size() > 0 ? (String [])list.toArray(new String [0]) : null; 295 } 296 } 297 | Popular Tags |