[KARAF-4344] Migrating to DS using target feature
Project: http://git-wip-us.apache.org/repos/asf/karaf-decanter/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-decanter/commit/629395e7 Tree: http://git-wip-us.apache.org/repos/asf/karaf-decanter/tree/629395e7 Diff: http://git-wip-us.apache.org/repos/asf/karaf-decanter/diff/629395e7 Branch: refs/heads/DS-target Commit: 629395e787d8807162f9a74c0c560264787d47bd Parents: e5992a3 Author: Christian Schneider <[email protected]> Authored: Thu Feb 18 12:12:19 2016 +0100 Committer: Christian Schneider <[email protected]> Committed: Thu Feb 18 17:06:53 2016 +0100 ---------------------------------------------------------------------- appender/jdbc/pom.xml | 1 - .../karaf/decanter/appender/jdbc/Activator.java | 136 ------------------- .../decanter/appender/jdbc/JdbcAppender.java | 45 ++++-- .../appender/jdbc/TestJdbcAppender.java | 10 +- appender/jms/pom.xml | 1 - .../karaf/decanter/appender/jms/Activator.java | 118 ---------------- .../decanter/appender/jms/JmsAppender.java | 35 ++++- 7 files changed, 74 insertions(+), 272 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/629395e7/appender/jdbc/pom.xml ---------------------------------------------------------------------- diff --git a/appender/jdbc/pom.xml b/appender/jdbc/pom.xml index bdd1e87..86ece70 100644 --- a/appender/jdbc/pom.xml +++ b/appender/jdbc/pom.xml @@ -62,7 +62,6 @@ <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> - <Bundle-Activator>org.apache.karaf.decanter.appender.jdbc.Activator</Bundle-Activator> <Private-Package>org.apache.karaf.decanter.appender.jdbc</Private-Package> </instructions> </configuration> http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/629395e7/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/Activator.java ---------------------------------------------------------------------- diff --git a/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/Activator.java b/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/Activator.java deleted file mode 100644 index 557dca0..0000000 --- a/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/Activator.java +++ /dev/null @@ -1,136 +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.karaf.decanter.appender.jdbc; - -import java.util.Dictionary; -import java.util.Hashtable; - -import javax.sql.DataSource; - -import org.apache.karaf.decanter.api.marshaller.Marshaller; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.cm.ConfigurationException; -import org.osgi.service.cm.ManagedService; -import org.osgi.service.event.EventConstants; -import org.osgi.service.event.EventHandler; -import org.osgi.util.tracker.ServiceTracker; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@SuppressWarnings("rawtypes") -public class Activator implements BundleActivator { - private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); - private static final String CONFIG_PID = "org.apache.karaf.decanter.appender.jdbc"; - private ServiceTracker<Marshaller, ServiceRegistration> tracker; - - @Override - public void start(final BundleContext bundleContext) throws Exception { - LOGGER.debug("Starting Decanter JDBC appender"); - tracker = new ServiceTracker<Marshaller, ServiceRegistration>(bundleContext, Marshaller.class, null) { - @Override - public ServiceRegistration addingService(ServiceReference<Marshaller> reference) { - Dictionary<String, String> properties = new Hashtable<>(); - properties.put(Constants.SERVICE_PID, CONFIG_PID); - Marshaller marshaller = context.getService(reference); - return bundleContext.registerService(ManagedService.class.getName(), - new ConfigUpdater(bundleContext, marshaller), - properties); - } - - @Override - public void removedService(ServiceReference<Marshaller> reference, ServiceRegistration service) { - service.unregister(); - super.removedService(reference, service); - } - }; - tracker.open(); - } - - @Override - public void stop(BundleContext bundleContext) throws Exception { - LOGGER.debug("Stopping Decanter JDBC appender"); - tracker.close(); - } - - private final class ConfigUpdater implements ManagedService { - private BundleContext bundleContext; - private Marshaller marshaller; - private ServiceTracker<DataSource, ServiceRegistration> dsTracker; - - public ConfigUpdater(final BundleContext bundleContext, Marshaller marshaller) { - this.bundleContext = bundleContext; - this.marshaller = marshaller; - } - - @SuppressWarnings("unchecked") - @Override - public void updated(Dictionary config) throws ConfigurationException { - LOGGER.debug("Updating Decanter JDBC managed service"); - if (dsTracker != null) { - dsTracker.close(); - dsTracker = null; - } - if (config == null) { - return; - } - final String dataSourceName = getValue(config, "datasource.name", "jdbc/decanter"); - final String tableName = getValue(config, "table.name", "decanter"); - final String dialect = getValue(config, "dialect", "generic"); - final String filterSt = "(&(" + Constants.OBJECTCLASS + "=" + DataSource.class.getName() + ")" - + "(|(osgi.jndi.service.name=" + dataSourceName + ")(datasource=" + dataSourceName + ")(name=" + dataSourceName + ")(service.id=" + dataSourceName + ")))"; - Filter filter; - try { - filter = bundleContext.createFilter(filterSt); - } catch (InvalidSyntaxException e) { - throw new ConfigurationException("datasource.name", "Unable to create DataSource filter " + filterSt, e); - } - LOGGER.info("Tracking DataSource " + filterSt); - dsTracker = new ServiceTracker<DataSource, ServiceRegistration>(bundleContext, filter, null) { - - @Override - public ServiceRegistration addingService(ServiceReference<DataSource> reference) { - LOGGER.debug("DataSource acquired. Starting JDBC appender ({}/{})", dataSourceName, tableName); - DataSource dataSource = context.getService(reference); - JdbcAppender appender = new JdbcAppender(tableName, dialect, marshaller, dataSource); - Dictionary<String, String> properties = new Hashtable<>(); - properties.put(EventConstants.EVENT_TOPIC, "decanter/collect/*"); - return bundleContext.registerService(EventHandler.class, appender, properties); - } - - @Override - public void removedService(ServiceReference reference, ServiceRegistration serviceReg) { - serviceReg.unregister(); - super.removedService(reference, serviceReg); - } - - }; - dsTracker.open(); - } - - private String getValue(Dictionary<String, Object> config, String key, String defaultValue) { - String value = (String)config.get(key); - return (value != null) ? value : defaultValue; - } - } - -} http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/629395e7/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/JdbcAppender.java ---------------------------------------------------------------------- diff --git a/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/JdbcAppender.java b/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/JdbcAppender.java index 59ee194..7ab4e4f 100644 --- a/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/JdbcAppender.java +++ b/appender/jdbc/src/main/java/org/apache/karaf/decanter/appender/jdbc/JdbcAppender.java @@ -20,16 +20,26 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; +import java.util.Dictionary; import javax.sql.DataSource; import org.apache.karaf.decanter.api.marshaller.Marshaller; +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.Reference; import org.osgi.service.event.Event; import org.osgi.service.event.EventConstants; import org.osgi.service.event.EventHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Component( + name = "org.apache.karaf.decanter.appender.jdbc", + immediate = true, + property = EventConstants.EVENT_TOPIC + "=decanter/collect/*" +) public class JdbcAppender implements EventHandler { private final static Logger LOGGER = LoggerFactory.getLogger(JdbcAppender.class); @@ -43,16 +53,21 @@ public class JdbcAppender implements EventHandler { private final static String insertQueryTemplate = "INSERT INTO TABLENAME(timestamp, content) VALUES(?,?)"; - private String tableName; - private String dialect; private Marshaller marshaller; private DataSource dataSource; - - public JdbcAppender(String tableName, String dialect, Marshaller marshaller, DataSource dataSource) { - this.tableName = tableName; - this.dialect = (dialect == null) ? "generic" : dialect; - this.marshaller = marshaller; - this.dataSource = dataSource; + + String tableName; + String dialect; + + @SuppressWarnings("unchecked") + @Activate + public void activate(ComponentContext context) { + open(context.getProperties()); + } + + public void open(Dictionary<String, Object> config) { + this.tableName = getValue(config, "table.name", "decanter"); + this.dialect = getValue(config, "dialect", "generic"); try (Connection connection = dataSource.getConnection()) { createTable(connection); } catch (Exception e) { @@ -60,7 +75,10 @@ public class JdbcAppender implements EventHandler { } } - + private String getValue(Dictionary<String, Object> config, String key, String defaultValue) { + String value = (String)config.get(key); + return (value != null) ? value : defaultValue; + } @Override public void handleEvent(Event event) { @@ -99,5 +117,14 @@ public class JdbcAppender implements EventHandler { LOGGER.trace("Can't create table {}", e); } } + + @Reference + public void setMarshaller(Marshaller marshaller) { + this.marshaller = marshaller; + } + @Reference(target="(osgi.jndi.service.name=jdbc/decanter)") + public void setDataSource(DataSource dataSource) { + this.dataSource = dataSource; + } } http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/629395e7/appender/jdbc/src/test/java/org/apache/karaf/decanter/appender/jdbc/TestJdbcAppender.java ---------------------------------------------------------------------- diff --git a/appender/jdbc/src/test/java/org/apache/karaf/decanter/appender/jdbc/TestJdbcAppender.java b/appender/jdbc/src/test/java/org/apache/karaf/decanter/appender/jdbc/TestJdbcAppender.java index 7a21323..45433c1 100644 --- a/appender/jdbc/src/test/java/org/apache/karaf/decanter/appender/jdbc/TestJdbcAppender.java +++ b/appender/jdbc/src/test/java/org/apache/karaf/decanter/appender/jdbc/TestJdbcAppender.java @@ -21,7 +21,9 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Dictionary; import java.util.HashMap; +import java.util.Hashtable; import java.util.Map; import javax.json.Json; @@ -51,7 +53,13 @@ public class TestJdbcAppender { deleteTable(dataSource); - JdbcAppender appender = new JdbcAppender(TABLE_NAME, "derby", marshaller, dataSource); + JdbcAppender appender = new JdbcAppender(); + appender.setMarshaller(marshaller); + appender.setDataSource(dataSource); + Dictionary<String, Object> config = new Hashtable<>(); + config.put("dialect", "derby"); + appender.open(config); + Map<String, Object> properties = new HashMap<>(); properties.put(EventConstants.TIMESTAMP, TIMESTAMP); Event event = new Event(TOPIC, properties); http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/629395e7/appender/jms/pom.xml ---------------------------------------------------------------------- diff --git a/appender/jms/pom.xml b/appender/jms/pom.xml index 983c603..8758a1f 100644 --- a/appender/jms/pom.xml +++ b/appender/jms/pom.xml @@ -48,7 +48,6 @@ <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> - <Bundle-Activator>org.apache.karaf.decanter.appender.jms.Activator</Bundle-Activator> <Private-Package>org.apache.karaf.decanter.appender.jms</Private-Package> </instructions> </configuration> http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/629395e7/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/Activator.java ---------------------------------------------------------------------- diff --git a/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/Activator.java b/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/Activator.java deleted file mode 100644 index 6b8bccc..0000000 --- a/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/Activator.java +++ /dev/null @@ -1,118 +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.karaf.decanter.appender.jms; - -import java.util.Dictionary; -import java.util.Hashtable; - -import javax.jms.ConnectionFactory; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.cm.ConfigurationException; -import org.osgi.service.cm.ManagedService; -import org.osgi.service.event.EventConstants; -import org.osgi.service.event.EventHandler; -import org.osgi.util.tracker.ServiceTracker; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@SuppressWarnings("rawtypes") -public class Activator implements BundleActivator { - private final static Logger LOGGER = LoggerFactory.getLogger(Activator.class); - private final static String CONFIG_PID = "org.apache.karaf.decanter.appender.jms"; - - @Override - public void start(BundleContext bundleContext) throws Exception { - LOGGER.debug("Starting Decanter JMS appender"); - ConfigUpdater configUpdater = new ConfigUpdater(bundleContext); - Dictionary<String, String> properties = new Hashtable<>(); - properties.put(Constants.SERVICE_PID, CONFIG_PID); - bundleContext.registerService(ManagedService.class.getName(), configUpdater, properties); - } - - @Override - public void stop(BundleContext bundleContext) throws Exception { - LOGGER.debug("Stopping Decanter JMS appender"); - // Services will be unregistered automatically - } - - private final class ConfigUpdater implements ManagedService { - private BundleContext bundleContext; - private ServiceTracker<ConnectionFactory, ServiceRegistration> dsTracker; - - public ConfigUpdater(BundleContext bundleContext) { - this.bundleContext = bundleContext; - } - - @Override - public void updated(Dictionary config) throws ConfigurationException { - LOGGER.debug("Updating Decanter JMS appender"); - if (dsTracker != null) { - dsTracker.close(); - dsTracker = null; - } - if (config == null) { - return; - } - final String cfName = getProperty(config, "connection.factory.name", "jms/decanter"); - final String username = getProperty(config, "username", null); - final String password = getProperty(config, "password", null); - final String destinationName = getProperty(config, "destination.name", "decanter"); - final String destinationType = getProperty(config, "destination.type", "queue"); - final String filterSt = "(&(" + Constants.OBJECTCLASS + "=" + ConnectionFactory.class.getName() + ")" - + "(|(osgi.jndi.service.name=" + cfName + ")(name=" + cfName + ")(service.id=" + cfName + ")))"; - Filter filter; - try { - filter = bundleContext.createFilter(filterSt); - } catch (InvalidSyntaxException e) { - throw new ConfigurationException("connection.factory.name", "Unable to create filter " + filterSt, e); - } - LOGGER.info("Tracking ConnectionFactory " + filterSt); - dsTracker = new ServiceTracker<ConnectionFactory, ServiceRegistration>(bundleContext, filter, null) { - - @Override - public ServiceRegistration addingService(ServiceReference<ConnectionFactory> reference) { - LOGGER.info("ConnectionFactory acquired. Starting JMS appender ({} , {})", cfName, destinationName); - ConnectionFactory cf = context.getService(reference); - JmsAppender appender = new JmsAppender(cf, username, password, destinationName, destinationType); - Dictionary<String, String> properties = new Hashtable<>(); - properties.put(EventConstants.EVENT_TOPIC, "decanter/collect/*"); - return bundleContext.registerService(EventHandler.class, appender, properties); - } - - @Override - public void removedService(ServiceReference<ConnectionFactory> reference, ServiceRegistration serviceReg) { - serviceReg.unregister(); - super.removedService(reference, serviceReg); - } - - }; - dsTracker.open(); - } - - private String getProperty(Dictionary properties, String key, String defaultValue) { - return (properties.get(key) != null) ? (String) properties.get(key) : defaultValue; - } - } - -} http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/629395e7/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/JmsAppender.java ---------------------------------------------------------------------- diff --git a/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/JmsAppender.java b/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/JmsAppender.java index 8cce524..f72bfab 100644 --- a/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/JmsAppender.java +++ b/appender/jms/src/main/java/org/apache/karaf/decanter/appender/jms/JmsAppender.java @@ -16,6 +16,8 @@ */ package org.apache.karaf.decanter.appender.jms; +import java.util.Dictionary; + import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; @@ -24,11 +26,21 @@ import javax.jms.Message; import javax.jms.MessageProducer; import javax.jms.Session; +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.Reference; import org.osgi.service.event.Event; +import org.osgi.service.event.EventConstants; import org.osgi.service.event.EventHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Component( + name = "org.apache.karaf.decanter.appender.jms", + immediate = true, + property = EventConstants.EVENT_TOPIC + "=decanter/collect/*" +) public class JmsAppender implements EventHandler { private final static Logger LOGGER = LoggerFactory.getLogger(JmsAppender.class); @@ -39,12 +51,18 @@ public class JmsAppender implements EventHandler { private String destinationName; private String destinationType; - public JmsAppender(ConnectionFactory connectionFactory, String username, String password, String destinationName, String destinationType) { - this.connectionFactory = connectionFactory; - this.username = username; - this.password = password; - this.destinationName = destinationName; - this.destinationType = destinationType; + @SuppressWarnings("unchecked") + @Activate + public void activate(ComponentContext context) { + Dictionary<String, Object> config = context.getProperties(); + username = getProperty(config, "username", null); + password = getProperty(config, "password", null); + destinationName = getProperty(config, "destination.name", "decanter"); + destinationType = getProperty(config, "destination.type", "queue"); + } + + private String getProperty(Dictionary<String, Object> properties, String key, String defaultValue) { + return (properties.get(key) != null) ? (String) properties.get(key) : defaultValue; } @Override @@ -126,4 +144,9 @@ public class JmsAppender implements EventHandler { } } } + + @Reference(target="(osgi.jndi.service.name=jms/decanter)") + public void setConnectionFactory(ConnectionFactory connectionFactory) { + this.connectionFactory = connectionFactory; + } }
