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: */
017: package org.apache.mina.integration.jmx;
018:
019: import java.util.LinkedHashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.management.MBeanException;
024: import javax.management.MBeanParameterInfo;
025: import javax.management.ObjectName;
026: import javax.management.modelmbean.ModelMBeanAttributeInfo;
027: import javax.management.modelmbean.ModelMBeanOperationInfo;
028:
029: import org.apache.mina.common.IoFilter;
030: import org.apache.mina.common.IoSession;
031:
032: /**
033: * A JMX MBean wrapper for an {@link IoSession}.
034: *
035: * @author The Apache MINA Project (dev@mina.apache.org)
036: * @version $Rev: 601229 $, $Date: 2007-12-05 00:13:18 -0700 (Wed, 05 Dec 2007) $
037: */
038: public class IoSessionMBean extends ObjectMBean<IoSession> {
039:
040: public IoSessionMBean(IoSession source) {
041: super (source);
042: }
043:
044: @Override
045: protected Object getAttribute0(String fqan) throws Exception {
046: if (fqan.equals("attributes")) {
047: Map<String, String> answer = new LinkedHashMap<String, String>();
048: for (Object key : getSource().getAttributeKeys()) {
049: answer.put(String.valueOf(key), String
050: .valueOf(getSource().getAttribute(key)));
051: }
052: return answer;
053: }
054:
055: return super .getAttribute0(fqan);
056: }
057:
058: @Override
059: protected Object invoke0(String name, Object[] params,
060: String[] signature) throws Exception {
061: if (name.equals("addFilterFirst")) {
062: String filterName = (String) params[0];
063: ObjectName filterRef = (ObjectName) params[1];
064: IoFilter filter = getFilter(filterRef);
065: getSource().getFilterChain().addFirst(filterName, filter);
066: return null;
067: }
068:
069: if (name.equals("addFilterLast")) {
070: String filterName = (String) params[0];
071: ObjectName filterRef = (ObjectName) params[1];
072: IoFilter filter = getFilter(filterRef);
073: getSource().getFilterChain().addLast(filterName, filter);
074: return null;
075: }
076:
077: if (name.equals("addFilterBefore")) {
078: String filterBaseName = (String) params[0];
079: String filterName = (String) params[1];
080: ObjectName filterRef = (ObjectName) params[2];
081: IoFilter filter = getFilter(filterRef);
082: getSource().getFilterChain().addBefore(filterBaseName,
083: filterName, filter);
084: return null;
085: }
086:
087: if (name.equals("addFilterAfter")) {
088: String filterBaseName = (String) params[0];
089: String filterName = (String) params[1];
090: ObjectName filterRef = (ObjectName) params[2];
091: IoFilter filter = getFilter(filterRef);
092: getSource().getFilterChain().addAfter(filterBaseName,
093: filterName, filter);
094: return null;
095: }
096:
097: if (name.equals("removeFilter")) {
098: String filterName = (String) params[0];
099: getSource().getFilterChain().remove(filterName);
100: return null;
101: }
102:
103: return super .invoke(name, params, signature);
104: }
105:
106: private IoFilter getFilter(ObjectName filterRef)
107: throws MBeanException {
108: Object object = ObjectMBean.getSource(filterRef);
109: if (object == null) {
110: throw new MBeanException(new IllegalArgumentException(
111: "MBean not found: " + filterRef));
112: }
113: if (!(object instanceof IoFilter)) {
114: throw new MBeanException(new IllegalArgumentException(
115: "MBean '" + filterRef + "' is not an IoFilter."));
116: }
117:
118: return (IoFilter) object;
119: }
120:
121: @Override
122: protected void addExtraAttributes(
123: List<ModelMBeanAttributeInfo> attributes) {
124: attributes.add(new ModelMBeanAttributeInfo("attributes",
125: Map.class.getName(), "attributes", true, false, false));
126: }
127:
128: @Override
129: protected void addExtraOperations(
130: List<ModelMBeanOperationInfo> operations) {
131: operations
132: .add(new ModelMBeanOperationInfo(
133: "addFilterFirst",
134: "addFilterFirst",
135: new MBeanParameterInfo[] {
136: new MBeanParameterInfo("name",
137: String.class.getName(),
138: "the new filter name"),
139: new MBeanParameterInfo("filter",
140: ObjectName.class.getName(),
141: "the ObjectName reference to the filter") },
142: void.class.getName(),
143: ModelMBeanOperationInfo.ACTION));
144:
145: operations
146: .add(new ModelMBeanOperationInfo(
147: "addFilterLast",
148: "addFilterLast",
149: new MBeanParameterInfo[] {
150: new MBeanParameterInfo("name",
151: String.class.getName(),
152: "the new filter name"),
153: new MBeanParameterInfo("filter",
154: ObjectName.class.getName(),
155: "the ObjectName reference to the filter") },
156: void.class.getName(),
157: ModelMBeanOperationInfo.ACTION));
158:
159: operations
160: .add(new ModelMBeanOperationInfo(
161: "addFilterBefore",
162: "addFilterBefore",
163: new MBeanParameterInfo[] {
164: new MBeanParameterInfo("baseName",
165: String.class.getName(),
166: "the next filter name"),
167: new MBeanParameterInfo("name",
168: String.class.getName(),
169: "the new filter name"),
170: new MBeanParameterInfo("filter",
171: ObjectName.class.getName(),
172: "the ObjectName reference to the filter") },
173: void.class.getName(),
174: ModelMBeanOperationInfo.ACTION));
175:
176: operations
177: .add(new ModelMBeanOperationInfo(
178: "addFilterAfter",
179: "addFilterAfter",
180: new MBeanParameterInfo[] {
181: new MBeanParameterInfo("baseName",
182: String.class.getName(),
183: "the previous filter name"),
184: new MBeanParameterInfo("name",
185: String.class.getName(),
186: "the new filter name"),
187: new MBeanParameterInfo("filter",
188: ObjectName.class.getName(),
189: "the ObjectName reference to the filter") },
190: void.class.getName(),
191: ModelMBeanOperationInfo.ACTION));
192:
193: operations.add(new ModelMBeanOperationInfo("removeFilter",
194: "removeFilter",
195: new MBeanParameterInfo[] { new MBeanParameterInfo(
196: "name", String.class.getName(),
197: "the name of the filter to be removed"), },
198: void.class.getName(), ModelMBeanOperationInfo.ACTION));
199: }
200:
201: @Override
202: protected boolean isOperation(String methodName,
203: Class<?>[] paramTypes) {
204: // Ignore some IoSession methods.
205: if (methodName
206: .matches("(write|read|(remove|replace|contains)Attribute)")) {
207: return false;
208: }
209:
210: return super.isOperation(methodName, paramTypes);
211: }
212: }
|