KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > car > DownloadCARHandler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.console.car;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URL JavaDoc;
21 import javax.portlet.ActionRequest;
22 import javax.portlet.ActionResponse;
23 import javax.portlet.PortletException;
24 import javax.portlet.PortletSession;
25 import javax.portlet.RenderRequest;
26 import javax.portlet.RenderResponse;
27 import javax.security.auth.login.FailedLoginException JavaDoc;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.geronimo.console.MultiPageModel;
31 import org.apache.geronimo.console.ajax.ProgressInfo;
32 import org.apache.geronimo.console.util.PortletManager;
33 import org.apache.geronimo.kernel.repository.Artifact;
34 import org.apache.geronimo.system.plugin.PluginList;
35 import org.apache.geronimo.system.plugin.PluginMetadata;
36 import org.apache.geronimo.system.plugin.PluginInstaller;
37 import org.apache.geronimo.system.plugin.DownloadResults;
38
39 /**
40  * Handler for the initial download screen.
41  *
42  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
43  */

44 public class DownloadCARHandler extends BaseImportExportHandler {
45     private final static Log log = LogFactory.getLog(DownloadCARHandler.class);
46
47     public DownloadCARHandler() {
48         super(DOWNLOAD_MODE, "/WEB-INF/view/car/download.jsp");
49     }
50
51     public String JavaDoc actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
52         String JavaDoc configId = request.getParameter("configId");
53         String JavaDoc repo = request.getParameter("repository");
54         String JavaDoc user = request.getParameter("repo-user");
55         String JavaDoc pass = request.getParameter("repo-pass");
56         response.setRenderParameter("configId", configId);
57         response.setRenderParameter("repository", repo);
58         if(!isEmpty(user)) response.setRenderParameter("repo-user", user);
59         if(!isEmpty(pass)) response.setRenderParameter("repo-pass", pass);
60
61         return getMode();
62     }
63
64     public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
65         String JavaDoc configId = request.getParameter("configId");
66         String JavaDoc repo = request.getParameter("repository");
67         String JavaDoc user = request.getParameter("repo-user");
68         String JavaDoc pass = request.getParameter("repo-pass");
69         PluginMetadata config = null;
70         try {
71             PluginList list = (PluginList) request.getPortletSession(true).getAttribute(CONFIG_LIST_SESSION_KEY);
72             if(list == null) {
73                 list = PortletManager.getCurrentServer(request).getPluginInstaller().listPlugins(new URL JavaDoc(repo), user, pass);
74                 request.getPortletSession(true).setAttribute(CONFIG_LIST_SESSION_KEY, list);
75             }
76             for (int i = 0; i < list.getPlugins().length; i++) {
77                 PluginMetadata metadata = list.getPlugins()[i];
78                 if(metadata.getModuleId().toString().equals(configId)) {
79                     config = metadata;
80                     break;
81                 }
82             }
83         } catch (FailedLoginException JavaDoc e) {
84             throw new PortletException("Invalid login for Maven repository '"+repo+"'", e);
85         }
86         if(config == null) {
87             throw new PortletException("No configuration found for '"+configId+"'");
88         }
89         request.setAttribute("configId", configId);
90         request.setAttribute("dependencies", config.getDependencies());
91         request.setAttribute("repository", repo);
92         request.setAttribute("repouser", user);
93         request.setAttribute("repopass", pass);
94     }
95
96     public String JavaDoc actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
97         String JavaDoc repo = request.getParameter("repository");
98         String JavaDoc user = request.getParameter("repo-user");
99         String JavaDoc pass = request.getParameter("repo-pass");
100         boolean proceed = Boolean.valueOf(request.getParameter("proceed")).booleanValue();
101         if(proceed) {
102             String JavaDoc configId = request.getParameter("configId");
103
104             PluginList installList;
105             try {
106                 PluginList list = (PluginList) request.getPortletSession(true).getAttribute(CONFIG_LIST_SESSION_KEY);
107                 if(list == null) {
108                     list = PortletManager.getCurrentServer(request).getPluginInstaller().listPlugins(new URL JavaDoc(repo), user, pass);
109                     request.getPortletSession(true).setAttribute(CONFIG_LIST_SESSION_KEY, list);
110                 }
111                 installList = PluginList.createInstallList(list, Artifact.create(configId));
112             } catch (FailedLoginException JavaDoc e) {
113                 throw new PortletException("Invalid login for Maven repository '"+repo+"'", e);
114             }
115             if(installList == null) {
116                 throw new PortletException("No configuration found for '"+configId+"'");
117             }
118
119             PluginInstaller configInstaller = PortletManager.getCurrentServer(request).getPluginInstaller();
120             Object JavaDoc downloadKey = configInstaller.startInstall(installList, user, pass);
121             ProgressInfo progressInfo = new ProgressInfo();
122             request.getPortletSession(true).setAttribute(ProgressInfo.PROGRESS_INFO_KEY, progressInfo, PortletSession.APPLICATION_SCOPE);
123             // Kick off the download monitoring
124
new Thread JavaDoc(new Installer(configInstaller, downloadKey, progressInfo, request.getPortletSession(true))).start();
125
126             response.setRenderParameter("configId", configId);
127             response.setRenderParameter("repository", repo);
128             if(!isEmpty(user)) response.setRenderParameter("repo-user", user);
129             if(!isEmpty(pass)) response.setRenderParameter("repo-pass", pass);
130         }
131         return DOWNLOAD_STATUS_MODE+BEFORE_ACTION;
132     }
133
134     public static class Installer implements Runnable JavaDoc {
135         private PluginInstaller configInstaller;
136         private Object JavaDoc downloadKey;
137         private ProgressInfo progressInfo;
138         private PortletSession session;
139
140         public Installer(PluginInstaller configInstaller, Object JavaDoc downloadKey, ProgressInfo progressInfo, PortletSession session) {
141             this.configInstaller = configInstaller;
142             this.downloadKey = downloadKey;
143             this.progressInfo = progressInfo;
144             this.session = session;
145         }
146
147         public void run() {
148             DownloadResults results;
149
150             while (true) {
151                 results = configInstaller.checkOnInstall(downloadKey);
152                 progressInfo.setMainMessage(results.getCurrentMessage());
153                 progressInfo.setProgressPercent(results.getCurrentFilePercent());
154                 progressInfo.setFinished(results.isFinished());
155                 log.debug(progressInfo.getMainMessage());
156                 if (results.isFinished()) {
157                     log.debug("Installation finished");
158                     session.setAttribute(DOWNLOAD_RESULTS_SESSION_KEY, results);
159                     break;
160                 } else {
161                     try { Thread.sleep(1000); } catch (InterruptedException JavaDoc e) {
162                         log.error("Download monitor thread interrupted", e);
163                     }
164                 }
165             }
166         }
167     }
168 }
169
Popular Tags