changeset 797:d6145521e208

Merge
author Roman Kennke <rkennke@redhat.com>
date Wed, 21 Nov 2012 21:29:49 +0100
parents 9e42f29a200d (current diff) 09c2918d8656 (diff)
children e4010e59a149 1ba58e849ae8
files distribution/config/commands/webservice.properties distribution/pom.xml pom.xml web/server/pom.xml
diffstat 21 files changed, 126 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/DbService.java	Wed Nov 21 21:28:09 2012 +0100
+++ b/common/core/src/main/java/com/redhat/thermostat/common/DbService.java	Wed Nov 21 21:29:49 2012 +0100
@@ -43,15 +43,23 @@
     /**
      * Connects to the given database.
      * 
-     * @throws ConnectionException If DB connection cannot be established.
+     * @throws ConnectionException
+     *             If DB connection cannot be established.
      */
     void connect() throws ConnectionException;
-    
-    
+
     /**
      * Disconnects from the database.
      * 
      * @throws ConnectionException
      */
     void disconnect() throws ConnectionException;
-} 
\ No newline at end of file
+
+    /**
+     * @returns the storage URL which was used for connection.
+     * 
+     * @throws IllegalStateException
+     *             if not connected to storage.
+     */
+    String getConnectionUrl();
+}
\ No newline at end of file
--- a/common/core/src/main/java/com/redhat/thermostat/common/internal/DbServiceImpl.java	Wed Nov 21 21:28:09 2012 +0100
+++ b/common/core/src/main/java/com/redhat/thermostat/common/internal/DbServiceImpl.java	Wed Nov 21 21:29:49 2012 +0100
@@ -45,6 +45,7 @@
 import com.redhat.thermostat.common.dao.DAOFactoryImpl;
 import com.redhat.thermostat.storage.config.StartupConfiguration;
 import com.redhat.thermostat.storage.core.ConnectionException;
+import com.redhat.thermostat.storage.core.StorageException;
 import com.redhat.thermostat.storage.core.StorageProvider;
 import com.redhat.thermostat.storage.core.StorageProviderUtil;
 
@@ -55,43 +56,64 @@
     
     private DAOFactory daoFactory;
     private BundleContext context;
+    private String dbUrl;
     
-    DbServiceImpl(String username, String password, String dbUrl) {
-        this(FrameworkUtil.getBundle(DbService.class).getBundleContext(), getDAOFactory(username, password, dbUrl));
+    DbServiceImpl(String username, String password, String dbUrl) throws StorageException {
+        this(FrameworkUtil.getBundle(DbService.class).getBundleContext(), getDAOFactory(username, password, dbUrl), dbUrl);
     }
 
-    DbServiceImpl(BundleContext context, DAOFactory daoFactory) {
+    // for testing
+    DbServiceImpl(BundleContext context, DAOFactory daoFactory, String dbUrl) {
         this.daoFactory = daoFactory;
         this.context = context;
+        this.dbUrl = dbUrl;
     }
 
     public void connect() throws ConnectionException {
-        daoFactory.getConnection().connect();
-        registration = context.registerService(DbService.class, this, null);
-        daoFactory.registerDAOsAndStorageAsOSGiServices();
+        try {
+            daoFactory.getConnection().connect();
+            registration = context.registerService(DbService.class, this, null);
+            daoFactory.registerDAOsAndStorageAsOSGiServices();
+        } catch (Exception cause) {
+            throw new ConnectionException(cause);
+        }
     }
     
     public void disconnect() throws ConnectionException {
-        daoFactory.unregisterDAOsAndStorageAsOSGiServices();
-        daoFactory.getConnection().disconnect();
-        registration.unregister();
+        try {
+            daoFactory.unregisterDAOsAndStorageAsOSGiServices();
+            daoFactory.getConnection().disconnect();
+            registration.unregister();
+        } catch (Exception cause) {
+            throw new ConnectionException(cause);
+        }
     }
     
+    @Override
+    public String getConnectionUrl() {
+        return dbUrl;
+    }
+
     /**
      * Factory method for creating a DbService instance.
      * 
      * @param username
      * @param password
      * @param dbUrl
-     * @return
+     * @return a DbService instance
+     * @throws StorageException if no storage provider exists for the given {@code dbUrl}.
      */
-    public static DbService create(String username, String password, String dbUrl) {
+    public static DbService create(String username, String password, String dbUrl) throws StorageException {
         return new DbServiceImpl(username, password, dbUrl);
     }
 
-    private static DAOFactory getDAOFactory(String username, String password, String dbUrl) {
+    private static DAOFactory getDAOFactory(String username, String password, String dbUrl) throws StorageException {
         StartupConfiguration config = new ConnectionConfiguration(dbUrl, username, password);
         StorageProvider prov = StorageProviderUtil.getStorageProvider(config);
+        if (prov == null) {
+            // no suitable provider found
+            throw new StorageException("No storage found for URL " + dbUrl);
+        }
         return new DAOFactoryImpl(prov);
     }
     
--- a/common/core/src/test/java/com/redhat/thermostat/common/internal/DbServiceTest.java	Wed Nov 21 21:28:09 2012 +0100
+++ b/common/core/src/test/java/com/redhat/thermostat/common/internal/DbServiceTest.java	Wed Nov 21 21:29:49 2012 +0100
@@ -36,6 +36,7 @@
 
 package com.redhat.thermostat.common.internal;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -68,7 +69,7 @@
         daoFactory = mock(DAOFactory.class);
         when(daoFactory.getConnection()).thenReturn(connection);
 
-        dbService = new DbServiceImpl(context, daoFactory);
+        dbService = new DbServiceImpl(context, daoFactory, "http://someUrl.ignored.com");
     }
     
     @After
@@ -122,4 +123,12 @@
         // disconnect unregisters DbService
         assertNull(context.getServiceReference(DbService.class));
     }
+    
+    @Test
+    public void canGetStorageUrl() {
+        String connectionURL = "http://test.example.com:8082";
+
+        dbService = new DbServiceImpl(context, null, connectionURL);
+        assertEquals(connectionURL, dbService.getConnectionUrl());
+    }
 }
--- a/distribution/config/commands/agent.properties	Wed Nov 21 21:28:09 2012 +0100
+++ b/distribution/config/commands/agent.properties	Wed Nov 21 21:29:49 2012 +0100
@@ -1,21 +1,9 @@
 bundles = thermostat-agent-core-@project.version@.jar, \
           thermostat-web-common-@project.version@.jar, \
           thermostat-web-client-@project.version@.jar, \
-          commons-fileupload-@fileupload.version@.jar, \
-          commons-io-@commons-io.version@.jar, \
-          httpcore-osgi-@httpcomponents.version@.jar, \
-          httpclient-osgi-@httpcomponents.version@.jar, \
+          httpcomponents-core.jar, \
+          httpcomponents-client.jar, \
           gson.jar, \
-          jetty-continuation.jar, \
-          jetty-http.jar, \
-          jetty-io.jar, \
-          jetty-security.jar, \
-          jetty-server.jar, \
-          jetty-servlet.jar, \
-          jetty-util.jar, \
-          jetty-webapp.jar, \
-          jetty-xml.jar, \
-          javax-servlet.jar, \
           thermostat-osgi-process-handler-@project.version@.jar, \
           thermostat-common-core-@project.version@.jar, \
           thermostat-agent-cli-@project.version@.jar, \
--- a/distribution/config/commands/connect.properties	Wed Nov 21 21:28:09 2012 +0100
+++ b/distribution/config/commands/connect.properties	Wed Nov 21 21:29:49 2012 +0100
@@ -1,7 +1,12 @@
-# ConnectCommand is provided by the tools bundle, which is a bootstrap bundle, and requires no other bundles.
-bundles =
+# ConnectCommand is provided by the tools bundle, which is a bootstrap bundle.
+# In order to support web storage connections we add web bundles here
+bundles = thermostat-web-common-0.5.0-SNAPSHOT.jar, \
+          thermostat-web-client-0.5.0-SNAPSHOT.jar, \
+          httpcomponents-core.jar, \
+          httpcomponents-client.jar, \
+          gson.jar
 
-description = persistently connect to a database
+description = persistently connect to storage
 
 usage = connect -d <url> [-u <username>] [-p <password>]
 
--- a/distribution/config/commands/disconnect.properties	Wed Nov 21 21:28:09 2012 +0100
+++ b/distribution/config/commands/disconnect.properties	Wed Nov 21 21:29:49 2012 +0100
@@ -1,9 +1,9 @@
 # DisconnectCommand is provided by the tools bundle, which is a bootstrap bundle, and requires no other bundles.
 bundles =
 
-description = disconnect from the currently used database
+description = disconnect from the currently used storage
 
 usage = thermostat disconnect
 
 # No options necessary for this command
-#options =
\ No newline at end of file
+#options =
--- a/distribution/config/commands/gui.properties	Wed Nov 21 21:28:09 2012 +0100
+++ b/distribution/config/commands/gui.properties	Wed Nov 21 21:29:49 2012 +0100
@@ -1,5 +1,5 @@
 bundles = thermostat-common-core-@project.version@.jar, \
-          gson-2.2.2.jar, \
+          gson.jar, \
           thermostat-web-common-@project.version@.jar, \
           thermostat-web-client-@project.version@.jar, \
           thermostat-common-command-@project.version@.jar, \
@@ -24,8 +24,8 @@
           thermostat-gc-remote-collector-client-common-@project.version@.jar, \
           thermostat-gc-remote-collector-client-swing-@project.version@.jar, \
           thermostat-osgi-process-handler-@project.version@.jar, \
-          httpcore-osgi-@httpcomponents.version@.jar, \
-          httpclient-osgi-@httpcomponents.version@.jar, \
+          httpcomponents-core.jar, \
+          httpcomponents-client.jar, \
           netty.jar
 
 description = launches the GUI client
--- a/distribution/config/commands/webservice.properties	Wed Nov 21 21:28:09 2012 +0100
+++ b/distribution/config/commands/webservice.properties	Wed Nov 21 21:29:49 2012 +0100
@@ -2,8 +2,8 @@
           thermostat-web-server-@project.version@.jar, \
           thermostat-web-common-@project.version@.jar, \
           thermostat-thread-collector-@project.version@.jar, \
-          commons-fileupload-@fileupload.version@.jar, \
-          commons-io-@commons-io.version@.jar, \
+          commons-fileupload.jar, \
+          commons-io.jar, \
           gson.jar, \
           jetty-continuation.jar, \
           jetty-http.jar, \
--- a/distribution/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/distribution/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -223,6 +223,14 @@
                          resource="${project.build.directory}/libs/jetty-xml-8.1.5.v20120716.jar" />
                 <symlink link="${project.build.directory}/libs/javax-servlet.jar"
                          resource="${project.build.directory}/libs/javax.servlet-3.0.0.v201112011016.jar" />
+                <symlink link="${project.build.directory}/libs/commons-io.jar"
+                         resource="${project.build.directory}/libs/commons-io-2.4.jar" />
+                <symlink link="${project.build.directory}/libs/commons-fileupload.jar"
+                         resource="${project.build.directory}/libs/commons-fileupload-1.2.2.jar" />
+                <symlink link="${project.build.directory}/libs/httpcomponents-core.jar"
+                         resource="${project.build.directory}/libs/httpcore-osgi-4.1.2.jar" />
+                <symlink link="${project.build.directory}/libs/httpcomponents-client.jar"
+                         resource="${project.build.directory}/libs/httpclient-osgi-4.1.2.jar" />
               </target>
             </configuration>
             <goals>
--- a/eclipse/com.redhat.thermostat.eclipse.test.ui/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/eclipse/com.redhat.thermostat.eclipse.test.ui/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -30,6 +30,11 @@
       <layout>p2</layout>
       <url>file://${basedir}/../com.redhat.thermostat.eclipse.p2-repo/target/repository/</url>
     </repository>
+    <repository>
+      <id>local_eclipse_test_deps</id>
+      <layout>p2</layout>
+      <url>file://${basedir}/../test-deps-p2-repository/target/repository/</url>
+    </repository>
   </repositories>
 
   <build>
--- a/eclipse/com.redhat.thermostat.eclipse.test/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/eclipse/com.redhat.thermostat.eclipse.test/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -25,6 +25,11 @@
       <layout>p2</layout>
       <url>file://${basedir}/../com.redhat.thermostat.eclipse.p2-repo/target/repository/</url>
     </repository>
+    <repository>
+      <id>local_eclipse_test_deps</id>
+      <layout>p2</layout>
+      <url>file://${basedir}/../test-deps-p2-repository/target/repository/</url>
+    </repository>
   </repositories>
 
   <build>
--- a/eclipse/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/eclipse/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -112,11 +112,6 @@
       <url>${orbit-site}</url>
     </repository>
     <repository>
-      <id>local_eclipse_test_deps</id>
-      <layout>p2</layout>
-      <url>file://${basedir}/../test-deps-p2-repository/target/repository/</url>
-    </repository>
-    <repository>
       <id>local_jfreechart</id>
       <layout>p2</layout>
       <url>file://${basedir}/../jfreechart-p2-repository/target/repository/</url>
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Wed Nov 21 21:28:09 2012 +0100
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Wed Nov 21 21:29:49 2012 +0100
@@ -71,6 +71,7 @@
 import com.redhat.thermostat.launcher.CommonCommandOptions;
 import com.redhat.thermostat.launcher.Launcher;
 import com.redhat.thermostat.storage.core.ConnectionException;
+import com.redhat.thermostat.storage.core.StorageException;
 import com.redhat.thermostat.utils.keyring.Keyring;
 
 public class LauncherImpl implements Launcher {
@@ -279,12 +280,17 @@
                 }
                 String username = ctx.getArguments().getArgument(CommonCommandOptions.USERNAME_ARG);
                 String password = ctx.getArguments().getArgument(CommonCommandOptions.PASSWORD_ARG);
-                DbService service = dbServiceFactory.createDbService(username, password, dbUrl);
                 try {
+                    // this may throw storage exception
+                    DbService service = dbServiceFactory.createDbService(username, password, dbUrl);
                     // This registers the DbService if all goes well
                     service.connect();
+                } catch (StorageException ex) {
+                    throw new CommandException("Unsupported storage URL: " + dbUrl);
                 } catch (ConnectionException ex) {
-                    throw new CommandException("Could not connect to: " + dbUrl, ex);
+                    String error = ex.getMessage();
+                    String message = ( error == null ? "" : " Error: " + error );
+                    throw new CommandException("Could not connect to: " + dbUrl + message, ex);
                 }
             }
         }
--- a/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -88,7 +88,9 @@
     <netty.version>3.2.4.Final</netty.version>
     <httpcomponents.version>4.1.2</httpcomponents.version>
     <fileupload.version>1.2.2</fileupload.version>
+    <gson.version>2.2.2</gson.version>
     <jetty.version>8.1.5.v20120716</jetty.version>
+    <javax.servlet.version>2.5</javax.servlet.version>
   </properties>
 
   <repositories>
--- a/tools/src/main/java/com/redhat/thermostat/tools/LocaleResources.java	Wed Nov 21 21:28:09 2012 +0100
+++ b/tools/src/main/java/com/redhat/thermostat/tools/LocaleResources.java	Wed Nov 21 21:29:49 2012 +0100
@@ -51,6 +51,8 @@
 
     COMMAND_CONNECT_ALREADY_CONNECTED,
     COMMAND_CONNECT_FAILED_TO_CONNECT,
+    COMMAND_CONNECT_INVALID_STORAGE,
+    COMMAND_CONNECT_ERROR,
 
     COMMAND_DISCONNECT_NOT_CONNECTED,
     COMMAND_DISCONNECT_ERROR,
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/ConnectCommand.java	Wed Nov 21 21:28:09 2012 +0100
+++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/ConnectCommand.java	Wed Nov 21 21:29:49 2012 +0100
@@ -46,6 +46,7 @@
 import com.redhat.thermostat.common.utils.OSGIUtils;
 import com.redhat.thermostat.launcher.CommonCommandOptions;
 import com.redhat.thermostat.storage.core.ConnectionException;
+import com.redhat.thermostat.storage.core.StorageException;
 import com.redhat.thermostat.tools.LocaleResources;
 import com.redhat.thermostat.utils.keyring.Keyring;
 
@@ -79,7 +80,7 @@
         DbService service = OSGIUtils.getInstance().getServiceAllowNull(DbService.class);
         if (service != null) {
             // Already connected, bail out
-            throw new CommandException(translator.localize(LocaleResources.COMMAND_CONNECT_ALREADY_CONNECTED));
+            throw new CommandException(translator.localize(LocaleResources.COMMAND_CONNECT_ALREADY_CONNECTED, service.getConnectionUrl()));
         }
         if (prefs == null) {
             prefs = new ClientPreferences(OSGIUtils.getInstance().getService(Keyring.class));
@@ -90,11 +91,16 @@
         }
         String username = ctx.getArguments().getArgument(CommonCommandOptions.USERNAME_ARG);
         String password = ctx.getArguments().getArgument(CommonCommandOptions.PASSWORD_ARG);
-        service = dbServiceFactory.createDbService(username, password, dbUrl);
         try {
+            // may throw StorageException if storage url is not supported
+            service = dbServiceFactory.createDbService(username, password, dbUrl);
             service.connect();
+        } catch (StorageException ex) {
+            throw new CommandException(translator.localize(LocaleResources.COMMAND_CONNECT_INVALID_STORAGE, dbUrl));
         } catch (ConnectionException ex) {
-            throw new CommandException(translator.localize(LocaleResources.COMMAND_CONNECT_FAILED_TO_CONNECT, dbUrl), ex);
+            String error = ex.getMessage();
+            String message = ( error == null ? "" : " " + translator.localize(LocaleResources.COMMAND_CONNECT_ERROR, error) );
+            throw new CommandException(translator.localize(LocaleResources.COMMAND_CONNECT_FAILED_TO_CONNECT, dbUrl + message), ex);
         }
     }
 
--- a/tools/src/main/resources/com/redhat/thermostat/tools/strings.properties	Wed Nov 21 21:28:09 2012 +0100
+++ b/tools/src/main/resources/com/redhat/thermostat/tools/strings.properties	Wed Nov 21 21:29:49 2012 +0100
@@ -7,8 +7,10 @@
 VM_CPU_SERVICE_NOT_AVAILABLE = Unable to access vm cpu information (VmCpuStats not available)
 VM_MEMORY_SERVICE_NOT_AVAILABLE = Unable to access vm memory information (VmCpuStats not available)
 
-COMMAND_CONNECT_ALREADY_CONNECTED = Already connected to storage. Please use disconnect command to disconnect.
+COMMAND_CONNECT_ALREADY_CONNECTED = Already connected to storage: URL = {0}\nPlease use disconnect command to disconnect.
 COMMAND_CONNECT_FAILED_TO_CONNECT = Could not connect to db {0}
+COMMAND_CONNECT_INVALID_STORAGE = Unrecognized storage URL {0}
+COMMAND_CONNECT_ERROR = Error: {0}
 
 COMMAND_DISCONNECT_NOT_CONNECTED = Not connected to storage. You may use the connect command for establishing connections.
 COMMAND_DISCONNECT_ERROR = Failed to disconnect from database.
--- a/tools/src/test/java/com/redhat/thermostat/tools/cli/ConnectCommandTest.java	Wed Nov 21 21:28:09 2012 +0100
+++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/ConnectCommandTest.java	Wed Nov 21 21:29:49 2012 +0100
@@ -106,19 +106,21 @@
     }
 
     @Test
-    public void verifyConnectedThrowsException() {
+    public void verifyConnectedThrowsExceptionWithDiagnosticMessage() {
+        String dbUrl = "fluff";
         DbService dbService = mock(DbService.class);
         OSGIUtils utils = mock(OSGIUtils.class);
         PowerMockito.mockStatic(OSGIUtils.class);
         when(OSGIUtils.getInstance()).thenReturn(utils);
         when(utils.getServiceAllowNull(DbService.class)).thenReturn(dbService);
+        when(dbService.getConnectionUrl()).thenReturn(dbUrl);
 
         SimpleArguments args = new SimpleArguments();
-        args.addArgument("--dbUrl", "fluff");
+        args.addArgument("--dbUrl", dbUrl);
         try {
             cmd.run(cmdCtxFactory.createContext(args));
         } catch (CommandException e) {
-            assertEquals(translator.localize(LocaleResources.COMMAND_CONNECT_ALREADY_CONNECTED), e.getMessage());
+            assertEquals(translator.localize(LocaleResources.COMMAND_CONNECT_ALREADY_CONNECTED, dbUrl), e.getMessage());
         }
     }
     
--- a/web/client/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/web/client/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -77,20 +77,20 @@
     <dependency>
       <groupId>org.eclipse.jetty</groupId>
       <artifactId>jetty-server</artifactId>
-      <version>8.1.5.v20120716</version>
+      <version>${jetty.version}</version>
       <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.eclipse.jetty</groupId>
       <artifactId>jetty-webapp</artifactId>
-      <version>8.1.5.v20120716</version>
+      <version>${jetty.version}</version>
       <scope>test</scope>
     </dependency>
 
     <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
-      <version>2.2.2</version>
+      <version>${gson.version}</version>
     </dependency>
 
     <dependency>
--- a/web/common/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/web/common/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -61,7 +61,7 @@
     <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
-      <version>2.2.2</version>
+      <version>${gson.version}</version>
     </dependency>
 
     <dependency>
--- a/web/server/pom.xml	Wed Nov 21 21:28:09 2012 +0100
+++ b/web/server/pom.xml	Wed Nov 21 21:29:49 2012 +0100
@@ -86,14 +86,13 @@
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
-      <version>2.5</version>
-      <scope>provided</scope>
+      <version>${javax.servlet.version}</version>
     </dependency>
 
     <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
-      <version>2.2.2</version>
+      <version>${gson.version}</version>
     </dependency>
 
     <dependency>