]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/provisioning/ListModularDistributions.java
Introduction of annotation to handle MVC flows // Class cleaning
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / provisioning / ListModularDistributions.java
1 package org.argeo.slc.web.mvc.provisioning;
2
3 import java.util.Comparator;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.Set;
7 import java.util.SortedSet;
8 import java.util.TreeSet;
9
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import org.argeo.slc.build.BasicNameVersion;
14 import org.argeo.slc.build.BuildConstants;
15 import org.argeo.slc.build.ModularDistribution;
16 import org.argeo.slc.build.NameVersion;
17 import org.argeo.slc.msg.ObjectList;
18 import org.argeo.slc.msg.build.ModularDistributionDescriptor;
19 import org.argeo.slc.web.mvc.AbstractServiceController;
20 import org.springframework.web.servlet.ModelAndView;
21
22 /** List of distributions. */
23 public class ListModularDistributions extends AbstractServiceController
24 implements Comparator<ModularDistributionDescriptor> {
25 private Set<ModularDistribution> modularDistributions;
26
27 private String provisioningServletPath = "/dist";
28
29 @Override
30 protected void handleServiceRequest(HttpServletRequest request,
31 HttpServletResponse response, ModelAndView modelAndView)
32 throws Exception {
33
34 String baseUrl = "http://" + request.getServerName() + ":"
35 + request.getServerPort() + request.getContextPath()
36 + provisioningServletPath + "/";
37
38 SortedSet<ModularDistributionDescriptor> descriptors = new TreeSet<ModularDistributionDescriptor>(
39 this);
40
41 Set<String> names = new HashSet<String>();
42 Set<String> namesRelease = new HashSet<String>();
43
44 // Scan distributions
45 for (Iterator<ModularDistribution> it = modularDistributions.iterator(); it
46 .hasNext();) {
47 ModularDistribution md = it.next();
48 ModularDistributionDescriptor mdd = fromNameVersion(baseUrl, md);
49
50 descriptors.add(mdd);
51 names.add(mdd.getName());
52 if (!md.getVersion().contains(BuildConstants.SNAPSHOT))
53 namesRelease.add(mdd.getName());
54 }
55
56 // Add LATESTs and RELEASEs
57 for (String name : names)
58 descriptors.add(fromNameVersion(baseUrl, new BasicNameVersion(name,
59 BuildConstants.LATEST)));
60 for (String name : namesRelease)
61 descriptors.add(fromNameVersion(baseUrl, new BasicNameVersion(name,
62 BuildConstants.RELEASE)));
63
64 modelAndView.addObject(new ObjectList(descriptors));
65 }
66
67 public void setModularDistributions(
68 Set<ModularDistribution> modularDistributions) {
69 this.modularDistributions = modularDistributions;
70 }
71
72 public void setProvisioningServletPath(String provisioningServletPath) {
73 this.provisioningServletPath = provisioningServletPath;
74 }
75
76 protected ModularDistributionDescriptor fromNameVersion(String baseUrl,
77 NameVersion md) {
78 String moduleBase = baseUrl + md.getName() + "/" + md.getVersion()
79 + "/";
80 ModularDistributionDescriptor mdd = new ModularDistributionDescriptor();
81 mdd.setName(md.getName());
82 mdd.setVersion(md.getVersion());
83
84 mdd.getModulesDescriptors().put("modularDistribution",
85 moduleBase + "modularDistribution");
86 mdd.getModulesDescriptors().put("eclipse", moduleBase + "site.xml");
87 return mdd;
88
89 }
90
91 /** RELEASEs first, then LATESTs, then version */
92 public int compare(ModularDistributionDescriptor mdd1,
93 ModularDistributionDescriptor mdd2) {
94 final int BEFORE = -1;
95 final int AFTER = 1;
96
97 String n1 = mdd1.getName();
98 String v1 = mdd1.getVersion();
99 String n2 = mdd2.getName();
100 String v2 = mdd2.getVersion();
101
102 if (v1.equals(BuildConstants.RELEASE))
103 if (v2.equals(BuildConstants.RELEASE))
104 return n1.compareTo(n2);
105 else
106 return BEFORE;
107 else if (v2.equals(BuildConstants.RELEASE))
108 return AFTER;// we know 1 not RELEASE
109 else if (v1.equals(BuildConstants.LATEST))
110 if (v2.equals(BuildConstants.LATEST))
111 return n1.compareTo(n2);
112 else
113 return BEFORE;
114 else if (v2.equals(BuildConstants.LATEST))
115 return AFTER;// we know 1 not LATEST or RELEASE
116 else if (n1.equals(n2))
117 return v1.compareTo(v2);
118 else
119 return n1.compareTo(n2);
120 }
121
122 }