KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > servlets > InstallServlet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.servlets;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.*;
16
17 import javax.servlet.*;
18 import javax.servlet.http.*;
19
20 import org.eclipse.jface.wizard.*;
21 import org.eclipse.swt.custom.*;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.update.core.*;
24 import org.eclipse.update.internal.search.*;
25 import org.eclipse.update.internal.ui.*;
26 import org.eclipse.update.internal.ui.parts.*;
27 import org.eclipse.update.internal.ui.wizards.*;
28 import org.eclipse.update.operations.*;
29 import org.eclipse.update.search.*;
30
31 /**
32  *
33  * To change this generated comment edit the template variable "typecomment":
34  * Window>Preferences>Java>Templates.
35  * To enable and disable the creation of type comments go to
36  * Window>Preferences>Java>Code Generation.
37  */

38 public class InstallServlet extends HttpServlet {
39     private ServletConfig servletConfig;
40     public static final String JavaDoc SERVLET_NAME = "/InstallServlet"; //$NON-NLS-1$
41

42     public void init(ServletConfig config) throws ServletException {
43         this.servletConfig = config;
44     }
45
46     public void destroy() {
47     }
48
49     public ServletConfig getServletConfig() {
50         return servletConfig;
51     }
52
53     public String JavaDoc getServletInfo() {
54         return "Eclipse Install servlet"; //$NON-NLS-1$
55
}
56
57     public void service(
58         HttpServletRequest servletRequest,
59         HttpServletResponse servletResponse)
60         throws ServletException, IOException {
61         PrintWriter writer =
62             ServletsUtil.createResponsePrologue(servletResponse);
63         execute(writer, servletRequest);
64         ServletsUtil.createResponseEpilogue(
65             servletRequest,
66             servletResponse,
67             writer);
68     }
69
70     /**
71      * Method execute.
72      * @param servletRequest
73      */

74     private void execute(
75         PrintWriter writer,
76         HttpServletRequest servletRequest) {
77         String JavaDoc serverURL = servletRequest.getParameter("server"); //$NON-NLS-1$
78
String JavaDoc license = servletRequest.getParameter("license"); //$NON-NLS-1$
79
boolean needLicensePage = true;
80         if (license != null && license.equalsIgnoreCase("false")) //$NON-NLS-1$
81
needLicensePage = false;
82
83         String JavaDoc[] versionedIds = servletRequest.getParameterValues("feature"); //$NON-NLS-1$
84

85         if (serverURL == null) {
86             createPageError(writer, UpdateUI.getString("InstallServlet.unknownServerURL")); //$NON-NLS-1$
87
return;
88         }
89         if (versionedIds == null) {
90             createPageError(writer, UpdateUI.getString("InstallServlet.noFeatures")); //$NON-NLS-1$
91
return;
92         }
93         if (OperationsManager.isInProgress()) {
94             ServletsUtil.createError(writer, UpdateUI.getString("InstallServlet.inProgress"), null); //$NON-NLS-1$
95
return;
96         }
97 // if (DetailsForm.isInProgress()) {
98
// ServletsUtil.createError(writer, UpdateUI.getString("InstallServlet.inProgress"), null);
99
// return;
100
// }
101
try {
102             URL url = new URL(serverURL);
103             VersionedIdentifier[] vids =
104                 computeVersionedIdentifiers(versionedIds);
105             boolean success =
106                 executeInstall(writer, url, vids, needLicensePage);
107             if (success)
108                 ServletsUtil.createInfo(writer);
109         } catch (MalformedURLException e) {
110             createPageError(writer, UpdateUI.getFormattedMessage("InstallServlet.incorrectURLFormat", //$NON-NLS-1$
111
serverURL.toString()));
112         }
113     }
114
115     private void createPageError(PrintWriter writer, String JavaDoc problem) {
116         ServletsUtil.createError(writer, problem, UpdateUI.getString("InstallServlet.contactWebmaster")); //$NON-NLS-1$
117
}
118
119     private VersionedIdentifier[] computeVersionedIdentifiers(String JavaDoc[] array) {
120         ArrayList result = new ArrayList();
121         for (int i = 0; i < array.length; i++) {
122             String JavaDoc id_version = array[i];
123             int sep = id_version.lastIndexOf('_');
124             if (sep == -1)
125                 continue;
126             String JavaDoc id = id_version.substring(0, sep);
127             String JavaDoc version = id_version.substring(sep + 1);
128             VersionedIdentifier vid = new VersionedIdentifier(id, version);
129             result.add(vid);
130         }
131         return (VersionedIdentifier[]) result.toArray(
132             new VersionedIdentifier[result.size()]);
133     }
134
135     private boolean executeInstall(
136         final PrintWriter writer,
137         final URL siteURL,
138         final VersionedIdentifier[] vids,
139         final boolean needLicensePage) {
140
141         Display display = SWTUtil.getStandardDisplay();
142         final boolean[] result = new boolean[] { false };
143
144         display.syncExec(new Runnable JavaDoc() {
145             public void run() {
146                 final Shell shell = UpdateUI.getActiveWorkbenchShell();
147                 BusyIndicator.showWhile(shell.getDisplay(), new Runnable JavaDoc() {
148                     public void run() {
149                         result[0] =
150                             doExecuteInstall(
151                                 writer,
152                                 shell,
153                                 siteURL,
154                                 vids,
155                                 needLicensePage);
156                     }
157                 });
158             }
159         });
160         return result[0];
161     }
162
163     private boolean doExecuteInstall(
164         PrintWriter writer,
165         final Shell shell,
166         URL siteURL,
167         VersionedIdentifier[] vids,
168         final boolean needLicensePage) {
169             
170         shell.forceActive();
171
172         UpdateSearchScope searchScope = new UpdateSearchScope();
173         searchScope.addSearchSite(
174             siteURL.toString(),
175             siteURL,
176             new String JavaDoc[0]);
177
178         final UpdateSearchRequest searchRequest =
179             new UpdateSearchRequest(
180                 new SiteSearchCategory(),
181                 searchScope);
182     
183         searchRequest.addFilter(new VersionedIdentifiersFilter(vids));
184         searchRequest.addFilter(new EnvironmentFilter());
185         searchRequest.addFilter(new BackLevelFilter());
186
187         shell.getDisplay().asyncExec(new Runnable JavaDoc() {
188             public void run() {
189                 openWizard(shell, searchRequest);
190             }
191         });
192         return true;
193     }
194
195
196     private void openWizard(Shell shell, UpdateSearchRequest searchRequest) {
197         InstallWizard wizard = new InstallWizard(searchRequest);
198         WizardDialog dialog = new ResizableWizardDialog(shell, wizard);
199         dialog.create();
200 // dialog.getShell().setText(
201
// UpdateUI.getString(KEY_OPTIONAL_INSTALL_TITLE));
202
dialog.getShell().setSize(600, 500);
203         dialog.open();
204         if (wizard.isSuccessfulInstall())
205             UpdateUI.requestRestart();
206     }
207 }
208
Popular Tags