1 18 19 20 21 package sync4j.syncclient.spdm; 22 23 import java.util.Hashtable ; 24 import java.util.Properties ; 25 import java.util.Enumeration ; 26 import java.util.StringTokenizer ; 27 28 import java.io.*; 29 import java.lang.reflect.Method ; 30 31 import sync4j.syncclient.common.logging.Logger; 32 33 34 42 43 public class NodeImpl implements ManagementNode { 44 private static final String PROPERTY_CLASS_NAME = "className"; 46 47 49 private String context = null; 50 private String fullContext = null; 51 private ManagementNode parent = null; 52 53 Hashtable propertiesValues = null; 57 58 long lastModified = 0; 62 63 65 73 public NodeImpl (ManagementNode parent, String context) { 74 this.parent = parent; 75 this.context = context; 76 77 if (parent != null) { 78 String fc = parent.getFullContext(); 79 this.fullContext = fc 80 + ((fc.endsWith("/")) ? "" : "/") 81 + context; 82 } else { 83 this.fullContext = context; 84 } 85 } 86 87 89 94 public String getContext() { 95 return this.context; 96 } 97 98 104 public String getFullContext() { 105 return this.fullContext; 106 } 107 108 109 114 public Object getValue(String name) throws DMException { 115 Hashtable values = this.getValues(); 116 return values.get((Object ) name); 117 } 118 119 124 public Hashtable getValues() 125 throws DMException{ 126 try { 127 128 File f = (parent != null) 129 ? new File(parent.getFullContext(), context + ".properties") 130 : new File(context + ".properties") 131 ; 132 133 long filePropertiesLastModified = f.lastModified(); 134 135 if (lastModified != filePropertiesLastModified) { 139 140 propertiesValues = this.loadProperties(f); 141 142 lastModified = filePropertiesLastModified; 143 144 } 145 146 return propertiesValues; 147 148 } catch (IOException e){ 149 throw new DMException("Error loading values for the context " 150 + fullContext 151 + ": " 152 + e.getMessage() 153 , e); 154 } 155 } 156 157 166 public Hashtable getNodeValues(String node) 167 throws DMException { 168 try { 169 return loadProperties(new File(fullContext, node + ".properties")); 170 } catch (IOException e) { 171 throw new DMException( "Error reading the management node values in " + 172 fullContext + 173 '/' + 174 node, e); 175 } 176 } 177 178 188 public Object getNodeValue(String node, String name) 189 throws DMException { 190 Hashtable values = getNodeValues(node); 191 192 return values.get(name); 193 } 194 195 202 public Object getManagementObject(String node) throws DMException { 203 Object objectToReturn = null; 204 205 Hashtable properties = getNodeValues(node); 206 String className = (String )properties.get(PROPERTY_CLASS_NAME); 207 208 try { 209 objectToReturn = (Class.forName(className)).newInstance(); 210 } catch (Exception e) { 211 212 String msg = "Error loading class " + 213 className + 214 ": " + 215 e.getMessage(); 216 217 throw new DMException(msg); 218 } 219 220 Enumeration e = properties.keys(); 222 223 String key = null; 224 while (e.hasMoreElements()) { 225 key = (String )e.nextElement(); 226 if (!PROPERTY_CLASS_NAME.equals(key)) { 227 setProperty(objectToReturn, key, (String )properties.get(key)); 228 } 229 } 230 231 return objectToReturn; 232 } 233 234 243 public ManagementNode[] getChildren() throws DMException{ 244 File f = new File(fullContext); 245 246 String [] children = f.list(); 247 248 if ((children == null) || (children.length==0)) { 249 return new ManagementNode[0]; 250 } 251 252 ManagementNode[] nodes = new ManagementNode[children.length]; 253 for (int i=0; i<children.length; ++i){ 254 nodes[i] = new NodeImpl(this, removeExtension(children[i])); 255 } 256 257 return nodes; 258 } 259 260 267 public ManagementNode getChildNode(String subcontext) 268 throws DMException { 269 if ((!new File(fullContext, subcontext).exists() && !new File(fullContext, subcontext+".properties").exists())) { 270 throw new DMException("The sub context " 271 + subcontext 272 + " does not exist in context " 273 + fullContext 274 ); 275 } 276 277 return new NodeImpl(this, subcontext); 278 } 279 280 287 public ManagementNode getParent(){ 288 return this.parent; 289 } 290 291 299 public void setValue(String name, Object value) throws DMException { 300 301 File file = new File(fullContext + ".properties"); 302 setValue(file, "", name, value); 303 } 304 305 314 public void setValue(String node, String name, Object value) 315 throws DMException { 316 317 File file = new File(fullContext, node + ".properties"); 318 setValue(file, node, name, value); 319 } 320 321 322 329 public void removeNode(String node) 330 throws DMException { 331 332 Properties values = null; 333 334 File context = new File(fullContext, node); 335 File file = new File(fullContext, node + ".properties"); 336 337 if (file.exists()) { 338 file.delete(); 339 } 340 341 emptyTree(context); 342 } 343 344 346 355 private void setValue(File file, String node, String name, Object value) 356 throws DMException { 357 358 Properties values = null; 359 360 if(!file.exists()) { 363 String dir = file.getParent(); 364 365 try { 366 if (dir != null) { 367 new File(dir).mkdirs(); 368 } 369 } catch (Exception e) { 370 throw new DMException( "Error creating the management node " 371 + 372 + '/' 373 + node + " " + e 374 , e); 375 } 376 values = new Properties (); 377 } else { 378 try { 379 values = loadProperties(file); 380 } catch (IOException e) { 381 throw new DMException("Error reading the management node values in " + 382 fullContext + 383 '/' + 384 node, e ); 385 } 386 } 387 388 values.put(name, value); 389 390 try { 391 saveProperties(file, values); 392 } catch (IOException e) { 393 throw new DMException("Error writing the management node values in " + 394 fullContext + 395 '/' + 396 node, e); 397 } 398 } 399 400 private Properties loadProperties(File file) 401 throws IOException { 402 FileInputStream fis = null; 403 Properties prop = new Properties (); 404 405 try { 406 fis = new FileInputStream(file); 407 prop.load(fis); 408 return prop; 409 } finally { 410 if (fis != null) try { 411 fis.close(); 412 } catch (IOException e) {} 413 } 414 415 } 416 417 private void saveProperties(File file, Properties values) 418 throws IOException { 419 FileOutputStream fos = null; 420 421 try { 422 fos = new FileOutputStream(file); 423 values.save(fos, ""); 424 } finally { 425 if (fos != null) try { 426 fos.close(); 427 } catch (IOException e) {} 428 } 429 } 430 431 private String removeExtension(String context) { 432 int p = context.lastIndexOf('.'); 433 if (p < 0) { 434 return context; 435 } 436 437 if (p == 0) { 438 return ""; 439 } 440 441 return context.substring(0, p); 442 } 443 444 445 451 private void emptyTree(File dir) { 452 String [] files = dir.list(); 453 454 if (files == null) { 455 return; 456 } 457 458 File f; 459 for (int i=0; i < files.length;i++) { 460 f = new File(dir, files[i]); 461 if (f.isDirectory()) { 462 emptyTree(f); 463 } 464 f.delete(); 465 } 466 467 dir.delete(); 468 } 469 470 private void setProperty(Object obj, String key, String value) { 471 String methodName = ""; 472 473 char firstLetter = key.toUpperCase().charAt(0); 474 if (key.length() > 1) { 475 methodName = key.substring(1); 476 } 477 478 methodName = "set" + firstLetter + methodName; 479 480 Class objClass = obj.getClass(); 481 482 try { 483 Method m = objClass.getMethod(methodName, new Class [] { String .class }); 484 m.invoke(obj, new String [] {value}); 485 } catch (Exception e) { 486 String msg = "Property " + 487 key + 488 " not set to " + 489 value + 490 ". Method " + 491 methodName + 492 "(String s) not found in " + 493 objClass.getName() ; 494 495 if (Logger.isLoggable(Logger.DEBUG)) { 496 Logger.debug(msg); 497 } 498 } 499 } 500 501 } 502 | Popular Tags |