1 /*******************************************************************************
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland 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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.project.management;
14 import java.io.IOException;
15 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
25 import java.util.jar.Manifest;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository;
34 import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository;
35 import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepositoryFactory;
36 import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository;
37 import org.eclipse.equinox.internal.p2.metadata.repository.LocalMetadataRepository;
38 import org.eclipse.equinox.internal.p2.metadata.repository.SimpleMetadataRepositoryFactory;
39 import org.eclipse.equinox.p2.core.IProvisioningAgent;
40 import org.eclipse.equinox.p2.core.ProvisionException;
41 import org.eclipse.equinox.p2.internal.repository.mirroring.Mirroring;
42 import org.eclipse.equinox.p2.metadata.IArtifactKey;
43 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
44 import org.eclipse.equinox.p2.metadata.IVersionedId;
45 import org.eclipse.equinox.p2.metadata.VersionedId;
46 import org.eclipse.equinox.p2.query.IQuery;
47 import org.eclipse.equinox.p2.query.IQueryResult;
48 import org.eclipse.equinox.p2.query.QueryUtil;
49 import org.eclipse.equinox.p2.repository.artifact.ArtifactKeyQuery;
50 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
51 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
52 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
53 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
54 import org.simantics.graph.db.TransferableGraphException;
55 import org.simantics.graph.query.Graphs;
56 import org.simantics.graph.query.IGraph;
57 import org.simantics.graph.query.Paths;
58 import org.simantics.graph.query.Res;
59 import org.simantics.graph.query.UriUtils;
60 import org.simantics.graph.representation.TransferableGraph1;
61 import org.simantics.scl.reflection.OntologyVersions;
62 import org.simantics.utils.FileUtils;
65 * Bundlepool is a repository of artifacts and installable units.
67 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
69 @SuppressWarnings("restriction")
70 public class BundlePool {
72 /** Application default pool. It is located at "workspace/bundlepool" */
73 static BundlePool DEFAULT;
75 IMetadataRepository metadataRepository;
76 IArtifactRepository artifactRepository;
77 IProvisioningAgent metadataAgent;
78 IProvisioningAgent artifactAgent;
79 URI metadataAgentLocation;
80 URI artifactAgentLocation;
82 List<BundleInfo> userInstallables;
84 /** All features that were discovered in the repository */
85 Set<FeatureInfo> projectFeatures = new HashSet<FeatureInfo>();
90 * @return SPM Bundle Pool
92 public synchronized static BundlePool getDefault() {
93 if (DEFAULT == null) {
95 IPath path = Platform.getLocation().append("bundlepool");
96 URI metadataRepositoryLocation = path.toFile().toURI();
97 URI artifactRepositoryLocation = path.toFile().toURI();
98 IProvisioningAgent agent = P2Util.createAgent( path );
99 DEFAULT = new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent);
100 } catch (ProvisionException e) {
102 throw new RuntimeException(e);
109 * Create bundle pool in a directory on a local hard drive.
110 * This location will contain metadata repository, artifact repository and
111 * P2 workarea (Agent).
114 * @return bundle pool
116 public static BundlePool createAt(File location) {
118 File canonicalLocation = location.getCanonicalFile();
119 URI metadataRepositoryLocation = canonicalLocation.toURI();
120 URI artifactRepositoryLocation = canonicalLocation.toURI();
121 IPath agentLocation = new Path( canonicalLocation.toString() );
122 IProvisioningAgent agent = P2Util.createAgent( agentLocation );
123 return new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent);
124 } catch (IOException e) {
125 throw new RuntimeException(e);
126 } catch (org.eclipse.equinox.p2.core.ProvisionException e) {
127 throw new RuntimeException(e);
132 public BundlePool(URI metadataRepositoryLocation, URI artifactRepositoryLocation, IProvisioningAgent agent) throws ProvisionException {
134 this.metadataAgent = agent;
135 this.artifactAgent = agent;
137 SimpleMetadataRepositoryFactory metRepFactory = new SimpleMetadataRepositoryFactory();
138 metRepFactory.setAgent( metadataAgent );
140 metadataRepository = metRepFactory.load(metadataRepositoryLocation, 0, null);
141 } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {
142 new File(metadataRepositoryLocation).mkdirs();
143 Map<String, String> repoProperties = new HashMap<String, String>();
144 metadataRepository = metRepFactory.create(metadataRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);
147 // IMetadataRepository metadataRepository = new LocalMetadataRepository( agent, repositoryLocation, "My Metadata Repository", Collections.EMPTY_MAP );
148 // IMetadataRepository metadataRepository = getMetadataRepository(agent, repositoryLocation);
150 // IArtifactRepository artifactRepository = new SimpleArtifactRepositoryFactory().create(repositoryLocation, "Sample Artifact Repository", ArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, Collections.EMPTY_MAP);
151 // SimpleArtifactRepository artifactRepository = new SimpleArtifactRepository( agent, "My Simple Artifact Repository", repositoryLocation, Collections.EMPTY_MAP);
152 SimpleArtifactRepositoryFactory artRepFactory = new SimpleArtifactRepositoryFactory();
153 artRepFactory.setAgent( artifactAgent );
156 artifactRepository = artRepFactory.load(artifactRepositoryLocation, 0, null);
157 } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {
158 new File(artifactRepositoryLocation).mkdirs();
159 Map<String, String> repoProperties = new HashMap<String, String>();
160 artifactRepository = artRepFactory.create(artifactRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);
166 public BundlePool(IMetadataRepository metadataRepository, IArtifactRepository artifactRepository) throws ProvisionException {
167 this.metadataRepository = metadataRepository;
168 this.artifactRepository = artifactRepository;
169 this.metadataAgent = metadataRepository.getProvisioningAgent();
170 this.artifactAgent = artifactRepository.getProvisioningAgent();
175 * Init prepares with the following actions:
176 * o loads transferable graphs
177 * o discovers Features and collects Feature URIs, Artifacts and Installable Units
179 * @throws ProvisionException
181 protected void init() throws ProvisionException {
183 projectFeatures.clear();
185 ArrayList<TransferableGraph1> platformTGs = new ArrayList<TransferableGraph1>();
187 for (IArtifactKey key : getAllArtifactKeys()) {
188 boolean hasGraph = ProvisioningUtil.hasFile(artifactRepository, key, "graph.tg");
189 if (!hasGraph) continue;
192 TransferableGraph1 graph = ProvisioningUtil.getTransferableGraph(artifactRepository, key);
193 platformTGs.add(graph);
196 // Create new project, use all features available in Platform
197 final List<String> poolFeatures = new ArrayList<String>();
199 // Convert graph instances
200 GraphBundleEx l0 = getLayer0();
201 String l0v = l0.getMajor() + "." + l0.getMinor();
202 IGraph graph = Graphs.createGraph(new Paths(l0v), platformTGs);
204 for(Res feature : graph.getInstances(UriUtils.uriToPath(OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Project-0.0/Feature" )))) {
205 poolFeatures.add( feature.toString() );
208 } catch (TransferableGraphException e) {
209 throw new ProvisionException("Problem", e);
210 } catch (IOException e) {
211 throw new ProvisionException("Failed to read graph.tg", e);
215 public IMetadataRepository getMetadataRepository() {
216 return metadataRepository;
219 public IArtifactRepository getArtifactRepository() {
220 return artifactRepository;
223 public IArtifactKey[] getAllArtifactKeys() {
224 return artifactRepository.query( ArtifactKeyQuery.ALL_KEYS, null ).toArray( IArtifactKey.class );
227 public GraphBundleEx getLayer0() throws ProvisionException, TransferableGraphException {
228 IVersionedId vid = VersionedId.parse("org.simantics.layer0");
229 Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createLatestQuery( QueryUtil.createIUQuery(vid) ), null).toSet();
230 if (result.isEmpty()) throw new RuntimeException("Unexpectedly got no IU for "+vid);
231 if (result.size()>1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+vid);
232 IInstallableUnit iu = result.iterator().next();
233 IArtifactKey key = toSingleArtifact(iu);
234 TransferableGraph1 graph = getTransferableGraph( key );
235 return new GraphBundleEx( "Layer0", graph, key);
238 public TransferableGraph1 getTransferableGraph(IArtifactKey artifactKey) throws ProvisionException, TransferableGraphException {
239 return ProvisioningUtil.getTransferableGraph( artifactRepository, artifactKey);
247 * @param a list of features
248 * @throws ProvisionException
250 public void getFeatures(List<BundleInfo> features) throws ProvisionException {
251 IMetadataRepository metadataRepository = getMetadataRepository();
253 Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository);
254 for (IInstallableUnit iu : ius) {
255 if (ProvisioningUtil.isGroup(iu)) {
256 // if (P2Util.isFeature(iu)) {
257 BundleInfo b = BundleInfo.read(iu);
266 * @param id bundle id
267 * @return bundle info
269 public BundleInfo getBundleInfo(String id) {
270 IQuery<IInstallableUnit> query = QueryUtil.createLatestQuery( QueryUtil.createIUQuery(id) );
271 IQueryResult<IInstallableUnit> queryResult = metadataRepository.query(query, null);
272 IInstallableUnit[] array = queryResult.toArray( IInstallableUnit.class );
273 for (IInstallableUnit iu : array) {
274 BundleInfo bi = BundleInfo.read(iu);
281 * Get the features that visible for the end-user.
282 * User installable features are configured in Simantics manifest files
283 * (META-INF/SIMANTICS.MF as Simantics-Features-Bundle -property)
286 * @throws IOException
288 // public void getUserInstallableFeatures(Collection<BundleInfo> result) throws IOException {
289 // ArrayList<String> ids = new ArrayList<String>();
290 // ProvisioningUtil.getUserInstallables(artifactRepository, ids);
291 // for (String id : ids) {
292 // BundleInfo bi = getBundleInfo(id);
293 // if (bi != null) result.add( bi );
295 // // Add workspace bundle
296 // BundleInfo bi = getBundleInfo( "org.simantics.workbench.product" );
297 // if (bi != null) result.add( bi );
303 * @param a list of features
304 * @throws ProvisionException
306 // public void getInstallableFeatures(List<BundleInfo> features) throws ProvisionException {
307 // IMetadataRepository metadataRepository = getMetadataRepository();
309 // Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository);
310 // for (IInstallableUnit iu : ius) {
311 // if (ProvisioningUtil.isGroup(iu)) {
312 //// if (P2Util.isFeature(iu)) {
313 // BundleInfo b = BundleInfo.read(iu);
314 // features.add( b );
320 * Mirror source locations to the bundle pool.
323 * @param sourceLocations
325 * @throws ProvisionException
327 public IStatus download(IProgressMonitor monitor, URI[] sourceLocations) throws ProvisionException {
329 monitor.beginTask("Synchronizing Local repository with web repositories", 10);
331 @SuppressWarnings("unused")
332 IMetadataRepositoryManager metaRepoMgr = (IMetadataRepositoryManager) metadataAgent.getService(IMetadataRepositoryManager.SERVICE_NAME);
333 @SuppressWarnings("unused")
334 IArtifactRepositoryManager artsRepoMgr = (IArtifactRepositoryManager) artifactAgent.getService(IArtifactRepositoryManager.SERVICE_NAME);
338 monitor.setTaskName("Setting up local metadata repository");
339 LocalMetadataRepository dstMetaRepo = (LocalMetadataRepository) metadataRepository;
342 monitor.setTaskName("Setting up local artifact repository");
343 SimpleArtifactRepository dstArtsRepo = (SimpleArtifactRepository) artifactRepository;
346 // Create a source repos
347 monitor.setTaskName("Setting up remote artifact repository");
348 CompositeArtifactRepository srcArtsRepo = CompositeArtifactRepository.createMemoryComposite( metadataAgent /* ? */ );
349 for (URI uri : sourceLocations) srcArtsRepo.addChild(uri);
352 monitor.setTaskName("Setting up remote metadata repository");
353 CompositeMetadataRepository srcMetaRepo = CompositeMetadataRepository.createMemoryComposite( artifactAgent /* ? */ );
354 for (URI uri : sourceLocations) srcMetaRepo.addChild(uri);
357 monitor.setTaskName("Retrieving Installable Units");
358 @SuppressWarnings("unused")
359 Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(srcMetaRepo);
362 monitor.setTaskName("Mirroring Metadata");
363 // Get all IUs from source
364 IQueryResult<IInstallableUnit> allIUs = srcMetaRepo.query(QueryUtil.createIUAnyQuery(), monitor);
365 // Put the IUs to dst
366 dstMetaRepo.addInstallableUnits(allIUs.toUnmodifiableSet());
367 dstMetaRepo.addReferences(srcMetaRepo.getReferences());
370 monitor.setTaskName("Mirroring Artifacts");
372 boolean compare = false;
373 boolean failOnError = true;
374 //boolean raw = true;
375 boolean verbose = false;
376 boolean validate = false;
377 //boolean mirrorReferences = false;
378 String comparatorID = "";
380 Mirroring mirror = new Mirroring(srcArtsRepo, dstArtsRepo, false);
381 mirror.setCompare(compare);
382 mirror.setComparatorId(comparatorID);
383 // mirror.setBaseline(null);
384 mirror.setValidate(validate);
385 // mirror.setCompareExclusions();
387 IStatus result = mirror.run(failOnError, verbose);
388 if (result.getException()!=null)
389 throw new ProvisionException(result);
394 } catch (IllegalArgumentException iae) {
395 return new Status(IStatus.ERROR, "org.simantics.project", iae.getMessage(), iae);
399 public IInstallableUnit toSingleInstallableUnit(IVersionedId id) throws ProvisionException {
400 Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createIUQuery(id), null).toSet();
401 if (result.size() != 1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+id);
402 return result.iterator().next();
405 public IArtifactKey toSingleArtifact(IVersionedId id) throws ProvisionException {
406 IInstallableUnit iu = toSingleInstallableUnit(id);
407 if (iu.getArtifacts().size() != 1) throw new RuntimeException("Unexpectedly got more than one artifact for ");
408 return iu.getArtifacts().iterator().next();
412 * Get file from an artifact.
416 * @return inputstream or <tt>null</tt>. Inputstream must be closed.
417 * @throws IOException
419 public InputStream getFile(IArtifactKey key, String filename) throws IOException {
420 return ProvisioningUtil.getFile(artifactRepository, key, filename);
424 * Checks whether there exists a file in an artifact
428 * @return true file exists in the artifact
429 * @throws IOException
431 public boolean hasFile(IArtifactKey key, String filename) throws IOException {
432 return ProvisioningUtil.hasFile(artifactRepository, key, filename);
436 * Get META-INF/MANIFEST.MF
438 * @param repo artifact repo
439 * @param key key to artifact
440 * @return manifest or null
441 * @throws IOException
443 public Manifest getManifest(IArtifactKey key) throws IOException {
444 return ProvisioningUtil.getManifest(artifactRepository, key);
448 * Get META-INF/SIMANTICS.MF
450 * @param repo artifact repo
451 * @param key key to artifact
452 * @return manifest or null
453 * @throws IOException
455 public Manifest getSimanticsManifest(IArtifactKey key) throws IOException {
456 return ProvisioningUtil.getManifest(artifactRepository, key);
460 * Get list of user installable bundles
463 * @throws IOException
465 public void getUserInstallables(List<BundleInfo> result) throws IOException {
466 if (userInstallables==null) {
467 userInstallables = new ArrayList<BundleInfo>();
469 List<String> topLevelBundles = new ArrayList<String>();
470 ProvisioningUtil.getUserInstallables(artifactRepository, topLevelBundles);
472 for (String bundleId : topLevelBundles) {
473 IInstallableUnit ius[] = metadataRepository.query( QueryUtil.createIUQuery(bundleId) , null).toArray( IInstallableUnit.class );
474 for (IInstallableUnit iu : ius) {
475 userInstallables.add( BundleInfo.read(iu) );
480 result.addAll(userInstallables);
492 // * Get graph bundles
494 // * @param list to be populated with graph bundle ids
495 // * @throws ProvisionException
496 // * @throws IOException
498 // public void getGraphs(List<BundleInfo> graphs) throws ProvisionException, IOException {
499 // Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository);
500 // for (IInstallableUnit iu : ius) {
501 // if (isGraph(iu)) {
502 // BundleInfo b = BundleInfo.read(iu);
511 * @param list to be populated with graph bundle ids
512 * @throws ProvisionException
513 * @throws IOException
514 * @throws TransferableGraphException
516 public void getGraphBundles(List<GraphBundleEx> graphs) throws ProvisionException, IOException, TransferableGraphException {
517 Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository);
518 for (IInstallableUnit iu : ius) {
519 for (IArtifactKey key : iu.getArtifacts()) {
520 if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;
521 TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );
522 GraphBundleEx b = new GraphBundleEx(iu.getId(), tg, key);
529 * Get a list of all features. Features are uris, they are versionless.
531 * @throws TransferableGraphException
532 * @throws IOException
534 public Set<FeatureInfo> getFeatures() throws IOException, TransferableGraphException {
536 HashSet<FeatureInfo> result = new HashSet<FeatureInfo>();
537 Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository);
538 for (IInstallableUnit iu : ius) {
539 for (IArtifactKey key : iu.getArtifacts()) {
540 if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;
541 TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );
542 IGraph g = Graphs.createGraph(tg);
544 Res PublishedProjectFeatures = UriUtils.uriToPath( ProjectResource.URIs.PublishedProjectFeatures );
545 Path HasFeature = UriUtils.uriToPath( ProjectResource.URIs.HasFeature );
546 for(Res feature : g.getObjects(PublishedProjectFeatures, HasFeature)) {
547 String uri = feature.toString();
548 String name = feature instanceof PathChild ? ((PathChild)feature).name : uri;
550 FeatureInfo fi = new FeatureInfo(name, uri, null, null);
558 return Collections.emptySet();
562 * For a set of extact feature ids (id_version), find the set of graph bundles
563 * required in the database.
565 * @param featureIds feature
566 * @return graph bundles
568 public Set<IVersionedId> getRequiredGraphBundles(Collection<IVersionedId> featureIds) {
574 * Checks whether artifact is a graph bundle.
576 * @param key key to artifact
577 * @return <code>true</code> if is a graph bundle
578 * @throws IOException
580 public boolean isGraphArtifact(IArtifactKey key) throws IOException {
581 return ProvisioningUtil.isGraphArtifact(artifactRepository, key);
585 * Checks wheter iu is graph
589 * @throws IOException
591 public boolean isGraph(IInstallableUnit iu) throws IOException {
592 return ProvisioningUtil.isGraph(iu, metadataRepository, artifactRepository);
596 * Delete default repositories located in /P2 folder
597 * @throws IOException
599 public void delete() throws IOException {
600 File location1 = new File(metadataAgentLocation);
601 File location2 = new File(artifactAgentLocation);
602 File location3 = new File(metadataRepository.getLocation());
603 File location4 = new File(artifactRepository.getLocation());
604 FileUtils.deleteDir(location1);
605 FileUtils.deleteDir(location2);
606 FileUtils.deleteDir(location3);
607 FileUtils.deleteDir(location4);