1 package org
.argeo
.slc
.repo
.maven
;
4 import java
.util
.HashSet
;
8 import javax
.jcr
.Repository
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.Session
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.jcr
.JcrUtils
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.SlcNames
;
17 import org
.argeo
.slc
.SlcTypes
;
18 import org
.argeo
.slc
.repo
.RepoConstants
;
19 import org
.eclipse
.aether
.artifact
.Artifact
;
21 /** Create a distribution node from a set of artifacts */
22 public class IndexDistribution
implements Runnable
{
23 private final static Log log
= LogFactory
.getLog(IndexDistribution
.class);
24 private Repository repository
;
25 private String workspace
;
27 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
28 private String distributionsBasePath
= RepoConstants
.DISTRIBUTIONS_BASE_PATH
;
29 private String distributionName
;
33 Set
<Artifact
> artifacts
= new HashSet
<Artifact
>();
36 Session session
= null;
38 session
= repository
.login(workspace
);
39 syncDistribution(session
, artifacts
);
40 } catch (Exception e
) {
41 throw new SlcException("Cannot import distribution", e
);
43 JcrUtils
.logoutQuietly(session
);
47 protected void syncDistribution(Session jcrSession
, Set
<Artifact
> artifacts
) {
48 Long begin
= System
.currentTimeMillis();
50 JcrUtils
.mkdirs(jcrSession
, distributionsBasePath
+ '/'
52 artifacts
: for (Artifact artifact
: artifacts
) {
53 File file
= artifact
.getFile();
55 file
= MavenConventionsUtils
.artifactToFile(artifact
);
57 log
.warn("Generated file " + file
+ " for " + artifact
64 String parentPath
= artifactBasePath
65 + (artifactBasePath
.endsWith("/") ?
"" : "/")
66 + artifactParentPath(artifact
);
67 Node parentNode
= jcrSession
.getNode(parentPath
);
68 Node fileNode
= parentNode
.getNode(file
.getName());
70 if (fileNode
.hasProperty(SlcNames
.SLC_SYMBOLIC_NAME
)) {
71 String distPath
= bundleDistributionPath(fileNode
);
72 if (!jcrSession
.itemExists(distPath
)
74 .isNodeType(SlcTypes
.SLC_BUNDLE_ARTIFACT
))
75 jcrSession
.getWorkspace().clone(
76 jcrSession
.getWorkspace().getName(),
77 fileNode
.getPath(), distPath
, false);
78 if (log
.isDebugEnabled())
79 log
.debug("Indexed " + fileNode
);
81 } catch (Exception e
) {
82 log
.error("Could not index " + artifact
, e
);
83 jcrSession
.refresh(false);
88 Long duration
= (System
.currentTimeMillis() - begin
) / 1000;
89 if (log
.isDebugEnabled())
90 log
.debug("Indexed distribution in " + duration
+ "s");
91 } catch (Exception e
) {
92 throw new SlcException("Cannot synchronize distribution", e
);
96 private String
artifactParentPath(Artifact artifact
) {
97 return artifact
.getGroupId().replace('.', '/') + '/'
98 + artifact
.getArtifactId() + '/' + artifact
.getVersion();
101 private String
bundleDistributionPath(Node fileNode
) {
103 return distributionsBasePath
107 + fileNode
.getProperty(SlcNames
.SLC_SYMBOLIC_NAME
)
110 + fileNode
.getProperty(SlcNames
.SLC_BUNDLE_VERSION
)
112 } catch (RepositoryException e
) {
113 throw new SlcException("Cannot create distribution path for "
118 public void setDistributionName(String distributionName
) {
119 this.distributionName
= distributionName
;
122 public void setRepository(Repository repository
) {
123 this.repository
= repository
;
126 public void setWorkspace(String workspace
) {
127 this.workspace
= workspace
;