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