001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.console.car;
018
019 import java.io.IOException;
020 import java.net.URL;
021 import javax.portlet.ActionRequest;
022 import javax.portlet.ActionResponse;
023 import javax.portlet.PortletException;
024 import javax.portlet.PortletSession;
025 import javax.portlet.RenderRequest;
026 import javax.portlet.RenderResponse;
027 import javax.security.auth.login.FailedLoginException;
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030 import org.apache.geronimo.console.MultiPageModel;
031 import org.apache.geronimo.console.ajax.ProgressInfo;
032 import org.apache.geronimo.console.util.PortletManager;
033 import org.apache.geronimo.kernel.repository.Artifact;
034 import org.apache.geronimo.system.plugin.PluginList;
035 import org.apache.geronimo.system.plugin.PluginMetadata;
036 import org.apache.geronimo.system.plugin.PluginInstaller;
037 import org.apache.geronimo.system.plugin.DownloadResults;
038
039 /**
040 * Handler for the initial download screen.
041 *
042 * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
043 */
044 public class DownloadCARHandler extends BaseImportExportHandler {
045 private final static Log log = LogFactory.getLog(DownloadCARHandler.class);
046
047 public DownloadCARHandler() {
048 super(DOWNLOAD_MODE, "/WEB-INF/view/car/download.jsp");
049 }
050
051 public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
052 String configId = request.getParameter("configId");
053 String repo = request.getParameter("repository");
054 String user = request.getParameter("repo-user");
055 String pass = request.getParameter("repo-pass");
056 response.setRenderParameter("configId", configId);
057 response.setRenderParameter("repository", repo);
058 if(!isEmpty(user)) response.setRenderParameter("repo-user", user);
059 if(!isEmpty(pass)) response.setRenderParameter("repo-pass", pass);
060
061 return getMode();
062 }
063
064 public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
065 String configId = request.getParameter("configId");
066 String repo = request.getParameter("repository");
067 String user = request.getParameter("repo-user");
068 String pass = request.getParameter("repo-pass");
069 PluginMetadata config = null;
070 try {
071 PluginList list = (PluginList) request.getPortletSession(true).getAttribute(CONFIG_LIST_SESSION_KEY);
072 if(list == null) {
073 list = PortletManager.getCurrentServer(request).getPluginInstaller().listPlugins(new URL(repo), user, pass);
074 request.getPortletSession(true).setAttribute(CONFIG_LIST_SESSION_KEY, list);
075 }
076 for (int i = 0; i < list.getPlugins().length; i++) {
077 PluginMetadata metadata = list.getPlugins()[i];
078 if(metadata.getModuleId().toString().equals(configId)) {
079 config = metadata;
080 break;
081 }
082 }
083 } catch (FailedLoginException e) {
084 throw new PortletException("Invalid login for Maven repository '"+repo+"'", e);
085 }
086 if(config == null) {
087 throw new PortletException("No configuration found for '"+configId+"'");
088 }
089 request.setAttribute("configId", configId);
090 request.setAttribute("dependencies", config.getDependencies());
091 request.setAttribute("repository", repo);
092 request.setAttribute("repouser", user);
093 request.setAttribute("repopass", pass);
094 }
095
096 public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
097 String repo = request.getParameter("repository");
098 String user = request.getParameter("repo-user");
099 String pass = request.getParameter("repo-pass");
100 boolean proceed = Boolean.valueOf(request.getParameter("proceed")).booleanValue();
101 if(proceed) {
102 String 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(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 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 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(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 {
135 private PluginInstaller configInstaller;
136 private Object downloadKey;
137 private ProgressInfo progressInfo;
138 private PortletSession session;
139
140 public Installer(PluginInstaller configInstaller, Object 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 e) {
162 log.error("Download monitor thread interrupted", e);
163 }
164 }
165 }
166 }
167 }
168 }