1 18 package sync4j.syncclient.spap.installer; 19 20 import java.io.*; 21 22 import java.util.Vector ; 23 import java.util.Hashtable ; 24 25 import sync4j.syncclient.common.FileSystemTools; 26 import sync4j.syncclient.common.StringTools; 27 28 import sync4j.syncclient.sps.common.DataStoreMetadata; 29 import sync4j.syncclient.spap.Asset; 30 import sync4j.syncclient.spap.Application; 31 import sync4j.syncclient.spap.ApplicationManager; 32 import sync4j.syncclient.spap.ApplicationManagementException; 33 import sync4j.syncclient.spap.AssetManagementException; 34 import sync4j.syncclient.spap.AssetInstallationException; 35 import sync4j.syncclient.spap.installer.InstallationContext; 36 import sync4j.syncclient.spdm.ManagementNode; 37 import sync4j.syncclient.spdm.SimpleDeviceManager; 38 import sync4j.syncclient.spdm.DMException; 39 40 41 48 public class SPSInstaller { 49 50 private static final String PALMINSTALL = "palminstall.exe"; 51 private static final String PPCINSTALL = "ppcinstall.exe"; 52 private static final String CONTEXT_APPLICATIONS = "conduit/applications"; 53 54 public static int install(InstallationContext ctx) 55 throws AssetManagementException { 56 try { 57 ManagementNode appsNode = 58 SimpleDeviceManager.getDeviceManager().getManagementTree(CONTEXT_APPLICATIONS); 59 60 String adf = null; 61 Application app = null; 62 63 try { 64 adf = readADF(ctx.getWorkingDirectory()); 65 } catch (ApplicationManagementException e) { 66 app = ApplicationManager.applicationFromAsset(ctx.getAsset()); 70 } 71 72 if (app == null) { 73 app = ApplicationManager.applicationFromADF(adf); 74 app.setAssetId(ctx.getAsset().getId()); 75 saveStoreManagerClass( 76 ctx.getWorkingDirectory(), 77 ctx.getLibDirectory() 78 ); 79 } 80 81 registerApplication(appsNode, app); 82 83 installBinaries( ctx.getWorkingDirectory(), 84 ctx.getBinDirectory() , 85 PALMINSTALL , 86 ".pdb" ); 87 installBinaries( ctx.getWorkingDirectory(), 88 ctx.getBinDirectory() , 89 PALMINSTALL , 90 ".prc" ); 91 installBinaries( ctx.getWorkingDirectory(), 92 ctx.getBinDirectory() , 93 PPCINSTALL , 94 ".cab" ); 95 } catch (AssetInstallationException e) { 96 throw e; 97 } catch (AssetManagementException e) { 98 throw e; 99 } catch (Exception e) { 100 throw new AssetManagementException(e.getMessage(), e); 101 } 102 103 return 0; 104 } 105 106 public static int uninstall(InstallationContext ctx) 107 throws AssetManagementException { 108 ManagementNode appsNode = 109 SimpleDeviceManager.getDeviceManager().getManagementTree(CONTEXT_APPLICATIONS); 110 111 ManagementNode sourceNode = null; 112 ManagementNode[] sources = null; 113 114 Hashtable sourceValues = null; 115 116 String storeManagerPackage = null; 117 String storeManagerPath = null; 118 String storeManager = null; 119 String dirTmp = null; 120 121 String adf = null; 122 Application app = null; 123 try { 124 try { 125 adf = readADF(ctx.getWorkingDirectory()); 126 } catch (ApplicationManagementException e) { 127 app = ApplicationManager.applicationFromAsset(ctx.getAsset()); 131 } 132 133 if (app == null) { 134 app = ApplicationManager.applicationFromADF(adf); 135 136 deleteStoreManagerClasses(ctx.getWorkingDirectory(), ctx.getLibDirectory()); 137 } 138 139 appsNode.removeNode(app.getFixedURI()); 140 } catch (Exception e) { 141 throw new AssetManagementException("Error reading configuration information: " + e, e); 142 } 143 return 0; 144 } 145 146 155 private static void registerApplication(ManagementNode appsNode, Application app) 156 throws DMException { 157 String value; 158 159 String uri = app.getFixedURI() + "/application"; 163 appsNode.setValue( 164 uri, "applicationDisplayName", app.getDisplayName() 165 ); 166 appsNode.setValue( 167 uri, "applicationURI", app.getUri() 168 ); 169 value = app.getSupportUrl(); 170 171 if (value != null) { 172 appsNode.setValue(uri, "applicationSupportUrl", value); 173 } 174 value = app.getSupportEmail(); 175 if (value != null) { 176 appsNode.setValue(uri, "applicationSupportMail", value); 177 } 178 179 appsNode.setValue( 180 uri, "applicationAuthor", app.getAuthor() 181 ); 182 appsNode.setValue( 183 uri, "applicationDescription", app.getDescription() 184 ); 185 appsNode.setValue( 186 uri, "applicationVersion", app.getVersion() 187 ); 188 appsNode.setValue( 189 uri, "assetId", app.getAssetId() 190 ); 191 appsNode.setValue( 192 uri, "sync", "true" 193 ); 194 195 String name = new File(app.getUri()).getName(); 196 197 Vector stores = app.getDataStoresMetadata(); 201 DataStoreMetadata md; 202 int l = (stores != null) ? stores.size() : 0; 203 204 appsNode.setValue(app.getFixedURI() + "/spds/sources", "", ""); 206 207 String node = app.getFixedURI() + "/spds/sources/source"; 208 for (int i=0; i<l; ++i) { 209 md = (DataStoreMetadata)stores.elementAt(i); 210 appsNode.setValue( 211 node + String.valueOf(i + 1), "sourceURI", app.getContentId() + '/' + md.getName() 212 ); 213 appsNode.setValue( 214 node + String.valueOf(i + 1), "creatorId", app.getCreatorId() 215 ); 216 appsNode.setValue( 217 node + String.valueOf(i + 1), "dataStoreType", app.getDataStoreType() 218 ); 219 appsNode.setValue( 220 node + String.valueOf(i + 1), "syncModes", StringTools.join(md.getSyncModes()) 221 ); 222 appsNode.setValue( 223 node + String.valueOf(i + 1), "sync", md.getDefaultSync() 224 ); 225 appsNode.setValue( 226 node + String.valueOf(i + 1), "name", md.getDisplayName() 227 ); 228 appsNode.setValue( 229 node + String.valueOf(i + 1), "sourceClass", "sync4j.syncclient.spds.source.SPSSyncSource" 230 ); 231 appsNode.setValue( 232 node + String.valueOf(i + 1), "type", "xml/record" 233 ); 234 appsNode.setValue( 235 node + String.valueOf(i + 1), "storeManager", StringTools.replaceSpecial(name) 236 ); 237 appsNode.setValue( 238 node + String.valueOf(i + 1), "storeManagerPackage", app.getStoreManagerPkg() 239 ); 240 appsNode.setValue( 241 node + String.valueOf(i + 1), "sortDB", String.valueOf(md.isSoftSort()) 242 ); 243 if (md.getStoreVolume() != null) { 244 appsNode.setValue( 245 node + String.valueOf(i + 1), "storeVolume", md.getStoreVolume() 246 ); 247 } 248 } 249 } 250 251 259 private static void saveStoreManagerClass( 260 String workingDirectory, String destinationDirectory 261 ) throws IOException { 262 FileOutputStream out = null; 263 FileInputStream in = null; 264 265 int l = workingDirectory.length(); 266 267 try{ 268 String [] classes = FileSystemTools.getAllFiles(workingDirectory, ".class"); 269 270 File source, dest; 271 byte[] buf; 272 for (int i=0; i<classes.length; ++i) { 273 source = new File(classes[i]); 274 dest = new File(destinationDirectory, classes[i].substring(l)); 275 276 dest.getParentFile().mkdirs(); 277 278 279 buf = new byte[(int)source.length()]; 280 281 in = new FileInputStream(source); 282 in.read(buf); 283 in.close(); in = null; 284 285 out = new FileOutputStream(dest); 286 out.write(buf); 287 out.close(); out = null; 288 } 289 } finally { 290 if (in != null) { 291 in.close(); 292 } 293 if (out != null) { 294 out.close(); 295 } 296 } 297 } 298 299 306 private static void deleteStoreManagerClasses(String workingDirectory, 307 String destinationDirectory) 308 throws IOException { 309 int l = workingDirectory.length(); 310 311 String [] classes = FileSystemTools.getAllFiles(workingDirectory, ".class"); 312 313 File f, p; 314 String [] children; 315 for (int i=0; i<classes.length; ++i) { 316 f = new File(destinationDirectory, classes[i].substring(l)); 317 p = new File(f.getParent()); 318 319 f.delete(); 320 321 do { 325 children = p.list(); 326 327 if ((children != null) && (children.length == 0)) { 328 p.delete(); 329 p = new File(p.getParent()); 330 } 331 } while ((children != null) && (children.length == 0)); 332 } 333 } 334 335 342 private static String readADF(String workingDirectory) 343 throws ApplicationManagementException { 344 try { 345 String [] files = new File(workingDirectory).list(); 346 347 for (int i=0; i<files.length; ++i) { 348 if (files[i].toLowerCase().endsWith(".adf")) { 349 return new String ( 350 FileSystemTools.getFile(workingDirectory + File.separator + files[i]) 351 ); 352 } 353 } 354 } catch (IOException e) { 355 throw new ApplicationManagementException( 356 "Error reading the adf file in " + new File(workingDirectory).getAbsolutePath() + ": " + e, e 357 ); 358 } 359 360 throw new ApplicationManagementException( 361 "No Application Definition File (.adf) found in " + new File(workingDirectory).getAbsolutePath() 362 ); 363 } 364 365 375 private static void installBinaries(String workingDirectory, 376 String binDirectory , 377 String installProgram , 378 String fileType ) 379 throws AssetManagementException { 380 StringBuffer output = new StringBuffer (); 381 int c; 382 383 if (!fileType.startsWith(".")) { 384 fileType = "." + fileType; 385 } 386 387 try { 388 String cmd = binDirectory + File.separator + installProgram + " "; 389 390 String [] files = new File(workingDirectory).list(); 391 if (files == null) { 392 return; 393 } 394 395 for (int i=0; i<files.length; ++i) { 401 402 if (!(files[i].toLowerCase().endsWith(fileType.toLowerCase()))) { 403 continue; 404 } 405 406 if (PALMINSTALL.equals(installProgram)) { 407 cmd += (' ' + files[i]); 408 } else if (PPCINSTALL.equals(installProgram)) { 409 cmd += (" \"" + files[i] + "\""); 410 } 411 412 Process proc = Runtime.getRuntime().exec( 413 cmd, 414 null, 415 new File(workingDirectory) 416 ); 417 418 InputStream is = proc.getErrorStream(); 419 420 while((c = is.read()) != -1) { 421 output.append((char)c); 422 } 423 424 if (PALMINSTALL.equals(installProgram)) { 425 proc.waitFor(); 426 } 427 428 if (proc.exitValue() != 0) { 429 throw new AssetInstallationException( "Error executing " 430 + cmd 431 + ": " 432 + output.toString() 433 ); 434 } 435 } 436 } catch (AssetManagementException e) { 437 throw e; 438 } catch (Exception e) { 439 throw new AssetManagementException( 440 "Error executing the PRC/PDB installation: " + e, e 441 ); 442 } 443 } 444 } | Popular Tags |