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
.argeo
.api
.cms
.CmsLog
;
13 import org
.argeo
.jcr
.JcrUtils
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.SlcNames
;
16 import org
.argeo
.slc
.SlcTypes
;
17 import org
.argeo
.slc
.repo
.RepoConstants
;
18 import org
.eclipse
.aether
.artifact
.Artifact
;
20 /** Create a distribution node from a set of artifacts */
21 public class IndexDistribution
implements Runnable
{
22 private final static CmsLog log
= CmsLog
.getLog(IndexDistribution
.class);
23 private Repository repository
;
24 private String workspace
;
26 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
27 private String distributionsBasePath
= RepoConstants
.DISTRIBUTIONS_BASE_PATH
;
28 private String distributionName
;
32 Set
<Artifact
> artifacts
= new HashSet
<Artifact
>();
35 Session session
= null;
37 session
= repository
.login(workspace
);
38 syncDistribution(session
, artifacts
);
39 } catch (Exception e
) {
40 throw new SlcException("Cannot import distribution", e
);
42 JcrUtils
.logoutQuietly(session
);
46 protected void syncDistribution(Session jcrSession
, Set
<Artifact
> artifacts
) {
47 Long begin
= System
.currentTimeMillis();
49 JcrUtils
.mkdirs(jcrSession
, distributionsBasePath
+ '/'
51 artifacts
: for (Artifact artifact
: artifacts
) {
52 File file
= artifact
.getFile();
54 file
= MavenConventionsUtils
.artifactToFile(artifact
);
56 log
.warn("Generated file " + file
+ " for " + artifact
63 String parentPath
= artifactBasePath
64 + (artifactBasePath
.endsWith("/") ?
"" : "/")
65 + artifactParentPath(artifact
);
66 Node parentNode
= jcrSession
.getNode(parentPath
);
67 Node fileNode
= parentNode
.getNode(file
.getName());
69 if (fileNode
.hasProperty(SlcNames
.SLC_SYMBOLIC_NAME
)) {
70 String distPath
= bundleDistributionPath(fileNode
);
71 if (!jcrSession
.itemExists(distPath
)
73 .isNodeType(SlcTypes
.SLC_BUNDLE_ARTIFACT
))
74 jcrSession
.getWorkspace().clone(
75 jcrSession
.getWorkspace().getName(),
76 fileNode
.getPath(), distPath
, false);
77 if (log
.isDebugEnabled())
78 log
.debug("Indexed " + fileNode
);
80 } catch (Exception e
) {
81 log
.error("Could not index " + artifact
, e
);
82 jcrSession
.refresh(false);
87 Long duration
= (System
.currentTimeMillis() - begin
) / 1000;
88 if (log
.isDebugEnabled())
89 log
.debug("Indexed distribution in " + duration
+ "s");
90 } catch (Exception e
) {
91 throw new SlcException("Cannot synchronize distribution", e
);
95 private String
artifactParentPath(Artifact artifact
) {
96 return artifact
.getGroupId().replace('.', '/') + '/'
97 + artifact
.getArtifactId() + '/' + artifact
.getVersion();
100 private String
bundleDistributionPath(Node fileNode
) {
102 return distributionsBasePath
106 + fileNode
.getProperty(SlcNames
.SLC_SYMBOLIC_NAME
)
109 + fileNode
.getProperty(SlcNames
.SLC_BUNDLE_VERSION
)
111 } catch (RepositoryException e
) {
112 throw new SlcException("Cannot create distribution path for "
117 public void setDistributionName(String distributionName
) {
118 this.distributionName
= distributionName
;
121 public void setRepository(Repository repository
) {
122 this.repository
= repository
;
125 public void setWorkspace(String workspace
) {
126 this.workspace
= workspace
;