KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > welcome > AbsentSampleServlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.welcome;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.Set JavaDoc;
22 import javax.security.auth.login.FailedLoginException JavaDoc;
23 import javax.servlet.RequestDispatcher JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
44  * Stands in for servlets that are not yet installed, offering to install them.
45  *
46  * @version $Rev: 488139 $ $Date: 2006-12-18 01:51:59 -0500 (Mon, 18 Dec 2006) $
47  */

48 public class AbsentSampleServlet extends HttpServlet JavaDoc {
49     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
50         String JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
59         RequestDispatcher JavaDoc dispatcher = getServletContext().getRequestDispatcher("/sampleNotInstalled.jsp");
60         dispatcher.forward(request, response);
61     }
62
63     private void doInstall(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
64         Kernel kernel = KernelRegistry.getSingleKernel();
65         PluginInstaller installer = getPluginInstaller(kernel);
66         String JavaDoc moduleIdName = getInitParameter("moduleId");
67         moduleIdName = moduleIdName.replaceAll("SERVER", getServerType());
68         URL JavaDoc 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 JavaDoc[]{moduleIdName});
72         DownloadResults results = installer.install(new PluginList(new URL JavaDoc[]{repo, new URL JavaDoc("http://www.ibiblio.org/maven2/")},
73                                                     new PluginMetadata[]{target}), null, null);
74         if(results.isFailed()) {
75             throw new ServletException JavaDoc("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 JavaDoc("Unable to start sample application", e);
90                 } catch (LifecycleException e) {
91                     throw new ServletException JavaDoc("Unable to start sample application", e);
92                 }
93             }
94         }
95         response.sendRedirect(request.getContextPath()+request.getServletPath()+"/");
96     }
97
98     private String JavaDoc getServerType() {
99         return getServletContext().getServerInfo().toLowerCase().indexOf("jetty") > -1 ? "jetty" : "tomcat";
100     }
101
102     private PluginInstaller getPluginInstaller(Kernel kernel) throws ServletException JavaDoc {
103         Set JavaDoc installers = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
104         if(installers.size() == 0) {
105             throw new ServletException JavaDoc("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 JavaDoc getFirstPluginRepository(Kernel kernel) throws ServletException JavaDoc {
112         Set JavaDoc installers = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName()));
113         if(installers.size() == 0) {
114             throw new ServletException JavaDoc("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 JavaDoc[] urls = repos.getRepositories();
120         if(urls.length == 0) {
121             repos.refresh();
122             urls = repos.getRepositories();
123             if(urls.length == 0) {
124                 throw new ServletException JavaDoc("Unable to install sample application; unable to download repository list");
125             }
126         }
127         return urls[0];
128     }
129 }
130
Popular Tags