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.util.ArrayList;
021    import java.util.List;
022    
023    import javax.portlet.ActionRequest;
024    import javax.portlet.ActionResponse;
025    import javax.portlet.PortletException;
026    import javax.portlet.RenderRequest;
027    import javax.portlet.RenderResponse;
028    
029    import org.apache.geronimo.console.MultiPageModel;
030    import org.apache.geronimo.kernel.repository.Dependency;
031    import org.apache.geronimo.system.plugin.DownloadResults;
032    import org.apache.geronimo.system.plugin.PluginInstaller;
033    import org.apache.geronimo.system.plugin.PluginInstallerGBean;
034    import org.apache.geronimo.system.plugin.model.PluginArtifactType;
035    import org.apache.geronimo.system.plugin.model.PluginListType;
036    import org.apache.geronimo.system.plugin.model.PluginType;
037    
038    /**
039     * Handler for the screen that shows you plugin details before you go on and
040     * install it.
041     *
042     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
043     */
044    public class ViewPluginDownloadHandler extends BaseImportExportHandler {
045    
046        public ViewPluginDownloadHandler() {
047            super(VIEW_FOR_DOWNLOAD_MODE, "/WEB-INF/view/car/viewForDownload.jsp");
048        }
049    
050        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
051            String configId = request.getParameter("configId");
052            String[] pluginIds = request.getParameterValues("plugin");
053            if (configId != null) {
054                pluginIds = new String[]{configId};
055            }
056            String repo = request.getParameter("repository");
057            String user = request.getParameter("repo-user");
058            String pass = request.getParameter("repo-pass");
059            response.setRenderParameter("pluginIds", pluginIds);
060            response.setRenderParameter("repository", repo);
061            if (!isEmpty(user)) response.setRenderParameter("repo-user", user);
062            if (!isEmpty(pass)) response.setRenderParameter("repo-pass", pass);
063    
064            return getMode();
065        }
066    
067        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
068            PluginInstaller pluginInstaller = ManagementHelper.getManagementHelper(request).getPluginInstaller();
069    
070            String[] configIds = request.getParameterValues("pluginIds");
071            String repo = request.getParameter("repository");
072            String user = request.getParameter("repo-user");
073            String pass = request.getParameter("repo-pass");
074    
075            PluginListType list = getRepoPluginList(request, pluginInstaller, repo, user, pass);
076            PluginListType installList = getPluginsFromIds(configIds, list);
077            List<PluginInfoBean> plugins = new ArrayList<PluginInfoBean>();
078            for (PluginType pluginType: installList.getPlugin()) {
079                PluginInfoBean infoBean = new PluginInfoBean();
080                infoBean.setPlugin(pluginType);
081                infoBean.setPluginArtifact(pluginType.getPluginArtifact().get(0));
082                plugins.add(infoBean);
083            }
084    
085            boolean allInstallable = true;
086            // see if the plugin is installable.  if not then provide the details
087            String validationOk = "All requirements for this plugin have been met.";
088            for (PluginInfoBean plugin : plugins) {
089                StringBuffer validationNotOk = new StringBuffer();
090                PluginType holder = PluginInstallerGBean.copy(plugin.getPlugin(), plugin.getPluginArtifact());
091                try {
092                    pluginInstaller.validatePlugin(holder);
093                } catch (Exception e) {
094                    plugin.setInstallable(false);
095                    validationNotOk.append(e.getMessage());
096                    validationNotOk.append("<BR>\n");
097                }
098                Dependency[] missingPrereqs = pluginInstaller.checkPrerequisites(holder);
099                if (missingPrereqs.length > 0) {
100                    plugin.setInstallable(false);
101                    for (Dependency dep : missingPrereqs) {
102                        validationNotOk.append(" Missing prerequisite ");
103                        validationNotOk.append(dep.getArtifact().toString());
104                        validationNotOk.append("<BR>\n");
105                    }
106                }
107                if (plugin.isInstallable()) {
108                    plugin.setValidationMessage(validationOk);
109                } else {
110                    plugin.setValidationMessage(validationNotOk.toString());
111                    allInstallable = false;
112                }
113            }
114            request.setAttribute("plugins", plugins);
115            request.setAttribute("repository", repo);
116            request.setAttribute("repouser", user);
117            request.setAttribute("repopass", pass);
118            request.setAttribute("allInstallable", allInstallable);
119            request.setAttribute("mode", "viewForDownload-after");
120        }
121    
122        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
123            PluginInstaller pluginInstaller = ManagementHelper.getManagementHelper(request).getPluginInstaller();
124            String repo = request.getParameter("repository");
125            String user = request.getParameter("repo-user");
126            String pass = request.getParameter("repo-pass");
127            String[] configIds = request.getParameterValues("configId");
128    
129            PluginListType list = getRepoPluginList(request, pluginInstaller, repo, user, pass);
130            PluginListType installList = getPluginsFromIds(configIds, list);
131    
132            Object downloadKey = pluginInstaller.startInstall(installList, repo, false, user, pass);
133            DownloadResults results = pluginInstaller.checkOnInstall(downloadKey);
134            request.getPortletSession(true).setAttribute(DOWNLOAD_RESULTS_SESSION_KEY, results);
135            
136            response.setRenderParameter("configIds", configIds);
137            response.setRenderParameter("repository", repo);
138            response.setRenderParameter("downloadKey", downloadKey.toString());
139    
140            if (!isEmpty(user)) response.setRenderParameter("repo-user", user);
141            if (!isEmpty(pass)) response.setRenderParameter("repo-pass", pass);
142            return DOWNLOAD_STATUS_MODE + BEFORE_ACTION;
143        }
144    
145    }