⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 texi.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
package com.liuyang.jmx.mbeans.texi;

import java.util.Iterator;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.JMException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException;

public class Texi implements DynamicMBean {
	
	/**
	 * 开车方法
	 */		
	public String start() {
		state = RUN;		
		return "开车了";
	}
	/**
	 * 停车方法
	 */		
	public String stop() {
		state = NOTRUN;
		return "停车了";
	}	
	private static String RUN = "行驶状态";
	private static String NOTRUN = "停车状态";
	private static String STATE = "state";		
	private String state  = NOTRUN;
	/**
	 * 获取状态属性
	 */		
	public String getState() {
		return state;
	}
	/**
	 * 设置状态属性
	 */		
	public void setState(String st) {
		state = st;
	}	
	/**
	 * 获取属性值
	 */
	public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
		if (attribute == null || attribute.equals(""))
		  throw new IllegalArgumentException(
			  "没有这个属性"
		  );
		if (attribute.equals(STATE))
		  return state;
		throw new AttributeNotFoundException(
		  "属性 " + attribute + " 不存在。"
		);
	}
	/**
	 * 设置属性值
	 */
	public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
		if (attribute == null) throw new
		  AttributeNotFoundException("属性不存在");
		try {            
		  if (attribute.getName().equals(STATE))
			this.state = (String)attribute.getValue();
		  else throw new AttributeNotFoundException(
			  "属性 " + attribute + " 不存在。"
			);
		}
		catch (ClassCastException e) {
		  throw new InvalidAttributeValueException(
			"属性类型不同" +
			attribute.getValue().getClass().getName()
		  );
		}
	}
	/**
	 * 获取属性值列表
	 */
	public AttributeList getAttributes(String[] attributes) {
		if (attributes == null)
		  throw new IllegalArgumentException("null array");
      
		AttributeList list = new AttributeList();
    
		for (int i = 0; i < attributes.length; ++i) {
		  try {
			list.add(new Attribute(
				attributes[i], getAttribute(attributes[i])
			));
		  }catch (JMException ignored) {
		  }
		}
		return list;
	}
	/**
	 * 设置属性值表
	 */
	public AttributeList setAttributes(AttributeList list) {
		AttributeList results = new AttributeList();
		Iterator it           = list.iterator();
 		while (it.hasNext()) {
		  try {
			Attribute attr = (Attribute)it.next();
			setAttribute(attr);
			results.add(attr);
		  }catch (JMException ignored) {
		  }
		}   
		return results;
	}
	/**
	 * 调用这个MBean
	 */
	public Object invoke(String actionName,Object[] params,String[] signature) throws MBeanException, ReflectionException {
		if (actionName == null || actionName.equals(""))
		  throw new IllegalArgumentException("no operation");
		if (actionName.equals("start")){
			return start();			
		}else if (actionName.equals("stop")) {
			return stop();
		}else
		  throw new UnsupportedOperationException(
			"没这个属性 " + actionName
		  );
	}
	
	private MBeanInfo mbinfo;
	/**
	 * 获取MBean的MBeanInfo对象
	 */	
	public MBeanInfo getMBeanInfo() {
        
		if(mbinfo!=null)return mbinfo;
		String className    = getClass().getName();
		String description  = "Texi MBean";
		//创建状态属性信息
		MBeanAttributeInfo st   = new MBeanAttributeInfo(
		  STATE,                   // 属性名称
		  String.class.getName(),  // 属性数据类型
		  "行车状态属性.",         // 属性描述
		  true, true, true         // 设置访问权限 
		);
		//创建start操作信息				
		MBeanOperationInfo start = new MBeanOperationInfo(
		  "start",                          // 方法名称
		  "开车",							// 方法描述
		  null,                             // 方法输入参数类型
		  String.class.getName(),           // 方法返回参数类型
		  MBeanOperationInfo.INFO           // 设置这个操作方法的类型
		);
		//创建stop操作信息	
		MBeanOperationInfo stop = 
		  new MBeanOperationInfo(
			"stop",                      // name
			"停车方法",  // description
			new MBeanParameterInfo[] {          // signature
			},
			void.class.getName(),               // return type
			MBeanOperationInfo.ACTION           // impact
		);
		//创建构造函数信息	
		MBeanConstructorInfo defaultConstructor = 
		  new MBeanConstructorInfo(
			"Texi构造函数",
			"创建一个Texi.", null
		  );
		//集中属性信息			  
		MBeanAttributeInfo[] attributes = 
		  new MBeanAttributeInfo[] {
			st
		  };
		//集中构造函数信息				  
		MBeanConstructorInfo[] constructors = 
		  new MBeanConstructorInfo[] {
			defaultConstructor
		  };
		//集中操作方法信息			  
		MBeanOperationInfo[] operations = 
		  new MBeanOperationInfo[] {
			start,stop
		  };
		//创建MBeanInfo		  
		mbinfo = new MBeanInfo(
		  className, description, attributes,
		  constructors, operations, null
		);		
		return mbinfo;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -