Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,3 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.osgi.cf;

import java.util.Dictionary;
import java.util.Hashtable;

import jakarta.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;

@Component //
( //
name = "org.apache.activemq", //
immediate = true, //
configurationPolicy = ConfigurationPolicy.REQUIRE //
)
public class ConnectionFactoryProvider {

private static final String OSGI_JNDI_SERVICE_NAME = "osgi.jndi.service.name";
private ServiceRegistration<ConnectionFactory> reg;

@Activate
public void create(ComponentContext compContext) {
BundleContext context = compContext.getBundleContext();
Dictionary<String, Object> config = compContext.getProperties();
String brokerURL = getString(config, "url", "tcp://localhost:61616");
String jndiName = getString(config, OSGI_JNDI_SERVICE_NAME, "jms/local");
String userName = getString(config, "userName", null);
String password = getString(config, "password", null);
long expiryTimeout = Long.valueOf(getString(config, "expiryTimeout", "0"));
int idleTimeout = Integer.valueOf(getString(config, "idleTimeout", "30000"));
int maxConnections = Integer.valueOf(getString(config, "maxConnections", "8"));
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerURL);
if (userName != null) {
cf.setUserName(userName);
cf.setPassword(password);
}
PooledConnectionFactory pcf = new PooledConnectionFactory();
pcf.setConnectionFactory(cf);
pcf.setExpiryTimeout(expiryTimeout);
pcf.setIdleTimeout(idleTimeout);
pcf.setMaxConnections(maxConnections);
Dictionary<String, String> props = new Hashtable<String, String>();
props.put(OSGI_JNDI_SERVICE_NAME, jndiName);
reg = context.registerService(ConnectionFactory.class, pcf, props);
}

@Deactivate
public void deactivate() {
reg.unregister();
}

private String getString(Dictionary<String, Object> config, String key, String defaultValue) {
Object value = config.get(key);
return value != null ? value.toString() : defaultValue;
}
}