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   */
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(
105                     "MBean not found: " + filterRef));
106         }
107         if (!(object instanceof IoFilter)) {
108             throw new MBeanException(new IllegalArgumentException(
109                     "MBean '" + filterRef + "' is not an IoFilter."));
110         }
111         
112         return (IoFilter) object;
113     }
114 
115     @Override
116     protected void addExtraAttributes(List<ModelMBeanAttributeInfo> attributes) {
117         attributes.add(new ModelMBeanAttributeInfo(
118                 "attributes", Map.class.getName(), "attributes",
119                 true, false, false));
120     }
121     
122     @Override
123     protected void addExtraOperations(List<ModelMBeanOperationInfo> operations) {
124         operations.add(new ModelMBeanOperationInfo(
125                 "addFilterFirst", "addFilterFirst", new MBeanParameterInfo[] {
126                         new MBeanParameterInfo(
127                                 "name", String.class.getName(), "the new filter name"),
128                         new MBeanParameterInfo(
129                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
130                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
131 
132         operations.add(new ModelMBeanOperationInfo(
133                 "addFilterLast", "addFilterLast", new MBeanParameterInfo[] {
134                         new MBeanParameterInfo(
135                                 "name", String.class.getName(), "the new filter name"),
136                         new MBeanParameterInfo(
137                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
138                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
139 
140         operations.add(new ModelMBeanOperationInfo(
141                 "addFilterBefore", "addFilterBefore", new MBeanParameterInfo[] {
142                         new MBeanParameterInfo(
143                                 "baseName", String.class.getName(), "the next filter name"),
144                         new MBeanParameterInfo(
145                                 "name", String.class.getName(), "the new filter name"),
146                         new MBeanParameterInfo(
147                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
148                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
149 
150         operations.add(new ModelMBeanOperationInfo(
151                 "addFilterAfter", "addFilterAfter", new MBeanParameterInfo[] {
152                         new MBeanParameterInfo(
153                                 "baseName", String.class.getName(), "the previous filter name"),
154                         new MBeanParameterInfo(
155                                 "name", String.class.getName(), "the new filter name"),
156                         new MBeanParameterInfo(
157                                 "filter", ObjectName.class.getName(), "the ObjectName reference to the filter")
158                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
159         
160         operations.add(new ModelMBeanOperationInfo(
161                 "removeFilter", "removeFilter", new MBeanParameterInfo[] {
162                         new MBeanParameterInfo(
163                                 "name", String.class.getName(), "the name of the filter to be removed"),
164                 }, void.class.getName(), ModelMBeanOperationInfo.ACTION));
165     }
166 
167     @Override
168     protected boolean isOperation(String methodName, Class<?>[] paramTypes) {
169         // Ignore some IoSession methods.
170         if (methodName.matches(
171                 "(write|read|(remove|replace|contains)Attribute)")) {
172             return false;
173         }
174         
175         return super.isOperation(methodName, paramTypes);
176     }
177 }