work on fetch and normalization.
authorBruno Sinou <bsinou@argeo.org>
Tue, 25 Feb 2014 18:28:07 +0000 (18:28 +0000)
committerBruno Sinou <bsinou@argeo.org>
Tue, 25 Feb 2014 18:28:07 +0000 (18:28 +0000)
remove a potential bug (command image for context menu that were never freed)

git-svn-id: https://svn.argeo.org/slc/trunk@6853 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/NodeIndexer.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/PdeSourcesIndexer.java [new file with mode: 0644]
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoSync.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoUtils.java

index 3f882bd8b6fbf1bfc11f720ebc659c60fb837c0f..77798abd9e27205f69b405cee5f1c617c4466513 100644 (file)
@@ -20,7 +20,7 @@ import javax.jcr.observation.EventListener;
 
 /**
  * Adds metadata to an existing node, ideally via observation after it has been
- * added. THere is a similar concept in ModeShape with which this abstraction
+ * added. There is a similar concept in ModeShape with which this abstraction
  * may be merged in the future.
  */
 public interface NodeIndexer {
diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/PdeSourcesIndexer.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/PdeSourcesIndexer.java
new file mode 100644 (file)
index 0000000..589bd89
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * 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
index d145eb225202a045b701b4777b8e7b470b65de27..8d0147ec00c3259be9b0d608531fefc38ce3a30a 100644 (file)
@@ -16,7 +16,6 @@
 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;
@@ -51,12 +50,19 @@ import org.argeo.jcr.JcrUtils;
 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;
@@ -79,7 +85,7 @@ public class RepoSync implements Runnable {
        private RepositoryFactory repositoryFactory;
 
        private ArgeoMonitor monitor;
-       private List<String> sourceWkspList;
+       private Map<String, String> workspaceMap;
 
        // TODO fix monitor
        private Boolean filesOnly = false;
@@ -135,23 +141,25 @@ public class RepoSync implements Runnable {
                                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);
@@ -159,8 +167,9 @@ public class RepoSync implements Runnable {
                                } 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);
@@ -189,9 +198,8 @@ public class RepoSync implements Runnable {
        }
 
        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()
@@ -207,8 +215,6 @@ public class RepoSync implements Runnable {
                        throw new SlcException("Unexpected error while computing "
                                        + "the size of the fetch for workspace "
                                        + session.getWorkspace().getName(), e);
-               } finally {
-                       JcrUtils.logoutQuietly(sourceSession);
                }
        }
 
@@ -216,19 +222,7 @@ public class RepoSync implements Runnable {
                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 {
@@ -541,29 +535,41 @@ public class RepoSync implements Runnable {
                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) {
index 5f798c1444cc8af171af7469af7873e101c2ee90..a8c9cca23f8d8451dc816fca376e1262642d4e32 100644 (file)
@@ -385,7 +385,6 @@ public class RepoUtils implements ArgeoNames, SlcNames {
                        throw new SlcException("Cannot connect to repository " + repoNode,
                                        e);
                }
-
        }
 
        /**