/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.advisories;
import java.util.Iterator;
import java.util.List;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.message.ActiveMQDestination;
import org.codehaus.activemq.message.ActiveMQTopic;
import org.codehaus.activemq.message.ConnectionInfo;
import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
/**
* A helper class for listening for MessageConnection advisories
*
* * @version $Revision: 1.2 $
*/
public class ConnectionAdvisor implements MessageListener {
private static final Log log = LogFactory.getLog(ConnectionAdvisor.class);
private Connection connection;
private Session session;
private List listeners = new CopyOnWriteArrayList();
private SynchronizedBoolean started = new SynchronizedBoolean(false);
/**
* Construct a ConnectionAdvisor
* @param connection
* @param destination the destination to listen for Connection events
* @throws JMSException
*/
public ConnectionAdvisor(Connection connection) throws JMSException{
this.connection = connection;
}
/**
* start listening for advisories
* @throws JMSException
*
*/
public void start() throws JMSException{
if (started.commit(false,true)){
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
String advisoryName = ActiveMQDestination.CONNECTION_ADVISORY_PREFIX;
Destination advisoryDestination = new ActiveMQTopic(advisoryName);
MessageConsumer consumer = session.createConsumer(advisoryDestination);
consumer.setMessageListener(this);
}
}
/**
* stop listening for advisories
* @throws JMSException
*/
public void stop() throws JMSException{
if (started.commit(true,false)){
if (session != null){
session.close();
}
}
}
/**
* Add a listener
* @param l
*/
public void addListener(ConnectionAdvisoryEventListener l){
listeners.add(l);
}
/**
* Remove a listener
* @param l
*/
public void removeListener(ConnectionAdvisoryEventListener l){
listeners.remove(l);
}
/**
* OnMessage() implementation
* @param msg
*/
public void onMessage(Message msg){
if (msg instanceof ObjectMessage){
try {
ConnectionInfo info = (ConnectionInfo)((ObjectMessage)msg).getObject();
ConnectionAdvisoryEvent event = new ConnectionAdvisoryEvent(info);
fireEvent(event);
}
catch (JMSException e) {
log.error("Failed to process message: " + msg);
}
}
}
private void fireEvent(ConnectionAdvisoryEvent event){
for (Iterator i = listeners.iterator(); i.hasNext(); ){
ConnectionAdvisoryEventListener l = (ConnectionAdvisoryEventListener)i.next();
l.onEvent(event);
}
}
}