You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2014/05/13 19:53:52 UTC

svn commit: r1594303 - in /tomee/tomee/trunk/container/openejb-core/src: main/java/org/apache/openejb/cipher/ main/java/org/apache/openejb/config/ main/java/org/apache/openejb/resource/jdbc/ main/java/org/apache/openejb/resource/jdbc/cipher/ main/java/...

Author: jlmonteiro
Date: Tue May 13 17:53:52 2014
New Revision: 1594303

URL: http://svn.apache.org/r1594303
Log:
TOMEE-1204 Make it possible to use PasswordEncryption for everything

Added:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherException.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherFactory.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PlainTextPasswordCipher.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/StaticDESPasswordCipher.java
Modified:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Cipher.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PasswordCipher.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicDataSource.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicManagedDataSource.java
    tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java

Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java?rev=1594303&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java Tue May 13 17:53:52 2014
@@ -0,0 +1,52 @@
+/*
+ * 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.openejb.cipher;
+
+/**
+ * Implementations of {@link org.apache.openejb.cipher.PasswordCipher} allow to encode and decode passwords
+ * used to connect to a database.
+ * <p/>
+ * Several implementations may exist, as several encryption algorithms may be
+ * supported. One-way encryption algorithm (hash) can't be used as we need to
+ * give a plain password to the database. {@link #encrypt(String)} method is not
+ * mandatory as we don't need to encode a password, but it's useful to get the
+ * encrypted value for a given plain text password. In the case you have
+ * implemented both methods, you can use the PasswordCodec command line tool to
+ * encode/decode a password.
+ */
+public interface PasswordCipher {
+
+    /**
+     * Encodes a given plain text password and returns the encoded password.
+     * 
+     * @param plainPassword
+     *            The password to encode. May not be <code>null</code>, nor empty.
+     * @return The encoded password.
+     */
+    char[] encrypt(String plainPassword);
+
+    /**
+     * Decodes an encoded password and returns a plain text password.
+     * 
+     * @param encryptedPassword
+     *            The ciphered password to decode. May not be <code>null</code>,
+     *            nor empty.
+     * @return The plain text password.
+     */
+    String decrypt(char[] encryptedPassword);
+
+}

Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherException.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherException.java?rev=1594303&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherException.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherException.java Tue May 13 17:53:52 2014
@@ -0,0 +1,40 @@
+/*
+ * 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.openejb.cipher;
+
+public class PasswordCipherException extends RuntimeException {
+    public PasswordCipherException() {
+        super();
+    }
+
+    public PasswordCipherException(final String message) {
+        super(message);
+    }
+
+    public PasswordCipherException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+
+    public PasswordCipherException(final Throwable cause) {
+        super(cause);
+    }
+
+    protected PasswordCipherException(final String message, final Throwable cause, final boolean enableSuppression,
+                                      final boolean writableStackTrace) {
+        super(message, cause, enableSuppression, writableStackTrace);
+    }
+}

Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherFactory.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherFactory.java?rev=1594303&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherFactory.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipherFactory.java Tue May 13 17:53:52 2014
@@ -0,0 +1,107 @@
+/*
+ * 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.openejb.cipher;
+
+import org.apache.xbean.finder.ResourceFinder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Map;
+
+public class PasswordCipherFactory {
+
+    /**
+     * Create a {@link org.apache.openejb.cipher.PasswordCipher} instance from the
+     * passwordCipher class name.
+     *
+     * @param passwordCipherClass the password cipher to look for
+     * @return the password cipher from the passwordCipher class name
+     * optionally set.
+     */
+    public static PasswordCipher getPasswordCipher(final String passwordCipherClass) {
+        PasswordCipher cipher;
+        try {
+            cipher = doInternalPasswordCipher(PasswordCipher.class, passwordCipherClass);
+
+        } catch (final PasswordCipherException initial) {
+            try {
+                return doInternalPasswordCipher(org.apache.openejb.resource.jdbc.cipher.PasswordCipher.class, passwordCipherClass);
+
+            } catch (final PasswordCipherException ignore) {
+                throw initial;
+            }
+        }
+        return cipher;
+    }
+
+    private static <T extends PasswordCipher> T doInternalPasswordCipher(final Class<T> intf, final String passwordCipherClass){
+        // Load the password cipher class
+        Class<? extends T> pwdCipher;
+
+        // try looking for implementation in /META-INF/org.apache.openejb.cipher.PasswordCipher
+        final ResourceFinder finder = new ResourceFinder("META-INF/");
+        final Map<String, Class<? extends T>> impls;
+        try {
+            impls = finder.mapAllImplementations(intf);
+
+        } catch (final Throwable t) {
+            final String message =
+                    "Password cipher '" + passwordCipherClass +
+                            "' not found in META-INF/org.apache.openejb.cipher.PasswordCipher.";
+            throw new PasswordCipherException(message, t);
+        }
+        pwdCipher = impls.get(passwordCipherClass);
+
+        //
+        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+        final URL url = tccl.getResource("META-INF/" + intf.getName() + "/" + passwordCipherClass);
+        if (url != null) {
+            try {
+                final String clazz = new BufferedReader(new InputStreamReader(url.openStream())).readLine().trim();
+                pwdCipher = tccl.loadClass(clazz).asSubclass(intf);
+            } catch (final Exception e) {
+                // ignored
+            }
+        }
+
+        // if not found in META-INF/org.apache.openejb.cipher.PasswordCipher
+        // we can try to load the class.
+        if (null == pwdCipher) {
+            try {
+                try {
+                    pwdCipher = Class.forName(passwordCipherClass).asSubclass(intf);
+
+                } catch (final ClassNotFoundException cnfe) {
+                    pwdCipher = tccl.loadClass(passwordCipherClass).asSubclass(intf);
+                }
+            } catch (final Throwable t) {
+                final String message = "Cannot load password cipher class '" + passwordCipherClass + "'";
+                throw new PasswordCipherException(message, t);
+            }
+        }
+
+        // Create an instance
+        try {
+            return pwdCipher.newInstance();
+        } catch (final Throwable t) {
+            final String message = "Cannot create password cipher instance";
+            throw new PasswordCipherException(message, t);
+        }
+
+    }
+}

Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java?rev=1594303&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java Tue May 13 17:53:52 2014
@@ -0,0 +1,57 @@
+/*
+ * 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.openejb.cipher;
+
+/**
+ * This {@link org.apache.openejb.cipher.PlainTextPasswordCipher} is an {@link org.apache.openejb.cipher.PasswordCipher}
+ * implementation that does not use any encryption/decryption algorithm at all.
+ */
+public class PlainTextPasswordCipher implements PasswordCipher {
+
+    /**
+     * Returns the <code>encryptedPassword</code> as plain text string.
+     *
+     * @param encryptedPassword
+     *            the encoded password
+     * @return String the decoded password
+     *
+     * @see org.apache.openejb.cipher.PasswordCipher#decrypt(char[])
+     */
+    public String decrypt(final char[] encryptedPassword) {
+        if (null == encryptedPassword) {
+            throw new IllegalArgumentException("encodedPassword cannot be null.");
+        }
+        return new String(encryptedPassword);
+    }
+
+    /**
+     * Returns the <code>plainPassword</code> as plain text character array.
+     *
+     * @param plainPassword
+     *            the plain-text password
+     * @return the plain-text password as character array
+     *
+     * @see org.apache.openejb.cipher.PasswordCipher#encrypt(String)
+     */
+    public char[] encrypt(final String plainPassword) {
+        if (null == plainPassword) {
+            throw new IllegalArgumentException("plainPassword cannot be null.");
+        }
+        return plainPassword.toCharArray();
+    }
+
+}

Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java?rev=1594303&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java Tue May 13 17:53:52 2014
@@ -0,0 +1,99 @@
+/*
+ * 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.openejb.cipher;
+
+import org.apache.openejb.OpenEJBRuntimeException;
+import org.apache.openejb.util.Base64;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.SecretKeySpec;
+
+/**
+ * This {@link org.apache.openejb.cipher.PasswordCipher} implementation uses a the Triple-DES encryption
+ * algorithm.
+ */
+public class StaticDESPasswordCipher implements PasswordCipher {
+
+    private static final byte[] _3desData = {
+            (byte) 0x76, (byte) 0x6F, (byte) 0xBA, (byte) 0x39, (byte) 0x31,
+            (byte) 0x2F, (byte) 0x0D, (byte) 0x4A, (byte) 0xA3, (byte) 0x90,
+            (byte) 0x55, (byte) 0xFE, (byte) 0x55, (byte) 0x65, (byte) 0x61,
+            (byte) 0x13, (byte) 0x34, (byte) 0x82, (byte) 0x12, (byte) 0x17,
+            (byte) 0xAC, (byte) 0x77, (byte) 0x39, (byte) 0x19 };
+
+    private static final SecretKeySpec KEY = new SecretKeySpec(_3desData, "DESede");
+
+    /**
+     * The name of the transformation defines Triple-DES encryption
+     */
+    private static final String TRANSFORMATION = new String("DESede");
+
+    /**
+     * @see org.apache.openejb.cipher.PasswordCipher#encrypt(String)
+     * @throws RuntimeException
+     *             in any case of error.
+     */
+    public char[] encrypt(final String plainPassword) {
+        if (null == plainPassword || plainPassword.length() == 0) {
+            throw new IllegalArgumentException("plainPassword cannot be null nor empty.");
+        }
+
+        final byte[] plaintext = plainPassword.getBytes();
+        try {
+            // Get a 3DES Cipher object
+            final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
+            // Set it into encryption mode
+            cipher.init(Cipher.ENCRYPT_MODE, KEY);
+
+            // Encrypt data
+            final byte[] cipherText = cipher.doFinal(plaintext);
+            return new String(Base64.encodeBase64(cipherText)).toCharArray();
+
+        } catch (final Exception e) {
+            throw new OpenEJBRuntimeException(e);
+        }
+    }
+
+    /**
+     * @see org.apache.openejb.cipher.PasswordCipher#decrypt(char[])
+     * @throws RuntimeException
+     *             in any case of error.
+     */
+    public String decrypt(final char[] encodedPassword) {
+        if (null == encodedPassword || encodedPassword.length == 0) {
+            throw new IllegalArgumentException("encodedPassword cannot be null nor empty.");
+        }
+
+        try {
+            final byte[] cipherText = Base64.decodeBase64(
+                    String.valueOf(encodedPassword).getBytes());
+
+            // Get a 3DES Cipher object
+            final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
+            // Set it into decryption mode
+            cipher.init(Cipher.DECRYPT_MODE, KEY);
+
+            // Decrypt data
+            final String plainText = new String(cipher.doFinal(cipherText));
+            return plainText;
+
+        } catch (final Exception e) {
+            throw new OpenEJBRuntimeException(e);
+        }
+    }
+
+}

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java Tue May 13 17:53:52 2014
@@ -179,6 +179,8 @@ public class AutoConfig implements Dynam
     public synchronized AppModule deploy(final AppModule appModule) throws OpenEJBException {
         final AppResources appResources = new AppResources(appModule);
 
+        appResources.dump();
+
         processApplicationResources(appModule);
 
         for (final EjbModule ejbModule : appModule.getEjbModules()) {
@@ -1110,6 +1112,7 @@ public class AutoConfig implements Dynam
                                     final EjbDeployment ejbDeployment,
                                     final AppResources appResources,
                                     final EjbModule ejbModule) throws OpenEJBException {
+
         // skip destinations with lookup name
         if (ref.getLookupName() != null) {
             return;
@@ -2006,7 +2009,6 @@ public class AutoConfig implements Dynam
         if (resourceId.startsWith("osgi:")) {
             return resourceId;
         }
-
         return null;
     }
 
@@ -2158,17 +2160,42 @@ public class AutoConfig implements Dynam
 
     private static class AppResources {
 
+        private String appId;
+
         @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
         private final Set<String> resourceAdapterIds = new TreeSet<String>();
         private final Map<String, List<String>> resourceIdsByType = new TreeMap<String, List<String>>();
         private final Map<String, List<String>> resourceEnvIdsByType = new TreeMap<String, List<String>>();
         private final Map<String, List<String>> containerIdsByType = new TreeMap<String, List<String>>();
 
+        public void dump() {
+            for (final String s : resourceAdapterIds) {
+                logger.warning(appId + " module contains resource adapter id: " + s);
+            }
+            for (final String s : resourceIdsByType.keySet()) {
+                for (final String value : resourceIdsByType.get(s)) {
+                    logger.warning(appId + " module contains resource type: " + s + " --> " + value);
+                }
+            }
+            for (final String s : resourceEnvIdsByType.keySet()) {
+                for (final String value : resourceEnvIdsByType.get(s)) {
+                    logger.warning(appId + " module contains resource env type: " + s + " --> " + value);
+                }
+            }
+            for (final String s : containerIdsByType.keySet()) {
+                for (final String value : containerIdsByType.get(s)) {
+                    logger.warning(appId + " module contains container type: " + s + " --> " + value);
+                }
+            }
+        }
+
         public AppResources() {
         }
 
         public AppResources(final AppModule appModule) {
 
+            this.appId = appModule.getModuleId();
+
             //
             // DEVELOPERS NOTE:  if you change the id generation code here, you must change
             // the id generation code in ConfigurationFactory.configureApplication(AppModule appModule)
@@ -2332,5 +2359,6 @@ public class AutoConfig implements Dynam
             }
             return ids;
         }
+
     }
 }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Cipher.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Cipher.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Cipher.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/Cipher.java Tue May 13 17:53:52 2014
@@ -24,14 +24,13 @@ import org.apache.commons.cli.OptionBuil
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.PosixParser;
+import org.apache.openejb.cipher.PasswordCipherFactory;
+import org.apache.openejb.cipher.PasswordCipher;
 import org.apache.openejb.cli.SystemExitException;
-import org.apache.openejb.resource.jdbc.BasicDataSourceUtil;
-import org.apache.openejb.resource.jdbc.cipher.PasswordCipher;
 import org.apache.openejb.util.Join;
 import org.apache.openejb.util.Messages;
 import org.apache.xbean.finder.ResourceFinder;
 
-import java.sql.SQLException;
 import java.util.Map;
 
 /**
@@ -80,7 +79,7 @@ public class Cipher {
         }
 
         try {
-            final PasswordCipher cipher = BasicDataSourceUtil.getPasswordCipher(cipherName);
+            final PasswordCipher cipher = PasswordCipherFactory.getPasswordCipher(cipherName);
 
             if (line.hasOption("decrypt")) {
                 final String pwdArg = (String) line.getArgList().get(0);
@@ -88,12 +87,12 @@ public class Cipher {
                 System.out.println(cipher.decrypt(encryptdPassword));
 
             } else { // if option neither encrypt/decrypt is specified, we assume
-                     // it is encrypt.
+                // it is encrypt.
                 final String plainPassword = (String) line.getArgList().get(0);
                 System.out.println(new String(cipher.encrypt(plainPassword)));
             }
 
-        } catch (final SQLException e) {
+        } catch (final RuntimeException e) {
             System.out.println("Could not load password cipher implementation class. Check your classpath.");
 
             availableCiphers();
@@ -107,7 +106,7 @@ public class Cipher {
         try {
             final ResourceFinder finder = new ResourceFinder("META-INF/");
             final Map<String, Class<? extends PasswordCipher>> impls = finder.mapAllImplementations(PasswordCipher.class);
-            System.out.println("Available ciphers are: "+ Join.join(", ", impls.keySet()));
+            System.out.println("Available ciphers are: " + Join.join(", ", impls.keySet()));
         } catch (final Exception dontCare) {
             // no-op
         }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java Tue May 13 17:53:52 2014
@@ -1409,6 +1409,7 @@ public class ConfigurationFactory implem
         final OpenEjbConfiguration runningConfig = getRunningConfig();
         if (runningConfig != null) {
             for (final ResourceInfo resourceInfo : runningConfig.facilities.resources) {
+                logger.warning(String.format("Trying to match resource type %s with classname %s, service %s.", type, resourceInfo.className, resourceInfo.service));
                 if ((type != null && type.equals(resourceInfo.className) || isResourceType(resourceInfo.service, resourceInfo.types, type)) && implies(required, resourceInfo.properties)) {
                     resourceIds.add(resourceInfo.id);
                     resourceIds.addAll(resourceInfo.aliases);

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java Tue May 13 17:53:52 2014
@@ -16,14 +16,10 @@
  */
 package org.apache.openejb.resource.jdbc;
 
-import org.apache.openejb.resource.jdbc.cipher.PasswordCipher;
 import org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin;
 import org.apache.xbean.finder.ResourceFinder;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
 import java.sql.SQLException;
 import java.util.Map;
 
@@ -90,71 +86,4 @@ public final class BasicDataSourceUtil {
         return jdbcUrl;
     }
     
-    /**
-     * Create a {@link PasswordCipher} instance from the
-     *  passwordCipher class name.
-     * 
-     * @param passwordCipherClass the password cipher to look for
-     * @return the password cipher from the passwordCipher class name
-     *         optionally set.
-     * @throws SQLException
-     *             if the driver can not be found.
-     */
-    public static PasswordCipher getPasswordCipher(final String passwordCipherClass) throws SQLException {
-        // Load the password cipher class
-        Class<? extends PasswordCipher> pwdCipher;
-
-        // try looking for implementation in /META-INF/org.apache.openejb.resource.jdbc.org.apache.openejb.resource.jdbc.cipher.PasswordCipher
-        final ResourceFinder finder = new ResourceFinder("META-INF/");
-        final Map<String, Class<? extends PasswordCipher>> impls;
-        try {
-            impls = finder.mapAllImplementations(PasswordCipher.class);
-            
-        } catch (final Throwable t) {
-            final String message =
-                "Password cipher '" + passwordCipherClass +
-                "' not found in META-INF/org.apache.openejb.resource.jdbc.cipher.PasswordCipher.";
-            throw new SQLException(message, t);
-        }
-        pwdCipher = impls.get(passwordCipherClass);
-
-        //
-        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-        final URL url = tccl.getResource("META-INF/org.apache.openejb.resource.jdbc.cipher.PasswordCipher/" + passwordCipherClass);
-        if (url != null) {
-            try {
-                final String clazz = new BufferedReader(new InputStreamReader(url.openStream())).readLine().trim();
-                pwdCipher = tccl.loadClass(clazz).asSubclass(PasswordCipher.class);
-            } catch (final Exception e) {
-                // ignored
-            }
-        }
-
-        // if not found in META-INF/org.apache.openejb.resource.jdbc.org.apache.openejb.resource.jdbc.cipher.PasswordCipher
-        // we can try to load the class.
-        if (null == pwdCipher) {
-            try {
-                try {
-                    pwdCipher = Class.forName(passwordCipherClass).asSubclass(PasswordCipher.class);
-                    
-                } catch (final ClassNotFoundException cnfe) {
-                    pwdCipher = tccl.loadClass(passwordCipherClass).asSubclass(PasswordCipher.class);
-                }
-            } catch (final Throwable t) {
-                final String message = "Cannot load password cipher class '" + passwordCipherClass + "'";
-                throw new SQLException(message, t);
-            }
-        }
-
-        // Create an instance
-        final PasswordCipher cipher;
-        try {
-            cipher = pwdCipher.newInstance();
-        } catch (final Throwable t) {
-            final String message = "Cannot create password cipher instance";
-            throw new SQLException(message, t);
-        }
-
-        return cipher;
-    }
 }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PasswordCipher.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PasswordCipher.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PasswordCipher.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PasswordCipher.java Tue May 13 17:53:52 2014
@@ -17,36 +17,12 @@
 package org.apache.openejb.resource.jdbc.cipher;
 
 /**
- * Implementations of {@link PasswordCipher} allow to encode and decode passwords
- * used to connect to a database.
- * <p/>
- * Several implementations may exist, as several encryption algorithms may be
- * supported. One-way encryption algorithm (hash) can't be used as we need to
- * give a plain password to the database. {@link #encrypt(String)} method is not
- * mandatory as we don't need to encode a password, but it's useful to get the
- * encrypted value for a given plain text password. In the case you have
- * implemented both methods, you can use the PasswordCodec command line tool to
- * encode/decode a password.
+ * In order to reuse that same interface for every passwords in TomEE, it has been moved
+ * to another package. Just keeping that interface for compatibility reasons.
+ * 
+ * @See org.apache.openejb.cipher.PasswordCipher
  */
-public interface PasswordCipher {
-
-    /**
-     * Encodes a given plain text password and returns the encoded password.
-     * 
-     * @param plainPassword
-     *            The password to encode. May not be <code>null</code>, nor empty.
-     * @return The encoded password.
-     */
-    char[] encrypt(String plainPassword);
-
-    /**
-     * Decodes an encoded password and returns a plain text password.
-     * 
-     * @param encryptedPassword
-     *            The ciphered password to decode. May not be <code>null</code>,
-     *            nor empty.
-     * @return The plain text password.
-     */
-    String decrypt(char[] encryptedPassword);
-
+@Deprecated
+public interface PasswordCipher extends org.apache.openejb.cipher.PasswordCipher {
+    
 }

Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PlainTextPasswordCipher.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PlainTextPasswordCipher.java?rev=1594303&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PlainTextPasswordCipher.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/PlainTextPasswordCipher.java Tue May 13 17:53:52 2014
@@ -0,0 +1,21 @@
+/*
+ * 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.openejb.resource.jdbc.cipher;
+
+@Deprecated
+public class PlainTextPasswordCipher extends org.apache.openejb.cipher.PlainTextPasswordCipher implements PasswordCipher {
+}

Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/StaticDESPasswordCipher.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/StaticDESPasswordCipher.java?rev=1594303&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/StaticDESPasswordCipher.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/cipher/StaticDESPasswordCipher.java Tue May 13 17:53:52 2014
@@ -0,0 +1,21 @@
+/*
+ * 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.openejb.resource.jdbc.cipher;
+
+@Deprecated
+public class StaticDESPasswordCipher extends org.apache.openejb.cipher.StaticDESPasswordCipher implements PasswordCipher {
+}

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicDataSource.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicDataSource.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicDataSource.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicDataSource.java Tue May 13 17:53:52 2014
@@ -20,11 +20,11 @@ import org.apache.commons.dbcp.Connectio
 import org.apache.commons.dbcp.DataSourceConnectionFactory;
 import org.apache.commons.dbcp.managed.DataSourceXAConnectionFactory;
 import org.apache.openejb.OpenEJB;
+import org.apache.openejb.cipher.PasswordCipher;
+import org.apache.openejb.cipher.PasswordCipherFactory;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.resource.jdbc.BasicDataSourceUtil;
 import org.apache.openejb.resource.jdbc.IsolationLevels;
-import org.apache.openejb.resource.jdbc.cipher.PasswordCipher;
-import org.apache.openejb.resource.jdbc.cipher.PlainTextPasswordCipher;
 import org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin;
 import org.apache.openejb.util.reflection.Reflections;
 
@@ -38,7 +38,7 @@ import java.util.Properties;
 import java.util.concurrent.locks.ReentrantLock;
 import java.util.logging.Logger;
 
-@SuppressWarnings({"UnusedDeclaration"})
+@SuppressWarnings({ "UnusedDeclaration" })
 public class BasicDataSource extends org.apache.commons.dbcp.BasicDataSource {
 
     private static final ReentrantLock lock = new ReentrantLock();
@@ -50,7 +50,7 @@ public class BasicDataSource extends org
      * ciphered value.
      * <p/>
      * <em>The default is no codec.</em>. In other words, it means password is
-     * not ciphered. The {@link PlainTextPasswordCipher} can also be used.
+     * not ciphered. The {@link org.apache.openejb.cipher.PlainTextPasswordCipher} can also be used.
      */
     private String passwordCipher = null;
     private JMXBasicDataSource jmxDs = null;
@@ -217,7 +217,7 @@ public class BasicDataSource extends org
 
             // check password codec if available
             if (null != passwordCipher) {
-                final PasswordCipher cipher = BasicDataSourceUtil.getPasswordCipher(passwordCipher);
+                final PasswordCipher cipher = PasswordCipherFactory.getPasswordCipher(passwordCipher);
                 final String plainPwd = cipher.decrypt(password.toCharArray());
 
                 // override previous password value

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicManagedDataSource.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicManagedDataSource.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicManagedDataSource.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/dbcp/BasicManagedDataSource.java Tue May 13 17:53:52 2014
@@ -18,11 +18,11 @@ package org.apache.openejb.resource.jdbc
 
 import org.apache.commons.dbcp.ConnectionFactory;
 import org.apache.openejb.OpenEJB;
+import org.apache.openejb.cipher.PasswordCipher;
+import org.apache.openejb.cipher.PasswordCipherFactory;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.resource.jdbc.BasicDataSourceUtil;
 import org.apache.openejb.resource.jdbc.IsolationLevels;
-import org.apache.openejb.resource.jdbc.cipher.PasswordCipher;
-import org.apache.openejb.resource.jdbc.cipher.PlainTextPasswordCipher;
 import org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin;
 import org.apache.openejb.resource.jdbc.pool.XADataSourceResource;
 
@@ -34,7 +34,7 @@ import java.util.Properties;
 import java.util.concurrent.locks.ReentrantLock;
 import java.util.logging.Logger;
 
-@SuppressWarnings({"UnusedDeclaration"})
+@SuppressWarnings({ "UnusedDeclaration" })
 public class BasicManagedDataSource extends org.apache.commons.dbcp.managed.BasicManagedDataSource {
 
     private static final ReentrantLock lock = new ReentrantLock();
@@ -46,7 +46,7 @@ public class BasicManagedDataSource exte
      * ciphered value.
      * <p/>
      * <em>The default is no codec.</em>. In other words, it means password is
-     * not ciphered. The {@link PlainTextPasswordCipher} can also be used.
+     * not ciphered. The {@link org.apache.openejb.cipher.PlainTextPasswordCipher} can also be used.
      */
     private String passwordCipher = null;
     private JMXBasicDataSource jmxDs = null;
@@ -76,7 +76,7 @@ public class BasicManagedDataSource exte
 
     private void setJndiXaDataSource(final String xaDataSource) {
         setXaDataSourceInstance( // proxy cause we don't know if this datasource was created before or not the delegate
-            XADataSourceResource.proxy(getDriverClassLoader() != null ? getDriverClassLoader() : Thread.currentThread().getContextClassLoader(), xaDataSource));
+                XADataSourceResource.proxy(getDriverClassLoader() != null ? getDriverClassLoader() : Thread.currentThread().getContextClassLoader(), xaDataSource));
 
         if (getTransactionManager() == null) {
             setTransactionManager(OpenEJB.getTransactionManager());
@@ -220,7 +220,7 @@ public class BasicManagedDataSource exte
 
             // check password codec if available
             if (null != passwordCipher) {
-                final PasswordCipher cipher = BasicDataSourceUtil.getPasswordCipher(passwordCipher);
+                final PasswordCipher cipher = PasswordCipherFactory.getPasswordCipher(passwordCipher);
                 final String plainPwd = cipher.decrypt(password.toCharArray());
 
                 // override previous password value

Modified: tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java?rev=1594303&r1=1594302&r2=1594303&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java Tue May 13 17:53:52 2014
@@ -18,36 +18,37 @@
 package org.apache.openejb.resource.jdbc;
 
 import junit.framework.TestCase;
-import org.apache.openejb.resource.jdbc.cipher.PasswordCipher;
-import org.apache.openejb.resource.jdbc.cipher.PlainTextPasswordCipher;
-import org.apache.openejb.resource.jdbc.cipher.StaticDESPasswordCipher;
+import org.apache.openejb.cipher.PasswordCipherFactory;
+import org.apache.openejb.cipher.PasswordCipher;
+import org.apache.openejb.cipher.PlainTextPasswordCipher;
+import org.apache.openejb.cipher.StaticDESPasswordCipher;
 
 import java.sql.SQLException;
 
 public class PasswordCodecTest extends TestCase {
-	
-	private static final String PLAIN_PWD = "david"; 
-	
+
+    private static final String PLAIN_PWD = "david";
+
     public void testPlainCodec() {
-    	PasswordCipher cipher = new PlainTextPasswordCipher();
-    	assertEquals(PLAIN_PWD, new String(cipher.encrypt(PLAIN_PWD)));
-    	assertEquals(PLAIN_PWD, cipher.decrypt(PLAIN_PWD.toCharArray()));
+        PasswordCipher cipher = new PlainTextPasswordCipher();
+        assertEquals(PLAIN_PWD, new String(cipher.encrypt(PLAIN_PWD)));
+        assertEquals(PLAIN_PWD, cipher.decrypt(PLAIN_PWD.toCharArray()));
     }
-	
-	public void testStaticDesCodec() {
-		PasswordCipher cipher = new StaticDESPasswordCipher();
-		char[] tmp = cipher.encrypt(PLAIN_PWD);
-		assertEquals(PLAIN_PWD, cipher.decrypt(tmp));
+
+    public void testStaticDesCodec() {
+        PasswordCipher cipher = new StaticDESPasswordCipher();
+        char[] tmp = cipher.encrypt(PLAIN_PWD);
+        assertEquals(PLAIN_PWD, cipher.decrypt(tmp));
     }
-	
-	public void testGetDataSourcePlugin() throws Exception {
+
+    public void testGetDataSourcePlugin() throws Exception {
         // all current known plugins
         assertPluginClass("PlainText", PlainTextPasswordCipher.class);
         assertPluginClass("Static3DES", StaticDESPasswordCipher.class);
 
         // null
         try {
-            BasicDataSourceUtil.getPasswordCipher(null);
+            PasswordCipherFactory.getPasswordCipher(null);
             fail("Should throw an exception when no codec is found.");
         } catch (Exception e) {
             // OK
@@ -55,20 +56,20 @@ public class PasswordCodecTest extends T
 
         // empty string
         try {
-            BasicDataSourceUtil.getPasswordCipher("");
+            PasswordCipherFactory.getPasswordCipher("");
             fail("Should throw an exception when no codec is found.");
         } catch (Exception e) {
             // OK
         }
-        
+
         // try the FQN of the target codec
-        assertNotNull(BasicDataSourceUtil.getPasswordCipher(PlainTextPasswordCipher.class.getName()));
+        assertNotNull(PasswordCipherFactory.getPasswordCipher(PlainTextPasswordCipher.class.getName()));
     }
 
     private void assertPluginClass(String pluginName, Class<? extends PasswordCipher> pluginClass) throws SQLException {
-        PasswordCipher plugin = BasicDataSourceUtil.getPasswordCipher(pluginName);
+        PasswordCipher plugin = PasswordCipherFactory.getPasswordCipher(pluginName);
         assertNotNull(plugin);
-        assertSame(pluginClass, plugin.getClass());
+        assertTrue(pluginClass.isInstance(plugin));
     }
-    
-}
\ No newline at end of file
+
+}