📄 adminconnectionmanager.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2000-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* Date Author Changes
* 04/07/2000 jima Created
*/
package org.exolab.jms.server;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.JMSSecurityException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.core.foundation.HandleIfc;
import org.exolab.jms.authentication.AuthenticationMgr;
import org.exolab.jms.events.BasicEventManager;
import org.exolab.jms.events.Event;
import org.exolab.jms.events.EventHandler;
import org.exolab.jms.events.IllegalEventDefinedException;
/**
* The admin connection manager is responsible for managing all admin
* connections to the JmsServer.
* The connection manager is a singleton (at this point anyway)
* that is accessible through the instance class method. It is also responsible
* for holding a list of adminconnections.
*
* @version $Revision: 1.2 $ $Date: 2003/08/07 13:33:08 $
* @author <a href="mailto:knut@lerpold.no">Knut Lerpold</a>
* @see org.exolab.jms.server.AdminConnection
*/
public class AdminConnectionManager implements EventHandler {
/**
* The interval that garbage collection on the connection manager is
* executed to clean up abnormally terminated connections. This defaults
* to every minute.
*/
private long _gcInterval = 60 * 1000;
/**
* holds a list of active connections
*/
private Map _connections = null;
/**
* Holds the singleton instance of the class
*/
private static AdminConnectionManager _instance = null;
/**
* Used to stop multiple threads trying to init the singleton
*/
private static final Object _initializer = new Object();
/**
* This is the event that is fired to initiate garbage collection
* in the database
*/
private static final int CONNECTION_GC_EVENT = 1;
/**
* The logger
*/
private static final Log _log =
LogFactory.getLog(AdminConnectionManager.class);
/**
* The private constructor initialises the manager.
*/
private AdminConnectionManager() {
_connections = Collections.synchronizedMap(new HashMap());
// check to see whether the gcInterval has been overriden
if (System.getProperty("org.exolab.jms.connection.gcInterval")
!= null) {
try {
_gcInterval = Long.parseLong(System.getProperty(
"org.exolab.jms.connection.gcInterval"));
} catch (Exception ignore) {
// if the gcinterval is incorrectly specified then use
// the default
}
}
if (_gcInterval > 0) {
registerEvent();
}
}
/**
* The static method returns the singleton instance of the
* AdminConnectionManager. If one does not exist it is created prior to
* returning.
*
* @return AdminConnectionManager
*/
public static AdminConnectionManager instance() {
if (_instance == null) {
synchronized (_initializer) {
if (_instance == null) {
_instance = new AdminConnectionManager();
}
}
}
return _instance;
}
/**
* Create a connection with the specified client id.
* The connection contains
*
*
* @param id the client identity
* @param username the client's username
* @param password the client's password
* @return a new admin connection
* @throws JMSSecurityException if the client cannot be authenticated
* @throws JMSException if the connection cannot be created
*/
public AdminConnection createConnection(String id, String username,
String password)
throws JMSSecurityException, JMSException {
if (!AuthenticationMgr.instance().validateUser(username, password)) {
throw new JMSSecurityException("Failed to authenticate user "
+ username);
}
//creates a new connection for every connect.
AdminConnection result = new AdminConnection(id);
_connections.put(result.getIdentifierId(), result);
return result;
}
/**
* Return an Enumeration of all the connections currently registered with
* the connection manager
*
* @return Iterator
*/
public Iterator getConnections() {
return _connections.values().iterator();
}
/**
* Return the connection associated with a particular client identity. If
* such a connection does not exist then return null
*
* @param id identity of the client
* @return JmsConnection associated connection or null
*/
public AdminConnection getConnection(String id) {
return (AdminConnection) _connections.get(id);
}
/**
* Close the connection associated with a particular client identity. If
* a connection for the client does not exist then do nothing.
*
* @param identifierId the unique id for this connection
*/
public void closeConnection(String identifierId) {
AdminConnection connection =
(AdminConnection) _connections.remove(identifierId);
if (connection != null) {
connection.close();
}
}
// implementation of EventHandler.handleEvent
public void handleEvent(int event, Object callback, long time) {
try {
Object[] ids = _connections.keySet().toArray();
for (int index = 0; index < ids.length; index++) {
AdminConnection connection =
(AdminConnection) _connections.get(ids[index]);
if ((connection != null)
&& (!connection.isClientActive())) {
_log.info("Cleaning up connection " + ids[index]);
closeConnection((String) ids[index]);
}
}
} finally {
registerEvent();
}
}
// implementation of EventHandler.getHandle
public HandleIfc getHandle() {
// not used
return null;
}
/**
* Register an event with the event service. The event will tell the
* connection manager to initiate garbage collection
*/
private void registerEvent() {
try {
BasicEventManager.instance().registerEventRelative(
new Event(CONNECTION_GC_EVENT, this, null),
_gcInterval);
} catch (IllegalEventDefinedException exception) {
_log.error("AdminConnectionManager.registerEvent failed",
exception);
}
}
} //-- AdminConnectionManager
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -