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.net.SocketAddress;
20  import java.util.LinkedHashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import javax.management.MBeanOperationInfo;
25  import javax.management.MBeanParameterInfo;
26  import javax.management.ObjectName;
27  import javax.management.modelmbean.ModelMBeanOperationInfo;
28  
29  import ognl.Ognl;
30  
31  import org.apache.mina.core.service.IoService;
32  import org.apache.mina.core.session.IoSession;
33  import org.apache.mina.integration.ognl.IoSessionFinder;
34  
35  /**
36   * A JMX MBean wrapper for an {@link IoSession}.
37   *
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class IoServiceMBean extends ObjectMBean<IoService> {
41  
42      static String getSessionIdAsString(long l) {
43          // ID in MINA is a unsigned 32-bit integer.
44          String id = Long.toHexString(l).toUpperCase();
45          while (id.length() < 8) {
46              id = '0' + id; // padding
47          }
48          id = "0x" + id;
49          return id;
50      }
51  
52      public IoServiceMBean(IoService source) {
53          super(source);
54      }
55  
56      @Override
57      protected Object invoke0(String name, Object[] params, String[] signature) throws Exception {
58          if (name.equals("findSessions")) {
59              IoSessionFinder finder = new IoSessionFinder((String) params[0]);
60              return finder.find(getSource().getManagedSessions().values());
61          }
62  
63          if (name.equals("findAndRegisterSessions")) {
64              IoSessionFinder finder = new IoSessionFinder((String) params[0]);
65              Set<IoSession> registeredSessions = new LinkedHashSet<IoSession>();
66              for (IoSession s : finder.find(getSource().getManagedSessions().values())) {
67                  try {
68                      getServer().registerMBean(
69                              new IoSessionMBean(s),
70                              new ObjectName(getName().getDomain() + ":type=session,name="
71                                      + getSessionIdAsString(s.getId())));
72                      registeredSessions.add(s);
73                  } catch (Exception e) {
74                      LOGGER.warn("Failed to register a session as a MBean: " + s, e);
75                  }
76              }
77  
78              return registeredSessions;
79          }
80  
81          if (name.equals("findAndProcessSessions")) {
82              IoSessionFinder finder = new IoSessionFinder((String) params[0]);
83              String command = (String) params[1];
84              Object expr = Ognl.parseExpression(command);
85              Set<IoSession> matches = finder.find(getSource().getManagedSessions().values());
86  
87              for (IoSession s : matches) {
88                  try {
89                      Ognl.getValue(expr, s);
90                  } catch (Exception e) {
91                      LOGGER.warn("Failed to execute '" + command + "' for: " + s, e);
92                  }
93              }
94              return matches;
95          }
96  
97          return super.invoke0(name, params, signature);
98      }
99  
100     @Override
101     protected void addExtraOperations(List<ModelMBeanOperationInfo> operations) {
102         operations.add(new ModelMBeanOperationInfo("findSessions", "findSessions",
103                 new MBeanParameterInfo[] { new MBeanParameterInfo("ognlQuery", String.class.getName(),
104                         "a boolean OGNL expression") }, Set.class.getName(), MBeanOperationInfo.INFO));
105         operations.add(new ModelMBeanOperationInfo("findAndRegisterSessions", "findAndRegisterSessions",
106                 new MBeanParameterInfo[] { new MBeanParameterInfo("ognlQuery", String.class.getName(),
107                         "a boolean OGNL expression") }, Set.class.getName(), MBeanOperationInfo.ACTION_INFO));
108         operations.add(new ModelMBeanOperationInfo("findAndProcessSessions", "findAndProcessSessions",
109                 new MBeanParameterInfo[] {
110                         new MBeanParameterInfo("ognlQuery", String.class.getName(), "a boolean OGNL expression"),
111                         new MBeanParameterInfo("ognlCommand", String.class.getName(),
112                                 "an OGNL expression that modifies the state of the sessions in the match result") },
113                 Set.class.getName(), MBeanOperationInfo.ACTION_INFO));
114     }
115 
116     @Override
117     protected boolean isOperation(String methodName, Class<?>[] paramTypes) {
118         // Ignore some IoServide methods.
119         if (methodName.matches("(newSession|broadcast|(add|remove)Listener)")) {
120             return false;
121         }
122 
123         if ((methodName.equals("bind") || methodName.equals("unbind"))
124                 && (paramTypes.length > 1 || paramTypes.length == 1
125                         && !SocketAddress.class.isAssignableFrom(paramTypes[0]))) {
126             return false;
127         }
128 
129         return super.isOperation(methodName, paramTypes);
130     }
131 }