1 17 package org.apache.geronimo.console.car; 18 19 import java.io.IOException ; 20 import java.net.URL ; 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 ; 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 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 actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException { 52 String configId = request.getParameter("configId"); 53 String repo = request.getParameter("repository"); 54 String user = request.getParameter("repo-user"); 55 String 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 { 65 String configId = request.getParameter("configId"); 66 String repo = request.getParameter("repository"); 67 String user = request.getParameter("repo-user"); 68 String 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 (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 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 actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException { 97 String repo = request.getParameter("repository"); 98 String user = request.getParameter("repo-user"); 99 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 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 } 169 | Popular Tags |