File: BundleInstall.java

package info (click to toggle)
libequinox-osgi-java 3.9.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,068 kB
  • sloc: java: 57,768; makefile: 9
file content (132 lines) | stat: -rw-r--r-- 4,745 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
 * Copyright (c) 2005, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.internal.baseadaptor;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Dictionary;
import org.eclipse.osgi.baseadaptor.BaseData;
import org.eclipse.osgi.baseadaptor.hooks.StorageHook;
import org.eclipse.osgi.framework.adaptor.BundleData;
import org.eclipse.osgi.framework.adaptor.BundleOperation;
import org.eclipse.osgi.framework.debug.Debug;
import org.eclipse.osgi.framework.internal.core.ReferenceInputStream;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.*;

public class BundleInstall implements BundleOperation {
	private BaseData data;
	private URLConnection source;
	private BaseStorage storage;

	public BundleInstall(BaseData data, URLConnection source, BaseStorage storage) {
		this.data = data;
		this.source = source;
		this.storage = storage;
	}

	/**
	 * Begin the operation on the bundle (install, update, uninstall).
	 *
	 * @return BundleData object for the target bundle.
	 * @throws BundleException If a failure occured modifiying peristent storage.
	 */
	public BundleData begin() throws BundleException {
		try {
			InputStream in = null;
			try {
				data.setLastModified(System.currentTimeMillis());
				data.setStartLevel(storage.getInitialBundleStartLevel());
				StorageHook[] storageHooks = data.getAdaptor().getHookRegistry().getStorageHooks();
				StorageHook[] instanceHooks = new StorageHook[storageHooks.length];
				for (int i = 0; i < storageHooks.length; i++)
					instanceHooks[i] = storageHooks[i].create(data);
				data.setStorageHooks(instanceHooks);
				BaseStorageHook storageHook = (BaseStorageHook) data.getStorageHook(BaseStorageHook.KEY);
				in = source.getInputStream();
				URL sourceURL = source.getURL();
				String protocol = sourceURL == null ? null : sourceURL.getProtocol();
				if (in instanceof ReferenceInputStream) {
					URL reference = ((ReferenceInputStream) in).getReference();
					if (!"file".equals(reference.getProtocol())) //$NON-NLS-1$
						throw new BundleException(NLS.bind(AdaptorMsg.ADAPTOR_URL_CREATE_EXCEPTION, reference));
					storageHook.setReference(true);
					storageHook.setFileName(reference.getPath());
				} else {
					File genDir = storageHook.createGenerationDir();
					if (!genDir.exists())
						throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_CREATE_EXCEPTION, genDir.getPath()));
					storageHook.setReference(false);
					storageHook.setFileName(BaseStorage.BUNDLEFILE_NAME);
					File outFile = new File(genDir, storageHook.getFileName());
					if ("file".equals(protocol)) { //$NON-NLS-1$
						File inFile = new File(source.getURL().getPath());
						if (inFile.isDirectory())
							AdaptorUtil.copyDir(inFile, outFile);
						else
							AdaptorUtil.readFile(in, outFile);
					} else {
						AdaptorUtil.readFile(in, outFile);
					}
				}
				Dictionary<String, String> manifest = storage.loadManifest(data, true);
				for (int i = 0; i < instanceHooks.length; i++)
					instanceHooks[i].initialize(manifest);
			} finally {
				try {
					if (in != null)
						in.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		} catch (IOException ioe) {
			throw new BundleException(AdaptorMsg.BUNDLE_READ_EXCEPTION, BundleException.READ_ERROR, ioe);
		}

		return (data);
	}

	public void undo() {
		if (data != null) {
			try {
				data.close();
			} catch (IOException e) {
				if (Debug.DEBUG_GENERAL)
					Debug.println("Unable to close " + data + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}

		if (data != null) {
			BaseStorageHook storageHook = (BaseStorageHook) data.getStorageHook(BaseStorageHook.KEY);
			try {
				if (storageHook != null)
					storageHook.delete(false, BaseStorageHook.DEL_BUNDLE_STORE);
			} catch (IOException e) {
				data.getAdaptor().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, data.getBundle(), e);
			}
		}
	}

	public void commit(boolean postpone) throws BundleException {
		storage.processExtension(data, BaseStorage.EXTENSION_INSTALLED);
		storage.updateState(data, BundleEvent.INSTALLED);
		try {
			data.save();
		} catch (IOException e) {
			throw new BundleException(AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION, e);
		}

	}

}