1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.palm;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.PortletConfig;
27 import javax.portlet.PortletContext;
28 import javax.portlet.PortletException;
29 import javax.portlet.PortletMode;
30 import javax.portlet.PortletRequest;
31 import javax.portlet.RenderRequest;
32 import javax.portlet.RenderResponse;
33
34 import org.apache.jetspeed.CommonPortletServices;
35 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
36 import org.apache.jetspeed.components.portletregistry.RegistryException;
37 import org.apache.jetspeed.factory.PortletFactory;
38 import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
39 import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager;
40 import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManagerResult;
41 import org.apache.portals.bridges.common.GenericServletPortlet;
42 import org.apache.portals.gems.util.StatusMessage;
43 import org.apache.portals.messaging.PortletMessaging;
44
45 /***
46 * PALM Portlet
47 *
48 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
49 * @version $Id: PortletApplicationLifecycleManager.java 348264 2005-11-22 22:06:45Z taylor $
50 */
51 public class PortletApplicationLifecycleManager extends GenericServletPortlet
52 {
53 private ApplicationServerManager asm;
54 private PortletRegistry registry;
55 private PortletFactory portletFactory;
56 private boolean serverManagerAvailable;
57
58 public void init(PortletConfig config)
59 throws PortletException
60 {
61 super.init(config);
62 PortletContext context = getPortletContext();
63 registry = (PortletRegistry)context.getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
64 portletFactory = (PortletFactory)context.getAttribute(CommonPortletServices.CPS_PORTLET_FACTORY_COMPONENT);
65 asm = (ApplicationServerManager)context.getAttribute(CommonPortletServices.CPS_APPLICATION_SERVER_MANAGER_COMPONENT);
66 if (null == registry)
67 {
68 throw new PortletException("Failed to find the Portlet Registry on portlet initialization");
69 }
70 if (null == portletFactory)
71 {
72 throw new PortletException("Failed to find the Portlet Factory on portlet initialization");
73 }
74 serverManagerAvailable = (asm != null && asm.isConnected());
75 }
76
77 public void doView(RenderRequest request, RenderResponse response)
78 throws PortletException, IOException
79 {
80 request.setAttribute("serverManagerAvailable",serverManagerAvailable?Boolean.TRUE:Boolean.FALSE);
81
82 StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, "PALM", "status");
83 if (msg != null)
84 {
85 request.setAttribute("statusMsg", msg);
86 }
87 if ( request.getPortletSession().getAttribute("list") == null )
88 {
89 List list = new ArrayList();
90 Iterator apps = registry.getPortletApplications().iterator();
91 while (apps.hasNext())
92 {
93 MutablePortletApplication pa = (MutablePortletApplication)apps.next();
94 PortletApplicationStatusBean bean = new PortletApplicationStatusBean(pa, portletFactory.isPortletApplicationRegistered(pa));
95 list.add(bean);
96 }
97 request.getPortletSession().setAttribute("list",list);
98 }
99
100 super.doView(request, response);
101 }
102
103 public void processAction(ActionRequest request, ActionResponse response)
104 throws PortletException, IOException
105 {
106 if (request.getPortletMode() == PortletMode.VIEW)
107 {
108 String action = request.getParameter("action");
109 String value = request.getParameter("value");
110
111 if ( !isEmpty(action))
112 {
113
114 request.getPortletSession().removeAttribute("list");
115
116 if (!action.equals("refresh") && !isEmpty(value))
117 {
118 MutablePortletApplication pa = registry.getPortletApplication(value);
119 if ( pa == null )
120 {
121 publishStatusMessage(request, "PALM", "status", null, "Portlet Application for lookup value "+value+" no longer exists");
122 }
123 else if ( pa.getApplicationType() == MutablePortletApplication.LOCAL )
124 {
125
126 }
127 else
128 {
129 if (action.equals("start"))
130 {
131 startPA(request,pa);
132 }
133 else if (action.equals("stop"))
134 {
135 stopPA(request,pa);
136 }
137 else if (action.equals("undeploy"))
138 {
139 undeployPA(request,pa);
140 }
141 else if (action.equals("delete"))
142 {
143 deletePA(request,pa);
144 }
145 }
146 }
147 }
148 }
149 }
150
151 protected void publishStatusMessage(PortletRequest request, String portlet, String topic, Throwable e, String message)
152 {
153 if ( e != null )
154 {
155 message = message + ": " + e.toString();
156 Throwable cause = e.getCause();
157 if (cause != null)
158 {
159 message = message + ", " + cause.getMessage();
160 }
161 }
162 StatusMessage sm = new StatusMessage(message, StatusMessage.ERROR);
163 try
164 {
165
166 PortletMessaging.publish(request, portlet, topic, sm);
167 }
168 catch (Exception ee)
169 {
170 System.err.println("Failed to publish message: " + message);
171 }
172 }
173
174 protected void startPA(ActionRequest request, MutablePortletApplication pa)
175 {
176 if ( portletFactory.isPortletApplicationRegistered(pa))
177 {
178 publishStatusMessage(request, "PALM", "status", null, "Portlet Application "+pa.getName()+" already running");
179 }
180 else if ( !serverManagerAvailable || !asm.isConnected() )
181 {
182 publishStatusMessage(request, "PALM", "status", null, "Application Server Manager not available");
183 }
184 else
185 {
186 try
187 {
188 ApplicationServerManagerResult result = asm.start(pa.getWebApplicationDefinition().getContextRoot());
189 if ( !result.isOk() )
190 {
191 publishStatusMessage(request, "PALM", "status", null, result.getMessage());
192 }
193 }
194 catch (Exception e)
195 {
196 e.printStackTrace();
197 publishStatusMessage(request, "PAM", "status", e, "Could not start Portlet Application "+pa.getName());
198 }
199 }
200 }
201
202 protected void stopPA(ActionRequest request, MutablePortletApplication pa)
203 {
204 if ( !portletFactory.isPortletApplicationRegistered(pa))
205 {
206 publishStatusMessage(request, "PALM", "status", null, "Portlet Application "+pa.getName()+" no longer running");
207 }
208 else if ( !serverManagerAvailable || !asm.isConnected() )
209 {
210 publishStatusMessage(request, "PALM", "status", null, "Application Server Manager not available");
211 }
212 else
213 {
214 try
215 {
216 ApplicationServerManagerResult result = asm.stop(pa.getWebApplicationDefinition().getContextRoot());
217 if ( !result.isOk() )
218 {
219 publishStatusMessage(request, "PALM", "status", null, result.getMessage());
220 }
221 }
222 catch (Exception e)
223 {
224 e.printStackTrace();
225 publishStatusMessage(request, "PALM", "status", e, "Could not stop Portlet Application "+pa.getName());
226 }
227 }
228 }
229
230 protected void undeployPA(ActionRequest request, MutablePortletApplication pa)
231 {
232 if ( !serverManagerAvailable || !asm.isConnected() )
233 {
234 publishStatusMessage(request, "PALM", "status", null, "Application Server Manager not available");
235 }
236 else
237 {
238 try
239 {
240 ApplicationServerManagerResult result = asm.undeploy(pa.getWebApplicationDefinition().getContextRoot());
241 if ( !result.isOk() )
242 {
243 publishStatusMessage(request, "PALM", "status", null, result.getMessage());
244 }
245 }
246 catch (Exception e)
247 {
248 e.printStackTrace();
249 publishStatusMessage(request, "PALM", "status", e, "Could not undeploy Portlet Application "+pa.getName());
250 }
251 }
252 }
253
254 protected void deletePA(ActionRequest request, MutablePortletApplication pa)
255 {
256 if ( portletFactory.isPortletApplicationRegistered(pa))
257 {
258 publishStatusMessage(request, "PALM", "status", null, "Portlet Application "+pa.getName()+" is still running");
259 }
260 else
261 {
262 try
263 {
264 registry.removeApplication(pa);
265 }
266 catch (RegistryException e)
267 {
268 e.printStackTrace();
269 publishStatusMessage(request, "PALM", "status", e, "Could not delete Portlet Application "+pa.getName());
270 }
271 }
272 }
273
274 private boolean isEmpty(String s)
275 {
276 if (s == null) return true;
277
278 if (s.trim().equals("")) return true;
279
280 return false;
281 }
282 }