You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2014/01/29 16:08:49 UTC

git commit: KARAF-2741 Share TemplateUtils between jms and jdbc

Updated Branches:
  refs/heads/master eab9c5f9f -> 200946379


KARAF-2741 Share TemplateUtils between jms and jdbc


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

Branch: refs/heads/master
Commit: 200946379b9c0573a62bcf63658872a002da6c17
Parents: eab9c5f
Author: Christian Schneider <ch...@die-schneider.net>
Authored: Wed Jan 29 16:08:41 2014 +0100
Committer: Christian Schneider <ch...@die-schneider.net>
Committed: Wed Jan 29 16:08:41 2014 +0100

----------------------------------------------------------------------
 jdbc/core/pom.xml                               |  7 +-
 .../karaf/jdbc/internal/JdbcServiceImpl.java    | 88 +++++---------------
 jms/core/pom.xml                                |  7 +-
 .../karaf/jms/internal/JmsServiceImpl.java      |  7 +-
 .../apache/karaf/jms/internal/TemplateUtil.java | 87 -------------------
 .../org/apache/karaf/util/TemplateUtils.java    | 83 ++++++++++++++++++
 6 files changed, 124 insertions(+), 155 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/20094637/jdbc/core/pom.xml
----------------------------------------------------------------------
diff --git a/jdbc/core/pom.xml b/jdbc/core/pom.xml
index f1f562d..3587f8a 100644
--- a/jdbc/core/pom.xml
+++ b/jdbc/core/pom.xml
@@ -43,6 +43,10 @@
             <artifactId>org.osgi.core</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf</groupId>
+            <artifactId>org.apache.karaf.util</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
@@ -71,7 +75,8 @@
                             org.apache.karaf.jdbc
                         </Export-Package>
                         <Private-Package>
-                            org.apache.karaf.jdbc.internal
+                            org.apache.karaf.jdbc.internal,
+                            org.apache.karaf.util
                         </Private-Package>
                     </instructions>
                 </configuration>

http://git-wip-us.apache.org/repos/asf/karaf/blob/20094637/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 1e11938..1668116 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
@@ -17,11 +17,13 @@
 package org.apache.karaf.jdbc.internal;
 
 import org.apache.karaf.jdbc.JdbcService;
+import org.apache.karaf.util.TemplateUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 
 import javax.sql.DataSource;
+
 import java.io.*;
 import java.sql.*;
 import java.util.*;
@@ -52,79 +54,35 @@ public class JdbcServiceImpl implements JdbcService {
         }
 
         public void installBundle(BundleContext bundleContext, String version) throws Exception {
-            if (version != null) {
-                bundleContext.installBundle(this.bundleUrl + version, null).start();
-            } else {
-                bundleContext.installBundle(this.bundleUrl + this.defaultVersion, null).start();
-            }
+            String location = this.bundleUrl + getWithDefault(version, this.defaultVersion);
+            bundleContext.installBundle(location, null).start();
         }
 
-        public void copyDataSourceFile(File outFile, HashMap<String, String> properties) throws Exception {
-            if (!outFile.exists()) {
-                InputStream is = JdbcServiceImpl.class.getResourceAsStream(this.templateFile);
-                if (is == null) {
-                    throw new IllegalArgumentException("Resource " + this.templateFile + " doesn't exist");
-                }
-                try {
-                    // read it line at a time so that we can use the platform line ending when we write it out
-                    PrintStream out = new PrintStream(new FileOutputStream(outFile));
-                    try {
-                        Scanner scanner = new Scanner(is);
-                        while (scanner.hasNextLine()) {
-                            String line = scanner.nextLine();
-                            line = filter(line, properties);
-                            out.println(line);
-                        }
-                    } finally {
-                        safeClose(out);
-                    }
-                } finally {
-                    safeClose(is);
-                }
-            } else {
-                throw new IllegalArgumentException("File " + outFile.getPath() + " already exists. Remove it if you wish to recreate it.");
-            }
+        private String getWithDefault(String st, String defaultSt) {
+            return (st == null)? defaultSt : st;
         }
 
-        private void safeClose(InputStream is) throws IOException {
-            if (is == null)
-                return;
-            try {
-                is.close();
-            } catch (Throwable ignore) {
-                // nothing to do
+        public void copyDataSourceFile(File outFile, HashMap<String, String> properties) {
+            InputStream is = this.getClass().getResourceAsStream(templateFile);
+            if (is == null) {
+                throw new IllegalArgumentException("Template resource " + templateFile + " doesn't exist");
             }
+            TemplateUtils.createFromTemplate(outFile, is, properties);
         }
 
-        private void safeClose(OutputStream is) throws IOException {
-            if (is == null)
-                return;
-            try {
-                is.close();
-            } catch (Throwable ignore) {
-                // nothing to do
-            }
-        }
-
-        private String filter(String line, HashMap<String, String> props) {
-            for (Map.Entry<String, String> i : props.entrySet()) {
-                int p1 = line.indexOf(i.getKey());
-                if (p1 >= 0) {
-                    String l1 = line.substring(0, p1);
-                    String l2 = line.substring(p1 + i.getKey().length());
-                    line = l1 + i.getValue() + l2;
-                }
-            }
-            return line;
-        }
     }
 
     private BundleContext bundleContext;
 
     @Override
     public void create(String name, String type, String driverClassName, String version, String url, String user, String password, boolean tryToInstallBundles) throws Exception {
+        if (type == null) {
+            throw new IllegalStateException("No database type supplied");
+        }
+        TYPES dbType = TYPES.valueOf(type.toUpperCase());
+
         if (tryToInstallBundles) {
-            TYPES.valueOf(type.toUpperCase()).installBundle(bundleContext, version);
+            dbType.installBundle(bundleContext, version);
         }
 
         File karafBase = new File(System.getProperty("karaf.base"));
@@ -132,13 +90,13 @@ public class JdbcServiceImpl implements JdbcService {
         File outFile = new File(deployFolder, "datasource-" + name + ".xml");
 
         HashMap<String, String> properties = new HashMap<String, String>();
-        properties.put("${name}", name);
-        properties.put("${driver}", driverClassName);
-        properties.put("${url}", url);
-        properties.put("${user}", user);
-        properties.put("${password}", password);
+        properties.put("name", name);
+        properties.put("driver", driverClassName);
+        properties.put("url", url);
+        properties.put("user", user);
+        properties.put("password", password);
 
-        TYPES.valueOf(type.toUpperCase()).copyDataSourceFile(outFile, properties);
+        dbType.copyDataSourceFile(outFile, properties);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/karaf/blob/20094637/jms/core/pom.xml
----------------------------------------------------------------------
diff --git a/jms/core/pom.xml b/jms/core/pom.xml
index cb32295..105d8cc 100644
--- a/jms/core/pom.xml
+++ b/jms/core/pom.xml
@@ -54,6 +54,10 @@
             <version>5.9.0</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf</groupId>
+            <artifactId>org.apache.karaf.util</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
@@ -86,7 +90,8 @@
                             *
                         </Import-Package>
                         <Private-Package>
-                            org.apache.karaf.jms.internal
+                            org.apache.karaf.jms.internal,
+                            org.apache.karaf.util
                         </Private-Package>
                     </instructions>
                 </configuration>

http://git-wip-us.apache.org/repos/asf/karaf/blob/20094637/jms/core/src/main/java/org/apache/karaf/jms/internal/JmsServiceImpl.java
----------------------------------------------------------------------
diff --git a/jms/core/src/main/java/org/apache/karaf/jms/internal/JmsServiceImpl.java b/jms/core/src/main/java/org/apache/karaf/jms/internal/JmsServiceImpl.java
index b8b0f30..d51bc75 100644
--- a/jms/core/src/main/java/org/apache/karaf/jms/internal/JmsServiceImpl.java
+++ b/jms/core/src/main/java/org/apache/karaf/jms/internal/JmsServiceImpl.java
@@ -23,6 +23,7 @@ import org.apache.activemq.command.ActiveMQTopic;
 import org.apache.activemq.pool.PooledConnection;
 import org.apache.karaf.jms.JmsMessage;
 import org.apache.karaf.jms.JmsService;
+import org.apache.karaf.util.TemplateUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
@@ -74,7 +75,11 @@ public class JmsServiceImpl implements JmsService {
             properties.put("channel", splitted[3]);
             template = "connectionfactory-webspheremq.xml";
         }
-        TemplateUtil.createFromTemplate(outFile, template, properties);
+        InputStream is = this.getClass().getResourceAsStream(template);
+        if (is == null) {
+            throw new IllegalArgumentException("Template resource " + template + " doesn't exist");
+        }
+        TemplateUtils.createFromTemplate(outFile, is, properties);
     }
 
     private File getConnectionFactoryFile(String name) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/20094637/jms/core/src/main/java/org/apache/karaf/jms/internal/TemplateUtil.java
----------------------------------------------------------------------
diff --git a/jms/core/src/main/java/org/apache/karaf/jms/internal/TemplateUtil.java b/jms/core/src/main/java/org/apache/karaf/jms/internal/TemplateUtil.java
deleted file mode 100644
index bb9cb75..0000000
--- a/jms/core/src/main/java/org/apache/karaf/jms/internal/TemplateUtil.java
+++ /dev/null
@@ -1,87 +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.jms.internal;
-
-import java.io.Closeable;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.io.PrintStream;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Scanner;
-
-public class TemplateUtil {
-    private TemplateUtil() {
-    }
-
-    public static void createFromTemplate(File outFile, String resource, HashMap<String, String> properties) {
-        if (outFile.exists()) {
-            throw new IllegalArgumentException("File " + outFile.getPath()
-                                               + " already exists. Remove it if you wish to recreate it.");
-        }
-        InputStream is = TemplateUtil.class.getResourceAsStream(resource);
-        if (is == null) {
-            throw new IllegalArgumentException("Resource " + resource + " doesn't exist");
-        }
-        PrintStream out = null;
-        Scanner scanner = null;
-        try {
-            // read it line at a time so that we can use the platform line ending when we write it out
-            out = new PrintStream(new FileOutputStream(outFile));
-            scanner = new Scanner(is);
-
-            while (scanner.hasNextLine()) {
-                String line = scanner.nextLine();
-                line = filter(line, properties);
-                out.println(line);
-            }
-        } catch (FileNotFoundException e) {
-            throw new RuntimeException("Can not create " + outFile, e);
-        } finally {
-            safeClose(scanner);
-            safeClose(out);
-            safeClose(is);
-        }
-    }
-
-    private static String filter(String line, HashMap<String, String> props) {
-        for (Map.Entry<String, String> entry : props.entrySet()) {
-            String key = "${" + entry.getKey() + "}";
-            int p1 = line.indexOf(key);
-            if (p1 >= 0) {
-                String l1 = line.substring(0, p1);
-                String l2 = line.substring(p1 + key.length());
-                line = l1 + entry.getValue() + l2;
-            }
-        }
-        return line;
-    }
-
-    private static void safeClose(Closeable cl) {
-        if (cl == null) {
-            return;
-        }
-        try {
-            cl.close();
-        } catch (Throwable ignore) {
-            // nothing to do
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/karaf/blob/20094637/util/src/main/java/org/apache/karaf/util/TemplateUtils.java
----------------------------------------------------------------------
diff --git a/util/src/main/java/org/apache/karaf/util/TemplateUtils.java b/util/src/main/java/org/apache/karaf/util/TemplateUtils.java
new file mode 100644
index 0000000..067c491
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/TemplateUtils.java
@@ -0,0 +1,83 @@
+/*
+ * 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.util;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Scanner;
+
+public class TemplateUtils {
+    private TemplateUtils() {
+    }
+
+    public static void createFromTemplate(File outFile, InputStream templateIs, HashMap<String, String> properties) {
+        if (outFile.exists()) {
+            throw new IllegalArgumentException("File " + outFile.getPath()
+                                               + " already exists. Remove it if you wish to recreate it.");
+        }
+        PrintStream out = null;
+        Scanner scanner = null;
+        try {
+            // read it line at a time so that we can use the platform line ending when we write it out
+            out = new PrintStream(new FileOutputStream(outFile));
+            scanner = new Scanner(templateIs);
+
+            while (scanner.hasNextLine()) {
+                String line = scanner.nextLine();
+                line = filter(line, properties);
+                out.println(line);
+            }
+        } catch (FileNotFoundException e) {
+            throw new RuntimeException("Can not create " + outFile, e);
+        } finally {
+            safeClose(scanner);
+            safeClose(out);
+            safeClose(templateIs);
+        }
+    }
+
+    private static String filter(String line, HashMap<String, String> props) {
+        for (Map.Entry<String, String> entry : props.entrySet()) {
+            String key = "${" + entry.getKey() + "}";
+            int p1 = line.indexOf(key);
+            if (p1 >= 0) {
+                String l1 = line.substring(0, p1);
+                String l2 = line.substring(p1 + key.length());
+                line = l1 + entry.getValue() + l2;
+            }
+        }
+        return line;
+    }
+
+    private static void safeClose(Closeable cl) {
+        if (cl == null) {
+            return;
+        }
+        try {
+            cl.close();
+        } catch (Throwable ignore) {
+            // nothing to do
+        }
+    }
+
+}