karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject git commit: [KARAF-3261] jdbc commands don’t honour service rankings
Date Thu, 30 Oct 2014 09:25:41 GMT
Repository: karaf
Updated Branches:
  refs/heads/karaf-2.x 913c70a20 -> a555f84b1


[KARAF-3261] jdbc commands don’t honour service rankings


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/a555f84b
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/a555f84b
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/a555f84b

Branch: refs/heads/karaf-2.x
Commit: a555f84b10ccecd3a0caa11bbd447ebfd1b8d5d5
Parents: 913c70a
Author: Guillaume Nodet <gnodet@gmail.com>
Authored: Wed Oct 29 17:19:31 2014 +0100
Committer: Guillaume Nodet <gnodet@gmail.com>
Committed: Thu Oct 30 09:08:52 2014 +0100

----------------------------------------------------------------------
 .../karaf/jdbc/command/DataSourcesCommand.java  | 22 ++++++----
 .../java/org/apache/karaf/jdbc/JdbcService.java |  9 ++++
 .../karaf/jdbc/internal/JdbcServiceImpl.java    | 44 ++++++++++++++++----
 3 files changed, 61 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/a555f84b/jdbc/command/src/main/java/org/apache/karaf/jdbc/command/DataSourcesCommand.java
----------------------------------------------------------------------
diff --git a/jdbc/command/src/main/java/org/apache/karaf/jdbc/command/DataSourcesCommand.java
b/jdbc/command/src/main/java/org/apache/karaf/jdbc/command/DataSourcesCommand.java
index 1c85fe9..fae85e5 100644
--- a/jdbc/command/src/main/java/org/apache/karaf/jdbc/command/DataSourcesCommand.java
+++ b/jdbc/command/src/main/java/org/apache/karaf/jdbc/command/DataSourcesCommand.java
@@ -18,25 +18,33 @@ package org.apache.karaf.jdbc.command;
 
 import org.apache.felix.gogo.commands.Command;
 
-import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 @Command(scope = "jdbc", name = "datasources", description = "List the JDBC datasources")
 public class DataSourcesCommand extends JdbcCommandSupport {
 
-    private final static String JDBC_DATASOURCES_STRING_FORMAT = "%10s %15s %10s %45s %5s";
+    private final static String JDBC_DATASOURCES_STRING_FORMAT = "%20s %15s %10s %45s %5s";
 
     public Object doExecute() throws Exception {
 
         System.out.println(String.format(JDBC_DATASOURCES_STRING_FORMAT, "Name", "Product",
"Version", "URL", "Status"));
 
-        List<String> datasources = this.getJdbcService().datasources();
-        for (String datasource : datasources) {
+        Map<String, Set<String>> datasources = this.getJdbcService().aliases();
+        for (Map.Entry<String, Set<String>> entry : datasources.entrySet()) {
+            StringBuilder ids = new StringBuilder();
+            for (String id : entry.getValue()) {
+                if (ids.length() > 0) {
+                    ids.append(", ");
+                }
+                ids.append(id);
+            }
+            String id = ids.toString();
             try {
-                Map<String, String> info = this.getJdbcService().info(datasource);
-                System.out.println(String.format(JDBC_DATASOURCES_STRING_FORMAT, datasource,
info.get("db.product"), info.get("db.version"), info.get("url"), "OK"));
+                Map<String, String> info = this.getJdbcService().info(entry.getKey());
+                System.out.println(String.format(JDBC_DATASOURCES_STRING_FORMAT, id, info.get("db.product"),
info.get("db.version"), info.get("url"), "OK"));
             } catch (Exception e) {
-                System.out.println(String.format(JDBC_DATASOURCES_STRING_FORMAT, datasource,
"", "", "", "Error"));
+                System.out.println(String.format(JDBC_DATASOURCES_STRING_FORMAT, id, "",
"", "", "Error"));
             }
         }
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/a555f84b/jdbc/core/src/main/java/org/apache/karaf/jdbc/JdbcService.java
----------------------------------------------------------------------
diff --git a/jdbc/core/src/main/java/org/apache/karaf/jdbc/JdbcService.java b/jdbc/core/src/main/java/org/apache/karaf/jdbc/JdbcService.java
index d883a07..b99c95e 100644
--- a/jdbc/core/src/main/java/org/apache/karaf/jdbc/JdbcService.java
+++ b/jdbc/core/src/main/java/org/apache/karaf/jdbc/JdbcService.java
@@ -18,6 +18,7 @@ package org.apache.karaf.jdbc;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * JDBC Service.
@@ -53,6 +54,14 @@ public interface JdbcService {
     List<String> datasources() throws Exception;
 
     /**
+     * List the datasources available and
+     * group aliases for the same datasource.
+     *
+     * @return a list of aliases.
+     */
+    Map<String, Set<String>> aliases() throws Exception;
+
+    /**
      * List the JDBC datasources configuration file names present in the deploy folder.
      *
      * @return a list of the JDBC datasources configuration file names.

http://git-wip-us.apache.org/repos/asf/karaf/blob/a555f84b/jdbc/core/src/main/java/org/apache/karaf/jdbc/internal/JdbcServiceImpl.java
----------------------------------------------------------------------
diff --git a/jdbc/core/src/main/java/org/apache/karaf/jdbc/internal/JdbcServiceImpl.java b/jdbc/core/src/main/java/org/apache/karaf/jdbc/internal/JdbcServiceImpl.java
index 404079c..0b884b6 100644
--- a/jdbc/core/src/main/java/org/apache/karaf/jdbc/internal/JdbcServiceImpl.java
+++ b/jdbc/core/src/main/java/org/apache/karaf/jdbc/internal/JdbcServiceImpl.java
@@ -163,19 +163,49 @@ public class JdbcServiceImpl implements JdbcService {
         if (references != null) {
             for (ServiceReference reference : references) {
                 if (reference.getProperty("osgi.jndi.service.name") != null) {
-                    datasources.add((String) reference.getProperty("osgi.jndi.service.name"));
-                } else if (reference.getProperty("datasource") != null) {
-                    datasources.add((String) reference.getProperty("datasource"));
-                } else if (reference.getProperty("name") != null) {
-                    datasources.add((String) reference.getProperty("name"));
-                } else {
-                    datasources.add(reference.getProperty(Constants.SERVICE_ID).toString());
+                    datasources.add(reference.getProperty("osgi.jndi.service.name").toString());
                 }
+                if (reference.getProperty("datasource") != null) {
+                    datasources.add(reference.getProperty("datasource").toString());
+                }
+                if (reference.getProperty("name") != null) {
+                    datasources.add(reference.getProperty("name").toString());
+                }
+                datasources.add(reference.getProperty(Constants.SERVICE_ID).toString());
             }
         }
         return datasources;
     }
 
+    public Map<String, Set<String>> aliases() throws Exception {
+        Map<String, Set<String>> aliases = new LinkedHashMap<String, Set<String>>();
+
+        ServiceReference<?>[] references = bundleContext.getServiceReferences((String)
null,
+                "(|(" + Constants.OBJECTCLASS + "=" + DataSource.class.getName() + ")("
+                        + Constants.OBJECTCLASS + "=" + XADataSource.class.getName() + "))");
+        if (references != null) {
+            List<ServiceReference<?>> refs = Arrays.asList(references);
+            Collections.sort(refs);
+            Collections.reverse(refs);
+            for (ServiceReference<?> reference : refs) {
+                Set<String> names = new LinkedHashSet<String>();
+                if (reference.getProperty("osgi.jndi.service.name") != null) {
+                    names.add(reference.getProperty("osgi.jndi.service.name").toString());
+                }
+                if (reference.getProperty("datasource") != null) {
+                    names.add(reference.getProperty("datasource").toString());
+                }
+                if (reference.getProperty("name") != null) {
+                    names.add(reference.getProperty("name").toString());
+                }
+                String id = reference.getProperty(Constants.SERVICE_ID).toString();
+                names.add(id);
+                aliases.put(id, names);
+            }
+        }
+        return aliases;
+   }
+
     public List<String> datasourceFileNames() throws Exception {
         File karafBase = new File(System.getProperty("karaf.base"));
         File deployFolder = new File(karafBase, "deploy");


Mime
View raw message