Repository: karaf-decanter Updated Branches: refs/heads/master c41a748d5 -> cc2d1a140
[KARAF-4344] Migrate REST collector to DS Project: http://git-wip-us.apache.org/repos/asf/karaf-decanter/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-decanter/commit/cc2d1a14 Tree: http://git-wip-us.apache.org/repos/asf/karaf-decanter/tree/cc2d1a14 Diff: http://git-wip-us.apache.org/repos/asf/karaf-decanter/diff/cc2d1a14 Branch: refs/heads/master Commit: cc2d1a140519cff8362ca8d42e1c8ad0d8f5661b Parents: c41a748 Author: Christian Schneider <[email protected]> Authored: Wed Feb 17 14:53:20 2016 +0100 Committer: Christian Schneider <[email protected]> Committed: Wed Feb 17 14:53:20 2016 +0100 ---------------------------------------------------------------------- collector/rest/pom.xml | 1 - .../decanter/collector/rest/Activator.java | 160 ------------------- .../decanter/collector/rest/BeanHarvester.java | 136 ---------------- .../decanter/collector/rest/RestCollector.java | 51 ++++-- pom.xml | 56 ++++--- 5 files changed, 67 insertions(+), 337 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/cc2d1a14/collector/rest/pom.xml ---------------------------------------------------------------------- diff --git a/collector/rest/pom.xml b/collector/rest/pom.xml index c532f21..ddca8aa 100644 --- a/collector/rest/pom.xml +++ b/collector/rest/pom.xml @@ -25,7 +25,6 @@ <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> - <Bundle-Activator>org.apache.karaf.decanter.collector.rest.Activator</Bundle-Activator> </instructions> </configuration> </plugin> http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/cc2d1a14/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/Activator.java ---------------------------------------------------------------------- diff --git a/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/Activator.java b/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/Activator.java deleted file mode 100644 index 98166c1..0000000 --- a/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/Activator.java +++ /dev/null @@ -1,160 +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.collector.rest; - -import java.net.MalformedURLException; -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.karaf.decanter.api.marshaller.Unmarshaller; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.cm.ConfigurationException; -import org.osgi.service.cm.ManagedServiceFactory; -import org.osgi.service.event.EventAdmin; -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 ServiceTracker<EventAdmin, MarshallerTracker> tracker; - - public void start(final BundleContext bundleContext) throws Exception { - tracker = new ServiceTracker<EventAdmin, MarshallerTracker>(bundleContext, EventAdmin.class, null) { - - @Override - public MarshallerTracker addingService(ServiceReference<EventAdmin> reference) { - EventAdmin eventAdmin = bundleContext.getService(reference); - MarshallerTracker uTracker = new MarshallerTracker(bundleContext, eventAdmin); - uTracker.open(); - return uTracker; - } - - @Override - public void removedService(ServiceReference<EventAdmin> reference, MarshallerTracker uTracker) { - uTracker.close(); - super.removedService(reference, uTracker); - } - - }; - tracker.open(); - } - - public void stop(BundleContext bundleContext) throws Exception { - tracker.close(); - } - - class MarshallerTracker extends ServiceTracker<Unmarshaller, ServiceRegistration> { - - private EventAdmin eventAdmin; - - public MarshallerTracker(BundleContext context, EventAdmin eventAdmin) { - super(context, Unmarshaller.class, null); - this.eventAdmin = eventAdmin; - } - - @Override - public ServiceRegistration addingService(ServiceReference<Unmarshaller> reference) { - Unmarshaller unmarshaller = context.getService(reference); - ConfigManager configManager = new ConfigManager(eventAdmin, unmarshaller, context); - Hashtable<String, Object> properties = new Hashtable<String, Object>(); - properties.put(Constants.SERVICE_PID, "org.apache.karaf.decanter.collector.rest"); - return context.registerService(ManagedServiceFactory.class, configManager, properties); - } - - @Override - public void removedService(ServiceReference<Unmarshaller> reference, ServiceRegistration reg) { - reg.unregister(); - super.removedService(reference, reg); - } - - - } - - private final class ConfigManager implements ManagedServiceFactory { - - private final Map<String, ServiceRegistration> registrations = new ConcurrentHashMap<String, ServiceRegistration>(); - - private EventAdmin eventAdmin; - private BundleContext bundleContext; - private Unmarshaller unmarshaller; - - public ConfigManager(EventAdmin eventAdmin, Unmarshaller unmarshaller, BundleContext bundleContext) { - this.eventAdmin = eventAdmin; - this.unmarshaller = unmarshaller; - this.bundleContext = bundleContext; - } - - @Override - public String getName() { - return "Karaf Decanter JMX collector service factory"; - } - - @SuppressWarnings("unchecked") - @Override - public void updated(String pid, Dictionary properties) throws ConfigurationException { - LOGGER.debug("Updating JMX collector {}", pid); - ServiceRegistration registration = null; - try { - if (properties != null) { - String baseUrl = getProperty(properties, "url", null); - String username = getProperty(properties, "username", null); - String password = getProperty(properties, "password", null); - String[] paths = getProperty(properties, "paths", "").split(","); - properties.remove("password"); - properties.remove("username"); - RestCollector collector = new RestCollector(baseUrl, username, password, paths, eventAdmin, unmarshaller, properties); - Dictionary<String, String> serviceProperties = new Hashtable<String, String>(); - serviceProperties.put("decanter.collector.name", "rest"); - registration = bundleContext.registerService(Runnable.class, collector, serviceProperties); - } - } catch (MalformedURLException e) { - LOGGER.warn("Invalid url to fetch from", e); - } finally { - ServiceRegistration oldRegistration = (registration == null) ? registrations.remove(pid) : registrations.put(pid, registration); - if (oldRegistration != null) { - LOGGER.debug("Unregistering Decanter JMX collector {}", pid); - oldRegistration.unregister(); - } - } - } - - private String getProperty(Dictionary properties, String key, String defaultValue) { - return (properties.get(key) != null) ? (String) properties.get(key) : defaultValue; - } - - @Override - public void deleted(String pid) { - LOGGER.debug("Deleting JMX collector {}", pid); - ServiceRegistration registration = registrations.remove(pid); - if (registration != null) { - registration.unregister(); - } - } - - } - -} http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/cc2d1a14/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/BeanHarvester.java ---------------------------------------------------------------------- diff --git a/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/BeanHarvester.java b/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/BeanHarvester.java deleted file mode 100644 index 26577d4..0000000 --- a/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/BeanHarvester.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.collector.rest; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import javax.management.MBeanAttributeInfo; -import javax.management.MBeanServerConnection; -import javax.management.ObjectName; -import javax.management.openmbean.CompositeData; -import javax.management.openmbean.CompositeDataSupport; -import javax.management.openmbean.CompositeType; -import javax.management.openmbean.TabularData; -import javax.management.openmbean.TabularDataSupport; -import javax.management.openmbean.TabularType; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -class BeanHarvester { - private final static Logger LOGGER = LoggerFactory.getLogger(BeanHarvester.class); - private MBeanServerConnection connection; - private String type; - private String hostName; - private String hostAddress; - private String karafName; - - BeanHarvester(MBeanServerConnection connection, String type, String hostName, String karafName) throws UnknownHostException { - this.connection = connection; - this.type = type; - this.hostName = hostName; - if (hostName == null || hostName.isEmpty()) { - this.karafName = karafName; - this.hostAddress = InetAddress.getLocalHost().getHostAddress(); - this.hostName =InetAddress.getLocalHost().getHostName(); - } else { - this.karafName = null; - this.hostAddress = null; - this.hostName = hostName; - } - } - - Map<String, Object> harvestBean(ObjectName name) throws Exception { - MBeanAttributeInfo[] attributes = connection.getMBeanInfo(name).getAttributes(); - Map<String, Object> data = new HashMap<>(); - data.put("type", type); - data.put("ObjectName", name.toString()); - if (this.karafName != null) { - data.put("karafName", this.karafName); - } - if (this.hostAddress != null) { - data.put("hostAddress", this.hostAddress); - } - data.put("hostName", hostName); - - for (MBeanAttributeInfo attribute : attributes) { - try { - Object attributeObject = connection.getAttribute(name, attribute.getName()); - if (attributeObject instanceof String) { - data.put(attribute.getName(), (String) attributeObject); - } else if (attributeObject instanceof ObjectName) { - data.put(attribute.getName(), ((ObjectName) attributeObject).toString()); - } else if (attributeObject instanceof CompositeDataSupport || attributeObject instanceof CompositeData) { - CompositeData cds = (CompositeData) attributeObject; - CompositeType compositeType = cds.getCompositeType(); - Set<String> keySet = compositeType.keySet(); - Map<String, Object> composite = new HashMap<String, Object>(); - for (String key : keySet) { - Object cdsObject = cds.get(key); - composite.put(key, cdsObject); - } - data.put(attribute.getName(), composite); - } else if (attributeObject instanceof Long - || attributeObject instanceof Integer - || attributeObject instanceof Boolean - || attributeObject instanceof Float - || attributeObject instanceof Double) { - data.put(attribute.getName(), attributeObject); - } else if (attributeObject instanceof TabularDataSupport || attributeObject instanceof TabularData) { - TabularData tds = (TabularData) attributeObject; - TabularType tabularType = tds.getTabularType(); - CompositeType compositeType = tabularType.getRowType(); - Collection<?> values = tds.values(); - ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); - for (Object value : values) { - CompositeDataSupport cds = (CompositeDataSupport) value; - Set<String> keySet = compositeType.keySet(); - Map<String, Object> composite = new HashMap<String, Object>(); - list.add(composite); - for (String key : keySet) { - Object cdsObject = cds.get(key); - composite.put(key, cdsObject); - } - } - data.put(attribute.getName(), list); - } else if (attributeObject instanceof Object[]) { - data.put(attribute.getName(), (Object[]) attributeObject); - } else if (attributeObject instanceof long[]) { - data.put(attribute.getName(), (long[]) attributeObject); - } else if (attributeObject instanceof String[]) { - data.put(attribute.getName(), (String[]) attributeObject); - } else if (attributeObject instanceof int[]) { - data.put(attribute.getName(), (int[]) attributeObject); - } else { - data.put(attribute.getName(), attributeObject.toString()); - } - } catch (SecurityException se) { - LOGGER.error("SecurityException: ", se); - } catch (Exception e) { - LOGGER.debug("Could not read attribute " + name.toString() + " " + attribute.getName()); - } - - } - return data; - } -} http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/cc2d1a14/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/RestCollector.java ---------------------------------------------------------------------- diff --git a/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/RestCollector.java b/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/RestCollector.java index 9b45c75..064feb6 100644 --- a/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/RestCollector.java +++ b/collector/rest/src/main/java/org/apache/karaf/decanter/collector/rest/RestCollector.java @@ -24,6 +24,10 @@ import java.util.Enumeration; import java.util.Map; import org.apache.karaf.decanter.api.marshaller.Unmarshaller; +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.EventAdmin; import org.slf4j.Logger; @@ -32,29 +36,35 @@ import org.slf4j.LoggerFactory; /** * Decanter REST Collector */ +@Component( + configurationPid = "org.apache.karaf.decanter.collector.rest", + property = {"decanter.collector.name=rest"} +) public class RestCollector implements Runnable { private final static Logger LOGGER = LoggerFactory.getLogger(RestCollector.class); private String type; private URL url; private String[] paths; + private Dictionary<String, Object> properties; + private EventAdmin eventAdmin; - private Dictionary<String, String> properties; - private Unmarshaller marshaller; + private Unmarshaller unmarshaller; - public RestCollector(String url, - String username, - String password, - String[] paths, - EventAdmin eventAdmin, - Unmarshaller marshaller, - Dictionary<String, String> properties) - throws MalformedURLException { - this.marshaller = marshaller; - this.url = new URL(url); - this.eventAdmin = eventAdmin; - this.paths = paths; - this.properties = properties; + @Activate + public void activate(ComponentContext context) throws MalformedURLException { + Dictionary<String, Object> props = context.getProperties(); + this.url = new URL(getProperty(props, "url", null)); + getProperty(props, "username", null); + getProperty(props, "password", null); + this.paths = getProperty(props, "paths", "").split(","); + //props.remove("password"); + //props.remove("username"); + this.properties = props; + } + + private String getProperty(Dictionary<String, Object> properties, String key, String defaultValue) { + return (properties.get(key) != null) ? (String) properties.get(key) : defaultValue; } @Override @@ -64,7 +74,7 @@ public class RestCollector implements Runnable { try { URL complete = new URL(url, path); URLConnection connection = complete.openConnection(); - Map<String, Object> data = marshaller.unmarshal(connection.getInputStream()); + Map<String, Object> data = unmarshaller.unmarshal(connection.getInputStream()); data.put("type", "rest"); data.put("hostName", url.getHost()); data.put("remote.url", complete); @@ -90,5 +100,14 @@ public class RestCollector implements Runnable { } } } + + @Reference + public void setEventAdmin(EventAdmin eventAdmin) { + this.eventAdmin = eventAdmin; + } + @Reference + public void setUnmarshaller(Unmarshaller unmarshaller) { + this.unmarshaller = unmarshaller; + } } http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/cc2d1a14/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 6443edb..4fdd6bc 100644 --- a/pom.xml +++ b/pom.xml @@ -1,23 +1,17 @@ <?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <!-- - - 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. - --> + <!-- 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. --> <modelVersion>4.0.0</modelVersion> @@ -74,8 +68,8 @@ <connection>scm:git:https://git-wip-us.apache.org/repos/asf/karaf-decanter.git</connection> <developerConnection>scm:git:https://git-wip-us.apache.org/repos/asf/karaf-decanter.git</developerConnection> <url>https://git-wip-us.apache.org/repos/asf?p=karaf-decanter.git;a=summary</url> - <tag>HEAD</tag> - </scm> + <tag>HEAD</tag> + </scm> <issueManagement> <system>jira</system> @@ -178,7 +172,9 @@ </goals> <configuration> <target> - <copy file="NOTICE" todir="target/maven-shared-archive-resources/META-INF" failonerror="false" overwrite="true" /> + <copy file="NOTICE" + todir="target/maven-shared-archive-resources/META-INF" + failonerror="false" overwrite="true" /> </target> </configuration> </execution> @@ -216,6 +212,7 @@ <configuration> <instructions> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> + <_dsannotations>*</_dsannotations> </instructions> </configuration> <executions> @@ -262,7 +259,13 @@ <version>4.3.1</version> <scope>provided</scope> </dependency> - + + <dependency> + <groupId>biz.aQute.bnd</groupId> + <artifactId>biz.aQute.bnd.annotation</artifactId> + <version>3.1.0</version> + </dependency> + <!-- ActiveMQ --> <dependency> <groupId>org.apache.activemq</groupId> @@ -279,7 +282,7 @@ <artifactId>activemq-kahadb-store</artifactId> <version>${activemq.version}</version> </dependency> - + <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> @@ -321,7 +324,7 @@ </dependency> </dependencies> </dependencyManagement> - + <dependencies> <dependency> <groupId>org.osgi</groupId> @@ -334,6 +337,11 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>biz.aQute.bnd</groupId> + <artifactId>biz.aQute.bnd.annotation</artifactId> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <scope>provided</scope>
