001: package org.objectweb.jonas.webapp.jonasadmin.jonasmqconnect.util;
002:
003: import java.lang.reflect.Method;
004: import java.util.ArrayList;
005:
006: import javax.management.ObjectName;
007:
008: import org.objectweb.jonas.jmx.JonasManagementRepr;
009:
010: /**
011: * Util class for getting MQ class properties
012: *
013: * @author Yacine TOUIL
014: *
015: */
016: public class PropertiesUtil {
017:
018: /**
019: * Get Class Properties
020: * @param mbName
021: * @param mqClass
022: * @param getPropertyMethodName
023: * @return
024: */
025: public static ArrayList getClassProperties(ObjectName mbName,
026: Class mqClass, String getPropertyMethodName,
027: String serverName) {
028: return getClassProperties(mbName, mqClass,
029: getPropertyMethodName, null, serverName);
030: }
031:
032: /**
033: * Get Class Properties
034: * @param mbName
035: * @param mqClass
036: * @param getPropertyMethodName
037: * @return
038: */
039: public static ArrayList getClassProperties(ObjectName mbName,
040: Class mqClass, String getPropertyMethodName,
041: String[] propertiesToRemove, String serverName) {
042: ArrayList properties = new ArrayList();
043: Method[] mqMethod = mqClass.getMethods();
044: String[] signature = new String[] { "java.lang.String" };
045: Object[] params = new Object[] {};
046: for (int i = 0; i < mqMethod.length; i++) {
047: String propName = null;
048: String propValue = null;
049: try {
050: String name = mqMethod[i].getName();
051: if (name.startsWith("get")
052: && mqMethod[i].getParameterTypes().length == 0
053: && (mqMethod[i].getReturnType().equals(
054: String.class)
055: || mqMethod[i].getReturnType().equals(
056: int.class) || mqMethod[i]
057: .getReturnType().equals(Integer.class))
058: && !containStr(propertiesToRemove, name
059: .substring(3))) {
060: propName = name.substring(3);
061: params = new Object[] { propName };
062: propValue = ""
063: + JonasManagementRepr.invoke(mbName,
064: getPropertyMethodName, params,
065: signature, serverName);
066: }
067: } catch (Exception ex) {
068: // ignore property
069: continue;
070: }
071: if (propName != null && propName.length() > 0) {
072: properties.add(new Property(propName, propValue,
073: propName));
074: }
075: }
076: return properties;
077: }
078:
079: /**
080: * Get Properties
081: * @param mbName
082: * @param getPropertyMethodName
083: * @param properties
084: * @return
085: */
086: public static ArrayList getProperties(ObjectName mbName,
087: String getPropertyMethodName, ArrayList propertiesName,
088: String serverName) {
089: ArrayList properties = new ArrayList();
090: String[] signature = new String[] { "java.lang.String" };
091: Object[] params = new Object[] {};
092: for (int i = 0; i < propertiesName.size(); i++) {
093: String propName = null;
094: String propValue = null;
095: try {
096: propName = (String) propertiesName.get(i);
097: params = new Object[] { propName };
098: propValue = ""
099: + JonasManagementRepr.invoke(mbName,
100: getPropertyMethodName, params,
101: signature, serverName);
102: } catch (Exception ex) {
103: // ignore property
104: continue;
105: }
106: if (propName != null && propName.length() > 0) {
107: properties.add(new Property(propName, propValue,
108: propName));
109: }
110: }
111: return properties;
112: }
113:
114: /**
115: * Set Class Properties
116: * @param mbName
117: * @param mqClass
118: * @param getPropertyMethodName
119: * @return
120: */
121: public static void setClassProperties(ObjectName mbName, /* FWA - commented out: Class mqClass, */
122: String setPropertyMethodName, ArrayList properties,
123: String serverName) {
124: String[] signature = new String[] { "java.lang.String",
125: "java.lang.String" };
126: Object[] params = new Object[] {};
127:
128: for (int i = 0; i < properties.size(); i++) {
129: try {
130: Property property = (Property) properties.get(i);
131: String propName = property.getName();
132: String propValue = property.getValue();
133: params = new Object[] { propName, propValue };
134: JonasManagementRepr.invoke(mbName,
135: setPropertyMethodName, params, signature,
136: serverName);
137: } catch (Exception ex) {
138: LogUtils.print(ex.getMessage());
139: }
140: }
141: }
142:
143: /**
144: * Get Class Properties
145: * @param mbName
146: * @param mqClass
147: * @param getPropertyMethodName
148: * @return
149: */
150: public static ArrayList getJMSObjectProperties(ObjectName mbName,
151: String selector, String getPropertyMethodName,
152: String[] propertiesToRemove, String serverName,
153: String domainName) {
154:
155: ObjectName parentConnectorON = MqObjectNames
156: .getParentConnectorON(domainName, mbName);
157: Object[] params = new String[] { selector };
158: String[] signature = new String[] { "java.lang.String" };
159: String[] propertyNames = (String[]) JonasManagementRepr.invoke(
160: parentConnectorON, "listPropertyNames", params,
161: signature, serverName);
162:
163: ArrayList properties = new ArrayList();
164: for (int i = 0; i < propertyNames.length; i++) {
165: if (!containStr(propertiesToRemove, propertyNames[i])) {
166:
167: params = new Object[] { propertyNames[i] };
168: String propValue = null;
169: try {
170: propValue = ""
171: + JonasManagementRepr.invoke(mbName,
172: getPropertyMethodName, params,
173: signature, serverName);
174: } catch (Exception e) {
175: // TODO log a WARN record ? or nothing
176: }
177: properties.add(new Property(propertyNames[i],
178: propValue, propertyNames[i]));
179: }
180: }
181: return properties;
182: }
183:
184: /**
185: * if contain str in tab
186: * @param tab
187: * @param str
188: * @return
189: */
190: private static boolean containStr(String[] tab, String str) {
191: if (tab == null) {
192: return false;
193: }
194: for (int i = 0; i < tab.length; i++) {
195: if (tab[i].equals(str)) {
196: return true;
197: }
198: }
199: return false;
200: }
201: }
|