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