View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jboss.managed;
25  
26  import java.util.concurrent.TimeUnit;
27  import org.jboss.managed.api.ManagedOperation.Impact;
28  import org.jboss.managed.api.annotation.ManagementComponent;
29  import org.jboss.managed.api.annotation.ManagementObject;
30  import org.jboss.managed.api.annotation.ManagementOperation;
31  import org.jboss.managed.api.annotation.ManagementProperties;
32  import org.jboss.managed.api.annotation.ManagementProperty;
33  import org.jboss.managed.api.annotation.ViewUse;
34  import org.modeshape.common.util.CheckArg;
35  import org.modeshape.graph.connector.RepositoryConnectionPool;
36  
37  /**
38   * A <code>ManagedConnectionPool</code> instance is a JBoss managed object for a {@link RepositoryConnectionPool repository
39   * connection pool}.
40   */
41  @ManagementObject( name = "ModeShapeConnectionPool", description = "A ModeShape repository connection pool", componentType = @ManagementComponent( type = "ModeShape", subtype = "ConnectionPool" ), properties = ManagementProperties.EXPLICIT )
42  public final class ManagedConnectionPool implements ModeShapeManagedObject {
43  
44      /**
45       * The ModeShape object being managed and delegated to (never <code>null</code>).
46       */
47      private final RepositoryConnectionPool connectionPool;
48  
49      /**
50       * Creates a JBoss managed object for the specified connection pool.
51       * 
52       * @param connectionPool the connection pool being managed (never <code>null</code>)
53       */
54      public ManagedConnectionPool( RepositoryConnectionPool connectionPool ) {
55          CheckArg.isNotNull(connectionPool, "connectionPool");
56          this.connectionPool = connectionPool;
57      }
58  
59      /**
60       * Removes all connections. This is a JBoss managed operation.
61       * 
62       * @return <code>true</code> if successful
63       */
64      @ManagementOperation( description = "Removes all in-use connections from the pool", impact = Impact.WriteOnly )
65      public boolean flush() {
66          // TODO implement flush()
67          return false;
68      }
69  
70      /**
71       * Obtains the number of connections available for use. This is a JBoss managed readonly property.
72       * 
73       * @return the number of available connections
74       */
75      @ManagementProperty( name = "Available Connections", description = "The number of available connections", readOnly = true, use = ViewUse.STATISTIC )
76      public int getAvailableCount() {
77          return getMaxSize() - getSize();
78      }
79  
80      /**
81       * Obtains the number of connections that have been created. This is a JBoss managed readonly property.
82       * 
83       * @return the number of connections created
84       */
85      @ManagementProperty( name = "Connections Created", description = "The number of connections that have been created", readOnly = true, use = ViewUse.STATISTIC )
86      public long getCreatedCount() {
87          return this.connectionPool.getTotalConnectionsCreated();
88      }
89  
90      /**
91       * Obtains the number of connections that have been destroyed. This is a JBoss managed readonly property.
92       * 
93       * @return the number of connections destroyed
94       */
95      @ManagementProperty( name = "Connections Destroyed", description = "The number of connections that have been destroyed", readOnly = true, use = ViewUse.STATISTIC )
96      public long getDestroyedCount() {
97          // TODO how do you calculate connection destroyed count
98          return this.connectionPool.getTotalConnectionsCreated() - getSize();
99      }
100 
101     /**
102      * Obtains the total number of times connections have been used. This is a JBoss managed readonly property.
103      * 
104      * @return the number of times
105      */
106     @ManagementProperty( name = "Connections In-use", description = "The number of connections currently being used", readOnly = true, use = ViewUse.STATISTIC )
107     public int getInUseCount() {
108         return this.connectionPool.getInUseCount();
109     }
110 
111     @ManagementProperty( name = "In-use High Water Mark", description = "The in-use high water mark", readOnly = true, use = ViewUse.STATISTIC )
112     public int getInUseHighWaterMark() {
113         // TODO implement getInUseHighWaterMark()
114         return 0;
115     }
116 
117     /**
118      * Obtains the time a connection can remain idle before being closed. This is a JBoss managed writable property.
119      * 
120      * @return the new connection max idle time
121      */
122     @ManagementProperty( name = "Keep Alive Time", description = "The time in nanonseconds to keep an idle connection alive", readOnly = false, use = ViewUse.RUNTIME )
123     public long getKeepAliveTime() {
124         return this.connectionPool.getKeepAliveTime(TimeUnit.NANOSECONDS);
125     }
126 
127     /**
128      * Obtains the number of failed attempts at trying to a connection before throwing an error. This is a JBoss managed writable
129      * property.
130      * 
131      * @return the number of failed attempts to connect
132      */
133     @ManagementProperty( name = "Max Failed Attempts Before Error", description = "The number of failed attempts to establish a connection before throwing an error", readOnly = false, use = ViewUse.RUNTIME )
134     public int getMaxFailedAttemptsBeforeError() {
135         return this.connectionPool.getMaxFailedAttemptsBeforeError();
136     }
137 
138     /**
139      * Obtains the maximum size of the pool. This is a JBoss managed writable property.
140      * 
141      * @return the maximum pool size
142      */
143     @ManagementProperty( name = "Max size", description = "The maximum number of connections allowed", readOnly = false, use = ViewUse.RUNTIME )
144     public int getMaxSize() {
145         return this.connectionPool.getMaximumPoolSize();
146     }
147 
148     @ManagementProperty( name = "Min size", description = "The minimum number of connections allowed", readOnly = true, use = ViewUse.CONFIGURATION )
149     public int getMinSize() {
150         // TODO implement getMinSize()
151         return 0;
152     }
153 
154     /**
155      * Obtains the time to wait for a ping to complete. This is a JBoss managed writable property.
156      * 
157      * @return the time in nanoseconds
158      */
159     @ManagementProperty( name = "Ping Timeout", description = "The time in nanoseconds that ping should wait before timing out and failing", readOnly = false, use = ViewUse.RUNTIME )
160     public long getPingTimeout() {
161         return this.connectionPool.getPingTimeoutInNanos();
162     }
163 
164     /**
165      * Obtains the current number of connections. This is a JBoss managed readonly property.
166      * 
167      * @return the number of connections
168      */
169     @ManagementProperty( name = "Total Connections", description = "The total number of connections (in use and available)", readOnly = true, use = ViewUse.STATISTIC )
170     public int getSize() {
171         return this.connectionPool.getCorePoolSize();
172     }
173 
174     /**
175      * Indicates if connections should be validated before being used. This is a JBoss managed writable property.
176      * 
177      * @return <code>true</code> if connections are validated before being used
178      */
179     @ManagementProperty( name = "Validate Before Use", description = "Indicates if connections should be validated before being used", readOnly = false, use = ViewUse.RUNTIME )
180     public boolean getValidateConnectionBeforeUse() {
181         return this.connectionPool.getValidateConnectionBeforeUse();
182     }
183 
184     /**
185      * Sets the time a connection can remain idle before being closed. This is a JBoss managed property.
186      * 
187      * @param nanos the new connection max idle time (must be non-negative)
188      */
189     public void setKeepAliveTime( long nanos ) {
190         CheckArg.isNonNegative(nanos, "nanos");
191         this.connectionPool.setKeepAliveTime(nanos, TimeUnit.NANOSECONDS);
192     }
193 
194     /**
195      * Sets the maximum number of attempts to try and establish a connection. This is a JBoss managed property.
196      * 
197      * @param attempts the new maximum number of attempts (must be non-negative)
198      */
199     public void setMaxFailedAttemptsBeforeError( int attempts ) {
200         CheckArg.isNonNegative(attempts, "attempts");
201         this.connectionPool.setMaxFailedAttemptsBeforeError(attempts);
202     }
203 
204     /**
205      * Sets the maximum size of the number of connections allowed. This is a JBoss managed property.
206      * 
207      * @param size the new maximum pool size (must be positive)
208      */
209     public void setSize( int size ) {
210         CheckArg.isPositive(size, "size");
211         this.connectionPool.setMaximumPoolSize(size);
212     }
213 
214     /**
215      * Sets the time a ping will wait to complete. This is a JBoss managed property.
216      * 
217      * @param nanos the new time to wait in nanoseconds (must be non-negative)
218      */
219     public void setPingTimout( long nanos ) {
220         CheckArg.isNonNegative(nanos, "nanos");
221         this.connectionPool.setPingTimeout(nanos, TimeUnit.NANOSECONDS);
222     }
223 
224     /**
225      * Sets the flag indicating if the connection should be validated before being used. This is a JBoss managed property.
226      * 
227      * @param validate the new value validate flag value
228      */
229     public void setValidateConnectionBeforeUse( boolean validate ) {
230         this.connectionPool.setValidateConnectionBeforeUse(validate);
231     }
232 
233 }