]> 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
Move to SLC legacy
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / provisioning / ListModularDistributions.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.web.mvc.provisioning;
17
18 import java.util.Comparator;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.Set;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.argeo.slc.BasicNameVersion;
29 import org.argeo.slc.NameVersion;
30 import org.argeo.slc.build.BuildConstants;
31 import org.argeo.slc.build.ModularDistribution;
32 import org.argeo.slc.msg.ObjectList;
33 import org.argeo.slc.msg.build.ModularDistributionDescriptor;
34 import org.argeo.slc.web.mvc.AbstractServiceController;
35 import org.springframework.web.servlet.ModelAndView;
36
37 /** List of distributions. */
38 public class ListModularDistributions extends AbstractServiceController
39 implements Comparator<ModularDistributionDescriptor> {
40 private Set<ModularDistribution> modularDistributions;
41
42 private String provisioningServletPath = "/dist";
43
44 @Override
45 protected void handleServiceRequest(HttpServletRequest request,
46 HttpServletResponse response, ModelAndView modelAndView)
47 throws Exception {
48
49 String baseUrl = "http://" + request.getServerName() + ":"
50 + request.getServerPort() + request.getContextPath()
51 + provisioningServletPath + "/";
52
53 SortedSet<ModularDistributionDescriptor> descriptors = new TreeSet<ModularDistributionDescriptor>(
54 this);
55
56 Set<String> names = new HashSet<String>();
57 Set<String> namesRelease = new HashSet<String>();
58
59 // Scan distributions
60 for (Iterator<ModularDistribution> it = modularDistributions.iterator(); it
61 .hasNext();) {
62 ModularDistribution md = it.next();
63 ModularDistributionDescriptor mdd = fromNameVersion(baseUrl, md);
64
65 descriptors.add(mdd);
66 names.add(mdd.getName());
67 if (!md.getVersion().contains(BuildConstants.SNAPSHOT))
68 namesRelease.add(mdd.getName());
69 }
70
71 // Add LATESTs and RELEASEs
72 for (String name : names)
73 descriptors.add(fromNameVersion(baseUrl, new BasicNameVersion(name,
74 BuildConstants.LATEST)));
75 for (String name : namesRelease)
76 descriptors.add(fromNameVersion(baseUrl, new BasicNameVersion(name,
77 BuildConstants.RELEASE)));
78
79 modelAndView.addObject(new ObjectList(descriptors));
80 }
81
82 public void setModularDistributions(
83 Set<ModularDistribution> modularDistributions) {
84 this.modularDistributions = modularDistributions;
85 }
86
87 public void setProvisioningServletPath(String provisioningServletPath) {
88 this.provisioningServletPath = provisioningServletPath;
89 }
90
91 protected ModularDistributionDescriptor fromNameVersion(String baseUrl,
92 NameVersion md) {
93 String moduleBase = baseUrl + md.getName() + "/" + md.getVersion()
94 + "/";
95 ModularDistributionDescriptor mdd = new ModularDistributionDescriptor();
96 mdd.setName(md.getName());
97 mdd.setVersion(md.getVersion());
98
99 mdd.getModulesDescriptors().put("modularDistribution",
100 moduleBase + "modularDistribution");
101 mdd.getModulesDescriptors().put("eclipse", moduleBase + "site.xml");
102 return mdd;
103
104 }
105
106 /** RELEASEs first, then LATESTs, then version */
107 public int compare(ModularDistributionDescriptor mdd1,
108 ModularDistributionDescriptor mdd2) {
109 final int BEFORE = -1;
110 final int AFTER = 1;
111
112 String n1 = mdd1.getName();
113 String v1 = mdd1.getVersion();
114 String n2 = mdd2.getName();
115 String v2 = mdd2.getVersion();
116
117 if (v1.equals(BuildConstants.RELEASE))
118 if (v2.equals(BuildConstants.RELEASE))
119 return n1.compareTo(n2);
120 else
121 return BEFORE;
122 else if (v2.equals(BuildConstants.RELEASE))
123 return AFTER;// we know 1 not RELEASE
124 else if (v1.equals(BuildConstants.LATEST))
125 if (v2.equals(BuildConstants.LATEST))
126 return n1.compareTo(n2);
127 else
128 return BEFORE;
129 else if (v2.equals(BuildConstants.LATEST))
130 return AFTER;// we know 1 not LATEST or RELEASE
131 else if (n1.equals(n2))
132 return v1.compareTo(v2);
133 else
134 return n1.compareTo(n2);
135 }
136
137 }