changeset 2702:459d991a6ade

Add common-plugin module and SystemID service This patch creates a new common-plugin module, and adds a new SystemID service, usable by any and all plugins that require the SystemID. The systemID is currently implemented as the hex value of the SHA-1 hash of the value of HOSTNAME environment value on POSIX systems, and the COMPUTERNAME variable on Windows Reviewed-by: sgehwolf, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023854.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023788.html
author Simon Tooke <stooke@redhat.com>
date Wed, 21 Jun 2017 14:09:07 -0400
parents 23ccc7c79110
children c107bad9a308
files common/plugin/pom.xml common/plugin/src/main/java/com/redhat/thermostat/common/plugin/SystemID.java common/plugin/src/main/java/com/redhat/thermostat/common/plugin/SystemIDImpl.java common/plugin/src/test/java/com/redhat/thermostat/common/plugin/SystemIDImplTest.java common/pom.xml distribution/assembly/core-assembly-macosx.xml distribution/assembly/core-assembly-windows.xml distribution/assembly/core-assembly.xml distribution/pom.xml
diffstat 9 files changed, 388 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/plugin/pom.xml	Wed Jun 21 14:09:07 2017 -0400
@@ -0,0 +1,145 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Copyright 2012-2017 Red Hat, Inc.
+
+ This file is part of Thermostat.
+
+ Thermostat is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ Thermostat is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Thermostat; see the file COPYING.  If not see
+ <http://www.gnu.org/licenses/>.
+
+ Linking this code with other modules is making a combined work
+ based on this code.  Thus, the terms and conditions of the GNU
+ General Public License cover the whole combination.
+
+ As a special exception, the copyright holders of this code give
+ you permission to link this code with independent modules to
+ produce an executable, regardless of the license terms of these
+ independent modules, and to copy and distribute the resulting
+ executable under terms of your choice, provided that you also
+ meet, for each linked independent module, the terms and conditions
+ of the license of that module.  An independent module is a module
+ which is not derived from or based on this code.  If you modify
+ this code, you may extend this exception to your version of the
+ library, but you are not obligated to do so.  If you do not wish
+ to do so, delete this exception statement from your version.
+
+-->
+<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">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>com.redhat.thermostat</groupId>
+    <artifactId>thermostat-common</artifactId>
+    <version>1.99.12-SNAPSHOT</version>
+  </parent>
+  
+  <artifactId>thermostat-common-plugin</artifactId>
+  <packaging>bundle</packaging>
+
+  <name>Thermostat Common Plugin</name>
+  <url>${project.parent.url}</url>
+
+  <build>
+
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Bundle-SymbolicName>com.redhat.thermostat.common.plugin</Bundle-SymbolicName>
+            <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor>
+            <Export-Package>
+              com.redhat.thermostat.common.plugin
+            </Export-Package>
+            <Private-Package/>
+            <!-- Do not autogenerate uses clauses in Manifests -->
+            <_nouses>true</_nouses>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.redhat.thermostat</groupId>
+      <artifactId>thermostat-common-core</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.redhat.thermostat</groupId>
+      <artifactId>thermostat-common-test</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-api-mockito</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-module-junit4</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.eclipse.jetty</groupId>
+      <artifactId>jetty-client</artifactId>
+      <version>${jetty.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.framework</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.scr.annotations</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    
+    <dependency>
+      <groupId>com.redhat.thermostat</groupId>
+      <artifactId>thermostat-annotations</artifactId>
+      <version>${project.version}</version>
+      <scope>compile</scope>
+    </dependency>
+
+  </dependencies>
+
+</project>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/SystemID.java	Wed Jun 21 14:09:07 2017 -0400
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.plugin;
+
+import com.redhat.thermostat.annotations.Service;
+
+@Service
+public interface SystemID {
+    String getSystemID();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/SystemIDImpl.java	Wed Jun 21 14:09:07 2017 -0400
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.plugin;
+
+import com.redhat.thermostat.shared.config.OS;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+
+import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.logging.Logger;
+
+@Component
+@Service(value = SystemID.class)
+public class SystemIDImpl implements SystemID {
+
+    private Logger logger = Logger.getLogger(SystemIDImpl.class.getName());
+
+    private String cachedSystemID;
+
+    String getHostnameFromEnvironment() {
+        // Don't use PortableHostFactory.getInstance().getHostName() as we want to avoid pulling in the portability library
+        if (OS.IS_WINDOWS) {
+            return System.getenv("COMPUTERNAME");
+        }
+        else {
+            return System.getenv("HOSTNAME");
+        }
+    }
+
+    private String hash(final String str) {
+        try {
+            final MessageDigest digest = MessageDigest.getInstance("SHA-1");
+            digest.update(str.getBytes("utf8"));
+            final byte[] digestBytes = digest.digest();
+            return javax.xml.bind.DatatypeConverter.printHexBinary(digestBytes);
+        } catch (NoSuchAlgorithmException e) {
+            logger.severe("SHA-1 algorithm is not implemented");
+            return str;
+        }
+        catch (UnsupportedEncodingException e) {
+            logger.severe("utf-8 encoding is not implemented");
+            return str;
+        }
+    }
+
+    @Activate
+    public void activate() {
+        cachedSystemID = hash(getHostnameFromEnvironment());
+    }
+
+
+    public String getSystemID() {
+        return cachedSystemID;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/plugin/src/test/java/com/redhat/thermostat/common/plugin/SystemIDImplTest.java	Wed Jun 21 14:09:07 2017 -0400
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.plugin;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class SystemIDImplTest {
+
+    @Test
+    public void testUnactivatedReturnsNull() {
+        SystemID si = new SystemIDImpl();
+        assertNull(si.getSystemID());
+    }
+
+    @Test
+    public void testReturnsSomething() {
+        SystemIDImpl si = new SystemIDImpl();
+        si.activate();
+        final String id = si.getSystemID();
+        assertNotNull(id);
+        assertFalse(id.isEmpty());
+    }
+
+    @Test
+    public void testSameInstanceSameResult() {
+        final SystemIDImpl svcimpl = new SystemIDImpl();
+        svcimpl.activate();
+        assertEquals(svcimpl.getSystemID(), svcimpl.getSystemID());
+    }
+
+    @Test
+    public void testDifferentInstancesSameResult() {
+        SystemIDImpl svc1 = new SystemIDImpl();
+        svc1.activate();
+        SystemIDImpl svc2 = new SystemIDImpl();
+        svc2.activate();
+        assertEquals(svc1.getSystemID(), svc2.getSystemID());
+    }
+
+    static class TesSI extends SystemIDImpl {
+        String hn;
+
+        @Override
+        String getHostnameFromEnvironment() {
+            return hn != null ? hn : super.getHostnameFromEnvironment();
+        }
+    }
+
+    @Test
+    public void testDifferentHostDifferentResult() {
+        TesSI si = new TesSI();
+        si.activate();
+        final String id1 = si.getSystemID();
+        si.hn = "foo1";
+        si.activate();
+        final String id2 = si.getSystemID();
+        assertFalse(id1.equals(id2));
+    }
+}
--- a/common/pom.xml	Fri Jun 23 12:13:02 2017 +0200
+++ b/common/pom.xml	Wed Jun 21 14:09:07 2017 -0400
@@ -66,6 +66,7 @@
 
   <modules>
     <module>core</module>
+    <module>plugin</module>
     <module>portability</module>
     <module>test</module>
   </modules>
--- a/distribution/assembly/core-assembly-macosx.xml	Fri Jun 23 12:13:02 2017 +0200
+++ b/distribution/assembly/core-assembly-macosx.xml	Wed Jun 21 14:09:07 2017 -0400
@@ -61,6 +61,7 @@
         <include>com.redhat.thermostat:thermostat-agent-ipc-unixsocket-server</include>
         <include>com.redhat.thermostat:thermostat-agent-ipc-unixsocket-client</include>
         <include>com.redhat.thermostat:thermostat-common-core</include>
+        <include>com.redhat.thermostat:thermostat-common-plugin</include>
         <include>com.redhat.thermostat:thermostat-common-portability</include>
         <include>com.redhat.thermostat:thermostat-process-handler</include>
         <include>com.redhat.thermostat:thermostat-storage-core</include>
--- a/distribution/assembly/core-assembly-windows.xml	Fri Jun 23 12:13:02 2017 +0200
+++ b/distribution/assembly/core-assembly-windows.xml	Wed Jun 21 14:09:07 2017 -0400
@@ -61,6 +61,7 @@
         <include>com.redhat.thermostat:thermostat-agent-ipc-winpipes-server</include>
         <include>com.redhat.thermostat:thermostat-agent-ipc-winpipes-client</include>
         <include>com.redhat.thermostat:thermostat-common-core</include>
+        <include>com.redhat.thermostat:thermostat-common-plugin</include>
         <include>com.redhat.thermostat:thermostat-common-portability</include>
         <include>com.redhat.thermostat:thermostat-process-handler</include>
         <include>com.redhat.thermostat:thermostat-storage-core</include>
--- a/distribution/assembly/core-assembly.xml	Fri Jun 23 12:13:02 2017 +0200
+++ b/distribution/assembly/core-assembly.xml	Wed Jun 21 14:09:07 2017 -0400
@@ -59,6 +59,7 @@
         <include>com.redhat.thermostat:thermostat-agent-ipc-unixsocket-server</include>
         <include>com.redhat.thermostat:thermostat-agent-ipc-unixsocket-client</include>
         <include>com.redhat.thermostat:thermostat-common-core</include>
+        <include>com.redhat.thermostat:thermostat-common-plugin</include>
         <include>com.redhat.thermostat:thermostat-common-portability</include>
         <include>com.redhat.thermostat:thermostat-process-handler</include>
         <include>com.redhat.thermostat:thermostat-storage-core</include>
--- a/distribution/pom.xml	Fri Jun 23 12:13:02 2017 +0200
+++ b/distribution/pom.xml	Wed Jun 21 14:09:07 2017 -0400
@@ -453,6 +453,11 @@
     </dependency>
     <dependency>
       <groupId>com.redhat.thermostat</groupId>
+      <artifactId>thermostat-common-plugin</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.redhat.thermostat</groupId>
       <artifactId>thermostat-common-portability</artifactId>
       <version>${project.version}</version>
     </dependency>