1 17 package org.apache.geronimo.welcome; 18 19 import java.io.IOException ; 20 import java.net.URL ; 21 import java.util.Set ; 22 import javax.security.auth.login.FailedLoginException ; 23 import javax.servlet.RequestDispatcher ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import org.apache.geronimo.gbean.AbstractName; 29 import org.apache.geronimo.gbean.AbstractNameQuery; 30 import org.apache.geronimo.kernel.Kernel; 31 import org.apache.geronimo.kernel.KernelRegistry; 32 import org.apache.geronimo.kernel.config.ConfigurationUtil; 33 import org.apache.geronimo.kernel.config.ConfigurationManager; 34 import org.apache.geronimo.kernel.config.NoSuchConfigException; 35 import org.apache.geronimo.kernel.config.LifecycleException; 36 import org.apache.geronimo.kernel.repository.Artifact; 37 import org.apache.geronimo.system.plugin.PluginInstaller; 38 import org.apache.geronimo.system.plugin.PluginList; 39 import org.apache.geronimo.system.plugin.PluginMetadata; 40 import org.apache.geronimo.system.plugin.PluginRepositoryList; 41 import org.apache.geronimo.system.plugin.DownloadResults; 42 43 48 public class AbsentSampleServlet extends HttpServlet { 49 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 50 String install = request.getParameter("install"); 51 if(install != null && !install.equals("")) { 52 doInstall(request, response); 53 } else { 54 doMessage(request, response); 55 } 56 } 57 58 private void doMessage(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 59 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sampleNotInstalled.jsp"); 60 dispatcher.forward(request, response); 61 } 62 63 private void doInstall(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 64 Kernel kernel = KernelRegistry.getSingleKernel(); 65 PluginInstaller installer = getPluginInstaller(kernel); 66 String moduleIdName = getInitParameter("moduleId"); 67 moduleIdName = moduleIdName.replaceAll("SERVER", getServerType()); 68 URL repo = getFirstPluginRepository(kernel); 69 PluginMetadata target = new PluginMetadata("Sample Application", null, "Samples", "A sample application", 70 null, null, null, false, true); 71 target.setDependencies(new String []{moduleIdName}); 72 DownloadResults results = installer.install(new PluginList(new URL []{repo, new URL ("http://www.ibiblio.org/maven2/")}, 73 new PluginMetadata[]{target}), null, null); 74 if(results.isFailed()) { 75 throw new ServletException ("Unable to install sample application", results.getFailure()); 76 } 77 ConfigurationManager mgr = ConfigurationUtil.getConfigurationManager(kernel); 78 for (int i = 0; i < results.getInstalledConfigIDs().length; i++) { 79 Artifact artifact = results.getInstalledConfigIDs()[i]; 80 if(mgr.isConfiguration(artifact)) { 81 try { 82 if(!mgr.isLoaded(artifact)) { 83 mgr.loadConfiguration(artifact); 84 } 85 if(!mgr.isRunning(artifact)) { 86 mgr.startConfiguration(artifact); 87 } 88 } catch (NoSuchConfigException e) { 89 throw new ServletException ("Unable to start sample application", e); 90 } catch (LifecycleException e) { 91 throw new ServletException ("Unable to start sample application", e); 92 } 93 } 94 } 95 response.sendRedirect(request.getContextPath()+request.getServletPath()+"/"); 96 } 97 98 private String getServerType() { 99 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 application; unable to download repository list"); 125 } 126 } 127 return urls[0]; 128 } 129 } 130 | Popular Tags |