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.common.support.BaseIoSession;
29  import org.apache.mina.filter.LoggingFilter;
30  import org.apache.mina.management.IoSessionStat;
31  import org.apache.mina.management.StatCollector;
32  
33  /**
34   * @author The Apache Directory Project (mina-dev@directory.apache.org)
35   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
36   */
37  public class IoSessionManager implements IoSessionManagerMBean {
38  
39      private IoSession session;
40  
41      /**
42       * create the session manager
43       * @param session the MINA's session to manage
44       */
45      public IoSessionManager(IoSession session) {
46          this.session = session;
47      }
48  
49      /**
50       * @see archean.util.mina.IoSessionManagerMBean#isConnected()
51       */
52      public boolean isConnected() {
53          return session.isConnected();
54      }
55  
56      /**
57       * @see archean.util.mina.IoSessionManagerMBean#getReadBytes()
58       */
59      public long getReadBytes() {
60          return session.getReadBytes();
61      }
62  
63      /**
64       * @see archean.util.mina.IoSessionManagerMBean#getWrittenBytes()
65       */
66      public long getWrittenBytes() {
67          return session.getWrittenBytes();
68      }
69  
70      public long getReadMessages() {
71          return ((BaseIoSession) session).getReadMessages();
72      }
73  
74      public long getWrittenMessages() {
75          return ((BaseIoSession) session).getWrittenMessages();
76      }
77  
78      /**
79       * @see archean.util.mina.IoSessionManagerMBean#close()
80       */
81      public void close() {
82          session.close().join();
83      }
84  
85      /**
86       * @see archean.util.mina.IoSessionManagerMBean#getCreationTime()
87       */
88      public Date getCreationTime() {
89          return new Date(session.getCreationTime());
90      }
91  
92      /**
93       * @see archean.util.mina.IoSessionManagerMBean#getLastIoTime()
94       */
95      public Date getLastIoTime() {
96          return new Date(session.getLastIoTime());
97      }
98  
99      /**
100      * @see archean.util.mina.IoSessionManagerMBean#getLastReadTime()
101      */
102     public Date getLastReadTime() {
103         return new Date(session.getLastReadTime());
104     }
105 
106     /**
107      * @see archean.util.mina.IoSessionManagerMBean#getLastWriteTime()
108      */
109     public Date getLastWriteTime() {
110         return new Date(session.getLastWriteTime());
111     }
112 
113     /**
114      * @see archean.util.mina.IoSessionManagerMBean#getInstalledFilters()
115      */
116     public String[] getInstalledFilters() {
117         List filters = session.getFilterChain().getAll();
118         String[] res = new String[filters.size()];
119         for (int i = 0; i < res.length; i++) {
120             res[i] = ((IoFilterChain.Entry) filters.get(i)).getName();
121         }
122         return res;
123     }
124 
125     /**
126      * @see archean.util.mina.IoSessionManagerMBean#addLastLoggingFilter()
127      */
128     public void addLastLoggingFilter() {
129         LoggingFilter f = new LoggingFilter();
130         session.getFilterChain().addLast("LoggerLast", f);
131     }
132 
133     /**
134      * @see archean.util.mina.IoSessionManagerMBean#removeLastLoggingFilter()
135      */
136     public void removeLastLoggingFilter() {
137 
138         session.getFilterChain().remove("LoggerLast");
139     }
140 
141     /**
142      * @see archean.util.mina.IoSessionManagerMBean#addFirstLoggingFilter()
143      */
144     public void addFirstLoggingFilter() {
145         LoggingFilter f = new LoggingFilter();
146         session.getFilterChain().addFirst("LoggerFirst", f);
147     }
148 
149     public void removeFirstLoggingFilter() {
150 
151         session.getFilterChain().remove("LoggerFirst");
152     }
153 
154     //  IDLE monitoring
155 
156     /**
157      * @see archean.util.mina.IoSessionManagerMBean#getReadIdleTime()
158      */
159     public long getReadIdleTime() {
160         return session.getIdleTimeInMillis(IdleStatus.READER_IDLE);
161     }
162 
163     /**
164      * @see archean.util.mina.IoSessionManagerMBean#getWriteIdleTime()
165      */
166     public long getWriteIdleTime() {
167         return session.getIdleTimeInMillis(IdleStatus.WRITER_IDLE);
168     }
169 
170     /**
171      * @see archean.util.mina.IoSessionManagerMBean#getBothIdleTime()
172      */
173     public long getBothIdleTime() {
174         return session.getIdleTimeInMillis(IdleStatus.BOTH_IDLE);
175     }
176 
177     public float getByteReadThroughtput() {
178         IoSessionStat stats = (IoSessionStat) session
179                 .getAttribute(StatCollector.KEY);
180         if (stats == null)
181             return Float.NaN;
182         else
183             return stats.getByteReadThroughput();
184     }
185 
186     public float getByteWrittenThroughtput() {
187         IoSessionStat stats = (IoSessionStat) session
188                 .getAttribute(StatCollector.KEY);
189         if (stats == null)
190             return Float.NaN;
191         else
192             return stats.getByteWrittenThroughput();
193     }
194 
195     public float getMessageReadThroughtput() {
196         IoSessionStat stats = (IoSessionStat) session
197                 .getAttribute(StatCollector.KEY);
198         if (stats == null)
199             return Float.NaN;
200         else
201             return stats.getMessageReadThroughput();
202     }
203 
204     public float getMessageWrittenThroughtput() {
205         IoSessionStat stats = (IoSessionStat) session
206                 .getAttribute(StatCollector.KEY);
207         if (stats == null)
208             return Float.NaN;
209         else
210             return stats.getMessageWrittenThroughput();
211     }
212 }