2020import java .net .MalformedURLException ;
2121import java .net .URI ;
2222
23- import org .apache .activemq .broker .BrokerContextAware ;
23+ import java .util .Arrays ;
24+ import java .util .Set ;
25+ import java .util .stream .Collectors ;
2426import org .apache .activemq .broker .BrokerFactoryHandler ;
2527import org .apache .activemq .broker .BrokerService ;
2628import org .apache .activemq .spring .SpringBrokerContext ;
3537import org .springframework .beans .FatalBeanException ;
3638import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
3739import org .springframework .context .ApplicationContext ;
38- import org .springframework .context .ApplicationContextAware ;
3940import org .springframework .core .io .Resource ;
4041
4142/**
4243 *
4344 */
4445public class XBeanBrokerFactory implements BrokerFactoryHandler {
45- private static final transient Logger LOG = LoggerFactory .getLogger (XBeanBrokerFactory .class );
46+ private static final Logger LOG = LoggerFactory .getLogger (XBeanBrokerFactory .class );
47+
48+ public static final String XBEAN_BROKER_FACTORY_PROTOCOLS_PROP =
49+ "org.apache.activemq.xbean.XBEAN_BROKER_FACTORY_PROTOCOLS" ;
50+ public static final String DEFAULT_ALLOWED_PROTOCOLS = Utils .FILE_PROTOCOL + "," + Utils .CLASSPATH_PROTOCOL ;
51+
52+ private final Set <String > allowedProtocols ;
4653
4754 static {
4855 PropertyEditorManager .registerEditor (URI .class , URIEditor .class );
4956 }
5057
58+ public XBeanBrokerFactory () {
59+ final String allowedProtocols = System .getProperty (XBEAN_BROKER_FACTORY_PROTOCOLS_PROP ,
60+ DEFAULT_ALLOWED_PROTOCOLS );
61+
62+ // Asterisk will map to null which will allow all and skip checking
63+ // Empty string will map to an empty set and will deny all
64+ this .allowedProtocols = !allowedProtocols .equals ("*" ) ?
65+ Arrays .stream (allowedProtocols .split ("\\ s*,\\ s*" ))
66+ .filter (s -> !s .isBlank ())
67+ .collect (Collectors .toUnmodifiableSet ()) : null ;
68+ }
69+
5170 private boolean validate = true ;
71+
5272 public boolean isValidate () {
5373 return validate ;
5474 }
@@ -75,12 +95,10 @@ public BrokerService createBroker(URI config) throws Exception {
7595 if (broker == null ) {
7696 // lets try find by type
7797 String [] names = context .getBeanNamesForType (BrokerService .class );
78- for (int i = 0 ; i < names .length ; i ++) {
79- String name = names [i ];
80- broker = (BrokerService )context .getBean (name );
81- if (broker != null ) {
82- break ;
83- }
98+ for (String name : names ) {
99+ // No need to check for null, this will throw an exception if not found
100+ broker = (BrokerService ) context .getBean (name );
101+ break ;
84102 }
85103 }
86104 if (broker == null ) {
@@ -98,8 +116,8 @@ public BrokerService createBroker(URI config) throws Exception {
98116 }
99117
100118 protected ApplicationContext createApplicationContext (String uri ) throws MalformedURLException {
101- Resource resource = Utils .resourceFromString (uri );
102- LOG .debug ("Using " + resource + " from " + uri );
119+ Resource resource = Utils .resourceFromString (uri , allowedProtocols );
120+ LOG .debug ("Using {} from {}" , resource , uri );
103121 try {
104122 return new ResourceXmlApplicationContext (resource ) {
105123 @ Override
@@ -108,9 +126,14 @@ protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
108126 }
109127 };
110128 } catch (FatalBeanException errorToLog ) {
111- LOG .error ("Failed to load: " + resource + ", reason: " + errorToLog .getLocalizedMessage (), errorToLog );
129+ LOG .error ("Failed to load: {}, reason: {}" , resource , errorToLog .getLocalizedMessage (),
130+ errorToLog );
112131 throw errorToLog ;
113132 }
114133 }
115134
135+ // Package scope for testing
136+ Set <String > getAllowedProtocols () {
137+ return allowedProtocols ;
138+ }
116139}
0 commit comments