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       * Creates a new IoSessionMBean instance
40       * 
41       * @param source The IoSession to monitor
42       */
43      public IoSessionMBean(IoSession source) {
44          super(source);
45      }
46  
47      @Override
48      protected Object getAttribute0(String fqan) throws Exception {
49          if (fqan.equals("attributes")) {
50              Map<String, String> answer = new LinkedHashMap<String, String>();
51              
52              for (Object key : getSource().getAttributeKeys()) {
53                  answer.put(String.valueOf(key), String.valueOf(getSource().getAttribute(key)));
54              }
55              
56              return answer;
57          }
58  
59          return super.getAttribute0(fqan);
60      }
61  
62      @Override
63      protected Object invoke0(String name, Object[] params, String[] signature) throws Exception {
64          if (name.equals("addFilterFirst")) {
65              String filterName = (String) params[0];
66              ObjectName filterRef = (ObjectName) params[1];
67              IoFilter filter = getFilter(filterRef);
68              getSource().getFilterChain().addFirst(filterName, filter);
69              
70              return null;
71          }
72  
73          if (name.equals("addFilterLast")) {
74              String filterName = (String) params[0];
75              ObjectName filterRef = (ObjectName) params[1];
76              IoFilter filter = getFilter(filterRef);
77              getSource().getFilterChain().addLast(filterName, filter);
78              
79              return null;
80          }
81  
82          if (name.equals("addFilterBefore")) {
83              String filterBaseName = (String) params[0];
84              String filterName = (String) params[1];
85              ObjectName filterRef = (ObjectName) params[2];
86              IoFilter filter = getFilter(filterRef);
87              getSource().getFilterChain().addBefore(filterBaseName, filterName, filter);
88              return null;
89          }
90  
91          if (name.equals("addFilterAfter")) {
92              String filterBaseName = (String) params[0];
93              String filterName = (String) params[1];
94              ObjectName filterRef = (ObjectName) params[2];
95              IoFilter filter = getFilter(filterRef);
96              getSource().getFilterChain().addAfter(filterBaseName, filterName, filter);
97              
98              return null;
99          }
100 
101         if (name.equals("removeFilter")) {
102             String filterName = (String) params[0];
103             getSource().getFilterChain().remove(filterName);
104             
105             return null;
106         }
107 
108         return super.invoke0(name, params, signature);
109     }
110 
111     private IoFilter getFilter(ObjectName filterRef) throws MBeanException {
112         Object object = ObjectMBean.getSource(filterRef);
113         
114         if (object == null) {
115             throw new MBeanException(new IllegalArgumentException("MBean not found: " + filterRef));
116         }
117         
118         if (!(object instanceof IoFilter)) {
119             throw new MBeanException(new IllegalArgumentException("MBean '" + filterRef + "' is not an IoFilter."));
120         }
121 
122         return (IoFilter) object;
123     }
124 
125     @Override
126     protected void addExtraAttributes(List<ModelMBeanAttributeInfo> attributes) {
127         attributes
128                 .add(new ModelMBeanAttributeInfo("attributes", Map.class.getName(), "attributes", true, false, false));
129     }
130 
131     @Override
132     protected void addExtraOperations(List<ModelMBeanOperationInfo> operations) {
133         operations.add(new ModelMBeanOperationInfo("addFilterFirst", "addFilterFirst",
134                 new MBeanParameterInfo[] {
135                         new MBeanParameterInfo("name", String.class.getName(), "the new filter name"),
136                         new MBeanParameterInfo("filter", ObjectName.class.getName(),
137                                 "the ObjectName reference to the filter") }, void.class.getName(),
138                 ModelMBeanOperationInfo.ACTION));
139 
140         operations.add(new ModelMBeanOperationInfo("addFilterLast", "addFilterLast",
141                 new MBeanParameterInfo[] {
142                         new MBeanParameterInfo("name", String.class.getName(), "the new filter name"),
143                         new MBeanParameterInfo("filter", ObjectName.class.getName(),
144                                 "the ObjectName reference to the filter") }, void.class.getName(),
145                 ModelMBeanOperationInfo.ACTION));
146 
147         operations.add(new ModelMBeanOperationInfo("addFilterBefore", "addFilterBefore",
148                 new MBeanParameterInfo[] {
149                         new MBeanParameterInfo("baseName", String.class.getName(), "the next filter name"),
150                         new MBeanParameterInfo("name", String.class.getName(), "the new filter name"),
151                         new MBeanParameterInfo("filter", ObjectName.class.getName(),
152                                 "the ObjectName reference to the filter") }, void.class.getName(),
153                 ModelMBeanOperationInfo.ACTION));
154 
155         operations.add(new ModelMBeanOperationInfo("addFilterAfter", "addFilterAfter",
156                 new MBeanParameterInfo[] {
157                         new MBeanParameterInfo("baseName", String.class.getName(), "the previous filter name"),
158                         new MBeanParameterInfo("name", String.class.getName(), "the new filter name"),
159                         new MBeanParameterInfo("filter", ObjectName.class.getName(),
160                                 "the ObjectName reference to the filter") }, void.class.getName(),
161                 ModelMBeanOperationInfo.ACTION));
162 
163         operations.add(new ModelMBeanOperationInfo("removeFilter", "removeFilter",
164                 new MBeanParameterInfo[] { new MBeanParameterInfo("name", String.class.getName(),
165                         "the name of the filter to be removed"), }, void.class.getName(),
166                 ModelMBeanOperationInfo.ACTION));
167     }
168 
169     @Override
170     protected boolean isOperation(String methodName, Class<?>[] paramTypes) {
171         // Ignore some IoSession methods.
172         if (methodName.matches("(write|read|(remove|replace|contains)Attribute)")) {
173             return false;
174         }
175 
176         return super.isOperation(methodName, paramTypes);
177     }
178 }