KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > service > container > EditEarAction


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EditEarAction.java,v 1.14.2.1 2005/08/11 15:24:58 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.service.container;
27
28 import java.io.IOException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.management.MalformedObjectNameException JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import org.apache.struts.action.ActionForm;
40 import org.apache.struts.action.ActionForward;
41 import org.apache.struts.action.ActionMapping;
42 import org.objectweb.jonas.container.EJBServiceImpl;
43 import org.objectweb.jonas.jmx.J2eeObjectName;
44 import org.objectweb.jonas.jmx.JonasManagementRepr;
45 import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
46 import org.objectweb.jonas.webapp.jonasadmin.JonasBaseAction;
47 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
48 import org.objectweb.jonas.webapp.jonasadmin.common.ModuleItem;
49
50 /**
51  * @author Michel-Ange ANTON
52  * @author Adriana Danes
53  */

54
55 public class EditEarAction extends JonasBaseAction {
56
57 // --------------------------------------------------------- Public Methods
58
/**
59      * Used to initialize the name variable of a ModuleItem when we could not
60      * identify the J2EEModule corresponding to a given URL which containes such
61      * a module contained in a given ear.
62      * This situation can arrive only with WebModules (in this case the MBeans and
63      * their ObjectNames are created by CATALINA, not by JOnAS).
64      */

65     private static String JavaDoc NONAME = "";
66
67     public ActionForward executeAction(ActionMapping pMapping, ActionForm pForm
68             , HttpServletRequest JavaDoc pRequest, HttpServletResponse JavaDoc pResponse)
69     throws IOException JavaDoc, ServletException JavaDoc {
70
71         // Selected ear
72
String JavaDoc sObjectName = pRequest.getParameter("select");
73
74         ObjectName JavaDoc oObjectName = null;
75         String JavaDoc domainName = null;
76         String JavaDoc serverName = null;
77         String JavaDoc appName = null;
78
79         // Form used
80
EarForm oForm = null;
81         if (sObjectName != null) {
82             String JavaDoc sPath = null;
83             try {
84                 // Recreate ObjectName
85
oObjectName = ObjectName.getInstance(sObjectName);
86                 domainName = oObjectName.getDomain();
87                 serverName = oObjectName.getKeyProperty("J2EEServer");
88                 appName = oObjectName.getKeyProperty("name");
89                 // Build a new form
90
oForm = new EarForm();
91                 oForm.reset(pMapping, pRequest);
92                 m_Session.setAttribute("earForm", oForm);
93                 URL JavaDoc earUrl = (java.net.URL JavaDoc) JonasManagementRepr.getAttribute(oObjectName, "earUrl");
94                 sPath = earUrl.toString();
95                 //oForm.setPath(sPath);
96
oForm.setFilename(JonasAdminJmx.extractFilename(sPath));
97                 //oForm.setObjectName(sObjectName);
98
//oForm.setModuleName(appName);
99
} catch (Throwable JavaDoc t) {
100                 addGlobalError(t);
101                 saveErrors(pRequest, m_Errors);
102                 return (pMapping.findForward("Global Error"));
103             }
104         } else {
105             // Used last form in session
106
oForm = (EarForm) m_Session.getAttribute("earForm");
107         }
108
109         // Force the node selected in tree
110
m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER) + WhereAreYou.NODE_SEPARATOR
111                 + "services" + WhereAreYou.NODE_SEPARATOR + "ear" + WhereAreYou.NODE_SEPARATOR
112                 + oForm.getFilename(), true);
113
114         // Populate
115
try {
116             if (oObjectName != null) {
117                 oForm.setEarPath((URL JavaDoc) JonasManagementRepr.getAttribute(oObjectName, "earUrl"));
118                 oForm.setXmlDeploymentDescriptor(getStringAttribute(oObjectName, "deploymentDescriptor"));
119
120                 // Use wars management attribute to construct the EarForm.wars ModuleItem list
121
// This allows using OBJECT_NAME in JSP link parameter 'on' (see earWars.jsp)
122
URL JavaDoc[] warPaths = (URL JavaDoc[]) JonasManagementRepr.getAttribute(oObjectName, "wars");
123                 String JavaDoc[] webModuleObjectNames = (String JavaDoc[]) JonasManagementRepr.getAttribute(oObjectName, "webModules");
124                 String JavaDoc[] arrayPathContext = getPathContexts(webModuleObjectNames);
125                 ArrayList JavaDoc wars = new ArrayList JavaDoc();
126                 for (int i = 0; i < warPaths.length; i++) {
127                     URL JavaDoc warPath = warPaths[i];
128                     String JavaDoc path = warPath.getPath();
129                     // Looking for a possible association between the URL and the WebModule ObjectNames
130
// This will correctly work if the file names used to pack the wars are not dummy names
131
// but significant names allowing to distinguish the wars in case we have more than one
132
// in a ear.
133
int index = getWebModuleIndex(warPath, webModuleObjectNames);
134                     ModuleItem it = null;
135                     if (index > -1) {
136                         // Could found an association between the URL and an MBean ObjectName
137
// There is a risc that the association is not correct
138
it = new ModuleItem(arrayPathContext[index], webModuleObjectNames[index], path);
139                     } else {
140                         // Could not found an association between the URL and an MBean ObjectName
141
it = new ModuleItem(NONAME, NONAME, path);
142                     }
143                     wars.add(it);
144                 }
145                 oForm.setWars(wars);
146
147                 // Use ejbJars management attribute to construct the EarForm.ejbjars ModuleItem list
148
// This allows using OBJECT_NAME in JSP link parameter 'select' (see earJars.jsp)
149
URL JavaDoc[] jarPaths = (URL JavaDoc[]) JonasManagementRepr.getAttribute(oObjectName, "ejbJars");
150                 ArrayList JavaDoc ejbJars = new ArrayList JavaDoc();
151                 for (int i = 0; i < jarPaths.length; i++) {
152                     URL JavaDoc jarPath = jarPaths[i];
153                     String JavaDoc path = jarPath.getPath();
154                     String JavaDoc name = EJBServiceImpl.buildEJBModuleName(jarPath);
155                     ObjectName JavaDoc moduleObjectName = J2eeObjectName.getEJBModule(domainName, serverName, appName, name);
156                     String JavaDoc objectName = moduleObjectName.toString();
157                     ModuleItem it = new ModuleItem(name, objectName, path);
158                     ejbJars.add(it);
159                 }
160                 oForm.setEjbjars(ejbJars);
161
162                 // Keep URL list in EarForm for the moment, as did not made changes yet in order to replace
163
// file paths with OBJECT_NAME in the case of ResourceAdapterModules
164
//oForm.setListRars((URL[]) JonasManagementRepr.getAttribute(oObjectName, "rars"));
165
URL JavaDoc[] rarPaths = (URL JavaDoc[]) JonasManagementRepr.getAttribute(oObjectName, "rars");
166                 ArrayList JavaDoc rars = new ArrayList JavaDoc();
167                 for (int i = 0; i < rarPaths.length; i++) {
168                     URL JavaDoc rarPath = rarPaths[i];
169                     String JavaDoc path = rarPath.getPath();
170                     ObjectName JavaDoc resourceAdapterObjectNames = J2eeObjectName.getResourceAdapters(domainName, appName, serverName);
171                     Iterator JavaDoc itNames = JonasManagementRepr.queryNames(resourceAdapterObjectNames).iterator();
172                     ModuleItem it = null;
173                     while (itNames.hasNext()) {
174                         ObjectName JavaDoc resourceAdapterObjectName = (ObjectName JavaDoc) itNames.next();
175                         String JavaDoc raFileName = (String JavaDoc) JonasManagementRepr.getAttribute(resourceAdapterObjectName, "fileName");
176                         if (raFileName.equals(path)) {
177                             // found the ResourceAdapater
178
String JavaDoc objectName = resourceAdapterObjectName.toString();
179                             it = new ModuleItem(resourceAdapterObjectName.getKeyProperty("name"), objectName, path);
180                             rars.add(it);
181                         }
182                     }
183                 }
184                 oForm.setRars(rars);
185             }
186         } catch (Throwable JavaDoc t) {
187             addGlobalError(t);
188             saveErrors(pRequest, m_Errors);
189             return (pMapping.findForward("Global Error"));
190         }
191         // Forward to the jsp.
192
return (pMapping.findForward("Ear"));
193     }
194
195     /**
196      * Construct an array of Strings having the same size as the webModuleObjectNames array,
197      * where each String represents the pathContext part of the name key property of the
198      * corresponding OBJECT_NAME (same index in the 2 arrays).
199      * Note that the structure of the name key property of ObjectNames created by Catalina
200      * for WebModules is:
201      * //host/pathContext
202      * @param webModuleObjectNames array of OBJECT_NAMES corresponding to WebModule MBeans
203      * @return array of pathContexts
204      */

205     private String JavaDoc[] getPathContexts(String JavaDoc[] webModuleObjectNames) {
206         int nbNames = webModuleObjectNames.length;
207         String JavaDoc[] ret = new String JavaDoc[nbNames];
208         for (int i = 0; i < nbNames; i++) {
209             ObjectName JavaDoc oObjectName = null;
210             try {
211                 oObjectName = ObjectName.getInstance(webModuleObjectNames[i]);
212             } catch (MalformedObjectNameException JavaDoc e) {
213                 ret[i] = null;
214             }
215             String JavaDoc nameKeyProp = oObjectName.getKeyProperty("name");
216             ret[i] = WebAppItem.extractLabelPathContext(nameKeyProp);
217         }
218         return ret;
219     }
220     /**
221      * Look for the occurence of a String in arrayPathContext in the war URL.
222      * If found (index > -1) we suppose that this is the name used to construct
223      * the OBJECT_NAME of the MBean associated to the war.
224      * @param warPath the URL of a war contained in the current ear
225      * @param webModuleObjectNames array of WebModule MBean OBJECT_NAMEs which are
226      * associated to wars contained in the current ear.
227      * @return -1 if no association could be made, > -1 if an association was found
228      */

229     private int getWebModuleIndex(URL JavaDoc warPath, String JavaDoc[] webModuleObjectNames) {
230         // In Jetty, JSR77 is not fully compliant yet.
231
// So, should return -1 as warURL is not in the MBean.
232
// link to bug #303876
233
if (m_WhereAreYou != null && m_WhereAreYou.isJettyServer()) {
234             return -1;
235         }
236         int index = -1;
237         for (int i = 0; i < webModuleObjectNames.length; i++) {
238             ObjectName JavaDoc webModuleObjectName = null;
239             try {
240                 webModuleObjectName = ObjectName.getInstance(webModuleObjectNames[i]);
241             } catch (MalformedObjectNameException JavaDoc e) {
242                 return -1;
243             }
244             URL JavaDoc webModuleWarPath = (URL JavaDoc) JonasManagementRepr.getAttribute(webModuleObjectName, "warURL");
245             if (warPath.equals(webModuleWarPath)) {
246                 index = i;
247                 break;
248             }
249         }
250         return index;
251     }
252 }
253
Popular Tags