KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > resolver > StateObjectFactoryImpl


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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 package org.eclipse.osgi.internal.resolver;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.osgi.framework.internal.core.Constants;
16 import org.eclipse.osgi.internal.module.ResolverImpl;
17 import org.eclipse.osgi.service.resolver.*;
18 import org.eclipse.osgi.storagemanager.StorageManager;
19 import org.eclipse.osgi.util.ManifestElement;
20 import org.osgi.framework.*;
21
22 public class StateObjectFactoryImpl implements StateObjectFactory {
23
24     public BundleDescription createBundleDescription(Dictionary manifest, String JavaDoc location, long id) throws BundleException {
25         return createBundleDescription(null, manifest, location, id);
26     }
27
28     public BundleDescription createBundleDescription(State state, Dictionary manifest, String JavaDoc location, long id) throws BundleException {
29         BundleDescriptionImpl result = (BundleDescriptionImpl) StateBuilder.createBundleDescription((StateImpl) state, manifest, location);
30         result.setBundleId(id);
31         return result;
32     }
33
34     public BundleDescription createBundleDescription(long id, String JavaDoc symbolicName, Version version, String JavaDoc location, BundleSpecification[] required, HostSpecification host, ImportPackageSpecification[] imports, ExportPackageDescription[] exports, String JavaDoc[] providedPackages, boolean singleton) {
35         return createBundleDescription(id, symbolicName, version, location, required, host, imports, exports, providedPackages, singleton, true, true, null, null, null, null);
36     }
37
38     public BundleDescription createBundleDescription(long id, String JavaDoc symbolicName, Version version, String JavaDoc location, BundleSpecification[] required, HostSpecification host, ImportPackageSpecification[] imports, ExportPackageDescription[] exports, String JavaDoc[] providedPackages, boolean singleton, boolean attachFragments, boolean dynamicFragments, String JavaDoc platformFilter, String JavaDoc executionEnvironment, GenericSpecification[] genericRequires, GenericDescription[] genericCapabilities) {
39         // bug 154137 we need to parse the executionEnvironment param; no need to check for null, ManifestElement does that for us.
40
return createBundleDescription(id, symbolicName, version, location, required, host, imports, exports, singleton, attachFragments, dynamicFragments, platformFilter, ManifestElement.getArrayFromList(executionEnvironment), genericRequires, genericCapabilities);
41     }
42
43     public BundleDescription createBundleDescription(long id, String JavaDoc symbolicName, Version version, String JavaDoc location, BundleSpecification[] required, HostSpecification host, ImportPackageSpecification[] imports, ExportPackageDescription[] exports, boolean singleton, boolean attachFragments, boolean dynamicFragments, String JavaDoc platformFilter, String JavaDoc[] executionEnvironments, GenericSpecification[] genericRequires, GenericDescription[] genericCapabilities) {
44         BundleDescriptionImpl bundle = new BundleDescriptionImpl();
45         bundle.setBundleId(id);
46         bundle.setSymbolicName(symbolicName);
47         bundle.setVersion(version);
48         bundle.setLocation(location);
49         bundle.setRequiredBundles(required);
50         bundle.setHost(host);
51         bundle.setImportPackages(imports);
52         bundle.setExportPackages(exports);
53         bundle.setStateBit(BundleDescriptionImpl.SINGLETON, singleton);
54         bundle.setStateBit(BundleDescriptionImpl.ATTACH_FRAGMENTS, attachFragments);
55         bundle.setStateBit(BundleDescriptionImpl.DYNAMIC_FRAGMENTS, dynamicFragments);
56         bundle.setPlatformFilter(platformFilter);
57         bundle.setExecutionEnvironments(executionEnvironments);
58         bundle.setGenericRequires(genericRequires);
59         bundle.setGenericCapabilities(genericCapabilities);
60         return bundle;
61     }
62
63     public BundleDescription createBundleDescription(BundleDescription original) {
64         BundleDescriptionImpl bundle = new BundleDescriptionImpl();
65         bundle.setBundleId(original.getBundleId());
66         bundle.setSymbolicName(original.getSymbolicName());
67         bundle.setVersion(original.getVersion());
68         bundle.setLocation(original.getLocation());
69         BundleSpecification[] originalRequired = original.getRequiredBundles();
70         BundleSpecification[] newRequired = new BundleSpecification[originalRequired.length];
71         for (int i = 0; i < newRequired.length; i++)
72             newRequired[i] = createBundleSpecification(originalRequired[i]);
73         bundle.setRequiredBundles(newRequired);
74         ExportPackageDescription[] originalExports = original.getExportPackages();
75         ExportPackageDescription[] newExports = new ExportPackageDescription[originalExports.length];
76         for (int i = 0; i < newExports.length; i++)
77             newExports[i] = createExportPackageDescription(originalExports[i]);
78         bundle.setExportPackages(newExports);
79         ImportPackageSpecification[] originalImports = original.getImportPackages();
80         ImportPackageSpecification[] newImports = new ImportPackageSpecification[originalImports.length];
81         for (int i = 0; i < newImports.length; i++)
82             newImports[i] = createImportPackageSpecification(originalImports[i]);
83         bundle.setImportPackages(newImports);
84         if (original.getHost() != null)
85             bundle.setHost(createHostSpecification(original.getHost()));
86         bundle.setStateBit(BundleDescriptionImpl.SINGLETON, original.isSingleton());
87         bundle.setStateBit(BundleDescriptionImpl.ATTACH_FRAGMENTS, original.attachFragments());
88         bundle.setStateBit(BundleDescriptionImpl.DYNAMIC_FRAGMENTS, original.dynamicFragments());
89         bundle.setStateBit(BundleDescriptionImpl.HAS_DYNAMICIMPORT, original.hasDynamicImports());
90         bundle.setPlatformFilter(original.getPlatformFilter());
91         bundle.setExecutionEnvironments(original.getExecutionEnvironments());
92         bundle.setGenericCapabilities(createGenericCapabilities(original.getGenericCapabilities()));
93         bundle.setGenericRequires(createGenericRequires(original.getGenericRequires()));
94         return bundle;
95     }
96
97     private GenericDescription[] createGenericCapabilities(GenericDescription[] genericCapabilities) {
98         if (genericCapabilities == null || genericCapabilities.length == 0)
99             return null;
100         GenericDescription[] result = new GenericDescription[genericCapabilities.length];
101         for (int i = 0; i < genericCapabilities.length; i++) {
102             GenericDescriptionImpl cap = new GenericDescriptionImpl();
103             cap.setName(genericCapabilities[i].getName());
104             cap.setVersion(genericCapabilities[i].getVersion());
105             cap.setAttributes(genericCapabilities[i].getAttributes());
106             result[i] = cap;
107         }
108         return result;
109     }
110
111     private GenericSpecification[] createGenericRequires(GenericSpecification[] genericRequires) {
112         if (genericRequires == null || genericRequires.length == 0)
113             return null;
114         GenericSpecification[] result = new GenericSpecification[genericRequires.length];
115         for (int i = 0; i < genericRequires.length; i++) {
116             GenericSpecificationImpl req = new GenericSpecificationImpl();
117             req.setName(genericRequires[i].getName());
118             try {
119                 req.setMatchingFilter(genericRequires[i].getMatchingFilter());
120             } catch (InvalidSyntaxException e) {
121                 // do nothing; this filter should aready have been tested
122
}
123             result[i] = req;
124         }
125         return result;
126     }
127
128     public BundleSpecification createBundleSpecification(String JavaDoc requiredSymbolicName, VersionRange requiredVersionRange, boolean export, boolean optional) {
129         BundleSpecificationImpl bundleSpec = new BundleSpecificationImpl();
130         bundleSpec.setName(requiredSymbolicName);
131         bundleSpec.setVersionRange(requiredVersionRange);
132         bundleSpec.setExported(export);
133         bundleSpec.setOptional(optional);
134         return bundleSpec;
135     }
136
137     public BundleSpecification createBundleSpecification(BundleSpecification original) {
138         BundleSpecificationImpl bundleSpec = new BundleSpecificationImpl();
139         bundleSpec.setName(original.getName());
140         bundleSpec.setVersionRange(original.getVersionRange());
141         bundleSpec.setExported(original.isExported());
142         bundleSpec.setOptional(original.isOptional());
143         return bundleSpec;
144     }
145
146     public HostSpecification createHostSpecification(String JavaDoc hostSymbolicName, VersionRange versionRange) {
147         HostSpecificationImpl hostSpec = new HostSpecificationImpl();
148         hostSpec.setName(hostSymbolicName);
149         hostSpec.setVersionRange(versionRange);
150         return hostSpec;
151     }
152
153     public HostSpecification createHostSpecification(HostSpecification original) {
154         HostSpecificationImpl hostSpec = new HostSpecificationImpl();
155         hostSpec.setName(original.getName());
156         hostSpec.setVersionRange(original.getVersionRange());
157         return hostSpec;
158     }
159
160     public ImportPackageSpecification createImportPackageSpecification(String JavaDoc packageName, VersionRange versionRange, String JavaDoc bundleSymbolicName, VersionRange bundleVersionRange, Map directives, Map attributes, BundleDescription importer) {
161         ImportPackageSpecificationImpl packageSpec = new ImportPackageSpecificationImpl();
162         packageSpec.setName(packageName);
163         packageSpec.setVersionRange(versionRange);
164         packageSpec.setBundleSymbolicName(bundleSymbolicName);
165         packageSpec.setBundleVersionRange(bundleVersionRange);
166         packageSpec.setDirectives(directives);
167         packageSpec.setAttributes(attributes);
168         packageSpec.setBundle(importer);
169         return packageSpec;
170     }
171
172     public ImportPackageSpecification createImportPackageSpecification(ImportPackageSpecification original) {
173         ImportPackageSpecificationImpl packageSpec = new ImportPackageSpecificationImpl();
174         packageSpec.setName(original.getName());
175         packageSpec.setVersionRange(original.getVersionRange());
176         packageSpec.setBundleSymbolicName(original.getBundleSymbolicName());
177         packageSpec.setBundleVersionRange(original.getBundleVersionRange());
178         packageSpec.setDirectives(original.getDirectives());
179         packageSpec.setAttributes(original.getAttributes());
180         return packageSpec;
181     }
182
183     public ExportPackageDescription createExportPackageDescription(ExportPackageDescription original) {
184         return createExportPackageDescription(original.getName(), original.getVersion(), original.getDirectives(), original.getAttributes(), original.isRoot(), null);
185     }
186
187     public ExportPackageDescription createExportPackageDescription(String JavaDoc packageName, Version version, Map directives, Map attributes, boolean root, BundleDescription exporter) {
188         ExportPackageDescriptionImpl exportPackage = new ExportPackageDescriptionImpl();
189         exportPackage.setName(packageName);
190         exportPackage.setVersion(version);
191         exportPackage.setDirectives(directives);
192         exportPackage.setAttributes(attributes);
193         exportPackage.setRoot(root);
194         exportPackage.setExporter(exporter);
195         return exportPackage;
196     }
197
198     public GenericDescription createGenericDescription(String JavaDoc name, String JavaDoc type, Version version, Map attributes) {
199         GenericDescriptionImpl result = new GenericDescriptionImpl();
200         result.setName(name);
201         result.setType(type);
202         result.setVersion(version);
203         Object JavaDoc versionObj = attributes == null ? null : attributes.remove(Constants.VERSION_ATTRIBUTE);
204         if (versionObj instanceof Version) // this is just incase someone uses version:version as a key
205
result.setVersion((Version) versionObj);
206         Dictionary attrs = new Hashtable();
207         if (attributes != null)
208             for (Iterator keys = attributes.keySet().iterator(); keys.hasNext();) {
209                 Object JavaDoc key = keys.next();
210                 attrs.put(key, attributes.get(key));
211             }
212         result.setAttributes(attrs);
213         return result;
214     }
215
216     public GenericSpecification createGenericSpecification(String JavaDoc name, String JavaDoc type, String JavaDoc matchingFilter, boolean optional, boolean multiple) throws InvalidSyntaxException {
217         GenericSpecificationImpl result = new GenericSpecificationImpl();
218         result.setName(name);
219         result.setType(type);
220         result.setMatchingFilter(matchingFilter);
221         int resolution = 0;
222         if (optional)
223             resolution |= GenericSpecification.RESOLUTION_OPTIONAL;
224         if (multiple)
225             resolution |= GenericSpecification.RESOLUTION_MULTIPLE;
226         result.setResolution(resolution);
227         return result;
228     }
229
230     public SystemState createSystemState() {
231         SystemState state = new SystemState();
232         state.setFactory(this);
233         return state;
234     }
235
236     public State createState() {
237         return internalCreateState();
238     }
239
240     public State createState(boolean createResolver) {
241         State result = internalCreateState();
242         if (createResolver)
243             result.setResolver(new ResolverImpl(null, false));
244         return result;
245     }
246
247     public State createState(State original) {
248         StateImpl newState = internalCreateState();
249         newState.setTimeStamp(original.getTimeStamp());
250         BundleDescription[] bundles = original.getBundles();
251         for (int i = 0; i < bundles.length; i++)
252             newState.basicAddBundle(createBundleDescription(bundles[i]));
253         newState.setResolved(false);
254         newState.setPlatformProperties(original.getPlatformProperties());
255         return newState;
256     }
257
258     private StateImpl internalCreateState() {
259         StateImpl state = new UserState();
260         state.setFactory(this);
261         return state;
262     }
263
264     public SystemState readSystemState(File stateFile, File lazyFile, boolean lazyLoad, long expectedTimeStamp) throws IOException {
265         StateReader reader = new StateReader(stateFile, lazyFile, lazyLoad);
266         SystemState restoredState = new SystemState();
267         restoredState.setReader(reader);
268         restoredState.setFactory(this);
269         if (!reader.loadState(restoredState, expectedTimeStamp))
270             return null;
271         return restoredState;
272     }
273
274     public State readState(InputStream stream) throws IOException {
275         return internalReadStateDeprecated(internalCreateState(), new DataInputStream(stream), -1);
276     }
277
278     public State readState(DataInputStream stream) throws IOException {
279         return internalReadStateDeprecated(internalCreateState(), stream, -1);
280     }
281
282     public State readState(File stateDirectory) throws IOException {
283         return internalReadState(internalCreateState(), stateDirectory, -1);
284     }
285
286     private State internalReadStateDeprecated(StateImpl toRestore, DataInputStream stream, long expectedTimestamp) throws IOException {
287         StateReader reader = new StateReader();
288         if (!reader.loadStateDeprecated(toRestore, stream, expectedTimestamp))
289             return null;
290         return toRestore;
291     }
292
293     private State internalReadState(StateImpl toRestore, File stateDirectory, long expectedTimestamp) throws IOException {
294         File stateFile = new File(stateDirectory, StateReader.STATE_FILE);
295         File lazyFile = new File(stateDirectory, StateReader.LAZY_FILE);
296         if (!stateFile.exists() || !lazyFile.exists()) {
297             StorageManager storageManager = new StorageManager(stateDirectory, "none", true); //$NON-NLS-1$
298
try {
299                 // if the directory is pointing at the configuration directory then the base files will not exist
300
storageManager.open(true);
301                 // try using the storage manager to find the managed state files (bug 143255)
302
File managedState = storageManager.lookup(StateReader.STATE_FILE, false);
303                 File managedLazy = storageManager.lookup(StateReader.LAZY_FILE, false);
304                 if (managedState != null && managedLazy != null) {
305                     stateFile = managedState;
306                     lazyFile = managedLazy;
307                 }
308             } finally {
309                 storageManager.close();
310             }
311         }
312         StateReader reader = new StateReader(stateFile, lazyFile, false);
313         if (!reader.loadState(toRestore, expectedTimestamp))
314             return null;
315         return toRestore;
316     }
317
318     public void writeState(State state, DataOutputStream stream) throws IOException {
319         internalWriteStateDeprecated(state, stream);
320     }
321
322     public void writeState(State state, File stateDirectory) throws IOException {
323         if (stateDirectory == null)
324             throw new IOException();
325         StateWriter writer = new StateWriter();
326         File stateFile = new File(stateDirectory, StateReader.STATE_FILE);
327         File lazyFile = new File(stateDirectory, StateReader.LAZY_FILE);
328         writer.saveState((StateImpl) state, stateFile, lazyFile);
329     }
330
331     public void writeState(State state, OutputStream stream) throws IOException {
332         internalWriteStateDeprecated(state, new DataOutputStream(stream));
333     }
334
335     public void writeState(State state, File stateFile, File lazyFile) throws IOException {
336         StateWriter writer = new StateWriter();
337         writer.saveState((StateImpl) state, stateFile, lazyFile);
338     }
339
340     public void internalWriteStateDeprecated(State state, DataOutputStream stream) throws IOException {
341         if (state.getFactory() != this)
342             throw new IllegalArgumentException JavaDoc();
343         StateWriter writer = new StateWriter();
344         writer.saveStateDeprecated((StateImpl) state, stream);
345     }
346 }
347
Popular Tags