1 11 12 package org.eclipse.osgi.internal.baseadaptor; 13 14 import java.io.*; 15 import java.net.URL ; 16 import java.net.URLConnection ; 17 import java.util.Dictionary ; 18 import org.eclipse.osgi.baseadaptor.*; 19 import org.eclipse.osgi.baseadaptor.hooks.StorageHook; 20 import org.eclipse.osgi.framework.adaptor.BundleData; 21 import org.eclipse.osgi.framework.adaptor.BundleOperation; 22 import org.eclipse.osgi.framework.debug.Debug; 23 import org.eclipse.osgi.framework.internal.core.ReferenceInputStream; 24 import org.eclipse.osgi.util.NLS; 25 import org.osgi.framework.*; 26 27 public class BundleInstall implements BundleOperation { 28 private BaseData data; 29 private URLConnection source; 30 private BaseStorage storage; 31 32 public BundleInstall(BaseData data, URLConnection source, BaseStorage storage) { 33 this.data = data; 34 this.source = source; 35 this.storage = storage; 36 } 37 38 44 public BundleData begin() throws BundleException { 45 try { 46 InputStream in = null; 47 try { 48 data.setLastModified(System.currentTimeMillis()); 49 data.setStartLevel(storage.getInitialBundleStartLevel()); 50 StorageHook[] storageHooks = data.getAdaptor().getHookRegistry().getStorageHooks(); 51 StorageHook[] instanceHooks = new StorageHook[storageHooks.length]; 52 for (int i = 0; i < storageHooks.length; i++) 53 instanceHooks[i] = storageHooks[i].create(data); 54 data.setStorageHooks(instanceHooks); 55 BaseStorageHook storageHook = (BaseStorageHook) data.getStorageHook(BaseStorageHook.KEY); 56 in = source.getInputStream(); 57 URL sourceURL = source.getURL(); 58 String protocol = sourceURL == null ? null : sourceURL.getProtocol(); 59 if (in instanceof ReferenceInputStream) { 60 URL reference = ((ReferenceInputStream) in).getReference(); 61 if (!"file".equals(reference.getProtocol())) throw new BundleException(NLS.bind(AdaptorMsg.ADAPTOR_URL_CREATE_EXCEPTION, reference)); 63 storageHook.setReference(true); 64 storageHook.setFileName(reference.getPath()); 65 } else { 66 File genDir = storageHook.createGenerationDir(); 67 if (!genDir.exists()) 68 throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_CREATE_EXCEPTION, genDir.getPath())); 69 storageHook.setReference(false); 70 storageHook.setFileName(BaseStorage.BUNDLEFILE_NAME); 71 File outFile = new File(genDir, storageHook.getFileName()); 72 if ("file".equals(protocol)) { File inFile = new File(source.getURL().getPath()); 74 if (inFile.isDirectory()) 75 AdaptorUtil.copyDir(inFile, outFile); 76 else 77 AdaptorUtil.readFile(in, outFile); 78 } else { 79 AdaptorUtil.readFile(in, outFile); 80 } 81 } 82 Dictionary manifest = storage.loadManifest(data, true); 83 for (int i = 0; i < instanceHooks.length; i++) 84 instanceHooks[i].initialize(manifest); 85 } finally { 86 try { 87 if (in != null) 88 in.close(); 89 } catch (IOException e) { 90 } 92 } 93 } catch (IOException ioe) { 94 throw new BundleException(AdaptorMsg.BUNDLE_READ_EXCEPTION, ioe); 95 } 96 97 return (data); 98 } 99 100 public void undo() { 101 if (data != null) { 102 try { 103 data.close(); 104 } catch (IOException e) { 105 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 106 Debug.println("Unable to close " + data + ": " + e.getMessage()); } 108 } 109 110 if (data != null) { 111 BaseStorageHook storageHook = (BaseStorageHook) data.getStorageHook(BaseStorageHook.KEY); 112 try { 113 if (storageHook != null) 114 storageHook.delete(false, BaseStorageHook.DEL_BUNDLE_STORE); 115 } catch (IOException e) { 116 data.getAdaptor().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, data.getBundle(), e); 117 } 118 } 119 } 120 121 public void commit(boolean postpone) throws BundleException { 122 storage.processExtension(data, BaseStorage.EXTENSION_INSTALLED); 123 try { 124 data.save(); 125 } catch (IOException e) { 126 throw new BundleException(AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION, e); 127 } 128 storage.updateState(data, BundleEvent.INSTALLED); 129 } 130 131 } 132 | Popular Tags |