--- /dev/null
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.slc.repo;
+
+import javax.jcr.Binary;
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.commons.io.FilenameUtils;
+import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.NameVersion;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.aether.AetherUtils;
+import org.argeo.slc.repo.maven.MavenConventionsUtils;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.util.artifact.DefaultArtifact;
+
+/**
+ * Creates pde sources from a source {@link Artifact} with name
+ * "...-sources.jar"
+ */
+public class PdeSourcesIndexer implements NodeIndexer {
+ // private Log log = LogFactory.getLog(PdeSourcesIndexer.class);
+
+ private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
+
+ private ArtifactIndexer artifactIndexer;
+ private JarFileIndexer jarFileIndexer;
+
+ public PdeSourcesIndexer(ArtifactIndexer artifactIndexer,
+ JarFileIndexer jarFileIndexer) {
+ this.artifactIndexer = artifactIndexer;
+ this.jarFileIndexer = jarFileIndexer;
+ }
+
+ public Boolean support(String path) {
+ // TODO implement clean management of same name siblings
+ String name = FilenameUtils.getBaseName(path);
+ // int lastInd = name.lastIndexOf("[");
+ // if (lastInd != -1)
+ // name = name.substring(0, lastInd);
+ return name.endsWith("-sources")
+ && FilenameUtils.getExtension(path).equals("jar");
+ }
+
+ public void index(Node sourcesNode) {
+ try {
+ packageSourcesAsPdeSource(sourcesNode);
+ } catch (Exception e) {
+ throw new SlcException("Cannot generate pde sources for node "
+ + sourcesNode, e);
+ }
+ }
+
+ protected void packageSourcesAsPdeSource(Node sourcesNode) {
+ Binary origBinary = null;
+ Binary osgiBinary = null;
+ try {
+ Session session = sourcesNode.getSession();
+ Artifact sourcesArtifact = AetherUtils.convertPathToArtifact(
+ sourcesNode.getPath(), null);
+
+ // read name version from manifest
+ Artifact osgiArtifact = new DefaultArtifact(
+ sourcesArtifact.getGroupId(),
+ sourcesArtifact.getArtifactId(),
+ sourcesArtifact.getExtension(),
+ sourcesArtifact.getVersion());
+ String osgiPath = MavenConventionsUtils.artifactPath(
+ artifactBasePath, osgiArtifact);
+ osgiBinary = session.getNode(osgiPath).getNode(Node.JCR_CONTENT)
+ .getProperty(Property.JCR_DATA).getBinary();
+
+ NameVersion nameVersion = RepoUtils.readNameVersion(osgiBinary
+ .getStream());
+
+ // create PDe sources artifact
+ Artifact pdeSourceArtifact = new DefaultArtifact(
+ sourcesArtifact.getGroupId(),
+ sourcesArtifact.getArtifactId() + ".source",
+ sourcesArtifact.getExtension(),
+ sourcesArtifact.getVersion());
+ String targetSourceParentPath = MavenConventionsUtils
+ .artifactParentPath(artifactBasePath, pdeSourceArtifact);
+ String targetSourceFileName = MavenConventionsUtils
+ .artifactFileName(pdeSourceArtifact);
+ String targetSourceJarPath = targetSourceParentPath + '/'
+ + targetSourceFileName;
+
+ Node targetSourceParentNode = JcrUtils.mkfolders(session,
+ targetSourceParentPath);
+ origBinary = sourcesNode.getNode(Node.JCR_CONTENT)
+ .getProperty(Property.JCR_DATA).getBinary();
+ byte[] targetJarBytes = RepoUtils.packageAsPdeSource(
+ origBinary.getStream(), nameVersion);
+ JcrUtils.copyBytesAsFile(targetSourceParentNode,
+ targetSourceFileName, targetJarBytes);
+
+ // reindex
+ Node targetSourceJarNode = session.getNode(targetSourceJarPath);
+ artifactIndexer.index(targetSourceJarNode);
+ jarFileIndexer.index(targetSourceJarNode);
+ } catch (RepositoryException e) {
+ throw new SlcException("Cannot add PDE sources for " + sourcesNode,
+ e);
+ } finally {
+ JcrUtils.closeQuietly(origBinary);
+ JcrUtils.closeQuietly(osgiBinary);
+ }
+ }
+}
\ No newline at end of file
package org.argeo.slc.repo;
import java.io.InputStream;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.argeo.slc.SlcException;
import org.xml.sax.SAXException;
-/** Sync to from software repositories */
+/**
+ * Synchronise workspaces from a remote software repository to the local
+ * repository (Synchronisation in the other direction does not work).
+ *
+ * Workspaces are retrieved by name given a map that links the source with a
+ * target name. If a target workspace does not exist, it is created. Otherwise
+ * we copy the content of the source workspace into the target one.
+ */
public class RepoSync implements Runnable {
private final static Log log = LogFactory.getLog(RepoSync.class);
// Centralizes definition of workspaces that must be ignored by the sync.
- private final static List<String> IGNORED_WSKP_LIST = Arrays.asList(
+ private final static List<String> IGNORED_WKSP_LIST = Arrays.asList(
"security", "localrepo");
private final Calendar zero;
private RepositoryFactory repositoryFactory;
private ArgeoMonitor monitor;
- private List<String> sourceWkspList;
+ private Map<String, String> workspaceMap;
// TODO fix monitor
private Boolean filesOnly = false;
if (monitor != null && monitor.isCanceled())
break;
- if (sourceWkspList != null
- && !sourceWkspList.contains(sourceWorkspaceName))
+ if (workspaceMap != null
+ && !workspaceMap.containsKey(sourceWorkspaceName))
continue;
- if (IGNORED_WSKP_LIST.contains(sourceWorkspaceName))
+ if (IGNORED_WKSP_LIST.contains(sourceWorkspaceName))
continue;
Session sourceSession = null;
Session targetSession = null;
+ String targetWorkspaceName = workspaceMap
+ .get(sourceWorkspaceName);
try {
try {
targetSession = targetRepository.login(
- targetCredentials, sourceWorkspaceName);
+ targetCredentials, targetWorkspaceName);
} catch (NoSuchWorkspaceException e) {
targetDefaultSession.getWorkspace().createWorkspace(
- sourceWorkspaceName);
+ targetWorkspaceName);
targetSession = targetRepository.login(
- targetCredentials, sourceWorkspaceName);
+ targetCredentials, targetWorkspaceName);
}
sourceSession = sourceRepository.login(sourceCredentials,
sourceWorkspaceName);
} catch (Exception e) {
errors.put("Could not sync workspace "
+ sourceWorkspaceName, e);
- if (log.isDebugEnabled())
+ if (log.isErrorEnabled())
e.printStackTrace();
+
} finally {
JcrUtils.logoutQuietly(sourceSession);
JcrUtils.logoutQuietly(targetSession);
}
private long getNodesNumber(Session session) {
- if (IGNORED_WSKP_LIST.contains(session.getWorkspace().getName()))
+ if (IGNORED_WKSP_LIST.contains(session.getWorkspace().getName()))
return 0l;
- Session sourceSession = null;
try {
Query countQuery = session
.getWorkspace()
throw new SlcException("Unexpected error while computing "
+ "the size of the fetch for workspace "
+ session.getWorkspace().getName(), e);
- } finally {
- JcrUtils.logoutQuietly(sourceSession);
}
}
if (monitor != null) {
monitor.beginTask("Computing fetch size...", -1);
Long totalAmount = getNodesNumber(sourceSession);
- // if (sourceWkspList != null) {
- // for (String wkspName : sourceWkspList) {
- // totalAmount += getNodesNumber(wkspName);
- // }
- // } else
- // for (String sourceWorkspaceName : sourceDefaultSession
- // .getWorkspace().getAccessibleWorkspaceNames()) {
- // totalAmount += getNodesNumber(sourceWorkspaceName);
- // }
monitor.beginTask("Fetch", totalAmount.intValue());
-
- // if (log.isDebugEnabled())
- // log.debug("Nb of nodes to sync: " + totalAmount.intValue());
}
try {
return true;
}
- /** synchronise only one workspace retrieved by name */
+ /**
+ * Synchronises only one workspace, retrieved by name without changing its
+ * name.
+ */
public void setSourceWksp(String sourceWksp) {
if (sourceWksp != null && !sourceWksp.trim().equals("")) {
- List<String> list = new ArrayList<String>();
- list.add(sourceWksp);
- setSourceWkspList(list);
+ Map<String, String> map = new HashMap<String, String>();
+ map.put(sourceWksp, sourceWksp);
+ setWkspMap(map);
}
}
- /** synchronise a list workspace that will be retrieved by name */
- public void setSourceWkspList(List<String> sourceWkspList) {
+ /**
+ * Synchronises a map of workspaces that will be retrieved by name. If the
+ * target name is not defined (eg null or an empty string) for a given
+ * source workspace, we use the source name as target name.
+ */
+ public void setWkspMap(Map<String, String> workspaceMap) {
// clean the list to ease later use
- this.sourceWkspList = null;
- if (sourceWkspList != null) {
- for (String wkspName : sourceWkspList) {
- if (!wkspName.trim().equals("")) {
- // only instantiate if needed
- if (this.sourceWkspList == null)
- this.sourceWkspList = new ArrayList<String>();
- this.sourceWkspList.add(wkspName);
- }
+ this.workspaceMap = new HashMap<String, String>();
+ if (workspaceMap != null) {
+ workspaceNames: for (String srcName : workspaceMap.keySet()) {
+ String targetName = workspaceMap.get(srcName);
+
+ // Sanity check
+ if (srcName.trim().equals(""))
+ continue workspaceNames;
+ if (targetName == null || "".equals(targetName.trim()))
+ targetName = srcName;
+ this.workspaceMap.put(srcName, targetName);
}
}
+ // clean the map to ease later use
+ if (this.workspaceMap.size() == 0)
+ this.workspaceMap = null;
}
public void setMonitor(ArgeoMonitor monitor) {