git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@965820 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2010-07-20 12:07:27 +00:00
parent 5ea053920f
commit 11705e70b4
16 changed files with 150 additions and 104 deletions

View File

@ -0,0 +1,32 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.activemq.broker;
import java.util.Map;
/**
*
* Provide context object for broker classes
*
*/
public interface BrokerContext {
Object getBean(String name);
public Map getBeansOfType(Class type);
}

View File

@ -0,0 +1,27 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.activemq.broker;
/**
*
* Interface to be implemented by any object that wishes to have instance of @see BrokerContext
*
*/
public interface BrokerContextAware {
void setBrokerContext(BrokerContext brokerContext);
}

View File

@ -34,10 +34,12 @@ import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.apache.activemq.ActiveMQConnectionMetaData;
import org.apache.activemq.ConfigurationException;
import org.apache.activemq.Service;
@ -196,6 +198,7 @@ public class BrokerService implements Service {
private ThreadPoolExecutor executor;
private boolean slave = true;
private int schedulePeriodForDestinationPurge=5000;
private BrokerContext brokerContext;
static {
@ -2324,5 +2327,14 @@ public class BrokerService implements Service {
public void setSchedulePeriodForDestinationPurge(int schedulePeriodForDestinationPurge) {
this.schedulePeriodForDestinationPurge = schedulePeriodForDestinationPurge;
}
}
public BrokerContext getBrokerContext() {
return brokerContext;
}
public void setBrokerContext(BrokerContext brokerContext) {
this.brokerContext = brokerContext;
}
}

View File

@ -0,0 +1,30 @@
package org.apache.activemq.spring;
import java.util.Map;
import org.apache.activemq.broker.BrokerContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringBrokerContext implements BrokerContext, ApplicationContextAware {
ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public Object getBean(String name) {
try {
return applicationContext.getBean(name);
} catch (BeansException ex) {
return null;
}
}
public Map getBeansOfType(Class type) {
return applicationContext.getBeansOfType(type);
}
}

View File

@ -26,6 +26,8 @@ import java.util.Map;
import javax.jms.JMSException;
import org.apache.activemq.advisory.AdvisorySupport;
import org.apache.activemq.broker.BrokerContext;
import org.apache.activemq.broker.BrokerContextAware;
import org.apache.activemq.command.ActiveMQMapMessage;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQObjectMessage;
@ -34,7 +36,6 @@ import org.apache.activemq.util.JettisonMappedXmlDriver;
import org.codehaus.jettison.mapped.Configuration;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
@ -49,10 +50,10 @@ import com.thoughtworks.xstream.io.xml.XppReader;
* @author <a href="mailto:dejan@nighttale.net">Dejan Bosanac</a>
*/
public class JmsFrameTranslator extends LegacyFrameTranslator implements
ApplicationContextAware {
BrokerContextAware {
XStream xStream = null;
ApplicationContext applicationContext;
BrokerContext brokerContext;
public ActiveMQMessage convertFrame(ProtocolConverter converter,
StompFrame command) throws JMSException, ProtocolException {
@ -230,15 +231,13 @@ public class JmsFrameTranslator extends LegacyFrameTranslator implements
// -------------------------------------------------------------------------
protected XStream createXStream() {
XStream xstream = null;
if (applicationContext != null) {
String[] names = applicationContext
.getBeanNamesForType(XStream.class);
for (int i = 0; i < names.length; i++) {
String name = names[i];
xstream = (XStream) applicationContext.getBean(name);
if (xstream != null) {
break;
}
if (brokerContext != null) {
Map<String, XStream> beans = brokerContext.getBeansOfType(XStream.class);
for (XStream bean : beans.values()) {
if (bean != null) {
xstream = bean;
break;
}
}
}
@ -249,9 +248,8 @@ public class JmsFrameTranslator extends LegacyFrameTranslator implements
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
public void setBrokerContext(BrokerContext brokerContext) {
this.brokerContext = brokerContext;
}
}

View File

@ -27,6 +27,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
import javax.jms.JMSException;
import org.apache.activemq.broker.BrokerContext;
import org.apache.activemq.broker.BrokerContextAware;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQTempQueue;
@ -58,7 +60,6 @@ import org.apache.activemq.util.IOExceptionSupport;
import org.apache.activemq.util.IdGenerator;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.LongSequenceGenerator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
@ -89,12 +90,12 @@ public class ProtocolConverter {
private final AtomicBoolean connected = new AtomicBoolean(false);
private final FrameTranslator frameTranslator;
private final FactoryFinder FRAME_TRANSLATOR_FINDER = new FactoryFinder("META-INF/services/org/apache/activemq/transport/frametranslator/");
private final ApplicationContext applicationContext;
private final BrokerContext brokerContext;
public ProtocolConverter(StompTransport stompTransport, FrameTranslator translator, ApplicationContext applicationContext) {
public ProtocolConverter(StompTransport stompTransport, FrameTranslator translator, BrokerContext brokerContext) {
this.stompTransport = stompTransport;
this.frameTranslator = translator;
this.applicationContext = applicationContext;
this.brokerContext = brokerContext;
}
protected int generateCommandId() {
@ -145,8 +146,8 @@ public class ProtocolConverter {
if (header != null) {
translator = (FrameTranslator) FRAME_TRANSLATOR_FINDER
.newInstance(header);
if (translator instanceof ApplicationContextAware) {
((ApplicationContextAware)translator).setApplicationContext(applicationContext);
if (translator instanceof BrokerContextAware) {
((BrokerContextAware)translator).setBrokerContext(brokerContext);
}
}
} catch (Exception ignore) {

View File

@ -26,17 +26,16 @@ import java.util.Map;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import org.apache.activemq.broker.BrokerContext;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.BrokerServiceAware;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.nio.NIOTransport;
import org.apache.activemq.transport.nio.NIOTransportFactory;
import org.apache.activemq.transport.tcp.TcpTransport;
import org.apache.activemq.transport.tcp.TcpTransportServer;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.wireformat.WireFormat;
import org.apache.activemq.xbean.XBeanBrokerService;
import org.springframework.context.ApplicationContext;
/**
* A <a href="http://stomp.codehaus.org/">STOMP</a> over NIO transport factory
@ -45,7 +44,7 @@ import org.springframework.context.ApplicationContext;
*/
public class StompNIOTransportFactory extends NIOTransportFactory implements BrokerServiceAware {
private ApplicationContext applicationContext = null;
private BrokerContext brokerContext = null;
protected String getDefaultWireFormatType() {
return "stomp";
@ -64,7 +63,7 @@ public class StompNIOTransportFactory extends NIOTransportFactory implements Bro
}
public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
transport = new StompTransportFilter(transport, new LegacyFrameTranslator(), applicationContext);
transport = new StompTransportFilter(transport, new LegacyFrameTranslator(), brokerContext);
IntrospectionSupport.setProperties(transport, options);
return super.compositeConfigure(transport, format, options);
}
@ -76,9 +75,7 @@ public class StompNIOTransportFactory extends NIOTransportFactory implements Bro
}
public void setBrokerService(BrokerService brokerService) {
if (brokerService instanceof XBeanBrokerService) {
this.applicationContext = ((XBeanBrokerService)brokerService).getApplicationContext();
}
this.brokerContext = brokerService.getBrokerContext();
}
}

View File

@ -18,14 +18,13 @@ package org.apache.activemq.transport.stomp;
import java.util.Map;
import org.apache.activemq.broker.BrokerContext;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.BrokerServiceAware;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.tcp.SslTransportFactory;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.wireformat.WireFormat;
import org.apache.activemq.xbean.XBeanBrokerService;
import org.springframework.context.ApplicationContext;
/**
* A <a href="http://stomp.codehaus.org/">STOMP</a> over SSL transport factory
@ -34,22 +33,20 @@ import org.springframework.context.ApplicationContext;
*/
public class StompSslTransportFactory extends SslTransportFactory implements BrokerServiceAware {
private ApplicationContext applicationContext = null;
private BrokerContext brokerContext = null;
protected String getDefaultWireFormatType() {
return "stomp";
}
public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
transport = new StompTransportFilter(transport, new LegacyFrameTranslator(), applicationContext);
transport = new StompTransportFilter(transport, new LegacyFrameTranslator(), brokerContext);
IntrospectionSupport.setProperties(transport, options);
return super.compositeConfigure(transport, format, options);
}
public void setBrokerService(BrokerService brokerService) {
if (brokerService instanceof XBeanBrokerService) {
this.applicationContext = ((XBeanBrokerService)brokerService).getApplicationContext();
}
this.brokerContext = brokerService.getBrokerContext();
}
}

View File

@ -18,6 +18,7 @@ package org.apache.activemq.transport.stomp;
import java.util.Map;
import org.apache.activemq.broker.BrokerContext;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.BrokerServiceAware;
import org.apache.activemq.transport.Transport;
@ -25,7 +26,6 @@ import org.apache.activemq.transport.tcp.TcpTransportFactory;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.wireformat.WireFormat;
import org.apache.activemq.xbean.XBeanBrokerService;
import org.springframework.context.ApplicationContext;
/**
* A <a href="http://stomp.codehaus.org/">STOMP</a> transport factory
@ -34,14 +34,14 @@ import org.springframework.context.ApplicationContext;
*/
public class StompTransportFactory extends TcpTransportFactory implements BrokerServiceAware {
private ApplicationContext applicationContext = null;
private BrokerContext brokerContext = null;
protected String getDefaultWireFormatType() {
return "stomp";
}
public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
transport = new StompTransportFilter(transport, new LegacyFrameTranslator(), applicationContext);
transport = new StompTransportFilter(transport, new LegacyFrameTranslator(), brokerContext);
IntrospectionSupport.setProperties(transport, options);
return super.compositeConfigure(transport, format, options);
}
@ -53,8 +53,6 @@ public class StompTransportFactory extends TcpTransportFactory implements Broker
}
public void setBrokerService(BrokerService brokerService) {
if (brokerService instanceof XBeanBrokerService) {
this.applicationContext = ((XBeanBrokerService)brokerService).getApplicationContext();
}
this.brokerContext = brokerService.getBrokerContext();
}
}

View File

@ -21,8 +21,8 @@ import java.security.cert.X509Certificate;
import javax.jms.JMSException;
import org.apache.activemq.broker.BrokerContext;
import org.apache.activemq.command.Command;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportFilter;
import org.apache.activemq.transport.TransportListener;
@ -30,7 +30,6 @@ import org.apache.activemq.transport.tcp.SslTransport;
import org.apache.activemq.util.IOExceptionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
/**
* The StompTransportFilter normally sits on top of a TcpTransport that has been
@ -47,10 +46,10 @@ public class StompTransportFilter extends TransportFilter implements StompTransp
private boolean trace;
public StompTransportFilter(Transport next, FrameTranslator translator, ApplicationContext applicationContext) {
public StompTransportFilter(Transport next, FrameTranslator translator, BrokerContext brokerContext) {
super(next);
this.frameTranslator = translator;
this.protocolConverter = new ProtocolConverter(this, translator, applicationContext);
this.protocolConverter = new ProtocolConverter(this, translator, brokerContext);
}
public void oneway(Object o) throws IOException {

View File

@ -25,9 +25,6 @@ import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.usage.SystemUsage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* An ActiveMQ Message Broker. It consists of a number of transport
@ -43,11 +40,10 @@ import org.springframework.context.ApplicationContextAware;
* {code}
* @version $Revision: 1.1 $
*/
public class XBeanBrokerService extends BrokerService implements ApplicationContextAware {
public class XBeanBrokerService extends BrokerService {
private static final transient Log LOG = LogFactory.getLog(XBeanBrokerService.class);
private boolean start = true;
private ApplicationContext applicationContext = null;
public XBeanBrokerService() {
}
@ -112,15 +108,5 @@ public class XBeanBrokerService extends BrokerService implements ApplicationCont
public void setDestroyApplicationContextOnStop(boolean destroy) {
LOG.warn("destroyApplicationContextOnStop parameter is deprecated, please use shutdown hooks instead");
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}

View File

@ -1,39 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.activemq.xbean;
import java.net.URI;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextAwareTest extends MultipleTestsWithEmbeddedBrokerTest {
protected BrokerService createBroker() throws Exception {
return BrokerFactory.createBroker(new URI("xbean:org/apache/activemq/xbean/activemq.xml"));
}
public void testContextAware() {
assertTrue(broker instanceof XBeanBrokerService);
assertTrue(broker instanceof ApplicationContextAware);
ApplicationContext context = ((XBeanBrokerService)broker).getApplicationContext();
assertTrue(context.containsBean("org.apache.activemq.xbean.XBeanBrokerService"));
}
}

View File

@ -30,8 +30,10 @@
<bean class="org.apache.activemq.util.XStreamFactoryBean" name="xstream">
<property name="annotatedClass"><value>org.apache.activemq.transport.stomp.SamplePojo</value></property>
</bean>
<bean id="springContext" class="org.apache.activemq.spring.SpringBrokerContext" />
<broker useJmx="true" persistent="false" xmlns="http://activemq.apache.org/schema/core" populateJMSXUserID="true">
<broker useJmx="true" persistent="false" xmlns="http://activemq.apache.org/schema/core" populateJMSXUserID="true" brokerContext="#springContext">
<plugins>
<simpleAuthenticationPlugin>

View File

@ -31,7 +31,9 @@
<property name="annotatedClass"><value>org.apache.activemq.transport.stomp.SamplePojo</value></property>
</bean>
<broker start="false" useJmx="true" persistent="false" xmlns="http://activemq.apache.org/schema/core" populateJMSXUserID="true">
<bean id="springContext" class="org.apache.activemq.spring.SpringBrokerContext" />
<broker start="false" useJmx="true" persistent="false" xmlns="http://activemq.apache.org/schema/core" populateJMSXUserID="true" brokerContext="#springContext">
<plugins>
<simpleAuthenticationPlugin>

View File

@ -29,8 +29,10 @@
<property name="annotatedClass"><value>org.apache.activemq.transport.stomp.SamplePojo</value></property>
</bean>
<bean id="springContext" class="org.apache.activemq.spring.SpringBrokerContext" />
<!-- lets create an embedded ActiveMQ Broker -->
<amq:broker useJmx="true" persistent="false" start="false">
<amq:broker useJmx="true" persistent="false" start="false" brokerContext="#springContext">
<amq:plugins>
<amq:jaasCertificateAuthenticationPlugin configuration="cert-login"/>

View File

@ -31,7 +31,9 @@
<property name="annotatedClass"><value>org.apache.activemq.transport.stomp.SamplePojo</value></property>
</bean>
<broker useJmx="true" persistent="false" xmlns="http://activemq.apache.org/schema/core" populateJMSXUserID="true">
<bean id="springContext" class="org.apache.activemq.spring.SpringBrokerContext" />
<broker useJmx="true" persistent="false" xmlns="http://activemq.apache.org/schema/core" populateJMSXUserID="true" brokerContext="#springContext">
<plugins>
<simpleAuthenticationPlugin>