2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.web
.mvc
.provisioning
;
18 import java
.util
.Comparator
;
19 import java
.util
.HashSet
;
20 import java
.util
.Iterator
;
22 import java
.util
.SortedSet
;
23 import java
.util
.TreeSet
;
25 import javax
.servlet
.http
.HttpServletRequest
;
26 import javax
.servlet
.http
.HttpServletResponse
;
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
;
37 /** List of distributions. */
38 public class ListModularDistributions
extends AbstractServiceController
39 implements Comparator
<ModularDistributionDescriptor
> {
40 private Set
<ModularDistribution
> modularDistributions
;
42 private String provisioningServletPath
= "/dist";
45 protected void handleServiceRequest(HttpServletRequest request
,
46 HttpServletResponse response
, ModelAndView modelAndView
)
49 String baseUrl
= "http://" + request
.getServerName() + ":"
50 + request
.getServerPort() + request
.getContextPath()
51 + provisioningServletPath
+ "/";
53 SortedSet
<ModularDistributionDescriptor
> descriptors
= new TreeSet
<ModularDistributionDescriptor
>(
56 Set
<String
> names
= new HashSet
<String
>();
57 Set
<String
> namesRelease
= new HashSet
<String
>();
60 for (Iterator
<ModularDistribution
> it
= modularDistributions
.iterator(); it
62 ModularDistribution md
= it
.next();
63 ModularDistributionDescriptor mdd
= fromNameVersion(baseUrl
, md
);
66 names
.add(mdd
.getName());
67 if (!md
.getVersion().contains(BuildConstants
.SNAPSHOT
))
68 namesRelease
.add(mdd
.getName());
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
)));
79 modelAndView
.addObject(new ObjectList(descriptors
));
82 public void setModularDistributions(
83 Set
<ModularDistribution
> modularDistributions
) {
84 this.modularDistributions
= modularDistributions
;
87 public void setProvisioningServletPath(String provisioningServletPath
) {
88 this.provisioningServletPath
= provisioningServletPath
;
91 protected ModularDistributionDescriptor
fromNameVersion(String baseUrl
,
93 String moduleBase
= baseUrl
+ md
.getName() + "/" + md
.getVersion()
95 ModularDistributionDescriptor mdd
= new ModularDistributionDescriptor();
96 mdd
.setName(md
.getName());
97 mdd
.setVersion(md
.getVersion());
99 mdd
.getModulesDescriptors().put("modularDistribution",
100 moduleBase
+ "modularDistribution");
101 mdd
.getModulesDescriptors().put("eclipse", moduleBase
+ "site.xml");
106 /** RELEASEs first, then LATESTs, then version */
107 public int compare(ModularDistributionDescriptor mdd1
,
108 ModularDistributionDescriptor mdd2
) {
109 final int BEFORE
= -1;
112 String n1
= mdd1
.getName();
113 String v1
= mdd1
.getVersion();
114 String n2
= mdd2
.getName();
115 String v2
= mdd2
.getVersion();
117 if (v1
.equals(BuildConstants
.RELEASE
))
118 if (v2
.equals(BuildConstants
.RELEASE
))
119 return n1
.compareTo(n2
);
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
);
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
);
134 return n1
.compareTo(n2
);