]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/management/BundlePool.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / BundlePool.java
index 2679f6ad24448b89a45dde2b94ce5e496e904c73..aa8beb7e2bf1e85fdfad0eda3d8f54e172fa2906 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.project.management;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.net.URI;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.jar.Manifest;\r
-\r
-import org.eclipse.core.runtime.IPath;\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Path;\r
-import org.eclipse.core.runtime.Platform;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository;\r
-import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository;\r
-import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepositoryFactory;\r
-import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository;\r
-import org.eclipse.equinox.internal.p2.metadata.repository.LocalMetadataRepository;\r
-import org.eclipse.equinox.internal.p2.metadata.repository.SimpleMetadataRepositoryFactory;\r
-import org.eclipse.equinox.p2.core.IProvisioningAgent;\r
-import org.eclipse.equinox.p2.core.ProvisionException;\r
-import org.eclipse.equinox.p2.internal.repository.mirroring.Mirroring;\r
-import org.eclipse.equinox.p2.metadata.IArtifactKey;\r
-import org.eclipse.equinox.p2.metadata.IInstallableUnit;\r
-import org.eclipse.equinox.p2.metadata.IVersionedId;\r
-import org.eclipse.equinox.p2.metadata.VersionedId;\r
-import org.eclipse.equinox.p2.query.IQuery;\r
-import org.eclipse.equinox.p2.query.IQueryResult;\r
-import org.eclipse.equinox.p2.query.QueryUtil;\r
-import org.eclipse.equinox.p2.repository.artifact.ArtifactKeyQuery;\r
-import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;\r
-import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;\r
-import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;\r
-import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;\r
-import org.simantics.graph.db.TransferableGraphException;\r
-import org.simantics.graph.query.Graphs;\r
-import org.simantics.graph.query.IGraph;\r
-import org.simantics.graph.query.Paths;\r
-import org.simantics.graph.query.Res;\r
-import org.simantics.graph.query.UriUtils;\r
-import org.simantics.graph.representation.TransferableGraph1;\r
-import org.simantics.scl.reflection.OntologyVersions;\r
-import org.simantics.utils.FileUtils;\r
-\r
-/**\r
- * Bundlepool is a repository of artifacts and installable units. \r
- *\r
- * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
- */\r
-@SuppressWarnings("restriction")\r
-public class BundlePool {\r
-\r
-       /** Application default pool. It is located at "workspace/bundlepool" */\r
-       static BundlePool DEFAULT;      \r
-\r
-       IMetadataRepository metadataRepository; \r
-       IArtifactRepository artifactRepository;\r
-       IProvisioningAgent metadataAgent;\r
-       IProvisioningAgent artifactAgent;\r
-       URI metadataAgentLocation;\r
-       URI artifactAgentLocation;\r
-       \r
-       List<BundleInfo> userInstallables;\r
-\r
-       /** All features that were discovered in the repository */\r
-       Set<FeatureInfo> projectFeatures = new HashSet<FeatureInfo>(); \r
-       \r
-       /**\r
-        * Get SPM Bundle Pool\r
-        * \r
-        * @return SPM Bundle Pool\r
-        */\r
-       public synchronized static BundlePool getDefault() {\r
-               if (DEFAULT == null) {\r
-                       try {\r
-                               IPath path = Platform.getLocation().append("bundlepool");\r
-                               URI metadataRepositoryLocation = path.toFile().toURI();\r
-                               URI artifactRepositoryLocation = path.toFile().toURI();\r
-                               IProvisioningAgent agent = P2Util.createAgent( path );                          \r
-                               DEFAULT = new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent);\r
-                       } catch (ProvisionException e) {\r
-                               // Not expected\r
-                               throw new RuntimeException(e);\r
-                       }\r
-               }\r
-               return DEFAULT;\r
-       }\r
-\r
-       /**\r
-        * Create bundle pool in a directory on a local hard drive.  \r
-        * This location will contain metadata repository, artifact repository and \r
-        * P2 workarea (Agent).\r
-        * \r
-        * @param location\r
-        * @return bundle pool\r
-        */\r
-       public static BundlePool createAt(File location) {              \r
-               try {\r
-                       File canonicalLocation = location.getCanonicalFile();\r
-                       URI metadataRepositoryLocation = canonicalLocation.toURI();\r
-                       URI artifactRepositoryLocation = canonicalLocation.toURI();\r
-                       IPath agentLocation = new Path( canonicalLocation.toString() );\r
-                       IProvisioningAgent agent = P2Util.createAgent( agentLocation );                         \r
-                       return new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent); \r
-               } catch (IOException e) {\r
-                       throw new RuntimeException(e);\r
-               } catch (org.eclipse.equinox.p2.core.ProvisionException e) {\r
-                       throw new RuntimeException(e);\r
-               }\r
-               \r
-       }\r
-       \r
-       public BundlePool(URI metadataRepositoryLocation, URI artifactRepositoryLocation, IProvisioningAgent agent) throws ProvisionException {\r
-               \r
-               this.metadataAgent = agent;\r
-               this.artifactAgent = agent;\r
-               \r
-           SimpleMetadataRepositoryFactory metRepFactory = new SimpleMetadataRepositoryFactory();\r
-           metRepFactory.setAgent( metadataAgent );        \r
-           try {\r
-               metadataRepository = metRepFactory.load(metadataRepositoryLocation, 0, null);\r
-               } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {\r
-                       new File(metadataRepositoryLocation).mkdirs();\r
-               Map<String, String> repoProperties = new HashMap<String, String>();\r
-               metadataRepository = metRepFactory.create(metadataRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);\r
-               }\r
-       \r
-       //          IMetadataRepository metadataRepository = new LocalMetadataRepository( agent, repositoryLocation, "My Metadata Repository", Collections.EMPTY_MAP );\r
-       //          IMetadataRepository metadataRepository = getMetadataRepository(agent, repositoryLocation);\r
-           \r
-//             IArtifactRepository artifactRepository = new SimpleArtifactRepositoryFactory().create(repositoryLocation, "Sample Artifact Repository", ArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, Collections.EMPTY_MAP);\r
-//         SimpleArtifactRepository artifactRepository = new SimpleArtifactRepository( agent, "My Simple Artifact Repository", repositoryLocation, Collections.EMPTY_MAP);\r
-               SimpleArtifactRepositoryFactory artRepFactory = new SimpleArtifactRepositoryFactory();\r
-               artRepFactory.setAgent( artifactAgent );\r
-                       \r
-               try {\r
-                       artifactRepository = artRepFactory.load(artifactRepositoryLocation, 0, null);\r
-               } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {\r
-                       new File(artifactRepositoryLocation).mkdirs();\r
-               Map<String, String> repoProperties = new HashMap<String, String>();\r
-               artifactRepository = artRepFactory.create(artifactRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);\r
-               }\r
-               \r
-               init();\r
-       }\r
-       \r
-       public BundlePool(IMetadataRepository metadataRepository, IArtifactRepository artifactRepository) throws ProvisionException {\r
-               this.metadataRepository = metadataRepository;\r
-               this.artifactRepository = artifactRepository;\r
-               this.metadataAgent = metadataRepository.getProvisioningAgent();\r
-               this.artifactAgent = artifactRepository.getProvisioningAgent();\r
-               init();\r
-       }\r
-       \r
-       /**\r
-        * Init prepares with the following actions:\r
-        *  o loads transferable graphs\r
-        *  o discovers Features and collects Feature URIs, Artifacts and Installable Units\r
-        * \r
-        * @throws ProvisionException \r
-        */\r
-       protected void init() throws ProvisionException {\r
-               try {\r
-                       projectFeatures.clear();\r
-                       \r
-                       ArrayList<TransferableGraph1> platformTGs = new ArrayList<TransferableGraph1>();\r
-                       \r
-                       for (IArtifactKey key : getAllArtifactKeys()) {\r
-                               boolean hasGraph = ProvisioningUtil.hasFile(artifactRepository, key, "graph.tg");\r
-                               if (!hasGraph) continue;\r
-                               \r
-                               // Read Graph\r
-                               TransferableGraph1 graph = ProvisioningUtil.getTransferableGraph(artifactRepository, key);\r
-                               platformTGs.add(graph);\r
-                       }\r
-                       \r
-                       // Create new project, use all features available in Platform\r
-                       final List<String> poolFeatures = new ArrayList<String>();\r
-                       \r
-                       // Convert graph instances\r
-                       GraphBundleEx l0 = getLayer0();\r
-                       String l0v = l0.getMajor() + "." + l0.getMinor();\r
-                       IGraph graph = Graphs.createGraph(new Paths(l0v), platformTGs);\r
-               \r
-                       for(Res feature : graph.getInstances(UriUtils.uriToPath(OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Project-0.0/Feature" )))) {\r
-                               poolFeatures.add( feature.toString() );\r
-                       }                               \r
-               \r
-               } catch (TransferableGraphException e) {\r
-                       throw new ProvisionException("Problem", e);\r
-               } catch (IOException e) {\r
-                       throw new ProvisionException("Failed to read graph.tg", e);\r
-               }\r
-       }\r
-       \r
-       public IMetadataRepository getMetadataRepository() {\r
-               return metadataRepository;\r
-       }\r
-       \r
-       public IArtifactRepository getArtifactRepository() {\r
-               return artifactRepository;\r
-       }\r
-       \r
-       public IArtifactKey[] getAllArtifactKeys() {\r
-               return artifactRepository.query( ArtifactKeyQuery.ALL_KEYS, null ).toArray( IArtifactKey.class );\r
-       }\r
-                       \r
-       public GraphBundleEx getLayer0() throws ProvisionException, TransferableGraphException {\r
-               IVersionedId vid = VersionedId.parse("org.simantics.layer0");\r
-               Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createLatestQuery( QueryUtil.createIUQuery(vid) ), null).toSet();\r
-               if (result.isEmpty()) throw new RuntimeException("Unexpectedly got no IU for "+vid);\r
-               if (result.size()>1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+vid);\r
-               IInstallableUnit iu = result.iterator().next();\r
-               IArtifactKey key = toSingleArtifact(iu);\r
-               TransferableGraph1 graph = getTransferableGraph( key );\r
-               return new GraphBundleEx( "Layer0", graph, key);\r
-       }       \r
-       \r
-       public TransferableGraph1 getTransferableGraph(IArtifactKey artifactKey) throws ProvisionException, TransferableGraphException  {\r
-               return ProvisioningUtil.getTransferableGraph( artifactRepository, artifactKey);\r
-       }\r
-\r
-       \r
-       \r
-       /**\r
-        * Get all features.\r
-        * \r
-        * @param a list of features\r
-        * @throws ProvisionException \r
-        */\r
-       public void getFeatures(List<BundleInfo> features) throws ProvisionException {\r
-               IMetadataRepository metadataRepository = getMetadataRepository();\r
-               \r
-               Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
-               for (IInstallableUnit iu : ius) {               \r
-               if (ProvisioningUtil.isGroup(iu)) {\r
-//             if (P2Util.isFeature(iu)) {\r
-                       BundleInfo b = BundleInfo.read(iu);\r
-                       features.add( b );\r
-               }\r
-               }               \r
-       }\r
-       \r
-       /**\r
-        * Get bundle info\r
-        * \r
-        * @param id bundle id\r
-        * @return bundle info\r
-        */\r
-       public BundleInfo getBundleInfo(String id) {\r
-               IQuery<IInstallableUnit> query =  QueryUtil.createLatestQuery( QueryUtil.createIUQuery(id) );\r
-               IQueryResult<IInstallableUnit> queryResult = metadataRepository.query(query, null);\r
-               IInstallableUnit[] array = queryResult.toArray( IInstallableUnit.class );\r
-               for (IInstallableUnit iu : array) {\r
-                       BundleInfo bi = BundleInfo.read(iu);\r
-                       return bi;\r
-               }\r
-               return null;\r
-       }\r
-       \r
-       /**\r
-        * Get the features that visible for the end-user.\r
-        * User installable features are configured in Simantics manifest files\r
-        * (META-INF/SIMANTICS.MF as Simantics-Features-Bundle -property)\r
-        * \r
-        * @param result\r
-        * @throws IOException \r
-        */\r
-//     public void getUserInstallableFeatures(Collection<BundleInfo> result) throws IOException {\r
-//             ArrayList<String> ids = new ArrayList<String>();\r
-//             ProvisioningUtil.getUserInstallables(artifactRepository, ids);\r
-//             for (String id : ids) {\r
-//                     BundleInfo bi = getBundleInfo(id);\r
-//                     if (bi != null) result.add( bi );\r
-//             }\r
-//             // Add workspace bundle\r
-//             BundleInfo bi = getBundleInfo( "org.simantics.workbench.product" );\r
-//             if (bi != null) result.add( bi );\r
-//     }\r
-       \r
-       /**\r
-        * Get all groups.\r
-        * \r
-        * @param a list of features\r
-        * @throws ProvisionException \r
-        */\r
-//     public void getInstallableFeatures(List<BundleInfo> features) throws ProvisionException {\r
-//             IMetadataRepository metadataRepository = getMetadataRepository();\r
-//             \r
-//             Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
-//             for (IInstallableUnit iu : ius) {               \r
-//             if (ProvisioningUtil.isGroup(iu)) {\r
-////           if (P2Util.isFeature(iu)) {\r
-//                     BundleInfo b = BundleInfo.read(iu);\r
-//                     features.add( b );\r
-//             }\r
-//             }               \r
-//     }\r
-\r
-       /**\r
-        * Mirror source locations to the bundle pool.\r
-        * \r
-        * @param monitor\r
-        * @param sourceLocations\r
-        * @return\r
-        * @throws ProvisionException\r
-        */\r
-       public IStatus download(IProgressMonitor monitor, URI[] sourceLocations) throws ProvisionException {\r
-               try {\r
-                       monitor.beginTask("Synchronizing Local repository with web repositories", 10);          \r
-       \r
-                       @SuppressWarnings("unused")\r
-                       IMetadataRepositoryManager metaRepoMgr = (IMetadataRepositoryManager) metadataAgent.getService(IMetadataRepositoryManager.SERVICE_NAME);\r
-                       @SuppressWarnings("unused")\r
-                       IArtifactRepositoryManager artsRepoMgr = (IArtifactRepositoryManager) artifactAgent.getService(IArtifactRepositoryManager.SERVICE_NAME);\r
-       \r
-                       \r
-                       // Create dest repos\r
-                       monitor.setTaskName("Setting up local metadata repository");\r
-                       LocalMetadataRepository dstMetaRepo = (LocalMetadataRepository) metadataRepository;\r
-                       monitor.worked(1);\r
-                       \r
-                       monitor.setTaskName("Setting up local artifact repository");\r
-                       SimpleArtifactRepository dstArtsRepo = (SimpleArtifactRepository) artifactRepository;\r
-                       monitor.worked(1);\r
-                       \r
-                       // Create a source repos\r
-                       monitor.setTaskName("Setting up remote artifact repository");\r
-                       CompositeArtifactRepository srcArtsRepo = CompositeArtifactRepository.createMemoryComposite( metadataAgent /* ? */ );\r
-                       for (URI uri : sourceLocations) srcArtsRepo.addChild(uri);\r
-                       monitor.worked(1);\r
-                       \r
-                       monitor.setTaskName("Setting up remote metadata repository");           \r
-                       CompositeMetadataRepository srcMetaRepo = CompositeMetadataRepository.createMemoryComposite( artifactAgent /* ? */ );\r
-                       for (URI uri : sourceLocations) srcMetaRepo.addChild(uri);\r
-                       monitor.worked(1);\r
-                       \r
-                       monitor.setTaskName("Retrieving Installable Units");\r
-                       @SuppressWarnings("unused")\r
-                       Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(srcMetaRepo);\r
-                       monitor.worked(1);\r
-       \r
-                       monitor.setTaskName("Mirroring Metadata");\r
-                       // Get all IUs from source\r
-                       IQueryResult<IInstallableUnit> allIUs = srcMetaRepo.query(QueryUtil.createIUAnyQuery(), monitor);\r
-                       // Put the IUs to dst\r
-                       dstMetaRepo.addInstallableUnits(allIUs.toUnmodifiableSet());\r
-                       dstMetaRepo.addReferences(srcMetaRepo.getReferences());\r
-                       monitor.worked(1);\r
-                       \r
-                       monitor.setTaskName("Mirroring Artifacts");\r
-                       \r
-                       boolean compare = false;\r
-                       boolean failOnError = true;\r
-                       //boolean raw = true;\r
-                       boolean verbose = false;\r
-                       boolean validate = false;\r
-                       //boolean mirrorReferences = false;\r
-                       String comparatorID = "";\r
-                       \r
-                       Mirroring mirror = new Mirroring(srcArtsRepo, dstArtsRepo, false);\r
-                       mirror.setCompare(compare);\r
-                       mirror.setComparatorId(comparatorID);\r
-       //              mirror.setBaseline(null);\r
-                       mirror.setValidate(validate);\r
-       //              mirror.setCompareExclusions();\r
-       \r
-                       IStatus result = mirror.run(failOnError, verbose);\r
-                       if (result.getException()!=null)\r
-                               throw new ProvisionException(result);\r
-                       \r
-                       init();\r
-                       \r
-                       return result;\r
-               } catch (IllegalArgumentException iae) {\r
-                       return new Status(IStatus.ERROR, "org.simantics.project", iae.getMessage(), iae);\r
-               }\r
-       }\r
-\r
-       public IInstallableUnit toSingleInstallableUnit(IVersionedId id) throws ProvisionException {\r
-               Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createIUQuery(id), null).toSet();\r
-               if (result.size() != 1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+id);         \r
-               return result.iterator().next();                \r
-       }\r
-\r
-       public IArtifactKey toSingleArtifact(IVersionedId id) throws ProvisionException {\r
-               IInstallableUnit iu = toSingleInstallableUnit(id);\r
-               if (iu.getArtifacts().size() != 1) throw new RuntimeException("Unexpectedly got more than one artifact for ");\r
-               return iu.getArtifacts().iterator().next();\r
-       }\r
-\r
-       /**\r
-        * Get file from an artifact.\r
-        * \r
-        * @param key\r
-        * @param filename\r
-        * @return inputstream or <tt>null</tt>. Inputstream must be closed.\r
-        * @throws IOException \r
-        */\r
-       public InputStream getFile(IArtifactKey key, String filename) throws IOException {\r
-               return ProvisioningUtil.getFile(artifactRepository, key, filename);\r
-       }\r
-       \r
-       /**\r
-        * Checks whether there exists a file in an artifact\r
-        * \r
-        * @param key\r
-        * @param filename\r
-        * @return true file exists in the artifact\r
-        * @throws IOException\r
-        */\r
-       public boolean hasFile(IArtifactKey key, String filename) throws IOException {\r
-               return ProvisioningUtil.hasFile(artifactRepository, key, filename);\r
-       }\r
-       \r
-       /**\r
-        * Get META-INF/MANIFEST.MF\r
-        * \r
-        * @param repo artifact repo\r
-        * @param key key to artifact\r
-        * @return manifest or null\r
-        * @throws IOException \r
-        */\r
-       public Manifest getManifest(IArtifactKey key) throws IOException {\r
-               return ProvisioningUtil.getManifest(artifactRepository, key);\r
-       }\r
-       \r
-       /**\r
-        * Get META-INF/SIMANTICS.MF \r
-        * \r
-        * @param repo artifact repo\r
-        * @param key key to artifact\r
-        * @return manifest or null\r
-        * @throws IOException \r
-        */\r
-       public Manifest getSimanticsManifest(IArtifactKey key) throws IOException {\r
-               return ProvisioningUtil.getManifest(artifactRepository, key);\r
-       }\r
-       \r
-       /**\r
-        * Get list of user installable bundles\r
-        * \r
-        * @param result\r
-        * @throws IOException\r
-        */\r
-       public void getUserInstallables(List<BundleInfo> result) throws IOException {\r
-               if (userInstallables==null) {\r
-                       userInstallables = new ArrayList<BundleInfo>();\r
-                       \r
-                       List<String> topLevelBundles = new ArrayList<String>();\r
-                       ProvisioningUtil.getUserInstallables(artifactRepository, topLevelBundles);\r
-                       \r
-                       for (String bundleId : topLevelBundles) {\r
-                               IInstallableUnit ius[] = metadataRepository.query( QueryUtil.createIUQuery(bundleId) , null).toArray( IInstallableUnit.class );\r
-                               for (IInstallableUnit iu : ius) {\r
-                                       userInstallables.add( BundleInfo.read(iu) );\r
-                               }\r
-                       }                       \r
-                       \r
-               }\r
-               result.addAll(userInstallables);\r
-       }\r
-       \r
-       \r
-       \r
-       /////////// \r
-       /////////// GRAPHS\r
-       ///////////\r
-       \r
-\r
-       \r
-//     /**\r
-//      * Get graph bundles\r
-//      * \r
-//      * @param list to be populated with graph bundle ids\r
-//      * @throws ProvisionException \r
-//      * @throws IOException \r
-//      */\r
-//     public void getGraphs(List<BundleInfo> graphs) throws ProvisionException, IOException {\r
-//             Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
-//             for (IInstallableUnit iu : ius) {\r
-//             if (isGraph(iu)) {\r
-//                     BundleInfo b = BundleInfo.read(iu);\r
-//                     graphs.add( b );\r
-//             }\r
-//             }               \r
-//     }\r
-\r
-       /**\r
-        * Get graph bundles\r
-        * \r
-        * @param list to be populated with graph bundle ids\r
-        * @throws ProvisionException \r
-        * @throws IOException \r
-        * @throws TransferableGraphException \r
-        */\r
-       public void getGraphBundles(List<GraphBundleEx> graphs) throws ProvisionException, IOException, TransferableGraphException {\r
-               Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
-               for (IInstallableUnit iu : ius) {\r
-                       for (IArtifactKey key : iu.getArtifacts()) {\r
-                       if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;\r
-                       TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );\r
-                       GraphBundleEx b = new GraphBundleEx(iu.getId(), tg, key);\r
-                       graphs.add( b );\r
-                       }\r
-               }               \r
-       }\r
-       \r
-       /**\r
-        * Get a list of all features. Features are uris, they are versionless. \r
-        * @return fetures\r
-        * @throws TransferableGraphException \r
-        * @throws IOException \r
-        */\r
-       public Set<FeatureInfo> getFeatures() throws IOException, TransferableGraphException {\r
-               /*\r
-               HashSet<FeatureInfo> result = new HashSet<FeatureInfo>();\r
-               Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
-               for (IInstallableUnit iu : ius) {\r
-                       for (IArtifactKey key : iu.getArtifacts()) {\r
-                       if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;\r
-                       TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );\r
-                       IGraph g = Graphs.createGraph(tg);\r
-                               \r
-                               Res    PublishedProjectFeatures = UriUtils.uriToPath( ProjectResource.URIs.PublishedProjectFeatures );\r
-                               Path   HasFeature = UriUtils.uriToPath( ProjectResource.URIs.HasFeature );\r
-                               for(Res feature : g.getObjects(PublishedProjectFeatures, HasFeature)) {\r
-                                       String uri = feature.toString();\r
-                                       String name = feature instanceof PathChild ? ((PathChild)feature).name : uri;\r
-                                       \r
-                                       FeatureInfo fi = new FeatureInfo(name, uri, null, null);\r
-                                       result.add( fi );\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               return result;\r
-               */\r
-               return Collections.emptySet();\r
-       }\r
-       \r
-       /**\r
-        * For a set of extact feature ids (id_version), find the set of graph bundles \r
-        * required in the database.\r
-        * \r
-        * @param featureIds feature\r
-        * @return graph bundles\r
-        */\r
-       public Set<IVersionedId> getRequiredGraphBundles(Collection<IVersionedId> featureIds) {\r
-               \r
-               return null;\r
-       }\r
-       \r
-       /**\r
-        * Checks whether artifact is a graph bundle. \r
-        * \r
-        * @param key key to artifact\r
-        * @return <code>true</code> if is a graph bundle\r
-        * @throws IOException \r
-        */\r
-       public boolean isGraphArtifact(IArtifactKey key) throws IOException {\r
-               return ProvisioningUtil.isGraphArtifact(artifactRepository, key);\r
-       }\r
-\r
-       /**\r
-        * Checks wheter iu is graph\r
-        *  \r
-        * @param iu\r
-        * @return\r
-        * @throws IOException\r
-        */\r
-       public boolean isGraph(IInstallableUnit iu) throws IOException {\r
-               return ProvisioningUtil.isGraph(iu, metadataRepository, artifactRepository);\r
-       }\r
-\r
-       /**\r
-        * Delete default repositories located in /P2 folder\r
-        * @throws IOException\r
-        */\r
-       public void delete() throws IOException {\r
-               File location1 = new File(metadataAgentLocation);\r
-               File location2 = new File(artifactAgentLocation);\r
-               File location3 = new File(metadataRepository.getLocation());\r
-               File location4 = new File(artifactRepository.getLocation());\r
-               FileUtils.deleteDir(location1);\r
-               FileUtils.deleteDir(location2);\r
-               FileUtils.deleteDir(location3);\r
-               FileUtils.deleteDir(location4);\r
-               location1.mkdirs();\r
-               location2.mkdirs();\r
-               location3.mkdirs();\r
-               location4.mkdirs();\r
-       }       \r
-       \r
-       \r
-}\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland 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:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.project.management;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.jar.Manifest;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository;
+import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository;
+import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepositoryFactory;
+import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository;
+import org.eclipse.equinox.internal.p2.metadata.repository.LocalMetadataRepository;
+import org.eclipse.equinox.internal.p2.metadata.repository.SimpleMetadataRepositoryFactory;
+import org.eclipse.equinox.p2.core.IProvisioningAgent;
+import org.eclipse.equinox.p2.core.ProvisionException;
+import org.eclipse.equinox.p2.internal.repository.mirroring.Mirroring;
+import org.eclipse.equinox.p2.metadata.IArtifactKey;
+import org.eclipse.equinox.p2.metadata.IInstallableUnit;
+import org.eclipse.equinox.p2.metadata.IVersionedId;
+import org.eclipse.equinox.p2.metadata.VersionedId;
+import org.eclipse.equinox.p2.query.IQuery;
+import org.eclipse.equinox.p2.query.IQueryResult;
+import org.eclipse.equinox.p2.query.QueryUtil;
+import org.eclipse.equinox.p2.repository.artifact.ArtifactKeyQuery;
+import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
+import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
+import org.simantics.graph.db.TransferableGraphException;
+import org.simantics.graph.query.Graphs;
+import org.simantics.graph.query.IGraph;
+import org.simantics.graph.query.Paths;
+import org.simantics.graph.query.Res;
+import org.simantics.graph.query.UriUtils;
+import org.simantics.graph.representation.TransferableGraph1;
+import org.simantics.scl.reflection.OntologyVersions;
+import org.simantics.utils.FileUtils;
+
+/**
+ * Bundlepool is a repository of artifacts and installable units. 
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+@SuppressWarnings("restriction")
+public class BundlePool {
+
+       /** Application default pool. It is located at "workspace/bundlepool" */
+       static BundlePool DEFAULT;      
+
+       IMetadataRepository metadataRepository; 
+       IArtifactRepository artifactRepository;
+       IProvisioningAgent metadataAgent;
+       IProvisioningAgent artifactAgent;
+       URI metadataAgentLocation;
+       URI artifactAgentLocation;
+       
+       List<BundleInfo> userInstallables;
+
+       /** All features that were discovered in the repository */
+       Set<FeatureInfo> projectFeatures = new HashSet<FeatureInfo>(); 
+       
+       /**
+        * Get SPM Bundle Pool
+        * 
+        * @return SPM Bundle Pool
+        */
+       public synchronized static BundlePool getDefault() {
+               if (DEFAULT == null) {
+                       try {
+                               IPath path = Platform.getLocation().append("bundlepool");
+                               URI metadataRepositoryLocation = path.toFile().toURI();
+                               URI artifactRepositoryLocation = path.toFile().toURI();
+                               IProvisioningAgent agent = P2Util.createAgent( path );                          
+                               DEFAULT = new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent);
+                       } catch (ProvisionException e) {
+                               // Not expected
+                               throw new RuntimeException(e);
+                       }
+               }
+               return DEFAULT;
+       }
+
+       /**
+        * Create bundle pool in a directory on a local hard drive.  
+        * This location will contain metadata repository, artifact repository and 
+        * P2 workarea (Agent).
+        * 
+        * @param location
+        * @return bundle pool
+        */
+       public static BundlePool createAt(File location) {              
+               try {
+                       File canonicalLocation = location.getCanonicalFile();
+                       URI metadataRepositoryLocation = canonicalLocation.toURI();
+                       URI artifactRepositoryLocation = canonicalLocation.toURI();
+                       IPath agentLocation = new Path( canonicalLocation.toString() );
+                       IProvisioningAgent agent = P2Util.createAgent( agentLocation );                         
+                       return new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent); 
+               } catch (IOException e) {
+                       throw new RuntimeException(e);
+               } catch (org.eclipse.equinox.p2.core.ProvisionException e) {
+                       throw new RuntimeException(e);
+               }
+               
+       }
+       
+       public BundlePool(URI metadataRepositoryLocation, URI artifactRepositoryLocation, IProvisioningAgent agent) throws ProvisionException {
+               
+               this.metadataAgent = agent;
+               this.artifactAgent = agent;
+               
+           SimpleMetadataRepositoryFactory metRepFactory = new SimpleMetadataRepositoryFactory();
+           metRepFactory.setAgent( metadataAgent );        
+           try {
+               metadataRepository = metRepFactory.load(metadataRepositoryLocation, 0, null);
+               } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {
+                       new File(metadataRepositoryLocation).mkdirs();
+               Map<String, String> repoProperties = new HashMap<String, String>();
+               metadataRepository = metRepFactory.create(metadataRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);
+               }
+       
+       //          IMetadataRepository metadataRepository = new LocalMetadataRepository( agent, repositoryLocation, "My Metadata Repository", Collections.EMPTY_MAP );
+       //          IMetadataRepository metadataRepository = getMetadataRepository(agent, repositoryLocation);
+           
+//             IArtifactRepository artifactRepository = new SimpleArtifactRepositoryFactory().create(repositoryLocation, "Sample Artifact Repository", ArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, Collections.EMPTY_MAP);
+//         SimpleArtifactRepository artifactRepository = new SimpleArtifactRepository( agent, "My Simple Artifact Repository", repositoryLocation, Collections.EMPTY_MAP);
+               SimpleArtifactRepositoryFactory artRepFactory = new SimpleArtifactRepositoryFactory();
+               artRepFactory.setAgent( artifactAgent );
+                       
+               try {
+                       artifactRepository = artRepFactory.load(artifactRepositoryLocation, 0, null);
+               } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {
+                       new File(artifactRepositoryLocation).mkdirs();
+               Map<String, String> repoProperties = new HashMap<String, String>();
+               artifactRepository = artRepFactory.create(artifactRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);
+               }
+               
+               init();
+       }
+       
+       public BundlePool(IMetadataRepository metadataRepository, IArtifactRepository artifactRepository) throws ProvisionException {
+               this.metadataRepository = metadataRepository;
+               this.artifactRepository = artifactRepository;
+               this.metadataAgent = metadataRepository.getProvisioningAgent();
+               this.artifactAgent = artifactRepository.getProvisioningAgent();
+               init();
+       }
+       
+       /**
+        * Init prepares with the following actions:
+        *  o loads transferable graphs
+        *  o discovers Features and collects Feature URIs, Artifacts and Installable Units
+        * 
+        * @throws ProvisionException 
+        */
+       protected void init() throws ProvisionException {
+               try {
+                       projectFeatures.clear();
+                       
+                       ArrayList<TransferableGraph1> platformTGs = new ArrayList<TransferableGraph1>();
+                       
+                       for (IArtifactKey key : getAllArtifactKeys()) {
+                               boolean hasGraph = ProvisioningUtil.hasFile(artifactRepository, key, "graph.tg");
+                               if (!hasGraph) continue;
+                               
+                               // Read Graph
+                               TransferableGraph1 graph = ProvisioningUtil.getTransferableGraph(artifactRepository, key);
+                               platformTGs.add(graph);
+                       }
+                       
+                       // Create new project, use all features available in Platform
+                       final List<String> poolFeatures = new ArrayList<String>();
+                       
+                       // Convert graph instances
+                       GraphBundleEx l0 = getLayer0();
+                       String l0v = l0.getMajor() + "." + l0.getMinor();
+                       IGraph graph = Graphs.createGraph(new Paths(l0v), platformTGs);
+               
+                       for(Res feature : graph.getInstances(UriUtils.uriToPath(OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Project-0.0/Feature" )))) {
+                               poolFeatures.add( feature.toString() );
+                       }                               
+               
+               } catch (TransferableGraphException e) {
+                       throw new ProvisionException("Problem", e);
+               } catch (IOException e) {
+                       throw new ProvisionException("Failed to read graph.tg", e);
+               }
+       }
+       
+       public IMetadataRepository getMetadataRepository() {
+               return metadataRepository;
+       }
+       
+       public IArtifactRepository getArtifactRepository() {
+               return artifactRepository;
+       }
+       
+       public IArtifactKey[] getAllArtifactKeys() {
+               return artifactRepository.query( ArtifactKeyQuery.ALL_KEYS, null ).toArray( IArtifactKey.class );
+       }
+                       
+       public GraphBundleEx getLayer0() throws ProvisionException, TransferableGraphException {
+               IVersionedId vid = VersionedId.parse("org.simantics.layer0");
+               Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createLatestQuery( QueryUtil.createIUQuery(vid) ), null).toSet();
+               if (result.isEmpty()) throw new RuntimeException("Unexpectedly got no IU for "+vid);
+               if (result.size()>1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+vid);
+               IInstallableUnit iu = result.iterator().next();
+               IArtifactKey key = toSingleArtifact(iu);
+               TransferableGraph1 graph = getTransferableGraph( key );
+               return new GraphBundleEx( "Layer0", graph, key);
+       }       
+       
+       public TransferableGraph1 getTransferableGraph(IArtifactKey artifactKey) throws ProvisionException, TransferableGraphException  {
+               return ProvisioningUtil.getTransferableGraph( artifactRepository, artifactKey);
+       }
+
+       
+       
+       /**
+        * Get all features.
+        * 
+        * @param a list of features
+        * @throws ProvisionException 
+        */
+       public void getFeatures(List<BundleInfo> features) throws ProvisionException {
+               IMetadataRepository metadataRepository = getMetadataRepository();
+               
+               Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); 
+               for (IInstallableUnit iu : ius) {               
+               if (ProvisioningUtil.isGroup(iu)) {
+//             if (P2Util.isFeature(iu)) {
+                       BundleInfo b = BundleInfo.read(iu);
+                       features.add( b );
+               }
+               }               
+       }
+       
+       /**
+        * Get bundle info
+        * 
+        * @param id bundle id
+        * @return bundle info
+        */
+       public BundleInfo getBundleInfo(String id) {
+               IQuery<IInstallableUnit> query =  QueryUtil.createLatestQuery( QueryUtil.createIUQuery(id) );
+               IQueryResult<IInstallableUnit> queryResult = metadataRepository.query(query, null);
+               IInstallableUnit[] array = queryResult.toArray( IInstallableUnit.class );
+               for (IInstallableUnit iu : array) {
+                       BundleInfo bi = BundleInfo.read(iu);
+                       return bi;
+               }
+               return null;
+       }
+       
+       /**
+        * Get the features that visible for the end-user.
+        * User installable features are configured in Simantics manifest files
+        * (META-INF/SIMANTICS.MF as Simantics-Features-Bundle -property)
+        * 
+        * @param result
+        * @throws IOException 
+        */
+//     public void getUserInstallableFeatures(Collection<BundleInfo> result) throws IOException {
+//             ArrayList<String> ids = new ArrayList<String>();
+//             ProvisioningUtil.getUserInstallables(artifactRepository, ids);
+//             for (String id : ids) {
+//                     BundleInfo bi = getBundleInfo(id);
+//                     if (bi != null) result.add( bi );
+//             }
+//             // Add workspace bundle
+//             BundleInfo bi = getBundleInfo( "org.simantics.workbench.product" );
+//             if (bi != null) result.add( bi );
+//     }
+       
+       /**
+        * Get all groups.
+        * 
+        * @param a list of features
+        * @throws ProvisionException 
+        */
+//     public void getInstallableFeatures(List<BundleInfo> features) throws ProvisionException {
+//             IMetadataRepository metadataRepository = getMetadataRepository();
+//             
+//             Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); 
+//             for (IInstallableUnit iu : ius) {               
+//             if (ProvisioningUtil.isGroup(iu)) {
+////           if (P2Util.isFeature(iu)) {
+//                     BundleInfo b = BundleInfo.read(iu);
+//                     features.add( b );
+//             }
+//             }               
+//     }
+
+       /**
+        * Mirror source locations to the bundle pool.
+        * 
+        * @param monitor
+        * @param sourceLocations
+        * @return
+        * @throws ProvisionException
+        */
+       public IStatus download(IProgressMonitor monitor, URI[] sourceLocations) throws ProvisionException {
+               try {
+                       monitor.beginTask("Synchronizing Local repository with web repositories", 10);          
+       
+                       @SuppressWarnings("unused")
+                       IMetadataRepositoryManager metaRepoMgr = (IMetadataRepositoryManager) metadataAgent.getService(IMetadataRepositoryManager.SERVICE_NAME);
+                       @SuppressWarnings("unused")
+                       IArtifactRepositoryManager artsRepoMgr = (IArtifactRepositoryManager) artifactAgent.getService(IArtifactRepositoryManager.SERVICE_NAME);
+       
+                       
+                       // Create dest repos
+                       monitor.setTaskName("Setting up local metadata repository");
+                       LocalMetadataRepository dstMetaRepo = (LocalMetadataRepository) metadataRepository;
+                       monitor.worked(1);
+                       
+                       monitor.setTaskName("Setting up local artifact repository");
+                       SimpleArtifactRepository dstArtsRepo = (SimpleArtifactRepository) artifactRepository;
+                       monitor.worked(1);
+                       
+                       // Create a source repos
+                       monitor.setTaskName("Setting up remote artifact repository");
+                       CompositeArtifactRepository srcArtsRepo = CompositeArtifactRepository.createMemoryComposite( metadataAgent /* ? */ );
+                       for (URI uri : sourceLocations) srcArtsRepo.addChild(uri);
+                       monitor.worked(1);
+                       
+                       monitor.setTaskName("Setting up remote metadata repository");           
+                       CompositeMetadataRepository srcMetaRepo = CompositeMetadataRepository.createMemoryComposite( artifactAgent /* ? */ );
+                       for (URI uri : sourceLocations) srcMetaRepo.addChild(uri);
+                       monitor.worked(1);
+                       
+                       monitor.setTaskName("Retrieving Installable Units");
+                       @SuppressWarnings("unused")
+                       Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(srcMetaRepo);
+                       monitor.worked(1);
+       
+                       monitor.setTaskName("Mirroring Metadata");
+                       // Get all IUs from source
+                       IQueryResult<IInstallableUnit> allIUs = srcMetaRepo.query(QueryUtil.createIUAnyQuery(), monitor);
+                       // Put the IUs to dst
+                       dstMetaRepo.addInstallableUnits(allIUs.toUnmodifiableSet());
+                       dstMetaRepo.addReferences(srcMetaRepo.getReferences());
+                       monitor.worked(1);
+                       
+                       monitor.setTaskName("Mirroring Artifacts");
+                       
+                       boolean compare = false;
+                       boolean failOnError = true;
+                       //boolean raw = true;
+                       boolean verbose = false;
+                       boolean validate = false;
+                       //boolean mirrorReferences = false;
+                       String comparatorID = "";
+                       
+                       Mirroring mirror = new Mirroring(srcArtsRepo, dstArtsRepo, false);
+                       mirror.setCompare(compare);
+                       mirror.setComparatorId(comparatorID);
+       //              mirror.setBaseline(null);
+                       mirror.setValidate(validate);
+       //              mirror.setCompareExclusions();
+       
+                       IStatus result = mirror.run(failOnError, verbose);
+                       if (result.getException()!=null)
+                               throw new ProvisionException(result);
+                       
+                       init();
+                       
+                       return result;
+               } catch (IllegalArgumentException iae) {
+                       return new Status(IStatus.ERROR, "org.simantics.project", iae.getMessage(), iae);
+               }
+       }
+
+       public IInstallableUnit toSingleInstallableUnit(IVersionedId id) throws ProvisionException {
+               Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createIUQuery(id), null).toSet();
+               if (result.size() != 1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+id);         
+               return result.iterator().next();                
+       }
+
+       public IArtifactKey toSingleArtifact(IVersionedId id) throws ProvisionException {
+               IInstallableUnit iu = toSingleInstallableUnit(id);
+               if (iu.getArtifacts().size() != 1) throw new RuntimeException("Unexpectedly got more than one artifact for ");
+               return iu.getArtifacts().iterator().next();
+       }
+
+       /**
+        * Get file from an artifact.
+        * 
+        * @param key
+        * @param filename
+        * @return inputstream or <tt>null</tt>. Inputstream must be closed.
+        * @throws IOException 
+        */
+       public InputStream getFile(IArtifactKey key, String filename) throws IOException {
+               return ProvisioningUtil.getFile(artifactRepository, key, filename);
+       }
+       
+       /**
+        * Checks whether there exists a file in an artifact
+        * 
+        * @param key
+        * @param filename
+        * @return true file exists in the artifact
+        * @throws IOException
+        */
+       public boolean hasFile(IArtifactKey key, String filename) throws IOException {
+               return ProvisioningUtil.hasFile(artifactRepository, key, filename);
+       }
+       
+       /**
+        * Get META-INF/MANIFEST.MF
+        * 
+        * @param repo artifact repo
+        * @param key key to artifact
+        * @return manifest or null
+        * @throws IOException 
+        */
+       public Manifest getManifest(IArtifactKey key) throws IOException {
+               return ProvisioningUtil.getManifest(artifactRepository, key);
+       }
+       
+       /**
+        * Get META-INF/SIMANTICS.MF 
+        * 
+        * @param repo artifact repo
+        * @param key key to artifact
+        * @return manifest or null
+        * @throws IOException 
+        */
+       public Manifest getSimanticsManifest(IArtifactKey key) throws IOException {
+               return ProvisioningUtil.getManifest(artifactRepository, key);
+       }
+       
+       /**
+        * Get list of user installable bundles
+        * 
+        * @param result
+        * @throws IOException
+        */
+       public void getUserInstallables(List<BundleInfo> result) throws IOException {
+               if (userInstallables==null) {
+                       userInstallables = new ArrayList<BundleInfo>();
+                       
+                       List<String> topLevelBundles = new ArrayList<String>();
+                       ProvisioningUtil.getUserInstallables(artifactRepository, topLevelBundles);
+                       
+                       for (String bundleId : topLevelBundles) {
+                               IInstallableUnit ius[] = metadataRepository.query( QueryUtil.createIUQuery(bundleId) , null).toArray( IInstallableUnit.class );
+                               for (IInstallableUnit iu : ius) {
+                                       userInstallables.add( BundleInfo.read(iu) );
+                               }
+                       }                       
+                       
+               }
+               result.addAll(userInstallables);
+       }
+       
+       
+       
+       /////////// 
+       /////////// GRAPHS
+       ///////////
+       
+
+       
+//     /**
+//      * Get graph bundles
+//      * 
+//      * @param list to be populated with graph bundle ids
+//      * @throws ProvisionException 
+//      * @throws IOException 
+//      */
+//     public void getGraphs(List<BundleInfo> graphs) throws ProvisionException, IOException {
+//             Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); 
+//             for (IInstallableUnit iu : ius) {
+//             if (isGraph(iu)) {
+//                     BundleInfo b = BundleInfo.read(iu);
+//                     graphs.add( b );
+//             }
+//             }               
+//     }
+
+       /**
+        * Get graph bundles
+        * 
+        * @param list to be populated with graph bundle ids
+        * @throws ProvisionException 
+        * @throws IOException 
+        * @throws TransferableGraphException 
+        */
+       public void getGraphBundles(List<GraphBundleEx> graphs) throws ProvisionException, IOException, TransferableGraphException {
+               Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); 
+               for (IInstallableUnit iu : ius) {
+                       for (IArtifactKey key : iu.getArtifacts()) {
+                       if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;
+                       TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );
+                       GraphBundleEx b = new GraphBundleEx(iu.getId(), tg, key);
+                       graphs.add( b );
+                       }
+               }               
+       }
+       
+       /**
+        * Get a list of all features. Features are uris, they are versionless. 
+        * @return fetures
+        * @throws TransferableGraphException 
+        * @throws IOException 
+        */
+       public Set<FeatureInfo> getFeatures() throws IOException, TransferableGraphException {
+               /*
+               HashSet<FeatureInfo> result = new HashSet<FeatureInfo>();
+               Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); 
+               for (IInstallableUnit iu : ius) {
+                       for (IArtifactKey key : iu.getArtifacts()) {
+                       if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;
+                       TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );
+                       IGraph g = Graphs.createGraph(tg);
+                               
+                               Res    PublishedProjectFeatures = UriUtils.uriToPath( ProjectResource.URIs.PublishedProjectFeatures );
+                               Path   HasFeature = UriUtils.uriToPath( ProjectResource.URIs.HasFeature );
+                               for(Res feature : g.getObjects(PublishedProjectFeatures, HasFeature)) {
+                                       String uri = feature.toString();
+                                       String name = feature instanceof PathChild ? ((PathChild)feature).name : uri;
+                                       
+                                       FeatureInfo fi = new FeatureInfo(name, uri, null, null);
+                                       result.add( fi );
+                               }
+                       }
+               }
+               
+               return result;
+               */
+               return Collections.emptySet();
+       }
+       
+       /**
+        * For a set of extact feature ids (id_version), find the set of graph bundles 
+        * required in the database.
+        * 
+        * @param featureIds feature
+        * @return graph bundles
+        */
+       public Set<IVersionedId> getRequiredGraphBundles(Collection<IVersionedId> featureIds) {
+               
+               return null;
+       }
+       
+       /**
+        * Checks whether artifact is a graph bundle. 
+        * 
+        * @param key key to artifact
+        * @return <code>true</code> if is a graph bundle
+        * @throws IOException 
+        */
+       public boolean isGraphArtifact(IArtifactKey key) throws IOException {
+               return ProvisioningUtil.isGraphArtifact(artifactRepository, key);
+       }
+
+       /**
+        * Checks wheter iu is graph
+        *  
+        * @param iu
+        * @return
+        * @throws IOException
+        */
+       public boolean isGraph(IInstallableUnit iu) throws IOException {
+               return ProvisioningUtil.isGraph(iu, metadataRepository, artifactRepository);
+       }
+
+       /**
+        * Delete default repositories located in /P2 folder
+        * @throws IOException
+        */
+       public void delete() throws IOException {
+               File location1 = new File(metadataAgentLocation);
+               File location2 = new File(artifactAgentLocation);
+               File location3 = new File(metadataRepository.getLocation());
+               File location4 = new File(artifactRepository.getLocation());
+               FileUtils.deleteDir(location1);
+               FileUtils.deleteDir(location2);
+               FileUtils.deleteDir(location3);
+               FileUtils.deleteDir(location4);
+               location1.mkdirs();
+               location2.mkdirs();
+               location3.mkdirs();
+               location4.mkdirs();
+       }       
+       
+       
+}
+