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
.cms
.ArgeoNames
;
55 import org
.argeo
.cms
.ArgeoTypes
;
56 import org
.argeo
.jcr
.JcrMonitor
;
57 import org
.argeo
.jcr
.JcrUtils
;
58 import org
.argeo
.node
.NodeUtils
;
59 import org
.argeo
.node
.security
.Keyring
;
60 import org
.argeo
.slc
.DefaultNameVersion
;
61 import org
.argeo
.slc
.NameVersion
;
62 import org
.argeo
.slc
.SlcException
;
63 import org
.argeo
.slc
.SlcNames
;
64 import org
.argeo
.slc
.SlcTypes
;
65 import org
.argeo
.slc
.repo
.maven
.ArtifactIdComparator
;
66 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
67 import org
.eclipse
.aether
.artifact
.Artifact
;
68 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
69 import org
.osgi
.framework
.Constants
;
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
);
274 IOUtils
.closeQuietly(jarInputStream
);
279 /** Read the OSGi {@link NameVersion} */
280 public static NameVersion
readNameVersion(Manifest manifest
) {
281 DefaultNameVersion nameVersion
= new DefaultNameVersion();
282 nameVersion
.setName(manifest
.getMainAttributes().getValue(
283 Constants
.BUNDLE_SYMBOLICNAME
));
285 // Skip additional specs such as
287 if (nameVersion
.getName().indexOf(';') > -1) {
289 .setName(new StringTokenizer(nameVersion
.getName(), " ;")
293 nameVersion
.setVersion(manifest
.getMainAttributes().getValue(
294 Constants
.BUNDLE_VERSION
));
302 /** The artifact described by this node */
303 public static Artifact
asArtifact(Node node
) throws RepositoryException
{
304 if (node
.isNodeType(SlcTypes
.SLC_ARTIFACT_VERSION_BASE
)) {
305 // FIXME update data model to store packaging at this level
306 String extension
= "jar";
307 return new DefaultArtifact(node
.getProperty(SLC_GROUP_ID
)
309 node
.getProperty(SLC_ARTIFACT_ID
).getString(), extension
,
310 node
.getProperty(SLC_ARTIFACT_VERSION
).getString());
311 } else if (node
.isNodeType(SlcTypes
.SLC_ARTIFACT
)) {
312 return new DefaultArtifact(node
.getProperty(SLC_GROUP_ID
)
314 node
.getProperty(SLC_ARTIFACT_ID
).getString(), node
315 .getProperty(SLC_ARTIFACT_CLASSIFIER
).getString(),
316 node
.getProperty(SLC_ARTIFACT_EXTENSION
).getString(), node
317 .getProperty(SLC_ARTIFACT_VERSION
).getString());
318 } else if (node
.isNodeType(SlcTypes
.SLC_MODULE_COORDINATES
)) {
319 return new DefaultArtifact(node
.getProperty(SLC_CATEGORY
)
320 .getString(), node
.getProperty(SLC_NAME
).getString(),
321 "jar", node
.getProperty(SLC_VERSION
).getString());
323 throw new SlcException("Unsupported node type for " + node
);
328 * The path to the PDE source related to this artifact (or artifact version
329 * base). There may or there may not be a node at this location (the
330 * returned path will typically be used to test whether PDE sources are
331 * attached to this artifact).
333 public static String
relatedPdeSourcePath(String artifactBasePath
,
334 Node artifactNode
) throws RepositoryException
{
335 Artifact artifact
= asArtifact(artifactNode
);
336 Artifact pdeSourceArtifact
= new DefaultArtifact(artifact
.getGroupId(),
337 artifact
.getArtifactId() + ".source", artifact
.getExtension(),
338 artifact
.getVersion());
339 return MavenConventionsUtils
.artifactPath(artifactBasePath
,
344 * Copy this bytes array as an artifact, relative to the root of the
345 * repository (typically the workspace root node)
347 public static Node
copyBytesAsArtifact(Node artifactsBase
,
348 Artifact artifact
, byte[] bytes
) throws RepositoryException
{
349 String parentPath
= MavenConventionsUtils
.artifactParentPath(
350 artifactsBase
.getPath(), artifact
);
351 Node folderNode
= JcrUtils
.mkfolders(artifactsBase
.getSession(),
353 return JcrUtils
.copyBytesAsFile(folderNode
,
354 MavenConventionsUtils
.artifactFileName(artifact
), bytes
);
357 private RepoUtils() {
360 /** If a source return the base bundle name, does not change otherwise */
361 public static String
extractBundleNameFromSourceName(String sourceBundleName
) {
362 if (sourceBundleName
.endsWith(".source"))
363 return sourceBundleName
.substring(0, sourceBundleName
.length()
364 - ".source".length());
366 return sourceBundleName
;
370 * SOFTWARE REPOSITORIES
373 /** Retrieve repository based on information in the repo node */
374 public static Repository
getRepository(RepositoryFactory repositoryFactory
,
375 Keyring keyring
, Node repoNode
) {
377 Repository repository
;
378 if (repoNode
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
)) {
379 String uri
= repoNode
.getProperty(ARGEO_URI
).getString();
380 if (uri
.startsWith("http")) {// http, https
381 repository
= NodeUtils
.getRepositoryByUri(
382 repositoryFactory
, uri
);
383 } else if (uri
.startsWith("vm:")) {// alias
384 repository
= NodeUtils
.getRepositoryByUri(
385 repositoryFactory
, uri
);
387 throw new SlcException("Unsupported repository uri " + uri
);
391 throw new SlcException("Unsupported node type " + repoNode
);
393 } catch (RepositoryException e
) {
394 throw new SlcException("Cannot connect to repository " + repoNode
,
400 * Reads credentials from node, using keyring if there is a password. Can
401 * return null if no credentials needed (local repo) at all, but returns
402 * {@link GuestCredentials} if user id is 'anonymous' .
404 public static Credentials
getRepositoryCredentials(Keyring keyring
,
407 if (repoNode
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
)) {
408 if (!repoNode
.hasProperty(ARGEO_USER_ID
))
411 String userId
= repoNode
.getProperty(ARGEO_USER_ID
).getString();
412 if (userId
.equals("anonymous"))// FIXME hardcoded userId
413 return new GuestCredentials();
414 char[] password
= keyring
.getAsChars(repoNode
.getPath() + '/'
416 Credentials credentials
= new SimpleCredentials(userId
,
420 throw new SlcException("Unsupported node type " + repoNode
);
422 } catch (RepositoryException e
) {
423 throw new SlcException("Cannot connect to repository " + repoNode
,
429 * Shortcut to retrieve a session given variable information: Handle the
430 * case where we only have an URI of the repository, that we want to connect
431 * as anonymous or the case of a identified connection to a local or remote
434 * Callers must close the session once it has been used
436 public static Session
getRemoteSession(RepositoryFactory repositoryFactory
,
437 Keyring keyring
, Node repoNode
, String uri
, String workspaceName
) {
439 if (repoNode
== null && uri
== null)
440 throw new SlcException(
441 "At least one of repoNode and uri must be defined");
442 Repository currRepo
= null;
443 Credentials credentials
= null;
444 // Anonymous URI only workspace
445 if (repoNode
== null)
447 currRepo
= NodeUtils
.getRepositoryByUri(repositoryFactory
, uri
);
449 currRepo
= RepoUtils
.getRepository(repositoryFactory
, keyring
,
451 credentials
= RepoUtils
.getRepositoryCredentials(keyring
,
454 return currRepo
.login(credentials
, workspaceName
);
455 } catch (RepositoryException e
) {
456 throw new SlcException("Cannot connect to workspace "
457 + workspaceName
+ " of repository " + repoNode
458 + " with URI " + uri
, e
);
463 * Shortcut to retrieve a session on a remote Jrc Repository from
464 * information stored in a local argeo node or from an URI: Handle the case
465 * where we only have an URI of the repository, that we want to connect as
466 * anonymous or the case of a identified connection to a local or remote
469 * Callers must close the session once it has been used
471 public static Session
getRemoteSession(RepositoryFactory repositoryFactory
,
472 Keyring keyring
, Repository localRepository
, String repoNodePath
,
473 String uri
, String workspaceName
) {
474 Session localSession
= null;
475 Node repoNode
= null;
477 localSession
= localRepository
.login();
478 if (repoNodePath
!= null && localSession
.nodeExists(repoNodePath
))
479 repoNode
= localSession
.getNode(repoNodePath
);
481 return RepoUtils
.getRemoteSession(repositoryFactory
, keyring
,
482 repoNode
, uri
, workspaceName
);
483 } catch (RepositoryException e
) {
484 throw new SlcException("Cannot log to workspace " + workspaceName
485 + " for repo defined in " + repoNodePath
, e
);
487 JcrUtils
.logoutQuietly(localSession
);
492 * Write group indexes: 'binaries' lists all bundles and their versions,
493 * 'sources' list their sources, and 'sdk' aggregates both.
495 public static void writeGroupIndexes(Session session
,
496 String artifactBasePath
, String groupId
, String version
,
497 Set
<Artifact
> binaries
, Set
<Artifact
> sources
) {
499 Set
<Artifact
> indexes
= new TreeSet
<Artifact
>(
500 new ArtifactIdComparator());
501 Artifact binariesArtifact
= writeIndex(session
, artifactBasePath
,
502 groupId
, RepoConstants
.BINARIES_ARTIFACT_ID
, version
,
504 indexes
.add(binariesArtifact
);
505 if (sources
!= null) {
506 Artifact sourcesArtifact
= writeIndex(session
,
507 artifactBasePath
, groupId
,
508 RepoConstants
.SOURCES_ARTIFACT_ID
, version
, sources
);
509 indexes
.add(sourcesArtifact
);
512 writeIndex(session
, artifactBasePath
, groupId
,
513 RepoConstants
.SDK_ARTIFACT_ID
, version
, indexes
);
515 } catch (RepositoryException e
) {
516 throw new SlcException("Cannot write indexes for group " + groupId
,
521 /** Write a group index. */
522 private static Artifact
writeIndex(Session session
,
523 String artifactBasePath
, String groupId
, String artifactId
,
524 String version
, Set
<Artifact
> artifacts
) throws RepositoryException
{
525 Artifact artifact
= new DefaultArtifact(groupId
, artifactId
, "pom",
527 String pom
= MavenConventionsUtils
.artifactsAsDependencyPom(artifact
,
529 Node node
= RepoUtils
.copyBytesAsArtifact(
530 session
.getNode(artifactBasePath
), artifact
, pom
.getBytes());
531 addMavenChecksums(node
);
535 /** Add files containing the SHA-1 and MD5 checksums. */
536 public static void addMavenChecksums(Node node
) throws RepositoryException
{
538 String sha
= JcrUtils
.checksumFile(node
, "SHA-1");
539 JcrUtils
.copyBytesAsFile(node
.getParent(), node
.getName() + ".sha1",
541 String md5
= JcrUtils
.checksumFile(node
, "MD5");
542 JcrUtils
.copyBytesAsFile(node
.getParent(), node
.getName() + ".md5",
547 * Custom copy since the one in commons does not fit the needs when copying
548 * a workspace completely.
550 public static void copy(Node fromNode
, Node toNode
) {
551 copy(fromNode
, toNode
, null);
554 public static void copy(Node fromNode
, Node toNode
, JcrMonitor monitor
) {
556 String fromPath
= fromNode
.getPath();
558 monitor
.subTask("copying node :" + fromPath
);
559 if (log
.isDebugEnabled())
560 log
.debug("copy node :" + fromPath
);
562 // FIXME : small hack to enable specific workspace copy
563 if (fromNode
.isNodeType("rep:ACL")
564 || fromNode
.isNodeType("rep:system")) {
565 if (log
.isTraceEnabled())
566 log
.trace("node " + fromNode
+ " skipped");
571 for (NodeType mixinType
: fromNode
.getMixinNodeTypes()) {
572 toNode
.addMixin(mixinType
.getName());
576 for (NodeType mixinType
: toNode
.getMixinNodeTypes()) {
577 if (log
.isDebugEnabled())
578 log
.debug(mixinType
.getName());
581 // process properties
582 PropertyIterator pit
= fromNode
.getProperties();
583 properties
: while (pit
.hasNext()) {
584 Property fromProperty
= pit
.nextProperty();
585 String propName
= fromProperty
.getName();
587 String propertyName
= fromProperty
.getName();
588 if (toNode
.hasProperty(propertyName
)
589 && toNode
.getProperty(propertyName
).getDefinition()
593 if (fromProperty
.getDefinition().isProtected())
596 if (propertyName
.equals("jcr:created")
597 || propertyName
.equals("jcr:createdBy")
598 || propertyName
.equals("jcr:lastModified")
599 || propertyName
.equals("jcr:lastModifiedBy"))
602 if (fromProperty
.isMultiple()) {
603 toNode
.setProperty(propertyName
,
604 fromProperty
.getValues());
606 toNode
.setProperty(propertyName
,
607 fromProperty
.getValue());
609 } catch (RepositoryException e
) {
610 throw new SlcException("Cannot property " + propName
, e
);
614 // recursively process children nodes
615 NodeIterator nit
= fromNode
.getNodes();
616 while (nit
.hasNext()) {
617 Node fromChild
= nit
.nextNode();
618 Integer index
= fromChild
.getIndex();
619 String nodeRelPath
= fromChild
.getName() + "[" + index
+ "]";
621 if (toNode
.hasNode(nodeRelPath
))
622 toChild
= toNode
.getNode(nodeRelPath
);
624 toChild
= toNode
.addNode(fromChild
.getName(), fromChild
625 .getPrimaryNodeType().getName());
626 copy(fromChild
, toChild
);
629 // update jcr:lastModified and jcr:lastModifiedBy in toNode in
632 if (!toNode
.getDefinition().isProtected()
633 && toNode
.isNodeType(NodeType
.MIX_LAST_MODIFIED
))
634 JcrUtils
.updateLastModified(toNode
);
636 // Workaround to reduce session size: artifact is a saveable
638 if (toNode
.isNodeType(SlcTypes
.SLC_ARTIFACT
))
639 toNode
.getSession().save();
644 } catch (RepositoryException e
) {
645 throw new SlcException("Cannot copy " + fromNode
+ " to " + toNode
,