X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.repo%2Fsrc%2Forg%2Fargeo%2Fslc%2Frepo%2Fmaven%2FIndexDistribution.java;fp=org.argeo.slc.repo%2Fsrc%2Forg%2Fargeo%2Fslc%2Frepo%2Fmaven%2FIndexDistribution.java;h=fec27166a513c7251883d813b61b71cbee065ab1;hb=825d60c5348dbe3f5be25b0bccf7bdebfe694219;hp=0000000000000000000000000000000000000000;hpb=5e991fff5cba01858dcc5747a27e637325bc5c8e;p=gpl%2Fargeo-jcr.git diff --git a/org.argeo.slc.repo/src/org/argeo/slc/repo/maven/IndexDistribution.java b/org.argeo.slc.repo/src/org/argeo/slc/repo/maven/IndexDistribution.java new file mode 100644 index 0000000..fec2716 --- /dev/null +++ b/org.argeo.slc.repo/src/org/argeo/slc/repo/maven/IndexDistribution.java @@ -0,0 +1,129 @@ +package org.argeo.slc.repo.maven; + +import java.io.File; +import java.util.HashSet; +import java.util.Set; + +import javax.jcr.Node; +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + +import org.argeo.api.cms.CmsLog; +import org.argeo.jcr.JcrUtils; +import org.argeo.slc.SlcException; +import org.argeo.slc.SlcNames; +import org.argeo.slc.SlcTypes; +import org.argeo.slc.repo.RepoConstants; +import org.eclipse.aether.artifact.Artifact; + +/** Create a distribution node from a set of artifacts */ +public class IndexDistribution implements Runnable { + private final static CmsLog log = CmsLog.getLog(IndexDistribution.class); + private Repository repository; + private String workspace; + + private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH; + private String distributionsBasePath = RepoConstants.DISTRIBUTIONS_BASE_PATH; + private String distributionName; + + public void run() { + // TODO populate + Set artifacts = new HashSet(); + + // sync + Session session = null; + try { + session = repository.login(workspace); + syncDistribution(session, artifacts); + } catch (Exception e) { + throw new SlcException("Cannot import distribution", e); + } finally { + JcrUtils.logoutQuietly(session); + } + } + + protected void syncDistribution(Session jcrSession, Set artifacts) { + Long begin = System.currentTimeMillis(); + try { + JcrUtils.mkdirs(jcrSession, distributionsBasePath + '/' + + distributionName); + artifacts: for (Artifact artifact : artifacts) { + File file = artifact.getFile(); + if (file == null) { + file = MavenConventionsUtils.artifactToFile(artifact); + if (!file.exists()) { + log.warn("Generated file " + file + " for " + artifact + + " does not exist"); + continue artifacts; + } + } + + try { + String parentPath = artifactBasePath + + (artifactBasePath.endsWith("/") ? "" : "/") + + artifactParentPath(artifact); + Node parentNode = jcrSession.getNode(parentPath); + Node fileNode = parentNode.getNode(file.getName()); + + if (fileNode.hasProperty(SlcNames.SLC_SYMBOLIC_NAME)) { + String distPath = bundleDistributionPath(fileNode); + if (!jcrSession.itemExists(distPath) + && fileNode + .isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT)) + jcrSession.getWorkspace().clone( + jcrSession.getWorkspace().getName(), + fileNode.getPath(), distPath, false); + if (log.isDebugEnabled()) + log.debug("Indexed " + fileNode); + } + } catch (Exception e) { + log.error("Could not index " + artifact, e); + jcrSession.refresh(false); + throw e; + } + } + + Long duration = (System.currentTimeMillis() - begin) / 1000; + if (log.isDebugEnabled()) + log.debug("Indexed distribution in " + duration + "s"); + } catch (Exception e) { + throw new SlcException("Cannot synchronize distribution", e); + } + } + + private String artifactParentPath(Artifact artifact) { + return artifact.getGroupId().replace('.', '/') + '/' + + artifact.getArtifactId() + '/' + artifact.getVersion(); + } + + private String bundleDistributionPath(Node fileNode) { + try { + return distributionsBasePath + + '/' + + distributionName + + '/' + + fileNode.getProperty(SlcNames.SLC_SYMBOLIC_NAME) + .getString() + + '_' + + fileNode.getProperty(SlcNames.SLC_BUNDLE_VERSION) + .getString(); + } catch (RepositoryException e) { + throw new SlcException("Cannot create distribution path for " + + fileNode, e); + } + } + + public void setDistributionName(String distributionName) { + this.distributionName = distributionName; + } + + public void setRepository(Repository repository) { + this.repository = repository; + } + + public void setWorkspace(String workspace) { + this.workspace = workspace; + } + +}