View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.mina.integration.jmx;
18  
19  import java.util.LinkedHashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.management.MBeanException;
24  import javax.management.MBeanParameterInfo;
25  import javax.management.ObjectName;
26  import javax.management.modelmbean.ModelMBeanAttributeInfo;
27  import javax.management.modelmbean.ModelMBeanOperationInfo;
28  
29  import org.apache.mina.core.filterchain.IoFilter;
30  import org.apache.mina.core.session.IoSession;
31  
32  /**
33   * A JMX MBean wrapper for an {@link IoSession}.
34   * 
35   * @author The Apache MINA Project (dev@mina.apache.org)
36   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
37   */
38  public class IoSessionMBean extends ObjectMBean<IoSession> {
39  
40      public IoSessionMBean(IoSession source) {
41          super(source);
42      }
43      
44      @Override
45      protected Object getAttribute0(String fqan) throws Exception {
46          if (fqan.equals("attributes")) {
47              Map<String, String> answer = new LinkedHashMap<String, String>();
48              for (Object key: getSource().getAttributeKeys()) {
49                  answer.put(String.valueOf(key), String.valueOf(getSource().getAttribute(key)));
50              }
51              return answer;
52          }
53          
54          return super.getAttribute0(fqan);
55      }
56  
57      @Override
58      protected Object invoke0(String name, Object[] params, String[] signature) throws Exception {
59          if (name.equals("addFilterFirst")) {
60              String filterName = (String) params[0];
61              ObjectName filterRef = (ObjectName) params[1];
62              IoFilter filter = getFilter(filterRef);
63              getSource().getFilterChain().addFirst(filterName, filter);
64              return null;
65          }
66          
67          if (name.equals("addFilterLast")) {
68              String filterName = (String) params[0];
69              ObjectName filterRef = (ObjectName) params[1];
70              IoFilter filter = getFilter(filterRef);
71              getSource().getFilterChain().addLast(filterName, filter);
72              return null;
73          }
74          
75          if (name.equals("addFilterBefore")) {
76              String filterBaseName = (String) params[0];
77              String filterName = (String) params[1];
78              ObjectName filterRef = (ObjectName) params[2];
79              IoFilter filter = getFilter(filterRef);
80              getSource().getFilterChain().addBefore(filterBaseName, filterName, filter);
81              return null;
82          }
83          
84          if (name.equals("addFilterAfter")) {
85              String filterBaseName = (String) params[0];
86              String filterName = (String) params[1];
87              ObjectName filterRef = (ObjectName) params[2];
88              IoFilter filter = getFilter(filterRef);
89              getSource().getFilterChain().addAfter(filterBaseName, filterName, filter);
90              return null;
91          }
92          
93          if (name.equals("removeFilter")) {
94              String filterName = (String) params[0];
95              getSource().getFilterChain().remove(filterName);
96              return null;
97          }
98          
99          return super.invoke0(name, params, signature);
100     }
101 
102     private IoFilter getFilter(ObjectName filterRef) throws MBeanException {
103         Object object = ObjectMBean.getSource(filterRef);
104         if (object == null) {
105             throw new MBeanException(new IllegalArgumentException(
106                     "MBean not found: " + filterRef));
107         }
108         if (!(object instanceof IoFilter)) {
109             throw new MBeanException(new IllegalArgumentException(
110                     "MBean '" + filterRef + "' is not an IoFilter."));
111         }
112         
113         return (IoFilter) object;
114     }
115 
116     @Override
117     protected void addExtraAttributes(List<ModelMBeanAttributeInfo> attributes) {
118         attributes.add(new ModelMBeanAttributeInfo(
119                 "attributes", Map.class.getName(), "attributes",
120                 true, false, false));
121     }
122     
123     @Override
124     protected void addExtraOperations(List<ModelMBeanOperationInfo> operations) {
125         operations.add(new ModelMBeanOperationInfo(
126                 "addFilterFirst", "addFilterFirst", new MBeanParameterInfo[] {
127                         new MBeanParameterInfo(
128                                 "name", String.class.getName(), "the new filter name"),
129                         new MBeanParameterInfo(
130                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
131                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
132 
133         operations.add(new ModelMBeanOperationInfo(
134                 "addFilterLast", "addFilterLast", new MBeanParameterInfo[] {
135                         new MBeanParameterInfo(
136                                 "name", String.class.getName(), "the new filter name"),
137                         new MBeanParameterInfo(
138                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
139                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
140 
141         operations.add(new ModelMBeanOperationInfo(
142                 "addFilterBefore", "addFilterBefore", new MBeanParameterInfo[] {
143                         new MBeanParameterInfo(
144                                 "baseName", String.class.getName(), "the next filter name"),
145                         new MBeanParameterInfo(
146                                 "name", String.class.getName(), "the new filter name"),
147                         new MBeanParameterInfo(
148                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
149                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
150 
151         operations.add(new ModelMBeanOperationInfo(
152                 "addFilterAfter", "addFilterAfter", new MBeanParameterInfo[] {
153                         new MBeanParameterInfo(
154                                 "baseName", String.class.getName(), "the previous filter name"),
155                         new MBeanParameterInfo(
156                                 "name", String.class.getName(), "the new filter name"),
157                         new MBeanParameterInfo(
158                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
159                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
160         
161         operations.add(new ModelMBeanOperationInfo(
162                 "removeFilter", "removeFilter", new MBeanParameterInfo[] {
163                         new MBeanParameterInfo(
164                                 "name", String.class.getName(), "the name of the filter to be removed"),
165                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
166     }
167 
168     @Override
169     protected boolean isOperation(String methodName, Class<?>[] paramTypes) {
170         // Ignore some IoSession methods.
171         if (methodName.matches(
172                 "(write|read|(remove|replace|contains)Attribute)")) {
173             return false;
174         }
175         
176         return super.isOperation(methodName, paramTypes);
177     }
178 }