?? destinationmanager.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 2001-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: DestinationManager.java,v 1.2 2005/03/18 03:58:39 tanderson Exp $
*/
package org.exolab.jms.messagemgr;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
import javax.jms.InvalidDestinationException;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.config.AdministeredDestinations;
import org.exolab.jms.config.AdministeredQueue;
import org.exolab.jms.config.AdministeredTopic;
import org.exolab.jms.config.ConfigurationManager;
import org.exolab.jms.config.Subscriber;
import org.exolab.jms.gc.GarbageCollectable;
import org.exolab.jms.gc.GarbageCollectionService;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.persistence.DatabaseService;
import org.exolab.jms.persistence.PersistenceAdapter;
import org.exolab.jms.persistence.PersistenceException;
import org.exolab.jms.persistence.SQLHelper;
import org.exolab.jms.server.NamingHelper;
import org.exolab.jms.service.ServiceException;
/**
* The destination manager is responsible for creating and managing the
* lifecycle of {@link DestinationCache} objects. The destination manager is
* also responsible for managing messages, that are received by the message
* manager, which do not have any registered {@link DestinationCache}
*
* @author <a href="mailto:jima@comware.com.au">Jim Alateras</a>
* @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
* @version $Revision: 1.2 $ $Date: 2005/03/18 03:58:39 $
*/
public class DestinationManager
implements MessageManagerEventListener, GarbageCollectable {
/**
* This structure maintains a list of active caches.
*/
private Map _caches = Collections.synchronizedMap(new HashMap());
/**
* The set of persistent and non-persistent destinations, keyed on name.
*/
private HashMap _destinationCache = new HashMap();
/**
* Maintains a list of wildcard destinations, which can either be durable or
* transient.
*/
private LinkedList _wildcardDestinations = new LinkedList();
/**
* Maintains a linked list of DestinationEventListener objects. These
* listeners will be informed when destinations are added or destroyed.
*/
private LinkedList _listeners = new LinkedList();
/**
* Manage the singleton instance of the DestinationManager.
*/
private static volatile DestinationManager _instance = null;
/**
* The logger.
*/
private static final Log _log =
LogFactory.getLog(DestinationManager.class);
/**
* Construct a new <code>DestinationManager</code>.
*
* @throws ServiceException if the service cannot be initialised
*/
private DestinationManager() throws ServiceException {
init();
// register with the GarbageCollectionService
GarbageCollectionService.instance().register(this);
}
/**
* Create the singleton instance of the destination manager.
*
* @return the singleton instance
* @throws ServiceException if the service cannot be initialised
*/
public static DestinationManager createInstance() throws ServiceException {
_instance = new DestinationManager();
return _instance;
}
/**
* Return the singleton destination manager.
*
* @return the singleton instance, or <code>null</code> if it hasn't been
* initialised
*/
public static DestinationManager instance() {
return _instance;
}
/**
* Returns the cache for the supplied destination. If the cache doesn't
* exist, it will be created, and any listeners notified.
*
* @param dest the destination of the cache to return
* @return the cache associated with <code>dest</code>
* @throws InvalidDestinationException if <code>dest</code> doesn't exist
* @throws JMSException if the cache can't be created
*/
public DestinationCache getDestinationCache(JmsDestination dest)
throws JMSException {
DestinationCache result;
try {
result = getDestinationCache(dest, null);
} catch (PersistenceException exception) {
String msg = "Failed to create cache for destination "
+ dest.getName();
_log.error(msg, exception);
throw new JMSException(msg + ": " + exception.getMessage());
}
return result;
}
/**
* Returns the cache for the supplied destination. If the cache doesn't
* exist, it will be created, and any listeners notified.
*
* @param dest the destination of the cache to return
* @return the cache associated with <code>dest</code>
* @throws InvalidDestinationException if <code>dest</code> doesn't exist
* @throws JMSException if the cache can't be created
* @throws PersistenceException for any persistence error
*/
public synchronized DestinationCache getDestinationCache(
JmsDestination dest, Connection connection) throws JMSException,
PersistenceException {
DestinationCache result = (DestinationCache) _caches.get(dest);
if (result == null) {
if (dest instanceof JmsTopic && ((JmsTopic) dest).isWildCard()) {
throw new InvalidDestinationException(
"Cannot cache messages for wildcarded topic: "
+ dest.getName());
}
JmsDestination existing = getDestination(dest.getName());
if (existing == null) {
throw new InvalidDestinationException(
"Destination does not exist: " + dest.getName());
}
if (existing.getPersistent()) {
if (connection != null) {
result = createPersistentCache(existing, connection);
} else {
result = createPersistentCache(existing);
}
} else {
result = createNonPersistentCache(existing);
}
_caches.put(dest, result);
// notify the listeners that a new destination has been added
// to the destination manager
notifyDestinationAdded(dest, result);
}
return result;
}
/**
* Create a persistent {@link DestinationCache} for the specified
* destination.
*
* @param dest the destination to create the cache for
* @param connection the database connection to use
* @return a new cache
* @throws JMSException for any JMS error
* @throws PersistenceException for any persistence error
*/
protected DestinationCache createPersistentCache(JmsDestination dest,
Connection connection)
throws JMSException, PersistenceException {
DestinationCache result;
if (dest instanceof JmsTopic) {
result = new TopicDestinationCache((JmsTopic) dest, connection);
} else {
result = new QueueDestinationCache((JmsQueue) dest, connection);
}
return result;
}
/**
* Create a persistent {@link DestinationCache} for the specified
* destination.
*
* @param dest the destination to create the cache for
* @return a new cache
* @throws JMSException if the cache cannot be created
* @throws PersistenceException for any persistence error
*/
protected DestinationCache createPersistentCache(JmsDestination dest)
throws JMSException, PersistenceException {
DestinationCache result;
Connection connection = null;
try {
connection = DatabaseService.getConnection();
result = createPersistentCache(dest, connection);
connection.commit();
} catch (JMSException exception) {
SQLHelper.rollback(connection);
throw exception;
} catch (SQLException exception) {
SQLHelper.rollback(connection);
throw new PersistenceException(exception);
} finally {
SQLHelper.close(connection);
}
return result;
}
/**
* Create a non-persistent {@link DestinationCache} for the specified
* destination.
*
* @param dest the destination to create the cache for
* @return a new cache
* @throws JMSException if the cache cannot be created
*/
protected DestinationCache createNonPersistentCache(JmsDestination dest)
throws JMSException {
DestinationCache result;
if (dest instanceof JmsTopic) {
result = new TopicDestinationCache((JmsTopic) dest);
} else {
result = new QueueDestinationCache((JmsQueue) dest);
}
// notify the listeners that a new destination has been added
// to the destination manager
notifyDestinationAdded(dest, result);
_caches.put(dest, result);
return result;
}
/**
* Delete the specfied destination.
*
* @param cache the destination to destroy
*/
protected void destroyDestinationCache(DestinationCache cache) {
destroyDestinationCache(cache.getDestination());
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -