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 <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public class IoSessionMBean extends ObjectMBean<IoSession> {
38  
39      public IoSessionMBean(IoSession source) {
40          super(source);
41      }
42  
43      @Override
44      protected Object getAttribute0(String fqan) throws Exception {
45          if (fqan.equals("attributes")) {
46              Map<String, String> answer = new LinkedHashMap<String, String>();
47              for (Object key : getSource().getAttributeKeys()) {
48                  answer.put(String.valueOf(key), String.valueOf(getSource().getAttribute(key)));
49              }
50              return answer;
51          }
52  
53          return super.getAttribute0(fqan);
54      }
55  
56      @Override
57      protected Object invoke0(String name, Object[] params, String[] signature) throws Exception {
58          if (name.equals("addFilterFirst")) {
59              String filterName = (String) params[0];
60              ObjectName filterRef = (ObjectName) params[1];
61              IoFilter filter = getFilter(filterRef);
62              getSource().getFilterChain().addFirst(filterName, filter);
63              return null;
64          }
65  
66          if (name.equals("addFilterLast")) {
67              String filterName = (String) params[0];
68              ObjectName filterRef = (ObjectName) params[1];
69              IoFilter filter = getFilter(filterRef);
70              getSource().getFilterChain().addLast(filterName, filter);
71              return null;
72          }
73  
74          if (name.equals("addFilterBefore")) {
75              String filterBaseName = (String) params[0];
76              String filterName = (String) params[1];
77              ObjectName filterRef = (ObjectName) params[2];
78              IoFilter filter = getFilter(filterRef);
79              getSource().getFilterChain().addBefore(filterBaseName, filterName, filter);
80              return null;
81          }
82  
83          if (name.equals("addFilterAfter")) {
84              String filterBaseName = (String) params[0];
85              String filterName = (String) params[1];
86              ObjectName filterRef = (ObjectName) params[2];
87              IoFilter filter = getFilter(filterRef);
88              getSource().getFilterChain().addAfter(filterBaseName, filterName, filter);
89              return null;
90          }
91  
92          if (name.equals("removeFilter")) {
93              String filterName = (String) params[0];
94              getSource().getFilterChain().remove(filterName);
95              return null;
96          }
97  
98          return super.invoke0(name, params, signature);
99      }
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 }