Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ca377a8621f2fc1f273f0d8dab1ea746841f9a9 (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2007 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.equinox.prov.installregistry;

import com.thoughtworks.xstream.XStream;
import java.io.*;
import java.net.URL;
import java.util.*;
import org.eclipse.equinox.internal.prov.engine.EngineActivator;
import org.eclipse.equinox.prov.core.eventbus.ProvisioningEventBus;
import org.eclipse.equinox.prov.core.eventbus.SynchronousProvisioningListener;
import org.eclipse.equinox.prov.core.helpers.ServiceHelper;
import org.eclipse.equinox.prov.core.location.AgentLocation;
import org.eclipse.equinox.prov.engine.*;
import org.eclipse.equinox.prov.metadata.IInstallableUnit;
import org.eclipse.osgi.service.datalocation.Location;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;

public class InstallRegistry implements IInstallRegistry {
	private static String STORAGE = "installRegistry.xml";

	// what is installed in each profile
	private Map profileRegistries = new HashMap(); // Profile id -> ProfileInstallRegistry
	//	private ProfileRegistry profileRegistry; // the corresponding ProfileRegistry
	//	private File location; // xml file containing install registry
	//	private IRepository metadataRepo;
	//	private final MetadataCache installedMetadata = new MetadataCache(
	//            new RepositoryGroup("InstallRegistry"), //$NON-NLS-1$
	//            MetadataCache.POLICY_NONE);

	private transient ServiceReference busReference;
	private transient ProvisioningEventBus bus;

	public InstallRegistry() {
		busReference = EngineActivator.getContext().getServiceReference(ProvisioningEventBus.class.getName());
		bus = (ProvisioningEventBus) EngineActivator.getContext().getService(busReference);
		restore();
		bus.addListener(new SynchronousProvisioningListener() {
			public void notify(EventObject o) {
				if (o instanceof InstallableUnitEvent) {
					InstallableUnitEvent event = (InstallableUnitEvent) o;
					if (event.isPre() || !event.getResult().isOK())
						return;
					IProfileInstallRegistry registry = getProfileInstallRegistry(event.getProfile());
					if (event.getOperand().second() != null) {
						registry.addInstallableUnits(event.getOperand().second().getOriginal());
					}
					if (event.getOperand().first() != null) {
						registry.removeInstallableUnits(event.getOperand().first().getOriginal());
					}
				} else if (o instanceof CommitOperationEvent) {
					persist();
					return;
				} else if (o instanceof RollbackOperationEvent) {
					restore();
					return;
				} else if (o instanceof ProfileEvent) {
					ProfileEvent pe = (ProfileEvent) o;
					if (pe.getReason() == ProfileEvent.REMOVED) {
						profileRegistries.remove(pe.getProfile().getProfileId());
						persist();
					}
				}
			}

		});
	}

	private void persist() {
		try {
			BufferedOutputStream bof = null;
			try {
				Location agent = (Location) ServiceHelper.getService(EngineActivator.getContext(), AgentLocation.class.getName());
				if (agent == null)
					// TODO should likely do something here since we failed to persist.
					return;
				if (!agent.getURL().getProtocol().equals("file"))
					throw new IOException("can't write at the given location");

				File outputFile = new File(agent.getURL().getFile(), STORAGE);
				if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs())
					throw new RuntimeException("can't persist profile registry");
				bof = new BufferedOutputStream(new FileOutputStream(outputFile, false));
				new XStream().toXML(profileRegistries, bof);
			} finally {
				if (bof != null)
					bof.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void restore() {
		try {
			BufferedInputStream bif = null;
			try {
				Location agent = (Location) ServiceHelper.getService(EngineActivator.getContext(), AgentLocation.class.getName());
				if (agent == null)
					// TODO should likely do something here since we failed to restore.
					return;
				bif = new BufferedInputStream(new URL(agent.getURL(), STORAGE).openStream());
				profileRegistries = (HashMap) new XStream().fromXML(bif);
			} finally {
				if (bif != null)
					bif.close();
			}
		} catch (FileNotFoundException e) {
			//This is ok.
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public IProfileInstallRegistry getProfileInstallRegistry(Profile profile) {
		String profileId = profile.getProfileId();
		IProfileInstallRegistry result = (IProfileInstallRegistry) this.profileRegistries.get(profileId);
		if (result == null) {
			result = new ProfileInstallRegistry(profileId);
			this.profileRegistries.put(profileId, result);
		}
		return result;
	}

	public Collection getProfileInstallRegistries() {
		return this.profileRegistries.values();
	}

	/**
	 * Install registry for a single profile.
	 */
	public class ProfileInstallRegistry implements IProfileInstallRegistry {
		private String profileId; // id profile this data applies to
		private Set installableUnits; //id 

		ProfileInstallRegistry(String profileId) {
			this.profileId = profileId;
			this.installableUnits = new HashSet();
		}

		public IInstallableUnit[] getInstallableUnits() {
			IInstallableUnit[] result = new IInstallableUnit[installableUnits.size()];
			return (IInstallableUnit[]) installableUnits.toArray(result);
		}

		public void addInstallableUnits(IInstallableUnit toAdd) {
			installableUnits.add(toAdd);
		}

		public void removeInstallableUnits(IInstallableUnit toRemove) {
			installableUnits.remove(toRemove);
		}

		public String getProfileId() {
			return profileId;
		}

		public IInstallableUnit getInstallableUnit(String id, String version) {
			for (Iterator i = installableUnits.iterator(); i.hasNext();) {
				IInstallableUnit iu = (IInstallableUnit) i.next();
				if (iu.getId().equals(id) && iu.getVersion().equals(new Version(version)))
					return iu;
			}
			return null;
		}
	}
}

Back to the top