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.admin.servlets;
038:
039: import java.io.IOException;
040: import java.io.PrintWriter;
041: import java.util.ArrayList;
042: import java.util.List;
043: import javax.ejb.EJB;
044: import javax.servlet.ServletException;
045: import javax.servlet.http.HttpServlet;
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.http.HttpServletResponse;
048: import org.netbeans.installer.infra.server.ejb.Manager;
049: import org.netbeans.installer.infra.server.ejb.ManagerException;
050: import org.netbeans.installer.product.components.Product;
051: import org.netbeans.installer.product.components.Group;
052: import org.netbeans.installer.product.RegistryNode;
053: import org.netbeans.installer.utils.StringUtils;
054: import org.netbeans.installer.utils.SystemUtils;
055: import org.netbeans.installer.utils.exceptions.ParseException;
056: import org.netbeans.installer.utils.helper.Platform;
057:
058: /**
059: *
060: * @author ks152834
061: * @version
062: */
063: public class ManageRegistries extends HttpServlet {
064: @EJB
065: private Manager manager;
066:
067: protected void processRequest(HttpServletRequest request,
068: HttpServletResponse response) throws ServletException,
069: IOException {
070: response.setContentType("text/html; charset=UTF-8");
071:
072: final PrintWriter out = response.getWriter();
073:
074: try {
075: final List<String> registries = manager.getRegistries();
076: final String userAgent = request.getHeader("User-Agent");
077:
078: Platform platform = SystemUtils.getCurrentPlatform();
079: if (userAgent.contains("Windows")) {
080: platform = Platform.WINDOWS;
081: }
082: if (userAgent.contains("PPC Mac OS")) {
083: platform = Platform.MACOSX_PPC;
084: }
085: if (userAgent.contains("Intel Mac OS")) {
086: platform = Platform.MACOSX_X86;
087: }
088: if (userAgent.contains("Linux")) {
089: platform = Platform.LINUX;
090: }
091: if (userAgent.contains("SunOS i86pc")) {
092: platform = Platform.SOLARIS_X86;
093: }
094: if (userAgent.contains("SunOS sun4u")) {
095: platform = Platform.SOLARIS_SPARC;
096: }
097:
098: if (request.getParameter("platform") != null) {
099: try {
100: platform = StringUtils.parsePlatform(request
101: .getParameter("platform"));
102: } catch (ParseException e) {
103: e.printStackTrace(out);
104: }
105: }
106:
107: out
108: .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
109: out.println("<html>");
110: out.println(" <head>");
111: out
112: .println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
113: out.println(" <title>Registries Manager</title>");
114: out
115: .println(" <link rel=\"stylesheet\" href=\"css/main.css\" type=\"text/css\"/>");
116: out
117: .println(" <script src=\"js/main.js\" type=\"text/javascript\"></script>");
118: out.println(" </head>");
119: out
120: .println(" <body onload=\"update_current_registry()\">");
121: out.println(" <div class=\"top-menu\">");
122: out
123: .println(" <a href=\"javascript: add_registry();\">Add Registry</a> |");
124: if (registries.size() > 0) {
125: out
126: .println(" <a href=\"javascript: remove_registry();\">Remove Registry</a> |");
127: out
128: .println(" <a href=\"javascript: delete_bundles();\">Delete Bundles</a> | ");
129: out
130: .println(" <a href=\"javascript: generate_bundles();\">Generate Bundles</a> | ");
131: out
132: .println(" <a href=\"javascript: export_registry();\">Export Registry</a> | ");
133: } else {
134: out.println(" Remove Registry |");
135: out.println(" Delete Bundles | ");
136: out.println(" Generate Bundles | ");
137: out.println(" Export Registry | ");
138: }
139: out
140: .println(" <a href=\"javascript: update_engine();\">Update Engine</a>");
141: out.println(" </div>");
142: out.println(" ");
143: if (registries.size() == 0) {
144: out.println(" <p>");
145: out
146: .println(" Currently there are no existing registries on this server.");
147: out.println(" </p>");
148: } else {
149: String selected = request.getParameter("registry");
150:
151: out
152: .println(" <select id=\"registries-select\" onchange=\"update_current_registry()\">");
153: for (String registry : registries) {
154: out.println(" <option value=\""
155: + registry
156: + "\""
157: + (registry.equals(selected) ? " selected"
158: : "") + ">" + registry
159: + "</option>");
160: }
161: out.println(" </select>");
162:
163: out
164: .println(" <select id=\"platforms-select\" onchange=\"update_target_platform()\">");
165: for (Platform temp : Platform.values()) {
166: out
167: .println(" <option value=\""
168: + temp.getCodeName()
169: + "\""
170: + (temp.equals(platform) ? " selected"
171: : "") + ">"
172: + temp.getDisplayName()
173: + "</option>");
174: }
175: out.println(" </select>");
176:
177: for (String registry : registries) {
178: out
179: .println(" <div class=\"registry\" id=\"registry-"
180: + registry + "\">");
181:
182: buildRegistryTable(out, registry, manager
183: .loadRegistry(registry).getRegistryRoot(),
184: platform);
185:
186: out.println(" </div>");
187: }
188: }
189: out.println(" ");
190: out
191: .println(" <form name=\"Form\" method=\"post\" enctype=\"multipart/form-data\">");
192: out
193: .println(" <input type=\"hidden\" name=\"fallback_base\" value=\""
194: + request.getRequestURL() + "\"/>");
195: out
196: .println(" <input type=\"hidden\" name=\"fallback\"/>");
197: out
198: .println(" <input type=\"hidden\" name=\"uid\"/>");
199: out
200: .println(" <input type=\"hidden\" name=\"version\"/>");
201: out
202: .println(" <input type=\"hidden\" name=\"platforms\"/>");
203: out
204: .println(" <div class=\"pop-up\" id=\"form-registry\">");
205: out.println(" <table>");
206: out.println(" <tr>");
207: out
208: .println(" <td colspan=\"2\">Please define a name for a new registry.</td>");
209: out.println(" </tr>");
210: out.println(" <tr>");
211: out
212: .println(" <td style=\"width: 100%\"><input type=\"text\" name=\"registry\" style=\"width: 100%\"/></td>");
213: out
214: .println(" <td><input type=\"submit\"/></td>");
215: out.println(" </tr>");
216: out.println(" <tr>");
217: out
218: .println(" <td colspan=\"2\"><a href=\"javascript: close_form_registry()\">close window</a></td>");
219: out.println(" </tr>");
220: out.println(" </table>");
221: out.println(" </div>");
222: out
223: .println(" <div class=\"pop-up\" id=\"form-codebase\">");
224: out.println(" <table>");
225: out.println(" <tr>");
226: out
227: .println(" <td colspan=\"2\">Please define the URL prefix for the export.</td>");
228: out.println(" </tr>");
229: out.println(" <tr>");
230: out
231: .println(" <td style=\"width: 100%\"><input type=\"text\" name=\"codebase\" style=\"width: 100%\"/></td>");
232: out
233: .println(" <td><input type=\"submit\"/></td>");
234: out.println(" </tr>");
235: out.println(" <tr>");
236: out
237: .println(" <td colspan=\"2\"><a href=\"javascript: close_form_codebase()\">close window</a></td>");
238: out.println(" </tr>");
239: out.println(" </table>");
240: out.println(" </div>");
241: out
242: .println(" <div class=\"pop-up\" id=\"form-archive\">");
243: out.println(" <table>");
244: out.println(" <tr>");
245: out
246: .println(" <td colspan=\"2\">Please point to a package.</td>");
247: out.println(" </tr>");
248: out.println(" <tr>");
249: out
250: .println(" <td style=\"width: 100%\"><input type=\"file\" name=\"archive\" style=\"width: 100%\"/></td>");
251: out
252: .println(" <td><input type=\"submit\"/></td>");
253: out.println(" </tr>");
254: out.println(" <tr>");
255: out
256: .println(" <td colspan=\"2\"><a href=\"javascript: close_form_archive()\">close window</a></td>");
257: out.println(" </tr>");
258: out.println(" </table>");
259: out.println(" </div>");
260: out.println(" </form>");
261: out.println(" <p class=\"small\">" + userAgent
262: + "</p>");
263: out.println(" </body>");
264: out.println("</html>");
265:
266: } catch (ManagerException e) {
267: e.printStackTrace(out);
268: }
269:
270: out.close();
271: }
272:
273: protected void doGet(HttpServletRequest request,
274: HttpServletResponse response) throws ServletException,
275: IOException {
276: processRequest(request, response);
277: }
278:
279: protected void doPost(HttpServletRequest request,
280: HttpServletResponse response) throws ServletException,
281: IOException {
282: processRequest(request, response);
283: }
284:
285: private void buildRegistryTable(PrintWriter out, String registry,
286: RegistryNode root, Platform platform) {
287: out.println(" <table class=\"registry\">");
288:
289: final ArrayList<RegistryNode> nodes = new ArrayList<RegistryNode>();
290: nodes.add(root);
291:
292: buildRegistryNodes(out, registry, nodes, platform);
293:
294: out.println(" </table>");
295: }
296:
297: private void buildRegistryNodes(PrintWriter out, String registry,
298: List<RegistryNode> nodes, Platform platform) {
299: for (RegistryNode node : nodes) {
300: try {
301: if (node instanceof Product) {
302: if (!((Product) node).getPlatforms().contains(
303: platform)) {
304: continue;
305: }
306: }
307:
308: String icon = null;
309: String displayName = node.getDisplayName();
310: String treeHandle = null;
311:
312: if (node.getIconUri() == null) {
313: icon = "img/default-icon.png";
314: } else {
315: icon = node.getIconUri().getRemote().toString();
316: }
317:
318: if (node.getChildren().size() > 0) {
319: treeHandle = "img/tree-handle-open.png";
320: } else {
321: treeHandle = "img/tree-handle-empty.png";
322: }
323:
324: String id = null;
325:
326: String uid = node.getUid();
327: String version = null;
328: String type = null;
329: String platforms = null;
330: String title = "";
331:
332: if (node instanceof Product) {
333: version = ((Product) node).getVersion().toString();
334: platforms = StringUtils.asString(((Product) node)
335: .getPlatforms(), " ");
336: title = StringUtils.asString(((Product) node)
337: .getPlatforms());
338: type = "component";
339:
340: id = registry + "_" + uid + "_" + version + "_"
341: + platforms.replace(" ", "_") + "_" + type;
342: }
343:
344: if (node instanceof Group) {
345: type = "group";
346:
347: id = registry + "_" + uid + "_" + type;
348: }
349:
350: out.println(" <tr id=\"" + id + "\">");
351:
352: out
353: .println(" <td class=\"tree-handle\"><img src=\""
354: + treeHandle
355: + "\" onclick=\"_expand('"
356: + id + "-children')\"/></td>");
357: out
358: .println(" <td class=\"icon\"><img src=\""
359: + icon + "\"/></td>");
360: out
361: .println(" <td class=\"display-name\" title=\""
362: + title + "\">" + displayName + "</td>");
363: if (node.getParent() != null) {
364: out
365: .println(" <td class=\"option\"><a href=\"javascript: remove_component('"
366: + uid
367: + "', '"
368: + version
369: + "', '"
370: + platforms
371: + "')\">Remove</a></td>");
372: } else {
373: out
374: .println(" <td class=\"option\"></td>");
375: }
376: out
377: .println(" <td class=\"option\"><a href=\"javascript: add_package('"
378: + uid
379: + "', '"
380: + version
381: + "', '"
382: + platforms
383: + "')\">Add Package</a></td>");
384:
385: out.println(" </tr>");
386:
387: if (node.getChildren().size() > 0) {
388: out.println(" <tr id=\"" + id
389: + "-children\">");
390:
391: out
392: .println(" <td class=\"tree-handle\"></td>");
393: out
394: .println(" <td colspan=\"4\" class=\"children\">");
395: out
396: .println(" <table class=\"registry\">");
397: buildRegistryNodes(out, registry, node
398: .getChildren(), platform);
399: out.println(" </table>");
400: out.println(" </td>");
401:
402: out.println(" </tr>");
403: }
404: } catch (Throwable e) {
405: e.printStackTrace(out);
406: }
407: }
408: }
409: }
|