001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.mina.integration.jmx;
018:
019: import java.net.SocketAddress;
020: import java.util.LinkedHashSet;
021: import java.util.List;
022: import java.util.Set;
023:
024: import javax.management.MBeanParameterInfo;
025: import javax.management.ObjectName;
026: import javax.management.modelmbean.ModelMBeanOperationInfo;
027:
028: import ognl.Ognl;
029:
030: import org.apache.mina.common.IoService;
031: import org.apache.mina.common.IoSession;
032: import org.apache.mina.integration.ognl.IoSessionFinder;
033:
034: /**
035: * A JMX MBean wrapper for an {@link IoSession}.
036: *
037: * @author The Apache MINA Project (dev@mina.apache.org)
038: * @version $Rev: 601229 $, $Date: 2007-12-05 00:13:18 -0700 (Wed, 05 Dec 2007) $
039: */
040: public class IoServiceMBean extends ObjectMBean<IoService> {
041:
042: static String getSessionIdAsString(long l) {
043: // ID in MINA is a unsigned 32-bit integer.
044: String id = Long.toHexString(l).toUpperCase();
045: while (id.length() < 8) {
046: id = '0' + id; // padding
047: }
048: id = "0x" + id;
049: return id;
050: }
051:
052: public IoServiceMBean(IoService source) {
053: super (source);
054: }
055:
056: @Override
057: protected Object invoke0(String name, Object[] params,
058: String[] signature) throws Exception {
059: if (name.equals("findSessions")) {
060: IoSessionFinder finder = new IoSessionFinder(
061: (String) params[0]);
062: return finder.find(getSource().getManagedSessions());
063: }
064:
065: if (name.equals("findAndRegisterSessions")) {
066: IoSessionFinder finder = new IoSessionFinder(
067: (String) params[0]);
068: Set<IoSession> registeredSessions = new LinkedHashSet<IoSession>();
069: for (IoSession s : finder.find(getSource()
070: .getManagedSessions())) {
071: try {
072: getServer().registerMBean(
073: new IoSessionMBean(s),
074: new ObjectName(getName().getDomain()
075: + ":type=session,name="
076: + getSessionIdAsString(s.getId())));
077: registeredSessions.add(s);
078: } catch (Exception e) {
079: logger.warn(
080: "Failed to register a session as a MBean: "
081: + s, e);
082: }
083: }
084:
085: return registeredSessions;
086: }
087:
088: if (name.equals("findAndProcessSessions")) {
089: IoSessionFinder finder = new IoSessionFinder(
090: (String) params[0]);
091: String command = (String) params[1];
092: Object expr = Ognl.parseExpression(command);
093: Set<IoSession> matches = finder.find(getSource()
094: .getManagedSessions());
095:
096: for (IoSession s : matches) {
097: try {
098: Ognl.getValue(expr, s);
099: } catch (Exception e) {
100: logger.warn("Failed to execute '" + command
101: + "' for: " + s, e);
102: }
103: }
104: return matches;
105: }
106:
107: return super .invoke0(name, params, signature);
108: }
109:
110: @Override
111: protected void addExtraOperations(
112: List<ModelMBeanOperationInfo> operations) {
113: operations.add(new ModelMBeanOperationInfo("findSessions",
114: "findSessions",
115: new MBeanParameterInfo[] { new MBeanParameterInfo(
116: "ognlQuery", String.class.getName(),
117: "a boolean OGNL expression") }, Set.class
118: .getName(), ModelMBeanOperationInfo.INFO));
119: operations
120: .add(new ModelMBeanOperationInfo(
121: "findAndRegisterSessions",
122: "findAndRegisterSessions",
123: new MBeanParameterInfo[] { new MBeanParameterInfo(
124: "ognlQuery", String.class.getName(),
125: "a boolean OGNL expression") },
126: Set.class.getName(),
127: ModelMBeanOperationInfo.ACTION_INFO));
128: operations
129: .add(new ModelMBeanOperationInfo(
130: "findAndProcessSessions",
131: "findAndProcessSessions",
132: new MBeanParameterInfo[] {
133: new MBeanParameterInfo("ognlQuery",
134: String.class.getName(),
135: "a boolean OGNL expression"),
136: new MBeanParameterInfo(
137: "ognlCommand",
138: String.class.getName(),
139: "an OGNL expression that modifies the state of the sessions in the match result") },
140: Set.class.getName(),
141: ModelMBeanOperationInfo.ACTION_INFO));
142: }
143:
144: @Override
145: protected boolean isOperation(String methodName,
146: Class<?>[] paramTypes) {
147: // Ignore some IoServide methods.
148: if (methodName
149: .matches("(newSession|broadcast|(add|remove)Listener)")) {
150: return false;
151: }
152:
153: if ((methodName.equals("bind") || methodName.equals("unbind"))
154: && (paramTypes.length > 1 || (paramTypes.length == 1 && !SocketAddress.class
155: .isAssignableFrom(paramTypes[0])))) {
156: return false;
157: }
158:
159: return super.isOperation(methodName, paramTypes);
160: }
161: }
|