View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.integration.jmx;
21  
22  import java.util.Date;
23  import java.util.List;
24  
25  import org.apache.mina.common.IdleStatus;
26  import org.apache.mina.common.IoFilterChain;
27  import org.apache.mina.common.IoSession;
28  import org.apache.mina.filter.LoggingFilter;
29  import org.apache.mina.management.IoSessionStat;
30  import org.apache.mina.management.StatCollector;
31  
32  /**
33   * @author The Apache Directory Project (mina-dev@directory.apache.org)
34   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
35   */
36  public class IoSessionManager implements IoSessionManagerMBean {
37  
38      private IoSession session;
39  
40      /**
41       * create the session manager
42       * @param session the MINA's session to manage
43       */
44      public IoSessionManager(IoSession session) {
45          this.session = session;
46      }
47  
48      public boolean isConnected() {
49          return session.isConnected();
50      }
51  
52      public long getReadBytes() {
53          return session.getReadBytes();
54      }
55  
56      public long getWrittenBytes() {
57          return session.getWrittenBytes();
58      }
59  
60      public long getReadMessages() {
61          return session.getReadMessages();
62      }
63  
64      public long getWrittenMessages() {
65          return session.getWrittenMessages();
66      }
67  
68      public void close() throws InterruptedException {
69          session.close().join();
70      }
71  
72      public Date getCreationTime() {
73          return new Date(session.getCreationTime());
74      }
75  
76      public Date getLastIoTime() {
77          return new Date(session.getLastIoTime());
78      }
79  
80      public Date getLastReadTime() {
81          return new Date(session.getLastReadTime());
82      }
83  
84      public Date getLastWriteTime() {
85          return new Date(session.getLastWriteTime());
86      }
87  
88      public String[] getInstalledFilters() {
89          List filters = session.getFilterChain().getAll();
90          String[] res = new String[filters.size()];
91          for (int i = 0; i < res.length; i++) {
92              res[i] = ((IoFilterChain.Entry) filters.get(i)).getName();
93          }
94          return res;
95      }
96  
97      public void addLastLoggingFilter() {
98          LoggingFilter f = new LoggingFilter();
99          session.getFilterChain().addLast("LoggerLast", f);
100     }
101 
102     public void removeLastLoggingFilter() {
103 
104         session.getFilterChain().remove("LoggerLast");
105     }
106 
107     public void addFirstLoggingFilter() {
108         LoggingFilter f = new LoggingFilter();
109         session.getFilterChain().addFirst("LoggerFirst", f);
110     }
111 
112     public void removeFirstLoggingFilter() {
113 
114         session.getFilterChain().remove("LoggerFirst");
115     }
116 
117     //  IDLE monitoring
118 
119     public long getReadIdleTime() {
120         return session.getIdleTimeInMillis(IdleStatus.READER_IDLE);
121     }
122 
123     public long getWriteIdleTime() {
124         return session.getIdleTimeInMillis(IdleStatus.WRITER_IDLE);
125     }
126 
127     public long getBothIdleTime() {
128         return session.getIdleTimeInMillis(IdleStatus.BOTH_IDLE);
129     }
130 
131     public float getByteReadThroughtput() {
132         IoSessionStat stats = (IoSessionStat) session
133                 .getAttribute(StatCollector.KEY);
134         if (stats == null)
135             return Float.NaN;
136         else
137             return stats.getByteReadThroughput();
138     }
139 
140     public float getByteWrittenThroughtput() {
141         IoSessionStat stats = (IoSessionStat) session
142                 .getAttribute(StatCollector.KEY);
143         if (stats == null)
144             return Float.NaN;
145         else
146             return stats.getByteWrittenThroughput();
147     }
148 
149     public float getMessageReadThroughtput() {
150         IoSessionStat stats = (IoSessionStat) session
151                 .getAttribute(StatCollector.KEY);
152         if (stats == null)
153             return Float.NaN;
154         else
155             return stats.getMessageReadThroughput();
156     }
157 
158     public float getMessageWrittenThroughtput() {
159         IoSessionStat stats = (IoSessionStat) session
160                 .getAttribute(StatCollector.KEY);
161         if (stats == null)
162             return Float.NaN;
163         else
164             return stats.getMessageWrittenThroughput();
165     }
166 }