001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.server.client.servlets;
038:
039: import java.io.IOException;
040: import java.io.PrintWriter;
041: import java.util.Arrays;
042: import java.util.HashMap;
043: import java.util.LinkedList;
044: import java.util.List;
045: import java.util.Map;
046: import javax.ejb.EJB;
047: import javax.servlet.ServletException;
048: import javax.servlet.http.HttpServlet;
049: import javax.servlet.http.HttpServletRequest;
050: import javax.servlet.http.HttpServletResponse;
051: import org.netbeans.installer.infra.server.ejb.Manager;
052: import org.netbeans.installer.infra.server.ejb.ManagerException;
053: import org.netbeans.installer.product.Registry;
054: import org.netbeans.installer.product.components.Group;
055: import org.netbeans.installer.product.dependencies.Requirement;
056: import org.netbeans.installer.product.components.Product;
057: import org.netbeans.installer.utils.helper.Dependency;
058: import org.netbeans.installer.utils.helper.Platform;
059:
060: /**
061: *
062: * @author Kirill Sorokin
063: * @version
064: */
065: public class Components extends HttpServlet {
066: @EJB
067: private Manager manager;
068:
069: protected void processRequest(HttpServletRequest request,
070: HttpServletResponse response) throws ServletException,
071: IOException {
072: final List<String> fixed = Arrays.asList("nb-ide");
073: final List<String> standard = Arrays.asList("nb-ide",
074: "glassfish");
075: final List<String> selected = Arrays.asList("nb-xml", "nb-soa",
076: "nb-identity", "glassfish", "openesb", "sjsam");
077: final Map<String, List<String>> desires = new HashMap<String, List<String>>();
078: desires.put("nb-visualweb", Arrays.asList("glassfish"));
079:
080: final Map<String, String> comments = new HashMap<String, String>();
081: comments.put("jdk", "WTF!T!&!&");
082:
083: try {
084: response.setContentType("text/javascript; charset=UTF-8");
085: final PrintWriter out = response.getWriter();
086:
087: final Registry registry = manager
088: .loadRegistry("NetBeans 6.1");
089:
090: final List<Product> products = registry.getProducts();
091: final List<Group> groups = registry.getGroups();
092:
093: final Map<Integer, Integer> productMapping = new HashMap<Integer, Integer>();
094:
095: final List<String> productUids = new LinkedList<String>();
096: final List<String> productVersions = new LinkedList<String>();
097: final List<String> productDisplayNames = new LinkedList<String>();
098: final List<String> productDownloadSizes = new LinkedList<String>();
099: final List<List<Platform>> productPlatforms = new LinkedList<List<Platform>>();
100: final List<String> productProperties = new LinkedList<String>();
101: final List<List<List<Integer>>> productRequirements = new LinkedList<List<List<Integer>>>();
102: final List<List<Integer>> productDesires = new LinkedList<List<Integer>>();
103: final List<String> productComments = new LinkedList<String>();
104:
105: final List<Integer> defaultGroupProducts = new LinkedList<Integer>();
106: final List<List<Integer>> groupProducts = new LinkedList<List<Integer>>();
107: final List<String> groupDisplayNames = new LinkedList<String>();
108:
109: for (int i = 0; i < products.size(); i++) {
110: final Product product = products.get(i);
111:
112: boolean existingFound = false;
113: for (int j = 0; j < productUids.size(); j++) {
114: if (productUids.get(j).equals(product.getUid())
115: && productVersions.get(j).equals(
116: product.getVersion().toString())) {
117: productPlatforms.get(j).addAll(
118: product.getPlatforms());
119: productMapping.put(i, j);
120: existingFound = true;
121: break;
122: }
123: }
124:
125: if (existingFound) {
126: continue;
127: }
128:
129: long size = (long) Math.ceil(((double) product
130: .getDownloadSize()) / 1024.);
131: productUids.add(product.getUid());
132: productVersions.add(product.getVersion().toString());
133: productDisplayNames.add(product.getDisplayName());
134: productDownloadSizes.add(Long.toString(size));
135: productPlatforms.add(product.getPlatforms());
136:
137: String properties = "PROPERTY_NONE";
138: if (fixed.contains(product.getUid())) {
139: properties += " | PROPERTY_FIXED";
140: }
141: if (standard.contains(product.getUid())) {
142: properties += " | PROPERTY_STANDARD";
143: }
144: if (selected.contains(product.getUid())) {
145: properties += " | PROPERTY_SELECTED";
146: }
147: productProperties.add(properties);
148:
149: List<List<Integer>> requirements = new LinkedList<List<Integer>>();
150: productRequirements.add(requirements);
151:
152: List<Integer> currentDesires = new LinkedList<Integer>();
153: productDesires.add(currentDesires);
154:
155: productComments.add("");
156:
157: productMapping.put(i, productUids.size() - 1);
158: }
159:
160: for (int i = 0; i < products.size(); i++) {
161: final int index = productMapping.get(i);
162: final Product product = products.get(i);
163:
164: List<List<Integer>> requirements = productRequirements
165: .get(index);
166: for (Dependency requirement : product
167: .getDependencies(Requirement.class)) {
168: List<Integer> requireeIds = new LinkedList<Integer>();
169: requirements.add(requireeIds);
170: for (Product requiree : registry
171: .getProducts(requirement)) {
172: requireeIds.add(productMapping.get(products
173: .indexOf(requiree)));
174: }
175: }
176:
177: List<Integer> currentDesires = productDesires
178: .get(index);
179: productDesires.set(index, currentDesires);
180: if (desires.get(product.getUid()) != null) {
181: for (String uid : desires.get(product.getUid())) {
182: for (Product desiree : products) {
183: if (desiree.getUid().equals(uid)) {
184: int indexOf = productMapping
185: .get(products.indexOf(desiree));
186: if (!currentDesires.contains(indexOf)) {
187: currentDesires.add(indexOf);
188: }
189: }
190: }
191: }
192: }
193:
194: if (comments.get(product.getUid()) != null) {
195: productComments.set(index, comments.get(product
196: .getUid()));
197: }
198: }
199:
200: out.println("product_uids = new Array();");
201: for (int i = 0; i < productUids.size(); i++) {
202: out.println(" product_uids[" + i + "] = \""
203: + productUids.get(i) + "\";");
204: }
205: out.println();
206:
207: out.println("product_versions = new Array();");
208: for (int i = 0; i < productVersions.size(); i++) {
209: out.println(" product_versions[" + i + "] = \""
210: + productVersions.get(i) + "\";");
211: }
212: out.println();
213:
214: out.println("product_display_names = new Array();");
215: for (int i = 0; i < productDisplayNames.size(); i++) {
216: out.println(" product_display_names[" + i + "] = \""
217: + productDisplayNames.get(i) + "\";");
218: }
219: out.println();
220:
221: out.println("product_download_sizes = new Array();");
222: for (int i = 0; i < productDownloadSizes.size(); i++) {
223: out.println(" product_download_sizes[" + i + "] = "
224: + productDownloadSizes.get(i) + ";");
225: }
226: out.println();
227:
228: out.println("product_platforms = new Array();");
229: for (int i = 0; i < productPlatforms.size(); i++) {
230: out.println(" product_platforms[" + i
231: + "] = new Array();");
232: for (int j = 0; j < productPlatforms.get(i).size(); j++) {
233: out.println(" product_platforms[" + i + "]["
234: + j + "] = \""
235: + productPlatforms.get(i).get(j) + "\";");
236: }
237: }
238: out.println();
239:
240: out.println("product_properties = new Array();");
241: for (int i = 0; i < productProperties.size(); i++) {
242: out.println(" product_properties[" + i + "] = "
243: + productProperties.get(i) + ";");
244: }
245: out.println();
246:
247: out.println("product_requirements = new Array();");
248: for (int i = 0; i < productRequirements.size(); i++) {
249: out.println(" product_requirements[" + i
250: + "] = new Array();");
251: for (int j = 0; j < productRequirements.get(i).size(); j++) {
252: out.println(" product_requirements[" + i
253: + "][" + j + "] = new Array();");
254: for (int k = 0; k < productRequirements.get(i).get(
255: j).size(); k++) {
256: out.println(" product_requirements["
257: + i
258: + "]["
259: + j
260: + "]["
261: + k
262: + "] = "
263: + productRequirements.get(i).get(j)
264: .get(k) + ";");
265: }
266: }
267: }
268:
269: out.println("product_desires = new Array();");
270: for (int i = 0; i < productDesires.size(); i++) {
271: out.println(" product_desires[" + i
272: + "] = new Array();");
273: for (int j = 0; j < productDesires.get(i).size(); j++) {
274: out.println(" product_desires[" + i + "]["
275: + j + "] = " + productDesires.get(i).get(j)
276: + ";");
277: }
278: }
279: out.println();
280:
281: out.println("product_comments = new Array();");
282: for (int i = 0; i < productComments.size(); i++) {
283: out.println(" product_comments[" + i + "] = \""
284: + productComments.get(i) + "\";");
285: }
286: out.println();
287:
288: for (int i = 0; i < productUids.size(); i++) {
289: defaultGroupProducts.add(Integer.valueOf(i));
290: }
291:
292: for (Group group : groups) {
293: // skip the registry root
294: if (group.getUid().equals("")) {
295: continue;
296: }
297:
298: List<Integer> components = new LinkedList<Integer>();
299: for (int i = 0; i < products.size(); i++) {
300: if (group.isAncestor(products.get(i))) {
301: Integer index = Integer.valueOf(productMapping
302: .get(i));
303: if (!components.contains(index)) {
304: components.add(index);
305: defaultGroupProducts.remove(index);
306: }
307: }
308: }
309:
310: groupProducts.add(components);
311: groupDisplayNames.add(group.getDisplayName());
312: }
313:
314: if (groupProducts.size() > 0) {
315: out.println("group_products = new Array();");
316: out.println(" group_products[0] = new Array();");
317: for (int j = 0; j < defaultGroupProducts.size(); j++) {
318: out.println(" group_products[0][" + j
319: + "] = " + defaultGroupProducts.get(j)
320: + ";");
321: }
322: for (int i = 0; i < groupProducts.size(); i++) {
323: out.println(" group_products[" + (i + 1)
324: + "] = new Array();");
325: for (int j = 0; j < groupProducts.get(i).size(); j++) {
326: out.println(" group_products[" + (i + 1)
327: + "][" + j + "] = "
328: + groupProducts.get(i).get(j) + ";");
329: }
330: }
331: out.println();
332:
333: out.println("group_display_names = new Array();");
334: out.println(" group_display_names[0] = \"\";");
335: for (int i = 0; i < groupDisplayNames.size(); i++) {
336: out.println(" group_display_names[" + (i + 1)
337: + "] = \"" + groupDisplayNames.get(i)
338: + "\";");
339: }
340: out.println();
341: } else {
342: out.println("group_products = new Array();");
343: out.println(" group_products[0] = new Array();");
344: for (int j = 0; j < defaultGroupProducts.size(); j++) {
345: out.println(" group_products[0][" + j
346: + "] = " + defaultGroupProducts.get(j)
347: + ";");
348: }
349: out.println();
350:
351: out.println("group_display_names = new Array();");
352: out.println(" group_display_names[0] = \"\";");
353: out.println();
354: }
355:
356: out.close();
357: } catch (ManagerException e) {
358: e.printStackTrace();
359: } catch (IOException e) {
360: e.printStackTrace();
361: }
362: }
363:
364: protected void doGet(HttpServletRequest request,
365: HttpServletResponse response) throws ServletException,
366: IOException {
367: processRequest(request, response);
368: }
369:
370: protected void doPost(HttpServletRequest request,
371: HttpServletResponse response) throws ServletException,
372: IOException {
373: processRequest(request, response);
374: }
375: }
|