KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc source;
30     private BaseStorage storage;
31
32     public BundleInstall(BaseData data, URLConnection JavaDoc source, BaseStorage storage) {
33         this.data = data;
34         this.source = source;
35         this.storage = storage;
36     }
37
38     /**
39      * Begin the operation on the bundle (install, update, uninstall).
40      *
41      * @return BundleData object for the target bundle.
42      * @throws BundleException If a failure occured modifiying peristent storage.
43      */

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 JavaDoc sourceURL = source.getURL();
58                 String JavaDoc protocol = sourceURL == null ? null : sourceURL.getProtocol();
59                 if (in instanceof ReferenceInputStream) {
60                     URL JavaDoc reference = ((ReferenceInputStream) in).getReference();
61                     if (!"file".equals(reference.getProtocol())) //$NON-NLS-1$
62
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)) { //$NON-NLS-1$
73
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 JavaDoc 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                     // do nothing
91
}
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()); //$NON-NLS-1$ //$NON-NLS-2$
107
}
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