001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.webui.util;
016:
017: import org.jmanage.core.management.ObjectAttribute;
018: import org.jmanage.core.management.ObjectOperationInfo;
019: import org.jmanage.core.management.ObjectAttributeInfo;
020:
021: import java.util.List;
022: import java.util.Iterator;
023: import java.util.Date;
024: import java.util.Arrays;
025: import java.math.BigInteger;
026: import java.math.BigDecimal;
027: import java.lang.reflect.Array;
028:
029: /**
030: * Contains some mbean utility methods, which are used in the web layer
031: *
032: * date: Oct 15, 2004
033: * @author Rakesh Kalra
034: */
035: public class MBeanUtils {
036:
037: public static String jsEscape(String str) {
038: StringBuffer buff = new StringBuffer(str.length());
039: for (int i = 0; i < str.length(); i++) {
040: final char ch = str.charAt(i);
041: if (ch == '"') {
042: buff.append(""");
043: } else if (ch == '\'') {
044: buff.append("\\");
045: buff.append(ch);
046: } else {
047: buff.append(ch);
048: }
049: }
050: return buff.toString();
051: }
052:
053: // TODO: value should be converted to actual data type in the proper
054: // classloader: e.g. in the case of javax.managed.ObjectName
055: public static ObjectAttribute getObjectAttribute(
056: List attributeList, ObjectAttributeInfo attrInfo) {
057:
058: String attrName = attrInfo.getName();
059: for (Iterator it = attributeList.iterator(); it.hasNext();) {
060: ObjectAttribute attribute = (ObjectAttribute) it.next();
061: if (attribute != null
062: && attribute.getName().equals(attrName)) {
063: return attribute;
064: }
065: }
066: return new ObjectAttribute(attrName,
067: ObjectAttribute.STATUS_NOT_FOUND, null);
068: }
069:
070: public static boolean isDataTypeEditable(String type) {
071: if (type.equals("boolean") || type.equals("char")
072: || type.equals("byte") || type.equals("short")
073: || type.equals("int") || type.equals("long")
074: || type.equals("float") || type.equals("double")
075: || type.equals("void")
076: || type.equals("java.lang.Boolean")
077: || type.equals("java.lang.Character")
078: || type.equals("java.lang.Byte")
079: || type.equals("java.lang.Short")
080: || type.equals("java.lang.Integer")
081: || type.equals("java.lang.Long")
082: || type.equals("java.lang.Float")
083: || type.equals("java.lang.Double")
084: || type.equals("java.lang.Void")
085: || type.equals("java.lang.String")
086: || type.equals("java.math.BigInteger")
087: || type.equals("java.math.BigDecimal")
088: // || type.equals("java.util.Date") -- currently not supported
089: || type.equals("javax.management.ObjectName")
090: || isEditableArrayType(type)) {
091:
092: return true;
093: }
094: return false;
095: }
096:
097: public static boolean isEditableArrayType(String type) {
098: if (type.equals("[B") // byte array
099: || type.equals("[C") // char array
100: || type.equals("[D") // double array
101: || type.equals("[F") // float array
102: || type.equals("[I") // int array
103: || type.equals("[J") // long array
104: || type.equals("[S") // short array
105: || type.equals("[Ljava.lang.Character;")
106: || type.equals("[Ljava.lang.Byte;")
107: || type.equals("[Ljava.lang.Short;")
108: || type.equals("[Ljava.lang.Integer;")
109: || type.equals("[Ljava.lang.Long;")
110: || type.equals("[Ljava.lang.Float;")
111: || type.equals("[Ljava.lang.Double;")
112: || type.equals("[Ljava.lang.String;")) {
113: return true;
114: }
115: return false;
116: }
117:
118: public static String getImpact(int impact) {
119: switch (impact) {
120: case ObjectOperationInfo.INFO:
121: return "Information";
122: case ObjectOperationInfo.ACTION:
123: return "Action";
124: case ObjectOperationInfo.ACTION_INFO:
125: return "Action and Information";
126: case ObjectOperationInfo.UNKNOWN:
127: return "Unknown";
128: default:
129: return "Invalid Impact Value";
130:
131: }
132: }
133: }
|