1 18 package sync4j.syncclient.spap; 19 20 import java.io.*; 21 import java.sql.Timestamp ; 22 import java.util.Date ; 23 import java.util.Vector ; 24 import java.util.Hashtable ; 25 import java.util.Properties ; 26 import java.util.Enumeration ; 27 import java.util.jar.JarFile ; 28 import java.util.zip.ZipEntry ; 29 30 import sync4j.syncclient.common.StringTools; 31 import sync4j.syncclient.spdm.*; 32 33 import sync4j.syncclient.sps.common.DataStoreMetadata; 34 import sync4j.syncclient.spap.*; 35 36 74 public class ApplicationManager { 75 76 78 public static String [] ALLOWED_SYNC_MODES = new String [] { 79 "none", "slow", "two-way", "one-way", "refresh" 80 }; 81 82 84 87 private ManagementNode applicationConfigNode; 88 89 90 92 100 public ApplicationManager(ManagementNode applicationConfigNode) { 101 this.applicationConfigNode = applicationConfigNode; 102 103 } 104 105 107 119 public Application install(File f) 120 throws ApplicationManagementException { 121 String xmlADF = null; 122 String applicationURI = null; 123 String adfFileName = null; 124 125 int l = 0; 126 127 Application app = null; 128 Asset asset = null; 129 try { 130 adfFileName = getADFNameFromJar(f); 131 xmlADF = readADFFromJar (f); 132 app = applicationFromADF(xmlADF); 133 asset = makeAsset(app, f); 134 135 AssetManager assetManager = AssetManager.getAssetManager(); 136 137 assetManager.addAsset(asset); 138 139 assetManager.installAsset(asset.getId()); 140 return app; 141 } catch (FileNotFoundException e) { 142 throw new ApplicationManagementException("Package " + f + " not found!", e); 143 } catch (IOException e) { 144 throw new ApplicationManagementException("Error reading package " + f, e); 145 } catch (AssetManagementException e) { 146 throw new ApplicationManagementException("Error storing the asset " + e, e); 147 } 148 } 149 150 158 public void uninstall(Application app) 159 throws ApplicationManagementException { 160 Asset asset = null; 161 try { 162 asset = makeAsset(app, null); 163 164 AssetManager assetManager = AssetManager.getAssetManager(); 165 assetManager.setAssetState(asset, "D"); 166 assetManager.removeAsset(asset.getId()); 167 } catch (AssetManagementException e) { 168 throw new ApplicationManagementException("Error storing the asset " + e, e); 169 } 170 } 171 172 177 public Application[] getApplications() 178 throws ApplicationManagementException { 179 String applicationsSyncTmp = null; 180 String applicationKeyTmp = null; 181 182 ManagementNode[] applicationNodes = null; 183 184 ManagementNode[] sources = null; 185 186 try { 187 188 applicationNodes = applicationConfigNode.getChildren(); 189 190 DataStoreMetadata dsmd; 191 192 Application[] applications = new Application[applicationNodes.length]; 193 for (int i = 0; i < applicationNodes.length; i++) { 194 applications[i] = new Application((String ) applicationNodes[i].getNodeValue("/application", "applicationURI")); 195 applications[i].setDisplayName((String ) applicationNodes[i].getNodeValue("/application", "applicationDisplayName")); 196 applications[i].setAuthor((String ) applicationNodes[i].getNodeValue("/application", "applicationAuthor")); 197 applications[i].setDescription((String ) applicationNodes[i].getNodeValue("/application", "applicationDescription")); 198 applications[i].setVersion((String ) applicationNodes[i].getNodeValue("/application", "applicationVersion")); 199 applications[i].setAssetId((String ) applicationNodes[i].getNodeValue("/application", "assetId")); 200 applications[i].setSync( 201 Boolean.valueOf((String ) applicationNodes[i].getNodeValue("/application", "sync")).booleanValue() 202 ); 203 204 sources = applicationNodes[i].getChildNode("spds/sources").getChildren(); 205 for (int j = 0; j < sources.length; j++) { 206 dsmd = new DataStoreMetadata((String )sources[j].getValue("sourceURI")); 207 dsmd.setDisplayName((String )sources[j].getValue("name")); 208 dsmd.setDefaultSync((String )sources[j].getValue("sync")); 209 dsmd.setSyncModes(StringTools.split((String )sources[j].getValue("syncModes"))); 210 211 applications[i].addDataStoreMetadata(dsmd); 212 } 213 } 214 215 return applications; 216 } catch (Exception e) { 217 throw new ApplicationManagementException("Error loading applications parameters: " + e, e); 218 } 219 } 220 221 222 231 public static Application applicationFromADF(String adf) 232 throws ApplicationManagementException { 233 try{ 234 String header = getXMLTagValue(adf, "header"); 238 239 if (StringTools.isEmpty(header)) { 240 throw new ApplicationManagementException("Missing <header>...</header> in ADF"); 241 } 242 243 String [] mandatoryFields = new String [] { 247 "application-name", "application-creator-id", "application-datastore-type", 248 "application-display-name", "application-description", "application-support-url", 249 "application-support-email", "store-manager-package", "content-id" 250 }; 251 252 String value; 253 for (int i=0; i<mandatoryFields.length; ++i) { 254 value = getXMLTagValue(header, mandatoryFields[i]); 255 256 if (StringTools.isEmpty(value)) { 257 throw new ApplicationManagementException( 258 "Missing <" + mandatoryFields[i] + ">...</" + mandatoryFields[i] + "> in ADF" 259 ); 260 } 261 } 262 263 Application app = new Application(getXMLTagValue(header, "application-name")); 267 268 app.setDisplayName (getXMLTagValue(header, "application-display-name" )); 269 app.setCreatorId (getXMLTagValue(header, "application-creator-id" )); 270 app.setDataStoreType (getXMLTagValue(header, "application-datastore-type")); 271 app.setContentId (getXMLTagValue(header, "content-id" )); 272 app.setDescription (getXMLTagValue(header, "application-description" )); 273 app.setSupportUrl (getXMLTagValue(header, "application-support-url" )); 274 app.setSupportEmail (getXMLTagValue(header, "application-support-email" )); 275 app.setStoreManagerPkg (getXMLTagValue(header, "store-manager-package" )); 276 app.setAuthor (getXMLTagValue(header, "application-author" )); 277 app.setVersion (getXMLTagValue(header, "application-version" )); 278 279 Vector datastores; 283 Vector xmlADFVector = new Vector (); 284 285 DataStoreMetadata md; 286 287 xmlADFVector.addElement(adf); 288 289 datastores = getXMLTag(xmlADFVector, "datastore"); 290 291 String [] syncModes; 292 String defaultSync; 293 int l = datastores.size(); 294 for (int i=0; i < l; ++i) { 295 md = new DataStoreMetadata(getXMLTagValue((String ) datastores.elementAt(i) , "name")); 296 297 md.setDisplayName(getXMLTagValue((String ) datastores.elementAt(i), "display-name")); 298 syncModes = getSyncModes(getXMLTagValue((String ) datastores.elementAt(i), "sync-modes"), ALLOWED_SYNC_MODES); 299 md.setSyncModes(syncModes); 300 defaultSync = getXMLTagValue((String ) datastores.elementAt(i), "default-sync"); 301 checkSyncMode(defaultSync, syncModes); 302 md.setDefaultSync(defaultSync); 303 304 try { 309 value = getXMLTagValue((String ) datastores.elementAt(i), "soft-sort"); 310 } catch (Exception e) { 311 value = "true"; 312 } 313 md.setSoftSort(new Boolean (value).booleanValue()); 314 315 try { 320 value = getXMLTagValue((String ) datastores.elementAt(i), "store-volume"); 321 } catch (Exception e) { 322 value = ""; 323 } 324 md.setStoreVolume(value); 325 326 app.addDataStoreMetadata(md); 327 } 328 329 return app; 330 331 } catch (Throwable t) { 332 throw new ApplicationManagementException("Unexpected error: " + t, t); 333 } 334 } 335 336 345 public static Application applicationFromAsset(Asset asset) 346 throws ApplicationManagementException { 347 Application a = new Application(asset.getName()); 348 349 a.setUri (asset.getName() ); 350 a.setAuthor (asset.getManufacturer() ); 351 a.setAssetId (asset.getId() ); 352 a.setVersion (asset.getNewVersion().getVersion()); 353 a.setDescription (asset.getDescription() ); 354 a.setDisplayName (asset.getName() ); 355 a.setDataStoresMetadata( new Vector () ); 356 357 return a; 358 } 359 360 368 public static Asset makeAsset(Application app, File packageFile) 369 throws ApplicationManagementException { 370 try{ 371 Properties p = new Properties (); 372 373 if (StringTools.isEmpty(app.getAssetId())) { 379 p.put(Asset.PROPERTY_ID, String.valueOf(System.currentTimeMillis())); 380 } else { 381 p.put(Asset.PROPERTY_ID, app.getAssetId()); 382 } 383 p.put(Asset.PROPERTY_NAME, app.getUri()); 384 p.put(Asset.PROPERTY_MANUFACTURER, app.getAuthor()); 385 p.put(Asset.PROPERTY_DESCRIPTION, app.getDescription()); 386 p.put(Asset.PROPERTY_STATE, "U"); 387 388 p.put( 389 AssetVersion.PROPERTY_VERSION, 390 app.getVersion() 391 ); 392 p.put( 393 AssetVersion.PROPERTY_RELEASE_DATE, 394 new Date (System.currentTimeMillis()).toString() 395 ); 396 p.put(AssetVersion.PROPERTY_RELEASE_NOTES, ""); 397 if (packageFile != null) { 398 p.put( 399 AssetVersion.PROPERTY_URL, 400 packageFile.toURL().toString() 401 ); 402 p.put( 403 AssetVersion.PROPERTY_SIZE_ASSET_FILE, 404 String.valueOf(packageFile.length()) 405 ); 406 } 407 p.put( 408 AssetVersion.PROPERTY_INSTALL_PROGRAM, 409 "sync4j.syncclient.spap.installer.SPSInstaller.class" 410 ); 411 p.put( 412 AssetVersion.PROPERTY_UNINSTALL_PROGRAM, 413 "sync4j.syncclient.spap.installer.SPSInstaller.class" 414 ); 415 p.put( 416 AssetVersion.PROPERTY_NEED_UNINSTALL_PREV, 417 "true" 418 ); 419 420 return new Asset(p, true); 421 } catch (Throwable t) { 422 throw new ApplicationManagementException("Unexpected error: " + t, t); 423 } 424 } 425 426 427 429 436 private static Vector getXMLTag(Vector xmlInput, String tag) 437 throws ApplicationManagementException { 438 439 Vector xmlReturn = new Vector (); 440 441 String xmlInputTag = null; 442 443 String startTag = null; 444 String endTag = null; 445 446 int i = 0; 447 448 startTag = "<" + tag + ">"; 449 endTag = "</" + tag + ">"; 450 451 for (int j=0; j < xmlInput.size(); j++) { 452 453 xmlInputTag = (String ) xmlInput.elementAt(j); 454 455 try { 456 457 while (xmlInputTag.indexOf(startTag) != -1) { 458 xmlReturn.addElement(xmlInputTag.substring(xmlInputTag.indexOf(startTag) + startTag.length(), xmlInputTag.indexOf(endTag))); 459 xmlInputTag = xmlInputTag.substring(xmlInputTag.indexOf(endTag) + endTag.length()); 460 i++; 461 } 462 463 } catch (StringIndexOutOfBoundsException e) { 464 throw new ApplicationManagementException( 465 "Error getting the value of <" + tag +">" 466 ); 467 } 468 469 } 470 471 return xmlReturn; 472 473 } 474 475 482 private static String getXMLTagValue(String xml, String tag) 483 throws ApplicationManagementException { 484 485 String startTag = null; 486 String endTag = null; 487 String value = null; 488 489 startTag = "<" + tag + ">"; 490 endTag = "</" + tag + ">"; 491 492 try { 493 value = xml.substring(xml.indexOf(startTag) + startTag.length(), xml.indexOf(endTag)); 494 } catch (StringIndexOutOfBoundsException e) { 495 throw new ApplicationManagementException( 496 "Error getting the value of <" + tag +">" 497 ); 498 } 499 500 value = value.trim(); 501 502 while (value.indexOf("\n") != -1) { 503 value = value.substring(0, value.indexOf("\n")) + value.substring(value.indexOf("\n") + 1); 504 } 505 506 return value.trim(); 507 508 } 509 510 520 private String getADFNameFromJar(File f) 521 throws ApplicationManagementException { 522 try { 523 JarFile jarFile = null; 524 525 Enumeration zipEntries = null; 526 527 ZipEntry zipEntry = null; 528 529 boolean findADF = false; 530 531 jarFile = new JarFile (f); 532 533 zipEntries = (new JarFile (f)).entries(); 534 535 String adfFileName; 536 while (zipEntries.hasMoreElements()) { 537 zipEntry = (ZipEntry ) zipEntries.nextElement(); 538 539 adfFileName = zipEntry.getName(); 540 541 if(adfFileName.endsWith(".adf")) { 542 return adfFileName; 543 } 544 } 545 } catch (IOException e) { 546 throw new ApplicationManagementException( 547 "Error reading the jar file " + f + ": " + e, e 548 ); 549 } 550 551 throw new ApplicationManagementException( 552 "No Application Definition File (.adf) found in " + f 553 ); 554 } 555 556 562 private String readADFFromJar(File f) 563 throws ApplicationManagementException, IOException { 564 565 JarFile jarFile = null; 566 567 Enumeration zipEntries = null; 568 569 ZipEntry zipEntry = null; 570 571 boolean findADF = false; 572 573 jarFile = new JarFile (f); 574 575 zipEntries = (new JarFile (f)).entries(); 576 577 String adfFileName; 578 while (zipEntries.hasMoreElements()) { 579 580 zipEntry = (ZipEntry ) zipEntries.nextElement(); 581 582 adfFileName = zipEntry.getName(); 583 584 if(adfFileName.endsWith(".adf")) { 585 findADF = true; 586 break; 587 } 588 589 } 590 591 if (!findADF) { 592 throw new ApplicationManagementException ( 593 "No Application Description File (.adf) found in package " + f 594 ); 595 } 596 597 return read (jarFile.getInputStream(zipEntry)); 598 599 } 600 601 606 private String read(InputStream is) throws IOException { 607 StringBuffer sb = new StringBuffer (); 608 609 try { 610 byte[] buf = new byte[1024]; 611 612 int nbyte = -1; 613 while ((nbyte = is.read(buf)) >= 0) { 614 sb.append(new String (buf, 0, nbyte)); 615 } 616 } finally { 617 is.close(); 618 } 619 620 return sb.toString(); 621 } 622 623 637 private static String [] getSyncModes(String s, String [] allowed) 638 throws IllegalArgumentException { 639 String [] modes = StringTools.split(s); 640 641 for (int i=0; i<modes.length; ++i) { 642 checkSyncMode(modes[i], allowed); 643 } 644 645 return modes; 646 } 647 648 656 private static void checkSyncMode(String mode, String [] allowed) { 657 for(int i=0; ((allowed != null) && (i<allowed.length)); ++i) { 658 if (allowed[i].equals(mode)) { 659 return; 660 } 661 } 662 663 throw new IllegalArgumentException ( 664 "Invalid sync mode '" + 665 mode + 666 "'; it must be one of (" + 667 StringTools.join(allowed) + 668 ")" 669 ); 670 } 671 672 } | Popular Tags |