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.welcome;
018    
019    import java.io.IOException;
020    import java.net.URL;
021    import java.util.List;
022    import java.util.Set;
023    
024    import javax.servlet.RequestDispatcher;
025    import javax.servlet.ServletException;
026    import javax.servlet.http.HttpServlet;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.geronimo.gbean.AbstractName;
031    import org.apache.geronimo.gbean.AbstractNameQuery;
032    import org.apache.geronimo.kernel.GBeanNotFoundException;
033    import org.apache.geronimo.kernel.Kernel;
034    import org.apache.geronimo.kernel.KernelRegistry;
035    import org.apache.geronimo.kernel.config.ConfigurationManager;
036    import org.apache.geronimo.kernel.config.ConfigurationUtil;
037    import org.apache.geronimo.kernel.config.LifecycleException;
038    import org.apache.geronimo.kernel.config.NoSuchConfigException;
039    import org.apache.geronimo.kernel.repository.Artifact;
040    import org.apache.geronimo.kernel.repository.Dependency;
041    import org.apache.geronimo.kernel.repository.ImportType;
042    import org.apache.geronimo.system.plugin.DownloadResults;
043    import org.apache.geronimo.system.plugin.PluginInstaller;
044    import org.apache.geronimo.system.plugin.PluginInstallerGBean;
045    import org.apache.geronimo.system.plugin.PluginRepositoryList;
046    import org.apache.geronimo.system.plugin.model.PluginArtifactType;
047    import org.apache.geronimo.system.plugin.model.PluginListType;
048    import org.apache.geronimo.system.plugin.model.PluginType;
049    
050    /**
051     * Stands in for servlets that are not yet installed, offering to install them.
052     *
053     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
054     */
055    public class AbsentSampleServlet extends HttpServlet {
056        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
057            String install = request.getParameter("install");
058            if (install != null && !install.equals("")) {
059                doInstall(request, response);
060            } else {
061                doMessage(request, response);
062            }
063        }
064    
065        private void doMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
066            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sampleNotInstalled.jsp");
067            dispatcher.forward(request, response);
068        }
069    
070        private void doInstall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
071            Kernel kernel = KernelRegistry.getSingleKernel();
072            PluginInstaller installer = getPluginInstaller(kernel);
073            String moduleIdName = getInitParameter("moduleId");
074            moduleIdName = moduleIdName.replaceAll("SERVER", getServerType());
075            URL repo = getFirstPluginRepository(kernel);
076            PluginType target = new PluginType();
077            target.setName("Sample Application");
078            target.setCategory("Samples");
079            target.setDescription("A sample application");
080            PluginArtifactType instance = new PluginArtifactType();
081            target.getPluginArtifact().add(instance);
082            instance.getDependency().add(PluginInstallerGBean.toDependencyType(new Dependency(Artifact.create(moduleIdName), ImportType.ALL), true));
083            PluginListType list = new PluginListType();
084            list.getPlugin().add(target);
085    //        list.getDefaultRepository().add(repo.toString());
086            //todo this is surely wrong
087            list.getDefaultRepository().add("http://www.ibiblio.org/maven2/");
088            DownloadResults results = installer.install(list, repo.toString(), false, null, null);
089            if (results.isFailed()) {
090                throw new ServletException("Unable to install sample application", results.getFailure());
091            }
092            ConfigurationManager mgr = ConfigurationUtil.getConfigurationManager(kernel);
093            for (Artifact artifact: results.getInstalledConfigIDs()) {
094                if (mgr.isConfiguration(artifact)) {
095                    try {
096                        if (!mgr.isLoaded(artifact)) {
097                            mgr.loadConfiguration(artifact);
098                        }
099                        if (!mgr.isRunning(artifact)) {
100                            mgr.startConfiguration(artifact);
101                        }
102                    } catch (NoSuchConfigException e) {
103                        throw new ServletException("Unable to start sample application", e);
104                    } catch (LifecycleException e) {
105                        throw new ServletException("Unable to start sample application", e);
106                    }
107                }
108            }
109            response.sendRedirect(request.getContextPath() + request.getServletPath() + "/");
110        }
111    
112        private String getServerType() {
113            return getServletContext().getServerInfo().toLowerCase().indexOf("jetty") > -1 ? "jetty" : "tomcat";
114        }
115    
116        private PluginInstaller getPluginInstaller(Kernel kernel) throws ServletException {
117            Set installers = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
118            if (installers.size() == 0) {
119                throw new ServletException("Unable to install sample application; no plugin installer found");
120            }
121            try {
122                return (PluginInstaller) kernel.getGBean((AbstractName) installers.iterator().next());
123            } catch (GBeanNotFoundException e) {
124                throw new ServletException("Unable to install sample application, plugin installer cannot be retrieved from kernel");
125            }
126        }
127    
128        private URL getFirstPluginRepository(Kernel kernel) throws ServletException {
129            Set installers = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName()));
130            if (installers.size() == 0) {
131                throw new ServletException("Unable to install sample application; no plugin repository list found");
132            }
133            PluginRepositoryList repos = ((PluginRepositoryList) kernel.getProxyManager().createProxy((AbstractName) installers.iterator().next(),
134                    PluginRepositoryList.class));
135    
136            List<URL> urls = repos.getRepositories();
137            if (urls.isEmpty()) {
138                repos.refresh();
139                urls = repos.getRepositories();
140                if (urls.isEmpty()) {
141                    throw new ServletException("Unable to install sample applicatoin; unable to download repository list");
142                }
143            }
144            return urls.get(0);
145        }
146    }