2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.repo
;
18 import java
.io
.ByteArrayOutputStream
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.io
.OutputStream
;
24 import java
.util
.Enumeration
;
25 import java
.util
.Iterator
;
27 import java
.util
.StringTokenizer
;
28 import java
.util
.TreeSet
;
29 import java
.util
.jar
.Attributes
;
30 import java
.util
.jar
.JarEntry
;
31 import java
.util
.jar
.JarFile
;
32 import java
.util
.jar
.JarInputStream
;
33 import java
.util
.jar
.JarOutputStream
;
34 import java
.util
.jar
.Manifest
;
35 import java
.util
.zip
.ZipInputStream
;
37 import javax
.jcr
.Credentials
;
38 import javax
.jcr
.GuestCredentials
;
39 import javax
.jcr
.Node
;
40 import javax
.jcr
.NodeIterator
;
41 import javax
.jcr
.Property
;
42 import javax
.jcr
.PropertyIterator
;
43 import javax
.jcr
.Repository
;
44 import javax
.jcr
.RepositoryException
;
45 import javax
.jcr
.RepositoryFactory
;
46 import javax
.jcr
.Session
;
47 import javax
.jcr
.SimpleCredentials
;
48 import javax
.jcr
.nodetype
.NodeType
;
50 import org
.apache
.commons
.io
.FilenameUtils
;
51 import org
.apache
.commons
.io
.IOUtils
;
52 import org
.apache
.commons
.logging
.Log
;
53 import org
.apache
.commons
.logging
.LogFactory
;
54 import org
.argeo
.ArgeoMonitor
;
55 import org
.argeo
.jcr
.ArgeoJcrUtils
;
56 import org
.argeo
.jcr
.ArgeoNames
;
57 import org
.argeo
.jcr
.ArgeoTypes
;
58 import org
.argeo
.jcr
.JcrUtils
;
59 import org
.argeo
.slc
.DefaultNameVersion
;
60 import org
.argeo
.slc
.NameVersion
;
61 import org
.argeo
.slc
.SlcException
;
62 import org
.argeo
.slc
.aether
.ArtifactIdComparator
;
63 import org
.argeo
.slc
.jcr
.SlcNames
;
64 import org
.argeo
.slc
.jcr
.SlcTypes
;
65 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
66 import org
.argeo
.util
.security
.Keyring
;
67 import org
.osgi
.framework
.Constants
;
68 import org
.sonatype
.aether
.artifact
.Artifact
;
69 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
71 /** Utilities around repo */
72 public class RepoUtils
implements ArgeoNames
, SlcNames
{
73 private final static Log log
= LogFactory
.getLog(RepoUtils
.class);
75 /** Packages a regular sources jar as PDE source. */
76 public static void packagesAsPdeSource(File sourceFile
,
77 NameVersion nameVersion
, OutputStream out
) throws IOException
{
78 if (isAlreadyPdeSource(sourceFile
)) {
79 FileInputStream in
= new FileInputStream(sourceFile
);
80 IOUtils
.copy(in
, out
);
81 IOUtils
.closeQuietly(in
);
83 String sourceSymbolicName
= nameVersion
.getName() + ".source";
85 Manifest sourceManifest
= null;
86 sourceManifest
= new Manifest();
87 sourceManifest
.getMainAttributes().put(
88 Attributes
.Name
.MANIFEST_VERSION
, "1.0");
89 sourceManifest
.getMainAttributes().putValue("Bundle-SymbolicName",
91 sourceManifest
.getMainAttributes().putValue("Bundle-Version",
92 nameVersion
.getVersion());
93 sourceManifest
.getMainAttributes().putValue(
94 "Eclipse-SourceBundle",
95 nameVersion
.getName() + ";version="
96 + nameVersion
.getVersion());
97 copyJar(sourceFile
, out
, sourceManifest
);
101 public static byte[] packageAsPdeSource(InputStream sourceJar
,
102 NameVersion nameVersion
) {
103 String sourceSymbolicName
= nameVersion
.getName() + ".source";
105 Manifest sourceManifest
= null;
106 sourceManifest
= new Manifest();
107 sourceManifest
.getMainAttributes().put(
108 Attributes
.Name
.MANIFEST_VERSION
, "1.0");
109 sourceManifest
.getMainAttributes().putValue("Bundle-SymbolicName",
111 sourceManifest
.getMainAttributes().putValue("Bundle-Version",
112 nameVersion
.getVersion());
113 sourceManifest
.getMainAttributes().putValue("Eclipse-SourceBundle",
114 nameVersion
.getName() + ";version=" + nameVersion
.getVersion());
116 return modifyManifest(sourceJar
, sourceManifest
);
120 * Check whether the file as already been packaged as PDE source, in order
121 * not to mess with Jar signing
123 private static boolean isAlreadyPdeSource(File sourceFile
) {
124 JarInputStream jarInputStream
= null;
127 jarInputStream
= new JarInputStream(new FileInputStream(sourceFile
));
129 Manifest manifest
= jarInputStream
.getManifest();
130 Iterator
<?
> it
= manifest
.getMainAttributes().keySet().iterator();
132 // containsKey() does not work, iterating...
134 if (it
.next().toString().equals("Eclipse-SourceBundle")) {
138 // boolean res = manifest.getMainAttributes().get(
139 // "Eclipse-SourceBundle") != null;
141 log
.info(sourceFile
+ " is already a PDE source");
143 } catch (Exception e
) {
144 // probably not a jar, skipping
145 if (log
.isDebugEnabled())
146 log
.debug("Skipping " + sourceFile
+ " because of "
150 IOUtils
.closeQuietly(jarInputStream
);
155 * Copy a jar, replacing its manifest with the provided one
160 private static void copyJar(File source
, OutputStream out
, Manifest manifest
)
162 JarFile sourceJar
= null;
163 JarOutputStream output
= null;
165 output
= manifest
!= null ?
new JarOutputStream(out
, manifest
)
166 : new JarOutputStream(out
);
167 sourceJar
= new JarFile(source
);
169 entries
: for (Enumeration
<?
> entries
= sourceJar
.entries(); entries
170 .hasMoreElements();) {
171 JarEntry entry
= (JarEntry
) entries
.nextElement();
173 && entry
.getName().equals("META-INF/MANIFEST.MF"))
176 InputStream entryStream
= sourceJar
.getInputStream(entry
);
177 JarEntry newEntry
= new JarEntry(entry
.getName());
178 // newEntry.setMethod(JarEntry.DEFLATED);
179 output
.putNextEntry(newEntry
);
180 IOUtils
.copy(entryStream
, output
);
183 IOUtils
.closeQuietly(output
);
185 if (sourceJar
!= null)
187 } catch (IOException e
) {
193 /** Copy a jar changing onlythe manifest */
194 public static void copyJar(InputStream in
, OutputStream out
,
196 JarInputStream jarIn
= null;
197 JarOutputStream jarOut
= null;
199 jarIn
= new JarInputStream(in
);
200 jarOut
= new JarOutputStream(out
, manifest
);
201 JarEntry jarEntry
= null;
202 while ((jarEntry
= jarIn
.getNextJarEntry()) != null) {
203 if (!jarEntry
.getName().equals("META-INF/MANIFEST.MF")) {
204 JarEntry newJarEntry
= new JarEntry(jarEntry
.getName());
205 jarOut
.putNextEntry(newJarEntry
);
206 IOUtils
.copy(jarIn
, jarOut
);
211 } catch (IOException e
) {
212 throw new SlcException("Could not copy jar with MANIFEST "
213 + manifest
.getMainAttributes(), e
);
215 if (!(in
instanceof ZipInputStream
))
216 IOUtils
.closeQuietly(jarIn
);
217 IOUtils
.closeQuietly(jarOut
);
221 /** Reads a jar file, modify its manifest */
222 public static byte[] modifyManifest(InputStream in
, Manifest manifest
) {
223 ByteArrayOutputStream out
= new ByteArrayOutputStream(200 * 1024);
225 copyJar(in
, out
, manifest
);
226 return out
.toByteArray();
228 IOUtils
.closeQuietly(out
);
232 /** Read the OSGi {@link NameVersion} */
233 public static NameVersion
readNameVersion(Artifact artifact
) {
234 File artifactFile
= artifact
.getFile();
235 if (artifact
.getExtension().equals("pom")) {
236 // hack to process jars which weirdly appear as POMs
237 File jarFile
= new File(artifactFile
.getParentFile(),
238 FilenameUtils
.getBaseName(artifactFile
.getPath()) + ".jar");
239 if (jarFile
.exists()) {
240 log
.warn("Use " + jarFile
+ " instead of " + artifactFile
241 + " for " + artifact
);
242 artifactFile
= jarFile
;
245 return readNameVersion(artifactFile
);
248 /** Read the OSGi {@link NameVersion} */
249 public static NameVersion
readNameVersion(File artifactFile
) {
251 return readNameVersion(new FileInputStream(artifactFile
));
252 } catch (Exception e
) {
253 // probably not a jar, skipping
254 if (log
.isDebugEnabled()) {
255 log
.debug("Skipping " + artifactFile
+ " because of " + e
);
256 // e.printStackTrace();
262 /** Read the OSGi {@link NameVersion} */
263 public static NameVersion
readNameVersion(InputStream in
) {
264 JarInputStream jarInputStream
= null;
266 jarInputStream
= new JarInputStream(in
);
267 return readNameVersion(jarInputStream
.getManifest());
268 } catch (Exception e
) {
269 // probably not a jar, skipping
270 if (log
.isDebugEnabled()) {
271 log
.debug("Skipping because of " + e
);
275 IOUtils
.closeQuietly(jarInputStream
);
280 /** Read the OSGi {@link NameVersion} */
281 public static NameVersion
readNameVersion(Manifest manifest
) {
282 DefaultNameVersion nameVersion
= new DefaultNameVersion();
283 nameVersion
.setName(manifest
.getMainAttributes().getValue(
284 Constants
.BUNDLE_SYMBOLICNAME
));
286 // Skip additional specs such as
288 if (nameVersion
.getName().indexOf(';') > -1) {
290 .setName(new StringTokenizer(nameVersion
.getName(), " ;")
294 nameVersion
.setVersion(manifest
.getMainAttributes().getValue(
295 Constants
.BUNDLE_VERSION
));
303 /** The artifact described by this node */
304 public static Artifact
asArtifact(Node node
) throws RepositoryException
{
305 if (node
.isNodeType(SlcTypes
.SLC_ARTIFACT_VERSION_BASE
)) {
306 // FIXME update data model to store packaging at this level
307 String extension
= "jar";
308 return new DefaultArtifact(node
.getProperty(SLC_GROUP_ID
)
310 node
.getProperty(SLC_ARTIFACT_ID
).getString(), extension
,
311 node
.getProperty(SLC_ARTIFACT_VERSION
).getString());
312 } else if (node
.isNodeType(SlcTypes
.SLC_ARTIFACT
)) {
313 return new DefaultArtifact(node
.getProperty(SLC_GROUP_ID
)
315 node
.getProperty(SLC_ARTIFACT_ID
).getString(), node
316 .getProperty(SLC_ARTIFACT_CLASSIFIER
).getString(),
317 node
.getProperty(SLC_ARTIFACT_EXTENSION
).getString(), node
318 .getProperty(SLC_ARTIFACT_VERSION
).getString());
320 throw new SlcException("Unsupported node type for " + node
);
325 * The path to the PDE source related to this artifact (or artifact version
326 * base). There may or there may not be a node at this location (the
327 * returned path will typically be used to test whether PDE sources are
328 * attached to this artifact).
330 public static String
relatedPdeSourcePath(String artifactBasePath
,
331 Node artifactNode
) throws RepositoryException
{
332 Artifact artifact
= asArtifact(artifactNode
);
333 Artifact pdeSourceArtifact
= new DefaultArtifact(artifact
.getGroupId(),
334 artifact
.getArtifactId() + ".source", artifact
.getExtension(),
335 artifact
.getVersion());
336 return MavenConventionsUtils
.artifactPath(artifactBasePath
,
341 * Copy this bytes array as an artifact, relative to the root of the
342 * repository (typically the workspace root node)
344 public static Node
copyBytesAsArtifact(Node artifactsBase
,
345 Artifact artifact
, byte[] bytes
) throws RepositoryException
{
346 String parentPath
= MavenConventionsUtils
.artifactParentPath(
347 artifactsBase
.getPath(), artifact
);
348 Node folderNode
= JcrUtils
.mkfolders(artifactsBase
.getSession(),
350 return JcrUtils
.copyBytesAsFile(folderNode
,
351 MavenConventionsUtils
.artifactFileName(artifact
), bytes
);
354 private RepoUtils() {
357 /** If a source return the base bundle name, does not change otherwise */
358 public static String
extractBundleNameFromSourceName(String sourceBundleName
) {
359 if (sourceBundleName
.endsWith(".source"))
360 return sourceBundleName
.substring(0, sourceBundleName
.length()
361 - ".source".length());
363 return sourceBundleName
;
367 * SOFTWARE REPOSITORIES
370 /** Retrieve repository based on information in the repo node */
371 public static Repository
getRepository(RepositoryFactory repositoryFactory
,
372 Keyring keyring
, Node repoNode
) {
374 Repository repository
;
375 if (repoNode
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
)) {
376 String uri
= repoNode
.getProperty(ARGEO_URI
).getString();
377 if (uri
.startsWith("http")) {// http, https
378 repository
= ArgeoJcrUtils
.getRepositoryByUri(
379 repositoryFactory
, uri
);
380 } else if (uri
.startsWith("vm:")) {// alias
381 repository
= ArgeoJcrUtils
.getRepositoryByUri(
382 repositoryFactory
, uri
);
384 throw new SlcException("Unsupported repository uri " + uri
);
388 throw new SlcException("Unsupported node type " + repoNode
);
390 } catch (RepositoryException e
) {
391 throw new SlcException("Cannot connect to repository " + repoNode
,
397 * Reads credentials from node, using keyring if there is a password. Can
398 * return null if no credentials needed (local repo) at all, but returns
399 * {@link GuestCredentials} if user id is 'anonymous' .
401 public static Credentials
getRepositoryCredentials(Keyring keyring
,
404 if (repoNode
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
)) {
405 if (!repoNode
.hasProperty(ARGEO_USER_ID
))
408 String userId
= repoNode
.getProperty(ARGEO_USER_ID
).getString();
409 if (userId
.equals("anonymous"))// FIXME hardcoded userId
410 return new GuestCredentials();
411 char[] password
= keyring
.getAsChars(repoNode
.getPath() + '/'
413 Credentials credentials
= new SimpleCredentials(userId
,
417 throw new SlcException("Unsupported node type " + repoNode
);
419 } catch (RepositoryException e
) {
420 throw new SlcException("Cannot connect to repository " + repoNode
,
426 * Shortcut to retrieve a session given variable information: Handle the
427 * case where we only have an URI of the repository, that we want to connect
428 * as anonymous or the case of a identified connection to a local or remote
431 * Callers must close the session once it has been used
433 public static Session
getRemoteSession(RepositoryFactory repositoryFactory
,
434 Keyring keyring
, Node repoNode
, String uri
, String workspaceName
) {
436 if (repoNode
== null && uri
== null)
437 throw new SlcException(
438 "At least one of repoNode and uri must be defined");
439 Repository currRepo
= null;
440 Credentials credentials
= null;
441 // Anonymous URI only workspace
442 if (repoNode
== null)
444 currRepo
= ArgeoJcrUtils
.getRepositoryByUri(repositoryFactory
,
447 currRepo
= RepoUtils
.getRepository(repositoryFactory
, keyring
,
449 credentials
= RepoUtils
.getRepositoryCredentials(keyring
,
452 return currRepo
.login(credentials
, workspaceName
);
453 } catch (RepositoryException e
) {
454 throw new SlcException("Cannot connect to workspace "
455 + workspaceName
+ " of repository " + repoNode
456 + " with URI " + uri
, e
);
461 * Shortcut to retrieve a session on a remote Jrc Repository from
462 * information stored in a local argeo node or from an URI: Handle the case
463 * where we only have an URI of the repository, that we want to connect as
464 * anonymous or the case of a identified connection to a local or remote
467 * Callers must close the session once it has been used
469 public static Session
getRemoteSession(RepositoryFactory repositoryFactory
,
470 Keyring keyring
, Repository localRepository
, String repoNodePath
,
471 String uri
, String workspaceName
) {
472 Session localSession
= null;
473 Node repoNode
= null;
475 localSession
= localRepository
.login();
476 if (repoNodePath
!= null && localSession
.nodeExists(repoNodePath
))
477 repoNode
= localSession
.getNode(repoNodePath
);
479 return RepoUtils
.getRemoteSession(repositoryFactory
, keyring
,
480 repoNode
, uri
, workspaceName
);
481 } catch (RepositoryException e
) {
482 throw new SlcException("Cannot log to workspace " + workspaceName
483 + " for repo defined in " + repoNodePath
, e
);
485 JcrUtils
.logoutQuietly(localSession
);
490 * Write group indexes: 'binaries' lists all bundles and their versions,
491 * 'sources' list their sources, and 'sdk' aggregates both.
493 public static void writeGroupIndexes(Session session
,
494 String artifactBasePath
, String groupId
, String version
,
495 Set
<Artifact
> binaries
, Set
<Artifact
> sources
) {
497 Set
<Artifact
> indexes
= new TreeSet
<Artifact
>(
498 new ArtifactIdComparator());
499 Artifact binariesArtifact
= writeIndex(session
, artifactBasePath
,
500 groupId
, RepoConstants
.BINARIES_ARTIFACT_ID
, version
,
502 indexes
.add(binariesArtifact
);
503 if (sources
!= null) {
504 Artifact sourcesArtifact
= writeIndex(session
,
505 artifactBasePath
, groupId
,
506 RepoConstants
.SOURCES_ARTIFACT_ID
, version
, sources
);
507 indexes
.add(sourcesArtifact
);
510 writeIndex(session
, artifactBasePath
, groupId
,
511 RepoConstants
.SDK_ARTIFACT_ID
, version
, indexes
);
513 } catch (RepositoryException e
) {
514 throw new SlcException("Cannot write indexes for group " + groupId
,
519 /** Write a group index. */
520 private static Artifact
writeIndex(Session session
,
521 String artifactBasePath
, String groupId
, String artifactId
,
522 String version
, Set
<Artifact
> artifacts
) throws RepositoryException
{
523 Artifact artifact
= new DefaultArtifact(groupId
, artifactId
, "pom",
525 String pom
= MavenConventionsUtils
.artifactsAsDependencyPom(artifact
,
527 Node node
= RepoUtils
.copyBytesAsArtifact(
528 session
.getNode(artifactBasePath
), artifact
, pom
.getBytes());
529 addMavenChecksums(node
);
533 /** Add files containing the SHA-1 and MD5 checksums. */
534 public static void addMavenChecksums(Node node
) throws RepositoryException
{
536 String sha
= JcrUtils
.checksumFile(node
, "SHA-1");
537 JcrUtils
.copyBytesAsFile(node
.getParent(), node
.getName() + ".sha1",
539 String md5
= JcrUtils
.checksumFile(node
, "MD5");
540 JcrUtils
.copyBytesAsFile(node
.getParent(), node
.getName() + ".md5",
545 * Custom copy since the one in commons does not fit the needs when copying
546 * a workspace completely.
548 public static void copy(Node fromNode
, Node toNode
) {
549 copy(fromNode
, toNode
, null);
552 public static void copy(Node fromNode
, Node toNode
, ArgeoMonitor monitor
) {
554 String fromPath
= fromNode
.getPath();
556 monitor
.subTask("copying node :" + fromPath
);
557 if (log
.isDebugEnabled())
558 log
.debug("copy node :" + fromPath
);
560 // FIXME : small hack to enable specific workspace copy
561 if (fromNode
.isNodeType("rep:ACL")
562 || fromNode
.isNodeType("rep:system")) {
563 if (log
.isTraceEnabled())
564 log
.trace("node " + fromNode
+ " skipped");
569 for (NodeType mixinType
: fromNode
.getMixinNodeTypes()) {
570 toNode
.addMixin(mixinType
.getName());
574 for (NodeType mixinType
: toNode
.getMixinNodeTypes()) {
575 if (log
.isDebugEnabled())
576 log
.debug(mixinType
.getName());
579 // process properties
580 PropertyIterator pit
= fromNode
.getProperties();
581 properties
: while (pit
.hasNext()) {
582 Property fromProperty
= pit
.nextProperty();
583 String propName
= fromProperty
.getName();
585 String propertyName
= fromProperty
.getName();
586 if (toNode
.hasProperty(propertyName
)
587 && toNode
.getProperty(propertyName
).getDefinition()
591 if (fromProperty
.getDefinition().isProtected())
594 if (propertyName
.equals("jcr:created")
595 || propertyName
.equals("jcr:createdBy")
596 || propertyName
.equals("jcr:lastModified")
597 || propertyName
.equals("jcr:lastModifiedBy"))
600 if (fromProperty
.isMultiple()) {
601 toNode
.setProperty(propertyName
,
602 fromProperty
.getValues());
604 toNode
.setProperty(propertyName
,
605 fromProperty
.getValue());
607 } catch (RepositoryException e
) {
608 throw new SlcException("Cannot property " + propName
, e
);
612 // recursively process children nodes
613 NodeIterator nit
= fromNode
.getNodes();
614 while (nit
.hasNext()) {
615 Node fromChild
= nit
.nextNode();
616 Integer index
= fromChild
.getIndex();
617 String nodeRelPath
= fromChild
.getName() + "[" + index
+ "]";
619 if (toNode
.hasNode(nodeRelPath
))
620 toChild
= toNode
.getNode(nodeRelPath
);
622 toChild
= toNode
.addNode(fromChild
.getName(), fromChild
623 .getPrimaryNodeType().getName());
624 copy(fromChild
, toChild
);
627 // update jcr:lastModified and jcr:lastModifiedBy in toNode in
630 if (!toNode
.getDefinition().isProtected()
631 && toNode
.isNodeType(NodeType
.MIX_LAST_MODIFIED
))
632 JcrUtils
.updateLastModified(toNode
);
634 // Workaround to reduce session size: artifact is a saveable
636 if (toNode
.isNodeType(SlcTypes
.SLC_ARTIFACT
))
637 toNode
.getSession().save();
642 } catch (RepositoryException e
) {
643 throw new SlcException("Cannot copy " + fromNode
+ " to " + toNode
,