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.Set; 022 import javax.security.auth.login.FailedLoginException; 023 import javax.servlet.RequestDispatcher; 024 import javax.servlet.ServletException; 025 import javax.servlet.http.HttpServlet; 026 import javax.servlet.http.HttpServletRequest; 027 import javax.servlet.http.HttpServletResponse; 028 import org.apache.geronimo.gbean.AbstractName; 029 import org.apache.geronimo.gbean.AbstractNameQuery; 030 import org.apache.geronimo.kernel.Kernel; 031 import org.apache.geronimo.kernel.KernelRegistry; 032 import org.apache.geronimo.kernel.config.ConfigurationUtil; 033 import org.apache.geronimo.kernel.config.ConfigurationManager; 034 import org.apache.geronimo.kernel.config.NoSuchConfigException; 035 import org.apache.geronimo.kernel.config.LifecycleException; 036 import org.apache.geronimo.kernel.repository.Artifact; 037 import org.apache.geronimo.system.plugin.PluginInstaller; 038 import org.apache.geronimo.system.plugin.PluginList; 039 import org.apache.geronimo.system.plugin.PluginMetadata; 040 import org.apache.geronimo.system.plugin.PluginRepositoryList; 041 import org.apache.geronimo.system.plugin.DownloadResults; 042 043 /** 044 * Stands in for servlets that are not yet installed, offering to install them. 045 * 046 * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $ 047 */ 048 public class AbsentSampleServlet extends HttpServlet { 049 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 050 String install = request.getParameter("install"); 051 if(install != null && !install.equals("")) { 052 doInstall(request, response); 053 } else { 054 doMessage(request, response); 055 } 056 } 057 058 private void doMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 059 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sampleNotInstalled.jsp"); 060 dispatcher.forward(request, response); 061 } 062 063 private void doInstall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 064 Kernel kernel = KernelRegistry.getSingleKernel(); 065 PluginInstaller installer = getPluginInstaller(kernel); 066 String moduleIdName = getInitParameter("moduleId"); 067 moduleIdName = moduleIdName.replaceAll("SERVER", getServerType()); 068 URL repo = getFirstPluginRepository(kernel); 069 PluginMetadata target = new PluginMetadata("Sample Application", null, "Samples", "A sample application", 070 null, null, null, false, true); 071 target.setDependencies(new String[]{moduleIdName}); 072 DownloadResults results = installer.install(new PluginList(new URL[]{repo, new URL("http://www.ibiblio.org/maven2/")}, 073 new PluginMetadata[]{target}), null, null); 074 if(results.isFailed()) { 075 throw new ServletException("Unable to install sample application", results.getFailure()); 076 } 077 ConfigurationManager mgr = ConfigurationUtil.getConfigurationManager(kernel); 078 for (int i = 0; i < results.getInstalledConfigIDs().length; i++) { 079 Artifact artifact = results.getInstalledConfigIDs()[i]; 080 if(mgr.isConfiguration(artifact)) { 081 try { 082 if(!mgr.isLoaded(artifact)) { 083 mgr.loadConfiguration(artifact); 084 } 085 if(!mgr.isRunning(artifact)) { 086 mgr.startConfiguration(artifact); 087 } 088 } catch (NoSuchConfigException e) { 089 throw new ServletException("Unable to start sample application", e); 090 } catch (LifecycleException e) { 091 throw new ServletException("Unable to start sample application", e); 092 } 093 } 094 } 095 response.sendRedirect(request.getContextPath()+request.getServletPath()+"/"); 096 } 097 098 private String getServerType() { 099 return getServletContext().getServerInfo().toLowerCase().indexOf("jetty") > -1 ? "jetty" : "tomcat"; 100 } 101 102 private PluginInstaller getPluginInstaller(Kernel kernel) throws ServletException { 103 Set installers = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName())); 104 if(installers.size() == 0) { 105 throw new ServletException("Unable to install sample application; no plugin installer found"); 106 } 107 return (PluginInstaller)kernel.getProxyManager().createProxy((AbstractName) installers.iterator().next(), 108 PluginInstaller.class); 109 } 110 111 private URL getFirstPluginRepository(Kernel kernel) throws ServletException { 112 Set installers = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName())); 113 if(installers.size() == 0) { 114 throw new ServletException("Unable to install sample application; no plugin repository list found"); 115 } 116 PluginRepositoryList repos = ((PluginRepositoryList) kernel.getProxyManager().createProxy((AbstractName) installers.iterator().next(), 117 PluginRepositoryList.class)); 118 119 URL[] urls = repos.getRepositories(); 120 if(urls.length == 0) { 121 repos.refresh(); 122 urls = repos.getRepositories(); 123 if(urls.length == 0) { 124 throw new ServletException("Unable to install sample applicatoin; unable to download repository list"); 125 } 126 } 127 return urls[0]; 128 } 129 }