001: /*
002: * Created on Apr 6, 2005
003: */
004: package org.enhydra.util;
005:
006: import javax.management.MBeanConstructorInfo;
007: import javax.management.MBeanException;
008: import javax.management.MBeanOperationInfo;
009: import javax.management.MBeanParameterInfo;
010: import javax.management.ObjectName;
011: import javax.management.ReflectionException;
012: import javax.management.RuntimeOperationsException;
013:
014: import com.lutris.appserver.server.Application;
015:
016: import java.lang.reflect.Method;
017:
018: /**
019: * EAF's Core MBean Implementation
020: *
021: * @author Slobodan Vujasinovic
022: * @author Tanja Jovanovic
023: */
024: public class EafConfigMBean extends AbsConfigMBean {
025:
026: protected Application application = null;
027:
028: public EafConfigMBean(Application application,
029: ObjectName objectName, String prefix, String[] includes,
030: String[] excludes) {
031: init(application, objectName, prefix, includes, excludes);
032: }
033:
034: public void init(Application app, ObjectName objectName,
035: String prefix, String[] includes, String[] excludes) {
036: this .application = app;
037: config = application.getConfig();
038: logChannel = application.getLogChannel();
039: super .initConfig(objectName, prefix, includes, excludes);
040: }
041:
042: /**
043: * Method returns MBean's MBeanOperationInfo Array
044: * @return MBeanOperationInfo[]
045: */
046: protected MBeanOperationInfo[] getMBeanOperationsInfo() {
047: MBeanParameterInfo[] params = null;
048: MBeanOperationInfo[] dOperations = new MBeanOperationInfo[5];
049: dOperations[0] = new MBeanOperationInfo("reset",
050: "reset(): reset attributes to their initial values",
051: params, "void", MBeanOperationInfo.ACTION);
052: dOperations[1] = new MBeanOperationInfo(
053: "saveAttributes",
054: "saveAttributes(): save the attribute values into the configuration file",
055: params, "void", MBeanOperationInfo.ACTION);
056: dOperations[2] = new MBeanOperationInfo("getAppInfo",
057: "getAppInfo(): get application information", params,
058: "String", MBeanOperationInfo.ACTION);
059:
060: params = new MBeanParameterInfo[2];
061: params[0] = new MBeanParameterInfo(
062: "attKey",
063: "java.lang.String",
064: "Attribute name! Please, "
065: + "use UNDERLINE (_) as attribute group separator.");
066: params[1] = new MBeanParameterInfo("attValue",
067: "java.lang.String", "Attribute value!");
068:
069: dOperations[3] = new MBeanOperationInfo(
070: "addAttribute",
071: "addAttribute(String key, String value): add new application parameter",
072: params, "void", MBeanOperationInfo.ACTION);
073:
074: params = new MBeanParameterInfo[1];
075: params[0] = new MBeanParameterInfo(
076: "attKey",
077: "java.lang.String",
078: "Attribute name! Please, "
079: + "use UNDERLINE (_) as attribute group separator.");
080:
081: dOperations[4] = new MBeanOperationInfo(
082: "removeAttribute",
083: "removeAttribute(String key): remove application parameter",
084: params, "void", MBeanOperationInfo.ACTION);
085:
086: return dOperations;
087: }
088:
089: /**
090: * Method returns MBean's MBeanConstructorInfo Array
091: * @return MBeanConstructorInfo[]
092: */
093: protected MBeanConstructorInfo[] getMBeanConstructorInfo() {
094: MBeanParameterInfo[] dParameters = new MBeanParameterInfo[5];
095: dParameters[0] = new MBeanParameterInfo("application",
096: "com.lutris.appserver.server.Application",
097: "Application Object");
098: dParameters[1] = new MBeanParameterInfo("objectName",
099: "javax.management.ObjectName", "MBean Name Object");
100: dParameters[2] = new MBeanParameterInfo("prefix",
101: "java.lang.String", "Configuration Parameter Prefix");
102: dParameters[3] = new MBeanParameterInfo("includes",
103: "java.lang.reflect.Array",
104: "Array of internal parameter prefixes to include");
105: dParameters[4] = new MBeanParameterInfo("excludes",
106: "java.lang.reflect.Array",
107: "Array of internal parameter prefixes to exclude");
108:
109: MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
110: dConstructors[0] = new MBeanConstructorInfo("ConfigMBean",
111: "ConfigMBean Object Constructor", dParameters);
112:
113: return dConstructors;
114: }
115:
116: /**
117: * Allows an operation to be invoked on the Dynamic MBean.
118: */
119: public Object invoke(String operationName, Object params[],
120: String signature[]) throws MBeanException,
121: ReflectionException {
122: // Check operationName is not null to avoid NullPointerException later on
123: if (operationName == null) {
124: throw new RuntimeOperationsException(
125: new IllegalArgumentException(
126: "Operation name cannot be null"),
127: "Cannot invoke a null operation in " + dClassName);
128: }
129: // Check for a recognized operation name and call the corresponding operation
130: if (operationName.equals("reset")) {
131: try {
132: reset();
133: } catch (Exception e) {
134: throw new MBeanException(e);
135: }
136: return null;
137: } else if (operationName.equals("saveAttributes")) {
138: saveAttributes();
139: return null;
140: } else if (operationName.equals("addAttribute")) {
141: if (params != null && params.length == 2) {
142: addAttribute((String) params[0], (String) params[1]);
143: }
144: return null;
145: } else if (operationName.equals("removeAttribute")) {
146: if (params != null && params.length == 1) {
147: removeAttribute((String) params[0]);
148: }
149: return null;
150: } else if (operationName.equals("getAppInfo")) {
151: return getAppInfo();
152: } else {
153: // unrecognized operation name:
154: throw new ReflectionException(new NoSuchMethodException(
155: operationName), "Cannot find the operation "
156: + operationName + " in " + dClassName);
157: }
158: }
159:
160: public String getAppInfo() {
161: String appInfo = null;
162: Class appClass = application.getClass();
163: try {
164: Method toHtmlMethod = appClass.getMethod("toHtml", null);
165: appInfo = (String) toHtmlMethod.invoke(application, null);
166: } catch (Exception exc) {
167: appInfo = application.toString();
168: }
169: return appInfo;
170: }
171:
172: }
|