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
.maven
;
19 import java
.util
.HashSet
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.Repository
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.Session
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.argeo
.jcr
.JcrUtils
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.argeo
.slc
.jcr
.SlcNames
;
32 import org
.argeo
.slc
.jcr
.SlcTypes
;
33 import org
.argeo
.slc
.repo
.RepoConstants
;
34 import org
.sonatype
.aether
.artifact
.Artifact
;
36 /** Create a distribution node from a set of artifacts */
37 public class IndexDistribution
implements Runnable
{
38 private final static Log log
= LogFactory
.getLog(IndexDistribution
.class);
39 private Repository repository
;
40 private String workspace
;
42 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
43 private String distributionsBasePath
= RepoConstants
.DISTRIBUTIONS_BASE_PATH
;
44 private String distributionName
;
48 Set
<Artifact
> artifacts
= new HashSet
<Artifact
>();
51 Session session
= null;
53 session
= repository
.login(workspace
);
54 syncDistribution(session
, artifacts
);
55 } catch (Exception e
) {
56 throw new SlcException("Cannot import distribution", e
);
58 JcrUtils
.logoutQuietly(session
);
62 protected void syncDistribution(Session jcrSession
, Set
<Artifact
> artifacts
) {
63 Long begin
= System
.currentTimeMillis();
65 JcrUtils
.mkdirs(jcrSession
, distributionsBasePath
+ '/'
67 artifacts
: for (Artifact artifact
: artifacts
) {
68 File file
= artifact
.getFile();
70 file
= MavenConventionsUtils
.artifactToFile(artifact
);
72 log
.warn("Generated file " + file
+ " for " + artifact
79 String parentPath
= artifactBasePath
80 + (artifactBasePath
.endsWith("/") ?
"" : "/")
81 + artifactParentPath(artifact
);
82 Node parentNode
= jcrSession
.getNode(parentPath
);
83 Node fileNode
= parentNode
.getNode(file
.getName());
85 if (fileNode
.hasProperty(SlcNames
.SLC_SYMBOLIC_NAME
)) {
86 String distPath
= bundleDistributionPath(fileNode
);
87 if (!jcrSession
.itemExists(distPath
)
89 .isNodeType(SlcTypes
.SLC_BUNDLE_ARTIFACT
))
90 jcrSession
.getWorkspace().clone(
91 jcrSession
.getWorkspace().getName(),
92 fileNode
.getPath(), distPath
, false);
93 if (log
.isDebugEnabled())
94 log
.debug("Indexed " + fileNode
);
96 } catch (Exception e
) {
97 log
.error("Could not index " + artifact
, e
);
98 jcrSession
.refresh(false);
103 Long duration
= (System
.currentTimeMillis() - begin
) / 1000;
104 if (log
.isDebugEnabled())
105 log
.debug("Indexed distribution in " + duration
+ "s");
106 } catch (Exception e
) {
107 throw new SlcException("Cannot synchronize distribution", e
);
111 private String
artifactParentPath(Artifact artifact
) {
112 return artifact
.getGroupId().replace('.', '/') + '/'
113 + artifact
.getArtifactId() + '/' + artifact
.getVersion();
116 private String
bundleDistributionPath(Node fileNode
) {
118 return distributionsBasePath
122 + fileNode
.getProperty(SlcNames
.SLC_SYMBOLIC_NAME
)
125 + fileNode
.getProperty(SlcNames
.SLC_BUNDLE_VERSION
)
127 } catch (RepositoryException e
) {
128 throw new SlcException("Cannot create distribution path for "
133 public void setDistributionName(String distributionName
) {
134 this.distributionName
= distributionName
;
137 public void setRepository(Repository repository
) {
138 this.repository
= repository
;
141 public void setWorkspace(String workspace
) {
142 this.workspace
= workspace
;