1 18 19 package sync4j.syncclient.spap; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.PrintWriter ; 23 import java.sql.Timestamp ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 27 28 29 import sync4j.syncclient.common.HashtableTools; 30 import sync4j.syncclient.spdm.DMException; 31 import sync4j.syncclient.spdm.DeviceManager; 32 import sync4j.syncclient.spdm.ManagementNode; 33 import sync4j.syncclient.spdm.SimpleDeviceManager; 34 35 36 58 public class AssetDAO { 59 60 62 63 private static final String CONFIG_ASSET_NODE_NAME = "asset"; 64 65 66 private static final String CONFIG_CURRENT_VERSION_NODE_NAME = "currentVersion"; 67 68 69 private static final String CONFIG_NEW_VERSION_NODE_NAME = "newVersion"; 70 71 72 private static final String CONFIG_MANAGEMENT_NODE_NAME = "spap/applications"; 73 74 75 76 77 79 80 private ManagementNode managementNode = null; 81 82 83 84 88 public AssetDAO() { 89 DeviceManager dm = SimpleDeviceManager.getDeviceManager(); 90 managementNode = dm.getManagementTree(CONFIG_MANAGEMENT_NODE_NAME); 91 } 92 93 101 public void setAsset(Asset asset, Timestamp time) throws AssetManagementException { 102 103 String manufacturer = asset.getManufacturer().toLowerCase(); 104 String name = asset.getName().toLowerCase(); 105 106 String state = asset.getState(); 107 String id = asset.getId(); 108 String description = null2empty(asset.getDescription()); 109 String lastUpdate = "" + time.getTime(); 110 111 AssetVersion currentVersion = asset.getCurrentVersion(); 112 AssetVersion newVersion = asset.getNewVersion(); 113 114 115 String assetNodePath = manufacturer + "/" + name + "/" + CONFIG_ASSET_NODE_NAME; 116 try { 117 118 managementNode.setValue(assetNodePath, Asset.PROPERTY_ID ,id); 120 managementNode.setValue(assetNodePath, Asset.PROPERTY_STATE ,state); 121 managementNode.setValue(assetNodePath, Asset.PROPERTY_NAME ,asset.getName()); 122 managementNode.setValue(assetNodePath, Asset.PROPERTY_DESCRIPTION , description); 123 managementNode.setValue(assetNodePath, Asset.PROPERTY_MANUFACTURER , asset.getManufacturer()); 124 managementNode.setValue(assetNodePath, Asset.PROPERTY_LAST_UPDATE, lastUpdate); 125 126 if (currentVersion != null) { 128 setAssetVersion(manufacturer, name, currentVersion, false); 129 } else { 130 managementNode.removeNode(manufacturer + "/" + name + "/" + CONFIG_CURRENT_VERSION_NODE_NAME); 131 } 132 133 if (newVersion != null) { 135 setAssetVersion(manufacturer, name, newVersion, true); 136 } else { 137 managementNode.removeNode(manufacturer + "/" + name + "/" + CONFIG_NEW_VERSION_NODE_NAME); 138 } 139 140 } 141 catch (DMException ex) { 142 throw new AssetManagementException(asset, "Error setting asset", ex); 143 } 144 145 } 146 147 154 public Asset getAsset(String idAsset) throws AssetManagementException { 155 Asset asset = null; 156 157 ManagementNode[] manufacturesNode = null; 158 159 try { 160 manufacturesNode = managementNode.getChildren(); 161 } 162 catch (DMException ex) { 163 throw new AssetManagementException("Asset with id: '" + 164 idAsset + "' not found", ex); 165 } 166 167 int numManufacturer = manufacturesNode.length; 168 169 ManagementNode manufacturerNode = null; 170 ManagementNode[] assetsNode = null; 171 ManagementNode assetNode = null; 172 173 boolean assetFound = false; 174 Hashtable currentVersion = null; 175 Hashtable newVersion = null; 176 177 int numAsset = 0; 178 for (int i=0; i<numManufacturer; i++) { 179 manufacturerNode = manufacturesNode[i]; 180 try { 181 assetsNode = manufacturerNode.getChildren(); 182 } 183 catch (DMException ex) { 184 continue; 185 } 186 187 numAsset = assetsNode.length; 188 for (int j=0; j<numAsset; j++) { 189 assetNode = assetsNode[j]; 190 191 String id = null; 192 193 try { 194 195 id = (String )assetNode.getNodeValue( 196 CONFIG_ASSET_NODE_NAME, Asset.PROPERTY_ID 197 ); 198 199 if (idAsset.equalsIgnoreCase(id)) { 200 201 asset = new Asset( 203 HashtableTools.hashtable2hashMap( 204 assetNode.getNodeValues(CONFIG_ASSET_NODE_NAME) 205 ) 206 ); 207 208 assetFound = true; 209 210 try { 212 currentVersion = assetNode.getNodeValues(CONFIG_CURRENT_VERSION_NODE_NAME); 213 asset.setCurrentVersion(HashtableTools.hashtable2hashMap(currentVersion)); 214 } 215 catch (DMException ex) { 216 } 218 219 try { 221 newVersion = assetNode.getNodeValues(CONFIG_NEW_VERSION_NODE_NAME); 222 asset.setNewVersion(HashtableTools.hashtable2hashMap(newVersion)); 223 } 224 catch (DMException ex) { 225 } 227 228 break; 230 231 } 232 233 } 234 catch (DMException ex) { 235 continue; 237 } 238 239 } 240 241 if (assetFound) { 242 break; 244 } 245 } 246 247 if (!assetFound) { 248 throw new AssetManagementException("Asset with id: '" + 249 idAsset + "' not found"); 250 } 251 252 return asset; 253 } 254 255 256 264 public Asset setAssetState(String idAsset, String state) 265 throws AssetManagementException { 266 Asset asset = getAsset(idAsset); 267 return setAssetState(asset,state); 268 } 269 270 278 public Asset setAssetState(Asset asset, String state) throws AssetManagementException { 279 String manufacturer = asset.getManufacturer().toLowerCase(); 280 String assetName = asset.getName().toLowerCase(); 281 282 ManagementNode assetNode = null; 283 284 java.util.Date time = new java.util.Date (); 285 286 try { 287 assetNode = managementNode.getChildNode(manufacturer + "/" 288 + assetName); 289 290 assetNode.setValue(CONFIG_ASSET_NODE_NAME, Asset.PROPERTY_STATE , state); 291 } 292 catch (DMException e) { 293 e.printStackTrace(); 294 return null; 295 } 296 297 asset.setState(state); 298 299 return asset; 300 } 301 302 307 public Vector getAllAsset() { 308 return listAsset(null); 309 } 310 311 318 public String getAssetState(String idAsset) throws AssetManagementException { 319 320 Asset asset = getAsset(idAsset); 321 322 return asset.getState(); 323 } 324 325 326 327 335 public Vector listAsset(String state) { 336 Vector assets = new Vector (); 337 338 ManagementNode[] manufacturesNode = null; 339 340 try { 341 manufacturesNode = managementNode.getChildren(); 342 } 343 catch (DMException ex) { 344 return assets; 346 } 347 348 int numManufacturer = manufacturesNode.length; 349 350 ManagementNode manufacturerNode = null; 351 ManagementNode[] assetsNode = null; 352 ManagementNode assetNode = null; 353 String assetName = null; 354 Hashtable currentVersion = null; 355 Hashtable newVersion = null; 356 AssetVersion currentVersionAsset = null; 357 AssetVersion newVersionAsset = null; 358 Asset asset = null; 359 String idAsset = null; 360 boolean foundNewVersion = false; 361 boolean foundCurrentVersion = false; 362 String assetState = null; 363 boolean assetFound = false; 364 365 int numAsset = 0; 366 367 369 for (int i=0; i<numManufacturer; i++) { 370 manufacturerNode = manufacturesNode[i]; 371 try { 372 assetsNode = manufacturerNode.getChildren(); 373 } 374 catch (DMException ex) { 375 continue; 376 } 377 numAsset = assetsNode.length; 378 379 380 382 for (int j=0; j<numAsset; j++) { 383 assetFound = false; 384 asset = null; 385 386 assetNode = assetsNode[j]; 387 388 try { 389 asset = new Asset( 390 HashtableTools.hashtable2hashMap( 391 assetNode.getNodeValues(CONFIG_ASSET_NODE_NAME) 392 ) 393 ); 394 currentVersion = null; 395 newVersion = null; 396 } 397 catch (DMException ex) { 398 } 400 401 if (asset == null) { 403 continue; 404 } 405 406 assetState = asset.getState(); 407 408 if (state != null) { 409 if (state.equals(assetState)) { 410 assetFound = true; 411 } 412 } else { 413 assetFound = true; 415 } 416 417 418 if (assetFound) { 419 try { 420 currentVersion = assetNode.getNodeValues( 421 CONFIG_CURRENT_VERSION_NODE_NAME 422 ); 423 } 424 catch (DMException ex) { 425 } 427 428 if (currentVersion != null) { 429 asset.setCurrentVersion( 430 new AssetVersion( 431 HashtableTools.hashtable2hashMap(currentVersion) 432 ) 433 ); 434 } 435 436 try { 437 newVersion = assetNode.getNodeValues(CONFIG_NEW_VERSION_NODE_NAME); 438 } 439 catch (DMException ex) { 440 } 442 443 if (newVersion != null) { 444 asset.setNewVersion( 445 new AssetVersion(HashtableTools.hashtable2hashMap(newVersion)) 446 ); 447 } 448 449 assets.addElement(asset); 450 } 451 } 452 } 453 return assets; 454 } 455 456 457 463 public void removeAsset(Asset asset) throws AssetManagementException { 464 String manufacturer = asset.getManufacturer().toLowerCase(); 465 String name = asset.getName().toLowerCase(); 466 467 try { 468 managementNode.removeNode(manufacturer + "/" + name); 469 470 ManagementNode manufacturerNode = 473 managementNode.getChildNode(manufacturer); 474 475 if (manufacturerNode.getChildren().length == 0) { 476 managementNode.removeNode(manufacturer); 477 } 478 } 479 catch (DMException ex) { 480 throw new AssetManagementException( 481 asset, "Error removing asset information from DM", ex 482 ); 483 } 484 } 485 486 494 public Asset setAssetAsNotValid(Asset asset, Throwable cause) 495 throws AssetManagementException { 496 497 String manufacturer = asset.getManufacturer().toLowerCase(); 498 String assetName = asset.getName().toLowerCase(); 499 500 ManagementNode assetNode = null; 501 502 String causeMessage = stackTraceToString(cause); 503 504 try { 505 assetNode = managementNode.getChildNode(manufacturer + "/" + assetName); 506 } 507 catch (DMException ex) { 508 throw new AssetManagementException( 509 asset, "Error during the setting state as NOT VALID", ex 510 ); 511 } 512 513 try { 514 assetNode.setValue(CONFIG_ASSET_NODE_NAME, Asset.PROPERTY_STATE , 515 Asset.STATE_NOT_VALID); 516 517 assetNode.setValue(CONFIG_ASSET_NODE_NAME, "CAUSE" , causeMessage); 518 } 519 catch (DMException ex) { 520 throw new AssetManagementException( 521 asset, "Error during the setting state as NOT VALID", ex 522 ); 523 } 524 525 asset.setState(Asset.STATE_NOT_VALID); 526 527 return asset; 528 } 529 530 531 538 private String null2empty(Object temp) { 539 if (temp==null) { 540 return ""; 541 } else { 542 return temp.toString(); 543 } 544 } 545 546 547 561 protected void setAssetVersion(String manufacturer, String name, 562 AssetVersion assetVersion, boolean isNewVersion) 563 throws DMException { 564 565 String nodePath = null; 566 567 if (isNewVersion) { 568 nodePath = manufacturer + "/" + name + "/" + 569 CONFIG_NEW_VERSION_NODE_NAME; 570 } else { 571 nodePath = manufacturer + "/" + name + "/" + 572 CONFIG_CURRENT_VERSION_NODE_NAME; 573 } 574 575 managementNode.setValue(nodePath, AssetVersion.PROPERTY_VERSION, assetVersion.getVersion()); 576 managementNode.setValue(nodePath, AssetVersion.PROPERTY_RELEASE_DATE , assetVersion.getReleaseDate()); 577 managementNode.setValue(nodePath, AssetVersion.PROPERTY_RELEASE_NOTES , null2empty(assetVersion.getReleaseNotes())); 578 managementNode.setValue(nodePath, AssetVersion.PROPERTY_URL, assetVersion.getUrl().toString()); 579 managementNode.setValue(nodePath, AssetVersion.PROPERTY_SIZE_ASSET_FILE, ""+assetVersion.getSizeContentFile()); 580 managementNode.setValue(nodePath, AssetVersion.PROPERTY_INSTALL_PROGRAM , null2empty(assetVersion.getInstallProgram())); 581 managementNode.setValue(nodePath, AssetVersion.PROPERTY_UNINSTALL_PROGRAM , null2empty(assetVersion.getUninstallProgram())); 582 managementNode.setValue(nodePath, AssetVersion.PROPERTY_NEED_UNINSTALL_PREV , null2empty(assetVersion.getNeedUninstallPrev())); 583 584 } 585 586 592 private static String stackTraceToString(Throwable e){ 593 String stackTraceString = ""; 594 ByteArrayOutputStream sBuf = new ByteArrayOutputStream ( 1024 ); 595 PrintWriter s = new PrintWriter ( sBuf ); 596 e.printStackTrace( s ); 597 s.flush(); 598 stackTraceString = sBuf.toString(); 599 s.close(); 600 return stackTraceString; 601 } 602 603 } | Popular Tags |