KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > baseadaptor > BundleUpdate


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.internal.baseadaptor;
13
14 import java.io.*;
15 import java.net.URL JavaDoc;
16 import java.net.URLConnection JavaDoc;
17 import java.util.Dictionary JavaDoc;
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 JavaDoc source;
30     private BaseStorage storage;
31
32     public BundleUpdate(BaseData data, URLConnection JavaDoc source, BaseStorage storage) {
33         this.data = data;
34         this.source = source;
35         this.storage = storage;
36     }
37
38     /**
39      * Perform the change to persistent storage.
40      *
41      * @return Bundle object for the target bundle.
42      * @throws BundleException if an error occurs
43      */

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             // load the storage hooks into the new data
51
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 JavaDoc) instanceHooks[i].getKey()));
56             }
57             newData.setStorageHooks(instanceHooks);
58             // get the new eclipse storage hooks
59
BaseStorageHook newStorageHook = (BaseStorageHook) newData.getStorageHook(BaseStorageHook.KEY);
60             InputStream in = source.getInputStream();
61             URL JavaDoc sourceURL = source.getURL();
62             String JavaDoc protocol = sourceURL == null ? null : sourceURL.getProtocol();
63             try {
64                 if (in instanceof ReferenceInputStream) {
65                     URL JavaDoc reference = ((ReferenceInputStream) in).getReference();
66                     if (!"file".equals(reference.getProtocol())) //$NON-NLS-1$
67
throw new BundleException(NLS.bind(AdaptorMsg.ADAPTOR_URL_CREATE_EXCEPTION, reference));
68                     // check to make sure we are not just trying to update to the same
69
// directory reference. This would be a no-op.
70
String JavaDoc 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)) { //$NON-NLS-1$
81
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 JavaDoc 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                     // nothing to do here
100
}
101             }
102         } catch (IOException e) {
103             throw new BundleException(AdaptorMsg.BUNDLE_READ_EXCEPTION, e);
104         }
105
106         return (newData);
107     }
108
109     /**
110      * Commit the change to persistent storage.
111      *
112      * @param postpone If true, the bundle's persistent
113      * storage cannot be immediately reclaimed.
114      * @throws BundleException If a failure occured modifiying peristent storage.
115      */

116
117     public void commit(boolean postpone) throws BundleException {
118         storage.processExtension(data, BaseStorage.EXTENSION_UNINSTALLED); // remove the old extension
119
storage.processExtension(newData, BaseStorage.EXTENSION_UPDATED); // update to the new one
120
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     /**
136      * Undo the change to persistent storage.
137      *
138      * @throws BundleException If a failure occured modifiying peristent storage.
139      */

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