Improve modular distributions ordering
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 24 Jul 2009 08:13:17 +0000 (08:13 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 24 Jul 2009 08:13:17 +0000 (08:13 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2748 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/msg/ObjectList.java
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/provisioning/ListModularDistributions.java
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/provisioning/ModularDistributionInterceptor.java
runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/build/BuildConstants.java [new file with mode: 0644]

index 720729ac08c06a1fb79b7b3d7ca8a8294febad3c..00f152ed1dd15fed23c62d250ca8a728ab3c0ec5 100644 (file)
@@ -2,6 +2,7 @@ package org.argeo.slc.msg;
 
 import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 public class ObjectList {
@@ -10,7 +11,7 @@ public class ObjectList {
        public ObjectList() {
        }
 
-       public ObjectList(List<? extends Serializable> objects) {
+       public ObjectList(Collection<? extends Serializable> objects) {
                this.objects.addAll(objects);
        }
 
index 13261dd7d4ed800097325c04d0c3de9d6b720946..3a684efcd2ff88fb262f7e8cad8dd41c37d32332 100644 (file)
@@ -1,49 +1,67 @@
 package org.argeo.slc.web.mvc.provisioning;
 
+import java.util.Comparator;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.argeo.slc.build.BasicNameVersion;
+import org.argeo.slc.build.BuildConstants;
 import org.argeo.slc.build.ModularDistribution;
+import org.argeo.slc.build.NameVersion;
 import org.argeo.slc.msg.ObjectList;
 import org.argeo.slc.msg.build.ModularDistributionDescriptor;
 import org.argeo.slc.web.mvc.AbstractServiceController;
 import org.springframework.web.servlet.ModelAndView;
 
 /** List of distributions. */
-public class ListModularDistributions extends AbstractServiceController {
+public class ListModularDistributions extends AbstractServiceController
+               implements Comparator<ModularDistributionDescriptor> {
        private Set<ModularDistribution> modularDistributions;
 
+       private String provisioningServletPath = "/dist";
+
        @Override
        protected void handleServiceRequest(HttpServletRequest request,
                        HttpServletResponse response, ModelAndView modelAndView)
                        throws Exception {
 
                String baseUrl = "http://" + request.getServerName() + ":"
-                               + request.getServerPort() + request.getContextPath() + "/dist"
-                               + "/";
+                               + request.getServerPort() + request.getContextPath()
+                               + provisioningServletPath + "/";
 
-               ObjectList ol = new ObjectList();
+               SortedSet<ModularDistributionDescriptor> descriptors = new TreeSet<ModularDistributionDescriptor>(
+                               this);
 
+               Set<String> names = new HashSet<String>();
+               Set<String> namesRelease = new HashSet<String>();
+
+               // Scan distributions
                for (Iterator<ModularDistribution> it = modularDistributions.iterator(); it
                                .hasNext();) {
                        ModularDistribution md = it.next();
-                       String moduleBase = baseUrl + md.getName() + "/" + md.getVersion()
-                                       + "/";
-                       ModularDistributionDescriptor mdd = new ModularDistributionDescriptor();
-                       mdd.setName(md.getName());
-                       mdd.setVersion(md.getVersion());
-
-                       mdd.getModulesDescriptors().put("modularDistribution",
-                                       moduleBase + "modularDistribution");
-                       mdd.getModulesDescriptors().put("eclipse", moduleBase + "site.xml");
+                       ModularDistributionDescriptor mdd = fromNameVersion(baseUrl, md);
 
-                       ol.getObjects().add(mdd);
+                       descriptors.add(mdd);
+                       names.add(mdd.getName());
+                       if (!md.getVersion().contains(BuildConstants.SNAPSHOT))
+                               namesRelease.add(mdd.getName());
                }
 
-               modelAndView.addObject(ol);
+               // Add LATESTs and RELEASEs
+               for (String name : names)
+                       descriptors.add(fromNameVersion(baseUrl, new BasicNameVersion(name,
+                                       BuildConstants.LATEST)));
+               for (String name : namesRelease)
+                       descriptors.add(fromNameVersion(baseUrl, new BasicNameVersion(name,
+                                       BuildConstants.RELEASE)));
+
+               modelAndView.addObject(new ObjectList(descriptors));
        }
 
        public void setModularDistributions(
@@ -51,4 +69,54 @@ public class ListModularDistributions extends AbstractServiceController {
                this.modularDistributions = modularDistributions;
        }
 
+       public void setProvisioningServletPath(String provisioningServletPath) {
+               this.provisioningServletPath = provisioningServletPath;
+       }
+
+       protected ModularDistributionDescriptor fromNameVersion(String baseUrl,
+                       NameVersion md) {
+               String moduleBase = baseUrl + md.getName() + "/" + md.getVersion()
+                               + "/";
+               ModularDistributionDescriptor mdd = new ModularDistributionDescriptor();
+               mdd.setName(md.getName());
+               mdd.setVersion(md.getVersion());
+
+               mdd.getModulesDescriptors().put("modularDistribution",
+                               moduleBase + "modularDistribution");
+               mdd.getModulesDescriptors().put("eclipse", moduleBase + "site.xml");
+               return mdd;
+
+       }
+
+       /** RELEASEs first, then LATESTs, then version */
+       public int compare(ModularDistributionDescriptor mdd1,
+                       ModularDistributionDescriptor mdd2) {
+               final int BEFORE = -1;
+               final int AFTER = 1;
+
+               String n1 = mdd1.getName();
+               String v1 = mdd1.getVersion();
+               String n2 = mdd2.getName();
+               String v2 = mdd2.getVersion();
+
+               if (v1.equals(BuildConstants.RELEASE))
+                       if (v2.equals(BuildConstants.RELEASE))
+                               return n1.compareTo(n2);
+                       else
+                               return BEFORE;
+               else if (v2.equals(BuildConstants.RELEASE))
+                       return AFTER;// we know 1 not RELEASE
+               else if (v1.equals(BuildConstants.LATEST))
+                       if (v2.equals(BuildConstants.LATEST))
+                               return n1.compareTo(n2);
+                       else
+                               return BEFORE;
+               else if (v2.equals(BuildConstants.LATEST))
+                       return AFTER;// we know 1 not LATEST or RELEASE
+               else if (n1.equals(n2))
+                       return v1.compareTo(v2);
+               else
+                       return n1.compareTo(n2);
+       }
+
 }
index 309b9960c2f76f197d4d3dfa250113c6753e9f88..3d3d2e6e21f45cd73248be6d511db47a26be0700 100644 (file)
@@ -11,15 +11,13 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.argeo.slc.SlcException;
 import org.argeo.slc.build.BasicNameVersion;
+import org.argeo.slc.build.BuildConstants;
 import org.argeo.slc.build.ModularDistribution;
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
 public class ModularDistributionInterceptor extends HandlerInterceptorAdapter {
        private Set<ModularDistribution> modularDistributions;
 
-       public final static String RELEASE = "RELEASE";
-       public final static String LATEST = "LATEST";
-
        @Override
        public boolean preHandle(HttpServletRequest request,
                        HttpServletResponse response, Object handler) throws Exception {
@@ -33,11 +31,11 @@ public class ModularDistributionInterceptor extends HandlerInterceptorAdapter {
                                .iterator(); it.hasNext();) {
                        ModularDistribution md = it.next();
                        if (md.getName().equals(distributionName)) {
-                               if (distributionVersion.equals(RELEASE)
-                                               && md.getVersion().contains("SNAPSHOT"))
+                               if (distributionVersion.equals(BuildConstants.RELEASE)
+                                               && md.getVersion().contains(BuildConstants.SNAPSHOT))
                                        continue distribs;
 
-                               else if (distributionVersion.equals(LATEST))
+                               else if (distributionVersion.equals(BuildConstants.LATEST))
                                        choices.put(md.getVersion(), md);
                                else if (distributionVersion.equals(md.getVersion())) {
                                        choices.put(md.getVersion(), md);
diff --git a/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/build/BuildConstants.java b/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/build/BuildConstants.java
new file mode 100644 (file)
index 0000000..3188d13
--- /dev/null
@@ -0,0 +1,12 @@
+package org.argeo.slc.build;
+
+public class BuildConstants {
+
+       public final static String RELEASE = "RELEASE";
+       public final static String LATEST = "LATEST";
+       public final static String SNAPSHOT = "SNAPSHOT";
+
+       private BuildConstants() {
+
+       }
+}