]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/BundlePool.java
Merge commit '728147df5d63a3333daff3d8c0e9bfd4f5597e3a'
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / BundlePool.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.project.management;\r
12 \r
13 import java.io.File;\r
14 import java.io.IOException;\r
15 import java.io.InputStream;\r
16 import java.net.URI;\r
17 import java.util.ArrayList;\r
18 import java.util.Collection;\r
19 import java.util.Collections;\r
20 import java.util.HashMap;\r
21 import java.util.HashSet;\r
22 import java.util.List;\r
23 import java.util.Map;\r
24 import java.util.Set;\r
25 import java.util.jar.Manifest;\r
26 \r
27 import org.eclipse.core.runtime.IPath;\r
28 import org.eclipse.core.runtime.IProgressMonitor;\r
29 import org.eclipse.core.runtime.IStatus;\r
30 import org.eclipse.core.runtime.Path;\r
31 import org.eclipse.core.runtime.Platform;\r
32 import org.eclipse.core.runtime.Status;\r
33 import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository;\r
34 import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository;\r
35 import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepositoryFactory;\r
36 import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository;\r
37 import org.eclipse.equinox.internal.p2.metadata.repository.LocalMetadataRepository;\r
38 import org.eclipse.equinox.internal.p2.metadata.repository.SimpleMetadataRepositoryFactory;\r
39 import org.eclipse.equinox.p2.core.IProvisioningAgent;\r
40 import org.eclipse.equinox.p2.core.ProvisionException;\r
41 import org.eclipse.equinox.p2.internal.repository.mirroring.Mirroring;\r
42 import org.eclipse.equinox.p2.metadata.IArtifactKey;\r
43 import org.eclipse.equinox.p2.metadata.IInstallableUnit;\r
44 import org.eclipse.equinox.p2.metadata.IVersionedId;\r
45 import org.eclipse.equinox.p2.metadata.VersionedId;\r
46 import org.eclipse.equinox.p2.query.IQuery;\r
47 import org.eclipse.equinox.p2.query.IQueryResult;\r
48 import org.eclipse.equinox.p2.query.QueryUtil;\r
49 import org.eclipse.equinox.p2.repository.artifact.ArtifactKeyQuery;\r
50 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;\r
51 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;\r
52 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;\r
53 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;\r
54 import org.simantics.graph.db.TransferableGraphException;\r
55 import org.simantics.graph.query.Graphs;\r
56 import org.simantics.graph.query.IGraph;\r
57 import org.simantics.graph.query.Paths;\r
58 import org.simantics.graph.query.Res;\r
59 import org.simantics.graph.query.UriUtils;\r
60 import org.simantics.graph.representation.TransferableGraph1;\r
61 import org.simantics.scl.reflection.OntologyVersions;\r
62 import org.simantics.utils.FileUtils;\r
63 \r
64 /**\r
65  * Bundlepool is a repository of artifacts and installable units. \r
66  *\r
67  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
68  */\r
69 @SuppressWarnings("restriction")\r
70 public class BundlePool {\r
71 \r
72         /** Application default pool. It is located at "workspace/bundlepool" */\r
73         static BundlePool DEFAULT;      \r
74 \r
75         IMetadataRepository metadataRepository; \r
76         IArtifactRepository artifactRepository;\r
77         IProvisioningAgent metadataAgent;\r
78         IProvisioningAgent artifactAgent;\r
79         URI metadataAgentLocation;\r
80         URI artifactAgentLocation;\r
81         \r
82         List<BundleInfo> userInstallables;\r
83 \r
84         /** All features that were discovered in the repository */\r
85         Set<FeatureInfo> projectFeatures = new HashSet<FeatureInfo>(); \r
86         \r
87         /**\r
88          * Get SPM Bundle Pool\r
89          * \r
90          * @return SPM Bundle Pool\r
91          */\r
92         public synchronized static BundlePool getDefault() {\r
93                 if (DEFAULT == null) {\r
94                         try {\r
95                                 IPath path = Platform.getLocation().append("bundlepool");\r
96                                 URI metadataRepositoryLocation = path.toFile().toURI();\r
97                                 URI artifactRepositoryLocation = path.toFile().toURI();\r
98                                 IProvisioningAgent agent = P2Util.createAgent( path );                          \r
99                                 DEFAULT = new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent);\r
100                         } catch (ProvisionException e) {\r
101                                 // Not expected\r
102                                 throw new RuntimeException(e);\r
103                         }\r
104                 }\r
105                 return DEFAULT;\r
106         }\r
107 \r
108         /**\r
109          * Create bundle pool in a directory on a local hard drive.  \r
110          * This location will contain metadata repository, artifact repository and \r
111          * P2 workarea (Agent).\r
112          * \r
113          * @param location\r
114          * @return bundle pool\r
115          */\r
116         public static BundlePool createAt(File location) {              \r
117                 try {\r
118                         File canonicalLocation = location.getCanonicalFile();\r
119                         URI metadataRepositoryLocation = canonicalLocation.toURI();\r
120                         URI artifactRepositoryLocation = canonicalLocation.toURI();\r
121                         IPath agentLocation = new Path( canonicalLocation.toString() );\r
122                         IProvisioningAgent agent = P2Util.createAgent( agentLocation );                         \r
123                         return new BundlePool(metadataRepositoryLocation, artifactRepositoryLocation, agent); \r
124                 } catch (IOException e) {\r
125                         throw new RuntimeException(e);\r
126                 } catch (org.eclipse.equinox.p2.core.ProvisionException e) {\r
127                         throw new RuntimeException(e);\r
128                 }\r
129                 \r
130         }\r
131         \r
132         public BundlePool(URI metadataRepositoryLocation, URI artifactRepositoryLocation, IProvisioningAgent agent) throws ProvisionException {\r
133                 \r
134                 this.metadataAgent = agent;\r
135                 this.artifactAgent = agent;\r
136                 \r
137             SimpleMetadataRepositoryFactory metRepFactory = new SimpleMetadataRepositoryFactory();\r
138             metRepFactory.setAgent( metadataAgent );        \r
139             try {\r
140                 metadataRepository = metRepFactory.load(metadataRepositoryLocation, 0, null);\r
141                 } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {\r
142                         new File(metadataRepositoryLocation).mkdirs();\r
143                 Map<String, String> repoProperties = new HashMap<String, String>();\r
144                 metadataRepository = metRepFactory.create(metadataRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);\r
145                 }\r
146         \r
147         //          IMetadataRepository metadataRepository = new LocalMetadataRepository( agent, repositoryLocation, "My Metadata Repository", Collections.EMPTY_MAP );\r
148         //          IMetadataRepository metadataRepository = getMetadataRepository(agent, repositoryLocation);\r
149             \r
150 //              IArtifactRepository artifactRepository = new SimpleArtifactRepositoryFactory().create(repositoryLocation, "Sample Artifact Repository", ArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, Collections.EMPTY_MAP);\r
151 //          SimpleArtifactRepository artifactRepository = new SimpleArtifactRepository( agent, "My Simple Artifact Repository", repositoryLocation, Collections.EMPTY_MAP);\r
152                 SimpleArtifactRepositoryFactory artRepFactory = new SimpleArtifactRepositoryFactory();\r
153                 artRepFactory.setAgent( artifactAgent );\r
154                         \r
155                 try {\r
156                         artifactRepository = artRepFactory.load(artifactRepositoryLocation, 0, null);\r
157                 } catch (org.eclipse.equinox.p2.core.ProvisionException pe) {\r
158                         new File(artifactRepositoryLocation).mkdirs();\r
159                 Map<String, String> repoProperties = new HashMap<String, String>();\r
160                 artifactRepository = artRepFactory.create(artifactRepositoryLocation, "SPM Bundle Pool", "file", repoProperties);\r
161                 }\r
162                 \r
163                 init();\r
164         }\r
165         \r
166         public BundlePool(IMetadataRepository metadataRepository, IArtifactRepository artifactRepository) throws ProvisionException {\r
167                 this.metadataRepository = metadataRepository;\r
168                 this.artifactRepository = artifactRepository;\r
169                 this.metadataAgent = metadataRepository.getProvisioningAgent();\r
170                 this.artifactAgent = artifactRepository.getProvisioningAgent();\r
171                 init();\r
172         }\r
173         \r
174         /**\r
175          * Init prepares with the following actions:\r
176          *  o loads transferable graphs\r
177          *  o discovers Features and collects Feature URIs, Artifacts and Installable Units\r
178          * \r
179          * @throws ProvisionException \r
180          */\r
181         protected void init() throws ProvisionException {\r
182                 try {\r
183                         projectFeatures.clear();\r
184                         \r
185                         ArrayList<TransferableGraph1> platformTGs = new ArrayList<TransferableGraph1>();\r
186                         \r
187                         for (IArtifactKey key : getAllArtifactKeys()) {\r
188                                 boolean hasGraph = ProvisioningUtil.hasFile(artifactRepository, key, "graph.tg");\r
189                                 if (!hasGraph) continue;\r
190                                 \r
191                                 // Read Graph\r
192                                 TransferableGraph1 graph = ProvisioningUtil.getTransferableGraph(artifactRepository, key);\r
193                                 platformTGs.add(graph);\r
194                         }\r
195                         \r
196                         // Create new project, use all features available in Platform\r
197                         final List<String> poolFeatures = new ArrayList<String>();\r
198                         \r
199                         // Convert graph instances\r
200                         GraphBundleEx l0 = getLayer0();\r
201                         String l0v = l0.getMajor() + "." + l0.getMinor();\r
202                         IGraph graph = Graphs.createGraph(new Paths(l0v), platformTGs);\r
203                 \r
204                         for(Res feature : graph.getInstances(UriUtils.uriToPath(OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Project-0.0/Feature" )))) {\r
205                                 poolFeatures.add( feature.toString() );\r
206                         }                               \r
207                 \r
208                 } catch (TransferableGraphException e) {\r
209                         throw new ProvisionException("Problem", e);\r
210                 } catch (IOException e) {\r
211                         throw new ProvisionException("Failed to read graph.tg", e);\r
212                 }\r
213         }\r
214         \r
215         public IMetadataRepository getMetadataRepository() {\r
216                 return metadataRepository;\r
217         }\r
218         \r
219         public IArtifactRepository getArtifactRepository() {\r
220                 return artifactRepository;\r
221         }\r
222         \r
223         public IArtifactKey[] getAllArtifactKeys() {\r
224                 return artifactRepository.query( ArtifactKeyQuery.ALL_KEYS, null ).toArray( IArtifactKey.class );\r
225         }\r
226                         \r
227         public GraphBundleEx getLayer0() throws ProvisionException, TransferableGraphException {\r
228                 IVersionedId vid = VersionedId.parse("org.simantics.layer0");\r
229                 Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createLatestQuery( QueryUtil.createIUQuery(vid) ), null).toSet();\r
230                 if (result.isEmpty()) throw new RuntimeException("Unexpectedly got no IU for "+vid);\r
231                 if (result.size()>1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+vid);\r
232                 IInstallableUnit iu = result.iterator().next();\r
233                 IArtifactKey key = toSingleArtifact(iu);\r
234                 TransferableGraph1 graph = getTransferableGraph( key );\r
235                 return new GraphBundleEx( "Layer0", graph, key);\r
236         }       \r
237         \r
238         public TransferableGraph1 getTransferableGraph(IArtifactKey artifactKey) throws ProvisionException, TransferableGraphException  {\r
239                 return ProvisioningUtil.getTransferableGraph( artifactRepository, artifactKey);\r
240         }\r
241 \r
242         \r
243         \r
244         /**\r
245          * Get all features.\r
246          * \r
247          * @param a list of features\r
248          * @throws ProvisionException \r
249          */\r
250         public void getFeatures(List<BundleInfo> features) throws ProvisionException {\r
251                 IMetadataRepository metadataRepository = getMetadataRepository();\r
252                 \r
253                 Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
254                 for (IInstallableUnit iu : ius) {               \r
255                 if (ProvisioningUtil.isGroup(iu)) {\r
256 //              if (P2Util.isFeature(iu)) {\r
257                         BundleInfo b = BundleInfo.read(iu);\r
258                         features.add( b );\r
259                 }\r
260                 }               \r
261         }\r
262         \r
263         /**\r
264          * Get bundle info\r
265          * \r
266          * @param id bundle id\r
267          * @return bundle info\r
268          */\r
269         public BundleInfo getBundleInfo(String id) {\r
270                 IQuery<IInstallableUnit> query =  QueryUtil.createLatestQuery( QueryUtil.createIUQuery(id) );\r
271                 IQueryResult<IInstallableUnit> queryResult = metadataRepository.query(query, null);\r
272                 IInstallableUnit[] array = queryResult.toArray( IInstallableUnit.class );\r
273                 for (IInstallableUnit iu : array) {\r
274                         BundleInfo bi = BundleInfo.read(iu);\r
275                         return bi;\r
276                 }\r
277                 return null;\r
278         }\r
279         \r
280         /**\r
281          * Get the features that visible for the end-user.\r
282          * User installable features are configured in Simantics manifest files\r
283          * (META-INF/SIMANTICS.MF as Simantics-Features-Bundle -property)\r
284          * \r
285          * @param result\r
286          * @throws IOException \r
287          */\r
288 //      public void getUserInstallableFeatures(Collection<BundleInfo> result) throws IOException {\r
289 //              ArrayList<String> ids = new ArrayList<String>();\r
290 //              ProvisioningUtil.getUserInstallables(artifactRepository, ids);\r
291 //              for (String id : ids) {\r
292 //                      BundleInfo bi = getBundleInfo(id);\r
293 //                      if (bi != null) result.add( bi );\r
294 //              }\r
295 //              // Add workspace bundle\r
296 //              BundleInfo bi = getBundleInfo( "org.simantics.workbench.product" );\r
297 //              if (bi != null) result.add( bi );\r
298 //      }\r
299         \r
300         /**\r
301          * Get all groups.\r
302          * \r
303          * @param a list of features\r
304          * @throws ProvisionException \r
305          */\r
306 //      public void getInstallableFeatures(List<BundleInfo> features) throws ProvisionException {\r
307 //              IMetadataRepository metadataRepository = getMetadataRepository();\r
308 //              \r
309 //              Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
310 //              for (IInstallableUnit iu : ius) {               \r
311 //              if (ProvisioningUtil.isGroup(iu)) {\r
312 ////            if (P2Util.isFeature(iu)) {\r
313 //                      BundleInfo b = BundleInfo.read(iu);\r
314 //                      features.add( b );\r
315 //              }\r
316 //              }               \r
317 //      }\r
318 \r
319         /**\r
320          * Mirror source locations to the bundle pool.\r
321          * \r
322          * @param monitor\r
323          * @param sourceLocations\r
324          * @return\r
325          * @throws ProvisionException\r
326          */\r
327         public IStatus download(IProgressMonitor monitor, URI[] sourceLocations) throws ProvisionException {\r
328                 try {\r
329                         monitor.beginTask("Synchronizing Local repository with web repositories", 10);          \r
330         \r
331                         @SuppressWarnings("unused")\r
332                         IMetadataRepositoryManager metaRepoMgr = (IMetadataRepositoryManager) metadataAgent.getService(IMetadataRepositoryManager.SERVICE_NAME);\r
333                         @SuppressWarnings("unused")\r
334                         IArtifactRepositoryManager artsRepoMgr = (IArtifactRepositoryManager) artifactAgent.getService(IArtifactRepositoryManager.SERVICE_NAME);\r
335         \r
336                         \r
337                         // Create dest repos\r
338                         monitor.setTaskName("Setting up local metadata repository");\r
339                         LocalMetadataRepository dstMetaRepo = (LocalMetadataRepository) metadataRepository;\r
340                         monitor.worked(1);\r
341                         \r
342                         monitor.setTaskName("Setting up local artifact repository");\r
343                         SimpleArtifactRepository dstArtsRepo = (SimpleArtifactRepository) artifactRepository;\r
344                         monitor.worked(1);\r
345                         \r
346                         // Create a source repos\r
347                         monitor.setTaskName("Setting up remote artifact repository");\r
348                         CompositeArtifactRepository srcArtsRepo = CompositeArtifactRepository.createMemoryComposite( metadataAgent /* ? */ );\r
349                         for (URI uri : sourceLocations) srcArtsRepo.addChild(uri);\r
350                         monitor.worked(1);\r
351                         \r
352                         monitor.setTaskName("Setting up remote metadata repository");           \r
353                         CompositeMetadataRepository srcMetaRepo = CompositeMetadataRepository.createMemoryComposite( artifactAgent /* ? */ );\r
354                         for (URI uri : sourceLocations) srcMetaRepo.addChild(uri);\r
355                         monitor.worked(1);\r
356                         \r
357                         monitor.setTaskName("Retrieving Installable Units");\r
358                         @SuppressWarnings("unused")\r
359                         Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(srcMetaRepo);\r
360                         monitor.worked(1);\r
361         \r
362                         monitor.setTaskName("Mirroring Metadata");\r
363                         // Get all IUs from source\r
364                         IQueryResult<IInstallableUnit> allIUs = srcMetaRepo.query(QueryUtil.createIUAnyQuery(), monitor);\r
365                         // Put the IUs to dst\r
366                         dstMetaRepo.addInstallableUnits(allIUs.toUnmodifiableSet());\r
367                         dstMetaRepo.addReferences(srcMetaRepo.getReferences());\r
368                         monitor.worked(1);\r
369                         \r
370                         monitor.setTaskName("Mirroring Artifacts");\r
371                         \r
372                         boolean compare = false;\r
373                         boolean failOnError = true;\r
374                         //boolean raw = true;\r
375                         boolean verbose = false;\r
376                         boolean validate = false;\r
377                         //boolean mirrorReferences = false;\r
378                         String comparatorID = "";\r
379                         \r
380                         Mirroring mirror = new Mirroring(srcArtsRepo, dstArtsRepo, false);\r
381                         mirror.setCompare(compare);\r
382                         mirror.setComparatorId(comparatorID);\r
383         //              mirror.setBaseline(null);\r
384                         mirror.setValidate(validate);\r
385         //              mirror.setCompareExclusions();\r
386         \r
387                         IStatus result = mirror.run(failOnError, verbose);\r
388                         if (result.getException()!=null)\r
389                                 throw new ProvisionException(result);\r
390                         \r
391                         init();\r
392                         \r
393                         return result;\r
394                 } catch (IllegalArgumentException iae) {\r
395                         return new Status(IStatus.ERROR, "org.simantics.project", iae.getMessage(), iae);\r
396                 }\r
397         }\r
398 \r
399         public IInstallableUnit toSingleInstallableUnit(IVersionedId id) throws ProvisionException {\r
400                 Set<IInstallableUnit> result = metadataRepository.query( QueryUtil.createIUQuery(id), null).toSet();\r
401                 if (result.size() != 1) throw new RuntimeException("Unexpectedly got more than one latest IU for "+id);         \r
402                 return result.iterator().next();                \r
403         }\r
404 \r
405         public IArtifactKey toSingleArtifact(IVersionedId id) throws ProvisionException {\r
406                 IInstallableUnit iu = toSingleInstallableUnit(id);\r
407                 if (iu.getArtifacts().size() != 1) throw new RuntimeException("Unexpectedly got more than one artifact for ");\r
408                 return iu.getArtifacts().iterator().next();\r
409         }\r
410 \r
411         /**\r
412          * Get file from an artifact.\r
413          * \r
414          * @param key\r
415          * @param filename\r
416          * @return inputstream or <tt>null</tt>. Inputstream must be closed.\r
417          * @throws IOException \r
418          */\r
419         public InputStream getFile(IArtifactKey key, String filename) throws IOException {\r
420                 return ProvisioningUtil.getFile(artifactRepository, key, filename);\r
421         }\r
422         \r
423         /**\r
424          * Checks whether there exists a file in an artifact\r
425          * \r
426          * @param key\r
427          * @param filename\r
428          * @return true file exists in the artifact\r
429          * @throws IOException\r
430          */\r
431         public boolean hasFile(IArtifactKey key, String filename) throws IOException {\r
432                 return ProvisioningUtil.hasFile(artifactRepository, key, filename);\r
433         }\r
434         \r
435         /**\r
436          * Get META-INF/MANIFEST.MF\r
437          * \r
438          * @param repo artifact repo\r
439          * @param key key to artifact\r
440          * @return manifest or null\r
441          * @throws IOException \r
442          */\r
443         public Manifest getManifest(IArtifactKey key) throws IOException {\r
444                 return ProvisioningUtil.getManifest(artifactRepository, key);\r
445         }\r
446         \r
447         /**\r
448          * Get META-INF/SIMANTICS.MF \r
449          * \r
450          * @param repo artifact repo\r
451          * @param key key to artifact\r
452          * @return manifest or null\r
453          * @throws IOException \r
454          */\r
455         public Manifest getSimanticsManifest(IArtifactKey key) throws IOException {\r
456                 return ProvisioningUtil.getManifest(artifactRepository, key);\r
457         }\r
458         \r
459         /**\r
460          * Get list of user installable bundles\r
461          * \r
462          * @param result\r
463          * @throws IOException\r
464          */\r
465         public void getUserInstallables(List<BundleInfo> result) throws IOException {\r
466                 if (userInstallables==null) {\r
467                         userInstallables = new ArrayList<BundleInfo>();\r
468                         \r
469                         List<String> topLevelBundles = new ArrayList<String>();\r
470                         ProvisioningUtil.getUserInstallables(artifactRepository, topLevelBundles);\r
471                         \r
472                         for (String bundleId : topLevelBundles) {\r
473                                 IInstallableUnit ius[] = metadataRepository.query( QueryUtil.createIUQuery(bundleId) , null).toArray( IInstallableUnit.class );\r
474                                 for (IInstallableUnit iu : ius) {\r
475                                         userInstallables.add( BundleInfo.read(iu) );\r
476                                 }\r
477                         }                       \r
478                         \r
479                 }\r
480                 result.addAll(userInstallables);\r
481         }\r
482         \r
483         \r
484         \r
485         /////////// \r
486         /////////// GRAPHS\r
487         ///////////\r
488         \r
489 \r
490         \r
491 //      /**\r
492 //       * Get graph bundles\r
493 //       * \r
494 //       * @param list to be populated with graph bundle ids\r
495 //       * @throws ProvisionException \r
496 //       * @throws IOException \r
497 //       */\r
498 //      public void getGraphs(List<BundleInfo> graphs) throws ProvisionException, IOException {\r
499 //              Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
500 //              for (IInstallableUnit iu : ius) {\r
501 //              if (isGraph(iu)) {\r
502 //                      BundleInfo b = BundleInfo.read(iu);\r
503 //                      graphs.add( b );\r
504 //              }\r
505 //              }               \r
506 //      }\r
507 \r
508         /**\r
509          * Get graph bundles\r
510          * \r
511          * @param list to be populated with graph bundle ids\r
512          * @throws ProvisionException \r
513          * @throws IOException \r
514          * @throws TransferableGraphException \r
515          */\r
516         public void getGraphBundles(List<GraphBundleEx> graphs) throws ProvisionException, IOException, TransferableGraphException {\r
517                 Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
518                 for (IInstallableUnit iu : ius) {\r
519                         for (IArtifactKey key : iu.getArtifacts()) {\r
520                         if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;\r
521                         TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );\r
522                         GraphBundleEx b = new GraphBundleEx(iu.getId(), tg, key);\r
523                         graphs.add( b );\r
524                         }\r
525                 }               \r
526         }\r
527         \r
528         /**\r
529          * Get a list of all features. Features are uris, they are versionless. \r
530          * @return fetures\r
531          * @throws TransferableGraphException \r
532          * @throws IOException \r
533          */\r
534         public Set<FeatureInfo> getFeatures() throws IOException, TransferableGraphException {\r
535                 /*\r
536                 HashSet<FeatureInfo> result = new HashSet<FeatureInfo>();\r
537                 Collection<IInstallableUnit> ius = ProvisioningUtil.getInstallableUnits(metadataRepository); \r
538                 for (IInstallableUnit iu : ius) {\r
539                         for (IArtifactKey key : iu.getArtifacts()) {\r
540                         if (!ProvisioningUtil.isGraphArtifact(artifactRepository, key)) continue;\r
541                         TransferableGraph1 tg = ProvisioningUtil.getTransferableGraph( artifactRepository, key );\r
542                         IGraph g = Graphs.createGraph(tg);\r
543                                 \r
544                                 Res    PublishedProjectFeatures = UriUtils.uriToPath( ProjectResource.URIs.PublishedProjectFeatures );\r
545                                 Path   HasFeature = UriUtils.uriToPath( ProjectResource.URIs.HasFeature );\r
546                                 for(Res feature : g.getObjects(PublishedProjectFeatures, HasFeature)) {\r
547                                         String uri = feature.toString();\r
548                                         String name = feature instanceof PathChild ? ((PathChild)feature).name : uri;\r
549                                         \r
550                                         FeatureInfo fi = new FeatureInfo(name, uri, null, null);\r
551                                         result.add( fi );\r
552                                 }\r
553                         }\r
554                 }\r
555                 \r
556                 return result;\r
557                 */\r
558                 return Collections.emptySet();\r
559         }\r
560         \r
561         /**\r
562          * For a set of extact feature ids (id_version), find the set of graph bundles \r
563          * required in the database.\r
564          * \r
565          * @param featureIds feature\r
566          * @return graph bundles\r
567          */\r
568         public Set<IVersionedId> getRequiredGraphBundles(Collection<IVersionedId> featureIds) {\r
569                 \r
570                 return null;\r
571         }\r
572         \r
573         /**\r
574          * Checks whether artifact is a graph bundle. \r
575          * \r
576          * @param key key to artifact\r
577          * @return <code>true</code> if is a graph bundle\r
578          * @throws IOException \r
579          */\r
580         public boolean isGraphArtifact(IArtifactKey key) throws IOException {\r
581                 return ProvisioningUtil.isGraphArtifact(artifactRepository, key);\r
582         }\r
583 \r
584         /**\r
585          * Checks wheter iu is graph\r
586          *  \r
587          * @param iu\r
588          * @return\r
589          * @throws IOException\r
590          */\r
591         public boolean isGraph(IInstallableUnit iu) throws IOException {\r
592                 return ProvisioningUtil.isGraph(iu, metadataRepository, artifactRepository);\r
593         }\r
594 \r
595         /**\r
596          * Delete default repositories located in /P2 folder\r
597          * @throws IOException\r
598          */\r
599         public void delete() throws IOException {\r
600                 File location1 = new File(metadataAgentLocation);\r
601                 File location2 = new File(artifactAgentLocation);\r
602                 File location3 = new File(metadataRepository.getLocation());\r
603                 File location4 = new File(artifactRepository.getLocation());\r
604                 FileUtils.deleteDir(location1);\r
605                 FileUtils.deleteDir(location2);\r
606                 FileUtils.deleteDir(location3);\r
607                 FileUtils.deleteDir(location4);\r
608                 location1.mkdirs();\r
609                 location2.mkdirs();\r
610                 location3.mkdirs();\r
611                 location4.mkdirs();\r
612         }       \r
613         \r
614         \r
615 }\r
616 \r