2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.web
.mvc
.provisioning
;
19 import java
.util
.Comparator
;
20 import java
.util
.HashSet
;
21 import java
.util
.Iterator
;
23 import java
.util
.SortedSet
;
24 import java
.util
.TreeSet
;
26 import javax
.servlet
.http
.HttpServletRequest
;
27 import javax
.servlet
.http
.HttpServletResponse
;
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
;
38 /** List of distributions. */
39 public class ListModularDistributions
extends AbstractServiceController
40 implements Comparator
<ModularDistributionDescriptor
> {
41 private Set
<ModularDistribution
> modularDistributions
;
43 private String provisioningServletPath
= "/dist";
46 protected void handleServiceRequest(HttpServletRequest request
,
47 HttpServletResponse response
, ModelAndView modelAndView
)
50 String baseUrl
= "http://" + request
.getServerName() + ":"
51 + request
.getServerPort() + request
.getContextPath()
52 + provisioningServletPath
+ "/";
54 SortedSet
<ModularDistributionDescriptor
> descriptors
= new TreeSet
<ModularDistributionDescriptor
>(
57 Set
<String
> names
= new HashSet
<String
>();
58 Set
<String
> namesRelease
= new HashSet
<String
>();
61 for (Iterator
<ModularDistribution
> it
= modularDistributions
.iterator(); it
63 ModularDistribution md
= it
.next();
64 ModularDistributionDescriptor mdd
= fromNameVersion(baseUrl
, md
);
67 names
.add(mdd
.getName());
68 if (!md
.getVersion().contains(BuildConstants
.SNAPSHOT
))
69 namesRelease
.add(mdd
.getName());
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
)));
80 modelAndView
.addObject(new ObjectList(descriptors
));
83 public void setModularDistributions(
84 Set
<ModularDistribution
> modularDistributions
) {
85 this.modularDistributions
= modularDistributions
;
88 public void setProvisioningServletPath(String provisioningServletPath
) {
89 this.provisioningServletPath
= provisioningServletPath
;
92 protected ModularDistributionDescriptor
fromNameVersion(String baseUrl
,
94 String moduleBase
= baseUrl
+ md
.getName() + "/" + md
.getVersion()
96 ModularDistributionDescriptor mdd
= new ModularDistributionDescriptor();
97 mdd
.setName(md
.getName());
98 mdd
.setVersion(md
.getVersion());
100 mdd
.getModulesDescriptors().put("modularDistribution",
101 moduleBase
+ "modularDistribution");
102 mdd
.getModulesDescriptors().put("eclipse", moduleBase
+ "site.xml");
107 /** RELEASEs first, then LATESTs, then version */
108 public int compare(ModularDistributionDescriptor mdd1
,
109 ModularDistributionDescriptor mdd2
) {
110 final int BEFORE
= -1;
113 String n1
= mdd1
.getName();
114 String v1
= mdd1
.getVersion();
115 String n2
= mdd2
.getName();
116 String v2
= mdd2
.getVersion();
118 if (v1
.equals(BuildConstants
.RELEASE
))
119 if (v2
.equals(BuildConstants
.RELEASE
))
120 return n1
.compareTo(n2
);
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
);
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
);
135 return n1
.compareTo(n2
);