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.jetspeed.portlets.palm;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletConfig;
027: import javax.portlet.PortletContext;
028: import javax.portlet.PortletException;
029: import javax.portlet.PortletMode;
030: import javax.portlet.PortletRequest;
031: import javax.portlet.RenderRequest;
032: import javax.portlet.RenderResponse;
033:
034: import org.apache.jetspeed.CommonPortletServices;
035: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
036: import org.apache.jetspeed.components.portletregistry.RegistryException;
037: import org.apache.jetspeed.factory.PortletFactory;
038: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
039: import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager;
040: import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManagerResult;
041: import org.apache.portals.bridges.common.GenericServletPortlet;
042: import org.apache.portals.gems.util.StatusMessage;
043: import org.apache.portals.messaging.PortletMessaging;
044:
045: /**
046: * PALM Portlet
047: *
048: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
049: * @version $Id: PortletApplicationLifecycleManager.java 348264 2005-11-22 22:06:45Z taylor $
050: */
051: public class PortletApplicationLifecycleManager extends
052: GenericServletPortlet {
053: private ApplicationServerManager asm;
054: private PortletRegistry registry;
055: private PortletFactory portletFactory;
056: private boolean serverManagerAvailable;
057:
058: public void init(PortletConfig config) throws PortletException {
059: super .init(config);
060: PortletContext context = getPortletContext();
061: registry = (PortletRegistry) context
062: .getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
063: portletFactory = (PortletFactory) context
064: .getAttribute(CommonPortletServices.CPS_PORTLET_FACTORY_COMPONENT);
065: asm = (ApplicationServerManager) context
066: .getAttribute(CommonPortletServices.CPS_APPLICATION_SERVER_MANAGER_COMPONENT);
067: if (null == registry) {
068: throw new PortletException(
069: "Failed to find the Portlet Registry on portlet initialization");
070: }
071: if (null == portletFactory) {
072: throw new PortletException(
073: "Failed to find the Portlet Factory on portlet initialization");
074: }
075: serverManagerAvailable = (asm != null && asm.isConnected());
076: }
077:
078: public void doView(RenderRequest request, RenderResponse response)
079: throws PortletException, IOException {
080: request.setAttribute("serverManagerAvailable",
081: serverManagerAvailable ? Boolean.TRUE : Boolean.FALSE);
082:
083: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
084: request, "PALM", "status");
085: if (msg != null) {
086: request.setAttribute("statusMsg", msg);
087: }
088: if (request.getPortletSession().getAttribute("list") == null) {
089: List list = new ArrayList();
090: Iterator apps = registry.getPortletApplications()
091: .iterator();
092: while (apps.hasNext()) {
093: MutablePortletApplication pa = (MutablePortletApplication) apps
094: .next();
095: PortletApplicationStatusBean bean = new PortletApplicationStatusBean(
096: pa, portletFactory
097: .isPortletApplicationRegistered(pa));
098: list.add(bean);
099: }
100: request.getPortletSession().setAttribute("list", list);
101: }
102:
103: super .doView(request, response);
104: }
105:
106: public void processAction(ActionRequest request,
107: ActionResponse response) throws PortletException,
108: IOException {
109: if (request.getPortletMode() == PortletMode.VIEW) {
110: String action = request.getParameter("action");
111: String value = request.getParameter("value");
112:
113: if (!isEmpty(action)) {
114: // enforce list is rebuild next doView
115: request.getPortletSession().removeAttribute("list");
116:
117: if (!action.equals("refresh") && !isEmpty(value)) {
118: MutablePortletApplication pa = registry
119: .getPortletApplication(value);
120: if (pa == null) {
121: publishStatusMessage(request, "PALM", "status",
122: null,
123: "Portlet Application for lookup value "
124: + value + " no longer exists");
125: } else if (pa.getApplicationType() == MutablePortletApplication.LOCAL) {
126: // TODO
127: } else // ( pa.getApplicationType() == MutablePortletApplication.WEBAPP )
128: {
129: if (action.equals("start")) {
130: startPA(request, pa);
131: } else if (action.equals("stop")) {
132: stopPA(request, pa);
133: } else if (action.equals("undeploy")) {
134: undeployPA(request, pa);
135: } else if (action.equals("delete")) {
136: deletePA(request, pa);
137: }
138: }
139: }
140: }
141: }
142: }
143:
144: protected void publishStatusMessage(PortletRequest request,
145: String portlet, String topic, Throwable e, String message) {
146: if (e != null) {
147: message = message + ": " + e.toString();
148: Throwable cause = e.getCause();
149: if (cause != null) {
150: message = message + ", " + cause.getMessage();
151: }
152: }
153: StatusMessage sm = new StatusMessage(message,
154: StatusMessage.ERROR);
155: try {
156: // TODO: fixme, bug in Pluto on portlet session
157: PortletMessaging.publish(request, portlet, topic, sm);
158: } catch (Exception ee) {
159: System.err.println("Failed to publish message: " + message);
160: }
161: }
162:
163: protected void startPA(ActionRequest request,
164: MutablePortletApplication pa) {
165: if (portletFactory.isPortletApplicationRegistered(pa)) {
166: publishStatusMessage(request, "PALM", "status", null,
167: "Portlet Application " + pa.getName()
168: + " already running");
169: } else if (!serverManagerAvailable || !asm.isConnected()) {
170: publishStatusMessage(request, "PALM", "status", null,
171: "Application Server Manager not available");
172: } else {
173: try {
174: ApplicationServerManagerResult result = asm
175: .start(pa.getWebApplicationDefinition()
176: .getContextRoot());
177: if (!result.isOk()) {
178: publishStatusMessage(request, "PALM", "status",
179: null, result.getMessage());
180: }
181: } catch (Exception e) {
182: e.printStackTrace();
183: publishStatusMessage(request, "PAM", "status", e,
184: "Could not start Portlet Application "
185: + pa.getName());
186: }
187: }
188: }
189:
190: protected void stopPA(ActionRequest request,
191: MutablePortletApplication pa) {
192: if (!portletFactory.isPortletApplicationRegistered(pa)) {
193: publishStatusMessage(request, "PALM", "status", null,
194: "Portlet Application " + pa.getName()
195: + " no longer running");
196: } else if (!serverManagerAvailable || !asm.isConnected()) {
197: publishStatusMessage(request, "PALM", "status", null,
198: "Application Server Manager not available");
199: } else {
200: try {
201: ApplicationServerManagerResult result = asm
202: .stop(pa.getWebApplicationDefinition()
203: .getContextRoot());
204: if (!result.isOk()) {
205: publishStatusMessage(request, "PALM", "status",
206: null, result.getMessage());
207: }
208: } catch (Exception e) {
209: e.printStackTrace();
210: publishStatusMessage(request, "PALM", "status", e,
211: "Could not stop Portlet Application "
212: + pa.getName());
213: }
214: }
215: }
216:
217: protected void undeployPA(ActionRequest request,
218: MutablePortletApplication pa) {
219: if (!serverManagerAvailable || !asm.isConnected()) {
220: publishStatusMessage(request, "PALM", "status", null,
221: "Application Server Manager not available");
222: } else {
223: try {
224: ApplicationServerManagerResult result = asm
225: .undeploy(pa.getWebApplicationDefinition()
226: .getContextRoot());
227: if (!result.isOk()) {
228: publishStatusMessage(request, "PALM", "status",
229: null, result.getMessage());
230: }
231: } catch (Exception e) {
232: e.printStackTrace();
233: publishStatusMessage(request, "PALM", "status", e,
234: "Could not undeploy Portlet Application "
235: + pa.getName());
236: }
237: }
238: }
239:
240: protected void deletePA(ActionRequest request,
241: MutablePortletApplication pa) {
242: if (portletFactory.isPortletApplicationRegistered(pa)) {
243: publishStatusMessage(request, "PALM", "status", null,
244: "Portlet Application " + pa.getName()
245: + " is still running");
246: } else {
247: try {
248: registry.removeApplication(pa);
249: } catch (RegistryException e) {
250: e.printStackTrace();
251: publishStatusMessage(request, "PALM", "status", e,
252: "Could not delete Portlet Application "
253: + pa.getName());
254: }
255: }
256: }
257:
258: private boolean isEmpty(String s) {
259: if (s == null)
260: return true;
261:
262: if (s.trim().equals(""))
263: return true;
264:
265: return false;
266: }
267: }
|