1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.core.setup;
16
17 import org.figure8.join.util.LogUtil;
18
19 import org.apache.commons.logging.Log;
20
21 import java.util.Properties;
22 import java.util.Enumeration;
23 /**
24 * Helper object for configuring ActiveMQ and its ConnectionFactory
25 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
26 * @version $Revision: 1.2 $
27 */
28 public class ActiveMQConfigurator{
29
30
31
32 /** Get a commons logger. */
33 protected static Log log = LogUtil.getLog(ActiveMQConfigurator.class);
34
35 /** Prefix of application properties related to ActiveMQ. */
36 protected final static String AMQ_PROPERTIES_PREFIX = "activemq.";
37 /** Name of ActiveMQ property telling J2EE compliance */
38 public final static String AMQ_J2EE_COMPLIANCE = AMQ_PROPERTIES_PREFIX + "J2EEcompliant";
39 /** Name of ActiveMQ property telling if it uses an embedded broker */
40 public final static String AMQ_EMBEDDED_BROKER = AMQ_PROPERTIES_PREFIX + "useEmbeddedBroker";
41 /** Name of ActiveMQ property telling the broker URL to use */
42 public final static String AMQ_BROKER_URL = AMQ_PROPERTIES_PREFIX + "brokerURL";
43 /** Name of ActiveMQ property telling the broker xml configuration path */
44 public final static String AMQ_BROKER_CONFIG = AMQ_PROPERTIES_PREFIX + "brokerXmlConfig";
45 /** Name of application property telling if ActiveMQ has been setup */
46 public final static String AMQ_SETUP_PROPERTY = AMQ_PROPERTIES_PREFIX + "setup";
47
48 /** Path to the default broker configuration file */
49 public final static String AMQ_BROKER_CONFIG_PATH = "activemq.xml";
50
51
52
53
54 /** Inner flag for knowing when to cache properties */
55 private boolean dirty = true;
56 /** Cached application properties for ActiveMQ */
57 private Properties cachedProperties = null;
58 /** Application configuration wrapper. */
59 private ApplicationConfig appConfig = null;
60
61
62
63
64 /** Creates a new instance of ActiveMQConfigurator */
65 public ActiveMQConfigurator(){
66 }
67
68
69
70
71 /** @param config <code>ApplicationConfig</code> instance */
72 public void setApplicationConfig(ApplicationConfig config){
73 this.appConfig = config;
74 }
75 /** @return The ApplicationConfig instance used */
76 public ApplicationConfig getApplicationConfig(){
77 return appConfig;
78 }
79
80 /**
81 * Get a specified ActiveMQ property using its key
82 * @param key The key of the property to retrieve
83 * @return The property value or null if no property with key
84 */
85 public String getActiveMQProperty(String key){
86 log.debug("Getting ActiveMQ property '" + key + "'");
87 return getActiveMQProperties().getProperty(key);
88 }
89
90 /**
91 * Extract ActiveMQ related properties from ApplicationConfig.
92 * @return A set of ActiveMQ properties already present into ApplicationConfig
93 */
94 public Properties getActiveMQProperties(){
95 log.debug("Extracting ActiveMQ properties from application config...");
96 if (dirty){
97 cachedProperties = new Properties();
98
99 Enumeration keys = appConfig.getProperties().keys();
100 while (keys != null && keys.hasMoreElements()){
101 String key = (String)keys.nextElement();
102 if (key.startsWith(AMQ_PROPERTIES_PREFIX)){
103 String rawKey = key.substring(key.lastIndexOf(".") + 1);
104 cachedProperties.setProperty(rawKey, appConfig.getProperty(key));
105 }
106 }
107 }
108 log.debug("Found " + cachedProperties.size() + " ActiveMQ related properties.");
109
110 return cachedProperties;
111 }
112
113 /**
114 * Configure the ActiveMQ connection factory properties for given broker.
115 * @param brokerUrl The Url of broker to configure connection factory for
116 * @throws BootstrapException if application conifguration cannot be updated
117 */
118 public synchronized void configureMessaging(String brokerUrl) throws BootstrapException{
119
120 Properties properties = new Properties();
121 properties.setProperty(AMQ_J2EE_COMPLIANCE, "false");
122 properties.setProperty(AMQ_EMBEDDED_BROKER, "false");
123
124
125 properties.setProperty(AMQ_BROKER_URL, brokerUrl);
126 properties.setProperty(AMQ_BROKER_CONFIG, AMQ_BROKER_CONFIG_PATH);
127 log.debug("Messaging broker is remote. Broker URL is : " + brokerUrl);
128
129
130 configureActiveMQ(properties);
131 }
132
133
134
135
136 /**
137 * Do the real job by filling application with ActiveMQ properties.
138 * @param properties ActiveMQ specific properties (starting with "activemq.")
139 * @throws BootstrapException if configuration backup fails
140 */
141 protected void configureActiveMQ(Properties properties) throws BootstrapException{
142 if (log.isDebugEnabled())
143 log.debug("Configuring ActiveMQ & Application with properties: " + properties);
144
145
146 Enumeration keys = properties.keys();
147 while (keys != null && keys.hasMoreElements()){
148 String key = (String)keys.nextElement();
149 appConfig.setProperty(key, properties.getProperty(key));
150 dirty = true;
151 }
152
153 try{
154
155 log.debug("Now saving configuration before launching connection factory...");
156 appConfig.save();
157
158 appConfig.setProperty(AMQ_SETUP_PROPERTY, "true");
159 }
160 catch (Exception e){
161 log.error("Execution of configuration backup & connection factory update fails...");
162 log.error("Here's the error message: " + e.getMessage());
163 throw new BootstrapException("Configuration of ActiveMQ fails !", e);
164 }
165 }
166 }