001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: */package org.objectweb.jonas.webapp.jonasadmin.joramplatform;
021:
022: import java.util.ArrayList;
023: import java.util.Calendar;
024: import java.util.Hashtable;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028: import java.util.StringTokenizer;
029: import java.util.Vector;
030:
031: import javax.management.MalformedObjectNameException;
032: import javax.management.ObjectName;
033: import javax.servlet.http.HttpServletRequest;
034:
035: import org.apache.struts.action.ActionForward;
036: import org.apache.struts.action.ActionMapping;
037: import org.objectweb.jonas.jmx.JonasManagementRepr;
038: import org.objectweb.jonas.jmx.JonasObjectName;
039: import org.objectweb.jonas.jmx.JoramObjectName;
040: import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
041: import org.objectweb.jonas.webapp.jonasadmin.deploy.BaseDeployAction;
042:
043: import org.objectweb.joram.client.jms.admin.DeadMQueue;
044:
045: /**
046: * @author Adriana Danes
047: */
048: public abstract class EditJoramBaseAction extends BaseDeployAction {
049:
050: ObjectName ejbServiceObjectName = null;
051:
052: /**
053: * Treat specific case where Joram RAR was unloaded
054: * @param t Exception to treat
055: * @param pMapping provided by executeAction
056: * @param pRequest provided by executeAction
057: * @return
058: */
059: protected ActionForward treatError(Throwable pThrowable,
060: ActionMapping pMapping, HttpServletRequest pRequest) {
061: String throwableClassName = pThrowable.getClass().getName();
062: Throwable t = null;
063: if (throwableClassName
064: .equals("javax.management.InstanceNotFoundException")) {
065: t = new Throwable(
066: "No MBeans found for the Joram platform. Try to refresh!",
067: pThrowable);
068: } else if (throwableClassName
069: .equals("javax.management.MalformedObjectNameException")) {
070: t = new Throwable(
071: "This is not a valid name for a Joram MBean ("
072: + pThrowable.getMessage() + ")", pThrowable);
073: } else {
074: pThrowable.printStackTrace();
075: t = new Throwable("Joram platform exception ("
076: + pThrowable.getMessage() + ")", pThrowable);
077: }
078: addGlobalError(t);
079: saveErrors(pRequest, m_Errors);
080: return (pMapping.findForward("Global Error"));
081: }
082:
083: protected String getPathToExportRepository() {
084: return m_WhereAreYou.getAdminJonasServerConfDir();
085: }
086:
087: /**
088: * Get the path to the joramAdmin.xml file
089: * @return
090: */
091: protected String getPathToReloadJoramAdmin() {
092: return m_WhereAreYou.getAdminJonasServerConfDir();
093: }
094:
095: /**
096: * Create a ItemDestination object from a String structured as follows:
097: * type=queue/topic, name=destName, id=#x.y.z
098: * @param joramAdminDestination String containing destination description
099: * @param serverName the name of the JOnAS server to which the current JORAM server is connected
100: * @return ItemDestination containing name, type, id and ObjectName of the MBean associated to the corresponding destination
101: * @throws MalformedObjectNameException could not construct destination ObjectName
102: */
103: public ItemDestination getDestinationItem(
104: String joramAdminDestination, String serverName)
105: throws MalformedObjectNameException {
106: ItemDestination destinationItem = new ItemDestination();
107: ObjectName destinationOn = null;
108: StringTokenizer st = new StringTokenizer(joramAdminDestination,
109: ",");
110: String type = null;
111: String name = null;
112: String id = null;
113: String serverId = null;
114: while (st.hasMoreTokens()) {
115: // A couple has the following structure: key=value
116: String couple = st.nextToken().trim();
117: int index = couple.indexOf("=");
118: String key = couple.substring(0, index);
119: String value = couple.substring(index + 1, couple.length());
120: if (key.equals("type")) {
121: type = value;
122: destinationItem.setType(type);
123: }
124: if (key.equals("name")) {
125: name = value;
126: destinationItem.setName(name);
127: }
128: if (key.equals("id")) {
129: id = value;
130: destinationItem.setId(id);
131: int indexBefore = id.indexOf("#");
132: int indexAfter = id.indexOf(".", indexBefore);
133: serverId = id.substring(indexBefore + 1, indexAfter);
134: destinationItem.setServerId(serverId);
135: }
136: }
137: if (name != null && type != null && id != null) {
138: if (type.equals("topic")) {
139: destinationOn = JoramObjectName.joramTopic(name);
140: } else if (type.equals("queue")) {
141: destinationOn = JoramObjectName.joramQueue(name);
142: }
143: if (destinationOn != null
144: && JonasAdminJmx.hasMBeanName(destinationOn,
145: serverName)) {
146: destinationItem.setRegistered(true);
147: // exists a destination MBean with the given type and name, check the id
148: String foundId = (String) JonasManagementRepr
149: .getAttribute(destinationOn, "Name", serverName);
150: if (id.equals(foundId)) {
151: destinationItem.setOn(destinationOn);
152: boolean deps = hasDeps(name, serverName);
153: destinationItem.setUsed(deps);
154: destinationItem.setManageable(true);
155: }
156: }
157: }
158: return destinationItem;
159: }
160:
161: /**
162: * Create a ItemUser object from a String structured as follows:
163: * User[anonymous]:#0.0.1035
164: * @param joramAdminUser String containing user description
165: * @param serverName the name of the JOnAS server to which the current JORAM server is connected
166: * @return ItemUser containing name and ObjectName of the MBean associated to the corresponding user
167: * @throws MalformedObjectNameException
168: */
169: protected ItemUser getUserItem(String joramAdminUser,
170: String serverName) throws MalformedObjectNameException {
171: ItemUser userItem = new ItemUser();
172: joramAdminUser.trim();
173: int indextBeforeName = joramAdminUser.indexOf("[");
174: int indexAfterName = joramAdminUser.indexOf("]",
175: indextBeforeName);
176: String name = joramAdminUser.substring(indextBeforeName + 1,
177: indexAfterName);
178: if (!name.equals("root")) {
179: userItem.setName(name);
180: int indexBeforeId = joramAdminUser.indexOf(":",
181: indexAfterName);
182: String idString = joramAdminUser.substring(
183: indexBeforeId + 1, joramAdminUser.length());
184: userItem.setId(idString);
185: userItem.setServerId(currentServerId(idString));
186: name = name.concat("[");
187: name = name.concat(idString);
188: name = name.concat("]");
189: ObjectName on = JoramObjectName.joramUser(name);
190: if (JonasAdminJmx.hasMBeanName(on, serverName)) {
191: userItem.setOn(on);
192: }
193: }
194: return userItem;
195: }
196:
197: /**
198: * Return the ObjectName of the User MBean corresponding to a given agent id
199: * @param userId Agent id for a user (#x.y.z.)
200: * @param serverName the name of the JOnAS server to which the current JORAM server is connected
201: * @return the Objectname of the User MBean having the ProxyId attribute equal to the userId
202: */
203: protected ObjectName getUserOn(String userId, String serverName) {
204: ObjectName userOn = null; // to be determined
205: ObjectName userOns = null; // template for User MBeans
206: try {
207: userOns = JoramObjectName.joramUsers();
208: Iterator it = JonasManagementRepr.queryNames(userOns,
209: serverName).iterator();
210: while (it.hasNext()) {
211: ObjectName on = (ObjectName) it.next(); // a user MBean
212: String proxyId = (String) JonasManagementRepr
213: .getAttribute(on, "ProxyId", serverName);
214: if (proxyId.equals(userId)) {
215: // found the user
216: userOn = on;
217: break;
218: }
219: }
220: } catch (MalformedObjectNameException e) {
221: // TODO Auto-generated catch block
222: e.printStackTrace();
223: }
224: return userOn;
225: }
226:
227: /**
228: * Return the name of the User corresponding to a given agent id
229: * @param userId Agent id for a user (#x.y.z.)
230: * @param jonasServerName the name of the JOnAS server to which the current JORAM server is connected
231: * @return the value of the name attribute of the User MBean having the ProxyId attribute equal to the userId
232: */
233: protected String getUserName(String userId, String jonasServerName) {
234: String name = null; // to be determined
235: ObjectName userOns = null; // template for User MBeans
236: try {
237: userOns = JoramObjectName.joramUsers();
238: Iterator it = JonasManagementRepr.queryNames(userOns,
239: jonasServerName).iterator();
240: while (it.hasNext()) {
241: ObjectName on = (ObjectName) it.next(); // a user MBean
242: String proxyId = (String) JonasManagementRepr
243: .getAttribute(on, "ProxyId", jonasServerName);
244: if (proxyId.equals(userId)) {
245: // found the user
246: name = (String) JonasManagementRepr.getAttribute(
247: on, "Name", jonasServerName);
248: break;
249: }
250: }
251: } catch (MalformedObjectNameException e) {
252: // TODO Auto-generated catch block
253: e.printStackTrace();
254: }
255: return name;
256: }
257:
258: public void printDestination(ItemDestination dest, String serverName) {
259: System.out.println("Destination in server JOnAS " + serverName
260: + " :");
261: System.out.println("- name " + dest.getName());
262: System.out.println("- type " + dest.getType());
263: System.out.println("- id " + dest.getId());
264: System.out.println("- servrId " + dest.getServerId());
265: System.out.println("- on " + dest.getOn());
266: }
267:
268: void printUser(ItemUser user) {
269: System.out.println("User:");
270: System.out.println("- name " + user.getName());
271: System.out.println("- id " + user.getId());
272: System.out.println("- on " + user.getOn());
273: }
274:
275: protected void initRefs(String jonasServerName) {
276: String[] asParam = new String[1];
277: String[] asSignature = new String[1];
278: asSignature[0] = "java.lang.String";
279: ejbServiceObjectName = JonasObjectName.ejbService();
280: if (!JonasManagementRepr.isRegistered(ejbServiceObjectName,
281: jonasServerName)) {
282: ejbServiceObjectName = null;
283: }
284: }
285:
286: protected boolean hasDeps(String pDestName, String jonasServerName) {
287: boolean ret = false;
288: String[] asParam = new String[1];
289: String[] asSignature = new String[1];
290: if (ejbServiceObjectName != null) {
291: asParam[0] = pDestName;
292: asSignature[0] = "java.lang.String";
293: Set deps = (Set) JonasManagementRepr.invoke(
294: ejbServiceObjectName,
295: "getJmsDestinationDependence", asParam,
296: asSignature, jonasServerName);
297: if (!deps.isEmpty()) {
298: ret = true;
299: }
300: }
301: return ret;
302: }
303:
304: /**
305: * Determine the server Id from a String structure as follows:
306: * id=#x.y.z. The serrver id to be returned is "y"
307: * @param id #x.y.z
308: * @return y
309: */
310: public static String currentServerId(String id) {
311: String serverId = null;
312: int indexBefore = id.indexOf(".");// first dod
313: int indexAfter = id.indexOf(".", indexBefore + 1);// second dot
314: serverId = id.substring(indexBefore + 1, indexAfter);
315: return serverId;
316: }
317:
318: protected void populateDestination(ObjectName destOn,
319: DestinationForm oForm, String serverName)
320: throws MalformedObjectNameException {
321: oForm.setName(getStringAttribute(destOn, "AdminName"));
322: //testReaderList(oForm);
323: DeadMQueue dmq = (DeadMQueue) JonasManagementRepr.getAttribute(
324: destOn, "DMQ", serverName);
325: if (dmq != null) {
326: // should be getAdminName() but not set in joram
327: oForm.setDmq(dmq.getName());
328: }
329: oForm.setId(getStringAttribute(destOn, "Name"));
330: oForm.setType(getStringAttribute(destOn, "Type"));
331: boolean readable = getBooleanAttribute(destOn, "FreelyReadable");
332: oForm.setFreelyReadable(readable);
333: if (readable) {
334: oForm.setReaderList(null);
335: } else {
336: List readerList = getListAttribute(destOn, "ReaderList");
337: //oForm.setReaderList(getStringAttributeFromList(readerList));
338: oForm
339: .setReaderList(getBaseItemList(readerList,
340: serverName));
341: }
342: boolean writeable = getBooleanAttribute(destOn,
343: "FreelyWriteable");
344: oForm.setFreelyWriteable(writeable);
345: if (writeable) {
346: oForm.setWriterList(null);
347: } else {
348: List writerList = getListAttribute(destOn, "WriterList");
349: //oForm.setWriterList(getStringAttributeFromList(writerList));
350: oForm
351: .setWriterList(getBaseItemList(writerList,
352: serverName));
353: }
354: }
355:
356: /**
357: * Treat a list (Vector) of Joram user items (example: User[anonymous]:#0.0.1035)
358: * @param inputList list of Joram user items
359: * @return a list of user item objects (ItemUser type)
360: * @throws MalformedObjectNameException
361: */
362: protected ArrayList getBaseItemList(List inputList,
363: String serverName) throws MalformedObjectNameException {
364: ArrayList arrayResult = new ArrayList();
365: ArrayList arrayInput = null;
366: try {
367: arrayInput = (ArrayList) inputList;
368: } catch (ClassCastException ce) {
369: try {
370: Vector vInputList = (Vector) inputList;
371: arrayInput = new ArrayList(vInputList);
372: } catch (ClassCastException cee) {
373: // TO DO, could be a LinkedList
374: throw cee;
375: }
376: }
377: if (arrayInput != null) {
378: for (int i = 0; i < arrayInput.size(); i++) {
379: String item = arrayInput.get(i).toString();
380: //System.out.println("In getBaseItem, treat item=" + item);
381: ItemUser userItem = getUserItem(item, serverName);
382: if (userItem.getName() != null) {
383: arrayResult.add(userItem);
384: }
385: }
386:
387: }
388: return arrayResult;
389: }
390:
391: /**
392: * Used to treat the ReaderList and the WriterList currently implemented
393: * by a Vector of Ids
394: * @param inputList list of elemements
395: * @return String containing the concateneted elements
396: */
397: private String getStringAttributeFromList(List inputList) {
398: StringBuffer buf = new StringBuffer();
399: /* check for choosen List implem in
400: * - ArrayList
401: * - Vector
402: */
403: ArrayList arrayInput = null;
404: try {
405: arrayInput = (ArrayList) inputList;
406: } catch (ClassCastException ce) {
407: try {
408: Vector vInputList = (Vector) inputList;
409: arrayInput = new ArrayList(vInputList);
410: } catch (ClassCastException cee) {
411: // TO DO, could be a LinkedList
412: throw cee;
413: }
414: }
415: if (arrayInput != null) {
416: for (int i = 0; i < arrayInput.size(); i++) {
417: buf.append(arrayInput.get(i).toString());
418: buf.append("\n");
419: }
420: return new String(buf);
421: } else {
422: return null;
423: }
424: }
425:
426: protected void getStatistics(ObjectName destOn,
427: MonitoringDestForm oMonitForm, DestinationForm oForm,
428: String serverName) {
429: Hashtable stats = (Hashtable) JonasManagementRepr.getAttribute(
430: destOn, "Statistic", serverName);
431: Calendar cal = Calendar.getInstance();
432: Long creationDateVale = (Long) stats.get("creationDate");
433: cal.setTimeInMillis(creationDateVale.longValue());
434: oForm.setCreationDate(cal.getTime().toString());
435: Long value = (Long) stats.get("nbMsgsReceiveSinceCreation");
436: oMonitForm.setNbMsgsReceiveSinceCreation(value.longValue());
437: value = (Long) stats.get("nbMsgsSendToDMQSinceCreation");
438: oMonitForm.setNbMsgsSendToDMQSinceCreation(value.longValue());
439: value = (Long) stats.get("nbMsgsDeliverSinceCreation");
440: oMonitForm.setNbMsgsDeliverSinceCreation(value.longValue());
441: oMonitForm.setName(getStringAttribute(destOn, "AdminName"));
442: oMonitForm.setType(getStringAttribute(destOn, "Type"));
443: }
444:
445: protected void populate(DestinationForm p_Form) throws Exception {
446: // Object name used
447: ObjectName oObjectName = p_Form.getOName();
448: // Populate
449: setBooleanAttribute(oObjectName, "FreelyReadable", p_Form
450: .getFreelyReadable());
451: setBooleanAttribute(oObjectName, "FreelyWriteable", p_Form
452: .getFreelyWriteable());
453: }
454:
455: protected void updateReadablesWriteables(DestinationForm pForm,
456: String jonasServerName) {
457: boolean freelyReadable = pForm.getFreelyReadable();
458: boolean freelyWriteable = pForm.getFreelyWriteable();
459: ObjectName on = pForm.getOName();
460: JonasManagementRepr.setAttribute(on, "FreelyReadable",
461: new Boolean(freelyReadable), jonasServerName);
462: JonasManagementRepr.setAttribute(on, "FreelyWriteable",
463: new Boolean(freelyWriteable), jonasServerName);
464: if (!freelyReadable || !freelyWriteable) {
465: // register in the session the currently managed destination id
466: m_Session.setAttribute("currentDestination", on.toString());
467: }
468: }
469:
470: void testReaderList(DestinationForm pForm, String jonasServerName) {
471: String name = pForm.getName();
472: ObjectName queueOn = pForm.getOName();
473: if (name.equals("mdbQueue")) {
474: ObjectName userOn = null;
475: // Look for user's toto MBean
476: try {
477: ObjectName userOns = JoramObjectName.joramUsers();
478: Iterator it = JonasManagementRepr.queryNames(userOns,
479: jonasServerName).iterator();
480: while (it.hasNext()) {
481: ObjectName item = (ObjectName) it.next();
482: String itemName = (String) JonasManagementRepr
483: .getAttribute(item, "Name", jonasServerName);
484: if (itemName.equals("toto")) {
485: // found MBean for user toto
486: userOn = item;
487: break;
488: }
489: }
490: } catch (MalformedObjectNameException e) {
491: // TODO Auto-generated catch block
492: e.printStackTrace();
493: }
494: // Add user as reader for mdbQueue
495: if (userOn != null) {
496: String userId = (String) JonasManagementRepr
497: .getAttribute(userOn, "ProxyId",
498: jonasServerName);
499: String[] asParams = { userId };
500: String[] asSignature = { "java.lang.String" };
501: JonasManagementRepr.invoke(queueOn, "addReader",
502: asParams, asSignature, jonasServerName);
503: }
504: }
505: }
506: }
|