KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > joramplatform > EditJoramBaseAction


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 package org.objectweb.jonas.webapp.jonasadmin.joramplatform;
23
24 import java.util.Calendar JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import org.objectweb.jonas.jmx.JonasManagementRepr;
36 import org.objectweb.jonas.jmx.JonasObjectName;
37 import org.objectweb.jonas.jmx.JoramObjectName;
38 import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
39 import org.objectweb.jonas.webapp.jonasadmin.JonasBaseAction;
40
41 /**
42  * @author Adriana Danes
43  */

44 public abstract class EditJoramBaseAction extends JonasBaseAction {
45
46     ObjectName JavaDoc ejbServiceObjectName = null;
47
48     /**
49      * Treat specific case where Joram RAR was unloaded
50      * @param t Exception to treat
51      * @param pMapping provided by executeAction
52      * @param pRequest provided by executeAction
53      * @return
54      */

55     protected ActionForward treatError(Throwable JavaDoc pThrowable, ActionMapping pMapping, HttpServletRequest JavaDoc pRequest) {
56         String JavaDoc throwableClassName = pThrowable.getClass().getName();
57         Throwable JavaDoc t = null;
58         if (throwableClassName.equals("javax.management.InstanceNotFoundException")) {
59             t = new Throwable JavaDoc("No MBeans found for the Joram platform. Try to refresh!", pThrowable);
60         } else if (throwableClassName.equals("javax.management.MalformedObjectNameException")) {
61             t = new Throwable JavaDoc("This is not a valid name for a Joram MBean (" + pThrowable.getMessage() + ")", pThrowable);
62         }
63         addGlobalError(t);
64         saveErrors(pRequest, m_Errors);
65         return (pMapping.findForward("Global Error"));
66     }
67
68     /**
69      * Create a ItemDestination object from a String structured as follows:
70      * type=queue/topic, name=destName, id=#x.y.z
71      * @param joramAdminDestination String containing destination description
72      * @return ItemDestination containing name, type, id and ObjectName of the MBean associated to the corresponding destination
73      * @throws MalformedObjectNameException
74      */

75     public ItemDestination getDestinationItem(String JavaDoc joramAdminDestination) throws MalformedObjectNameException JavaDoc {
76         ItemDestination destinationItem = new ItemDestination();
77         ObjectName JavaDoc destinationON = null;
78         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(joramAdminDestination, ",");
79         String JavaDoc type = null;
80         String JavaDoc name = null;
81         String JavaDoc id = null;
82         String JavaDoc serverId = null;
83         while (st.hasMoreTokens()) {
84             // A couple has the following structure: key=value
85
String JavaDoc couple = st.nextToken().trim();
86             int index = couple.indexOf("=");
87             String JavaDoc key = couple.substring(0, index);
88             String JavaDoc value = couple.substring(index+1, couple.length());
89             if (key.equals("type")) {
90                 type = value;
91                 destinationItem.setType(type);
92             }
93             if (key.equals("name")) {
94                 name = value;
95                 destinationItem.setName(name);
96             }
97             if (key.equals("id")) {
98                 id = value;
99                 destinationItem.setId(id);
100                 int indexBefore = id.indexOf("#");
101                 int indexAfter = id.indexOf(".", indexBefore);
102                 serverId = id.substring(indexBefore + 1, indexAfter);
103                 destinationItem.setServerId(serverId);
104             }
105             if (name != null && type != null) {
106                 if (type.equals("topic")) {
107                     destinationON = JoramObjectName.joramTopic(name);
108                 } else if (type.equals("queue")) {
109                     destinationON = JoramObjectName.joramQueue(name);
110                 }
111                 if (JonasAdminJmx.hasMBeanName(destinationON)) {
112                     boolean deps = hasDeps(name);
113                     destinationItem.setOn(destinationON);
114                     destinationItem.setUsed(deps);
115                 }
116             }
117         }
118         return destinationItem;
119     }
120
121     /**
122      * Create a ItemUser object from a String structured as follows:
123      * User[anonymous]:#0.0.1035
124      * @param joramAdminUser String containing user description
125      * @return ItemUser containing name and ObjectName of the MBean associated to the corresponding user
126      * @throws MalformedObjectNameException
127      */

128     protected ItemUser getUserItem(String JavaDoc joramAdminUser) throws MalformedObjectNameException JavaDoc {
129         ItemUser userItem = new ItemUser();
130         joramAdminUser.trim();
131         int indextBeforeName = joramAdminUser.indexOf("[");
132         int indexAfterName = joramAdminUser.indexOf("]", indextBeforeName);
133         String JavaDoc name = joramAdminUser.substring(indextBeforeName + 1, indexAfterName);
134         if (!name.equals("root")) {
135             userItem.setName(name);
136             int indexBeforeId = joramAdminUser.indexOf(":", indexAfterName);
137             String JavaDoc idString = joramAdminUser.substring(indexBeforeId + 1, joramAdminUser.length());
138             userItem.setId(idString);
139             name = name.concat("[");
140             name = name.concat(idString);
141             name = name.concat("]");
142             ObjectName JavaDoc on = JoramObjectName.joramUser(name);
143             if (JonasAdminJmx.hasMBeanName(on)) {
144                 userItem.setOn(on);
145             }
146         }
147         return userItem;
148     }
149
150     public void printDestination(ItemDestination dest) {
151         System.out.println("Destination:");
152         System.out.println("- name " + dest.getName());
153         System.out.println("- type " + dest.getType());
154         System.out.println("- id " + dest.getId());
155         System.out.println("- servrId " + dest.getServerId());
156         System.out.println("- on " + dest.getOn());
157     }
158
159     void printUser(ItemUser user) {
160         System.out.println("User:");
161         System.out.println("- name " + user.getName());
162         System.out.println("- id " + user.getId());
163         System.out.println("- on " + user.getOn());
164     }
165     protected void initRefs() {
166         String JavaDoc[] asParam = new String JavaDoc[1];
167         String JavaDoc[] asSignature = new String JavaDoc[1];
168         asSignature[0] = "java.lang.String";
169         ejbServiceObjectName = JonasObjectName.ejbService();
170         if (!JonasManagementRepr.isRegistered(ejbServiceObjectName)) {
171             ejbServiceObjectName = null;
172         }
173     }
174
175     protected boolean hasDeps(String JavaDoc pDestName) {
176         boolean ret = false;
177         String JavaDoc[] asParam = new String JavaDoc[1];
178         String JavaDoc[] asSignature = new String JavaDoc[1];
179         if (ejbServiceObjectName != null) {
180             asParam[0] = pDestName;
181             asSignature[0] = "java.lang.String";
182             Set JavaDoc deps = (Set JavaDoc) JonasManagementRepr.invoke(
183                     ejbServiceObjectName, "getJmsDestinationDependence",
184                     asParam, asSignature);
185             if (!deps.isEmpty()) {
186                 ret = true;
187             }
188         }
189         return ret;
190     }
191
192     /**
193      * Determine the server Id from a String structure as follows:
194      * id=#x.y.z. The serrver id to be returned is "x"
195      * @param id #x.y.z
196      * @return x
197      */

198     protected String JavaDoc currentServerId(String JavaDoc id) {
199         String JavaDoc serverId = null;
200         int indexBefore = id.indexOf("#");
201         int indexAfter = id.indexOf(".", indexBefore);
202         serverId = id.substring(indexBefore + 1, indexAfter);
203         return serverId;
204     }
205
206     protected void populateDestination(ObjectName JavaDoc destOn, DestinationForm oForm) {
207         oForm.setName(getStringAttribute(destOn, "AdminName"));
208         oForm.setDmq(getStringAttribute(destOn, "DMQ"));
209         oForm.setId(getStringAttribute(destOn, "Name"));
210         oForm.setType(getStringAttribute(destOn, "Type"));
211         oForm.setFreelyReadable(getBooleanAttribute(destOn, "FreelyReadable"));
212         oForm.setFreelyWriteable(getBooleanAttribute(destOn, "FreelyWriteable"));
213         oForm.setReaderList(null); // TO DO
214
oForm.setWriterList(null); // TO DO
215
}
216
217     protected void getStatistics(ObjectName JavaDoc destOn, MonitoringDestForm oMonitForm, DestinationForm oForm) {
218         Hashtable JavaDoc stats = (Hashtable JavaDoc) JonasManagementRepr.getAttribute(destOn, "Statistic");
219         Calendar JavaDoc cal = Calendar.getInstance();
220         Long JavaDoc creationDateVale = (Long JavaDoc) stats.get("creationDate");
221         cal.setTimeInMillis(creationDateVale.longValue());
222         oForm.setCreationDate(cal.getTime().toString());
223         Long JavaDoc value = (Long JavaDoc) stats.get("nbMsgsReceiveSinceCreation");
224         oMonitForm.setNbMsgsReceiveSinceCreation(value.longValue());
225         value = (Long JavaDoc) stats.get("nbMsgsSendToDMQSinceCreation");
226         oMonitForm.setNbMsgsSendToDMQSinceCreation(value.longValue());
227         value = (Long JavaDoc) stats.get("nbMsgsDeliverSinceCreation");
228         oMonitForm.setNbMsgsDeliverSinceCreation(value.longValue());
229         oMonitForm.setName(getStringAttribute(destOn, "AdminName"));
230         oMonitForm.setType(getStringAttribute(destOn, "Type"));
231     }
232 }
233
Popular Tags