001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.mina.integration.jmx; 018 019import java.util.LinkedHashMap; 020import java.util.List; 021import java.util.Map; 022 023import javax.management.MBeanException; 024import javax.management.MBeanParameterInfo; 025import javax.management.ObjectName; 026import javax.management.modelmbean.ModelMBeanAttributeInfo; 027import javax.management.modelmbean.ModelMBeanOperationInfo; 028 029import org.apache.mina.core.filterchain.IoFilter; 030import org.apache.mina.core.session.IoSession; 031 032/** 033 * A JMX MBean wrapper for an {@link IoSession}. 034 * 035 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 036 */ 037public class IoSessionMBean extends ObjectMBean<IoSession> { 038 039 public IoSessionMBean(IoSession source) { 040 super(source); 041 } 042 043 @Override 044 protected Object getAttribute0(String fqan) throws Exception { 045 if (fqan.equals("attributes")) { 046 Map<String, String> answer = new LinkedHashMap<String, String>(); 047 for (Object key : getSource().getAttributeKeys()) { 048 answer.put(String.valueOf(key), String.valueOf(getSource().getAttribute(key))); 049 } 050 return answer; 051 } 052 053 return super.getAttribute0(fqan); 054 } 055 056 @Override 057 protected Object invoke0(String name, Object[] params, String[] signature) throws Exception { 058 if (name.equals("addFilterFirst")) { 059 String filterName = (String) params[0]; 060 ObjectName filterRef = (ObjectName) params[1]; 061 IoFilter filter = getFilter(filterRef); 062 getSource().getFilterChain().addFirst(filterName, filter); 063 return null; 064 } 065 066 if (name.equals("addFilterLast")) { 067 String filterName = (String) params[0]; 068 ObjectName filterRef = (ObjectName) params[1]; 069 IoFilter filter = getFilter(filterRef); 070 getSource().getFilterChain().addLast(filterName, filter); 071 return null; 072 } 073 074 if (name.equals("addFilterBefore")) { 075 String filterBaseName = (String) params[0]; 076 String filterName = (String) params[1]; 077 ObjectName filterRef = (ObjectName) params[2]; 078 IoFilter filter = getFilter(filterRef); 079 getSource().getFilterChain().addBefore(filterBaseName, filterName, filter); 080 return null; 081 } 082 083 if (name.equals("addFilterAfter")) { 084 String filterBaseName = (String) params[0]; 085 String filterName = (String) params[1]; 086 ObjectName filterRef = (ObjectName) params[2]; 087 IoFilter filter = getFilter(filterRef); 088 getSource().getFilterChain().addAfter(filterBaseName, filterName, filter); 089 return null; 090 } 091 092 if (name.equals("removeFilter")) { 093 String filterName = (String) params[0]; 094 getSource().getFilterChain().remove(filterName); 095 return null; 096 } 097 098 return super.invoke0(name, params, signature); 099 } 100 101 private IoFilter getFilter(ObjectName filterRef) throws MBeanException { 102 Object object = ObjectMBean.getSource(filterRef); 103 if (object == null) { 104 throw new MBeanException(new IllegalArgumentException("MBean not found: " + filterRef)); 105 } 106 if (!(object instanceof IoFilter)) { 107 throw new MBeanException(new IllegalArgumentException("MBean '" + filterRef + "' is not an IoFilter.")); 108 } 109 110 return (IoFilter) object; 111 } 112 113 @Override 114 protected void addExtraAttributes(List<ModelMBeanAttributeInfo> attributes) { 115 attributes 116 .add(new ModelMBeanAttributeInfo("attributes", Map.class.getName(), "attributes", true, false, false)); 117 } 118 119 @Override 120 protected void addExtraOperations(List<ModelMBeanOperationInfo> operations) { 121 operations.add(new ModelMBeanOperationInfo("addFilterFirst", "addFilterFirst", 122 new MBeanParameterInfo[] { 123 new MBeanParameterInfo("name", String.class.getName(), "the new filter name"), 124 new MBeanParameterInfo("filter", ObjectName.class.getName(), 125 "the ObjectName reference to the filter") }, void.class.getName(), 126 ModelMBeanOperationInfo.ACTION)); 127 128 operations.add(new ModelMBeanOperationInfo("addFilterLast", "addFilterLast", 129 new MBeanParameterInfo[] { 130 new MBeanParameterInfo("name", String.class.getName(), "the new filter name"), 131 new MBeanParameterInfo("filter", ObjectName.class.getName(), 132 "the ObjectName reference to the filter") }, void.class.getName(), 133 ModelMBeanOperationInfo.ACTION)); 134 135 operations.add(new ModelMBeanOperationInfo("addFilterBefore", "addFilterBefore", 136 new MBeanParameterInfo[] { 137 new MBeanParameterInfo("baseName", String.class.getName(), "the next filter name"), 138 new MBeanParameterInfo("name", String.class.getName(), "the new filter name"), 139 new MBeanParameterInfo("filter", ObjectName.class.getName(), 140 "the ObjectName reference to the filter") }, void.class.getName(), 141 ModelMBeanOperationInfo.ACTION)); 142 143 operations.add(new ModelMBeanOperationInfo("addFilterAfter", "addFilterAfter", 144 new MBeanParameterInfo[] { 145 new MBeanParameterInfo("baseName", String.class.getName(), "the previous filter name"), 146 new MBeanParameterInfo("name", String.class.getName(), "the new filter name"), 147 new MBeanParameterInfo("filter", ObjectName.class.getName(), 148 "the ObjectName reference to the filter") }, void.class.getName(), 149 ModelMBeanOperationInfo.ACTION)); 150 151 operations.add(new ModelMBeanOperationInfo("removeFilter", "removeFilter", 152 new MBeanParameterInfo[] { new MBeanParameterInfo("name", String.class.getName(), 153 "the name of the filter to be removed"), }, void.class.getName(), 154 ModelMBeanOperationInfo.ACTION)); 155 } 156 157 @Override 158 protected boolean isOperation(String methodName, Class<?>[] paramTypes) { 159 // Ignore some IoSession methods. 160 if (methodName.matches("(write|read|(remove|replace|contains)Attribute)")) { 161 return false; 162 } 163 164 return super.isOperation(methodName, paramTypes); 165 } 166}