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.internal.core.ReferenceInputStream; 23 import org.eclipse.osgi.util.NLS; 24 import org.osgi.framework.*; 25 26 public class BundleUpdate implements BundleOperation { 27 private BaseData data; 28 private BaseData newData; 29 private URLConnection source; 30 private BaseStorage storage; 31 32 public BundleUpdate(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 newData = storage.createBaseData(data.getBundleID(), data.getLocation()); 47 newData.setLastModified(System.currentTimeMillis()); 48 newData.setStartLevel(data.getStartLevel()); 49 newData.setStatus(data.getStatus()); 50 StorageHook[] storageHooks = data.getAdaptor().getHookRegistry().getStorageHooks(); 52 StorageHook[] instanceHooks = new StorageHook[storageHooks.length]; 53 for (int i = 0; i < storageHooks.length; i++) { 54 instanceHooks[i] = storageHooks[i].create(newData); 55 instanceHooks[i].copy(data.getStorageHook((String ) instanceHooks[i].getKey())); 56 } 57 newData.setStorageHooks(instanceHooks); 58 BaseStorageHook newStorageHook = (BaseStorageHook) newData.getStorageHook(BaseStorageHook.KEY); 60 InputStream in = source.getInputStream(); 61 URL sourceURL = source.getURL(); 62 String protocol = sourceURL == null ? null : sourceURL.getProtocol(); 63 try { 64 if (in instanceof ReferenceInputStream) { 65 URL reference = ((ReferenceInputStream) in).getReference(); 66 if (!"file".equals(reference.getProtocol())) throw new BundleException(NLS.bind(AdaptorMsg.ADAPTOR_URL_CREATE_EXCEPTION, reference)); 68 String path = reference.getPath(); 71 newStorageHook.setReference(true); 72 newStorageHook.setFileName(path); 73 } else { 74 File genDir = newStorageHook.createGenerationDir(); 75 if (!genDir.exists()) 76 throw new BundleException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_CREATE_EXCEPTION, genDir.getPath())); 77 newStorageHook.setReference(false); 78 newStorageHook.setFileName(BaseStorage.BUNDLEFILE_NAME); 79 File outFile = new File(genDir, newStorageHook.getFileName()); 80 if ("file".equals(protocol)) { File inFile = new File(source.getURL().getPath()); 82 if (inFile.isDirectory()) { 83 AdaptorUtil.copyDir(inFile, outFile); 84 } else { 85 AdaptorUtil.readFile(in, outFile); 86 } 87 } else { 88 AdaptorUtil.readFile(in, outFile); 89 } 90 } 91 Dictionary manifest = storage.loadManifest(newData, true); 92 for (int i = 0; i < instanceHooks.length; i++) 93 instanceHooks[i].initialize(manifest); 94 } finally { 95 try { 96 if (in != null) 97 in.close(); 98 } catch (IOException ee) { 99 } 101 } 102 } catch (IOException e) { 103 throw new BundleException(AdaptorMsg.BUNDLE_READ_EXCEPTION, e); 104 } 105 106 return (newData); 107 } 108 109 116 117 public void commit(boolean postpone) throws BundleException { 118 storage.processExtension(data, BaseStorage.EXTENSION_UNINSTALLED); storage.processExtension(newData, BaseStorage.EXTENSION_UPDATED); try { 121 newData.setLastModified(System.currentTimeMillis()); 122 newData.save(); 123 } catch (IOException e) { 124 throw new BundleException(AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION, e); 125 } 126 storage.updateState(newData, BundleEvent.UPDATED); 127 BaseStorageHook oldStorageHook = (BaseStorageHook) data.getStorageHook(BaseStorageHook.KEY); 128 try { 129 oldStorageHook.delete(postpone, BaseStorageHook.DEL_GENERATION); 130 } catch (IOException e) { 131 data.getAdaptor().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, data.getBundle(), e); 132 } 133 } 134 135 140 public void undo() throws BundleException { 141 if (newData != null) { 142 BaseStorageHook newStorageHook = (BaseStorageHook) newData.getStorageHook(BaseStorageHook.KEY); 143 try { 144 if (newStorageHook != null) 145 newStorageHook.delete(false, BaseStorageHook.DEL_GENERATION); 146 } catch (IOException e) { 147 data.getAdaptor().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, data.getBundle(), e); 148 } 149 } 150 } 151 } 152 | Popular Tags |