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 2009/10/09 22:44:07 UTC
svn commit: r823678 - in /openejb/trunk/openejb3/container/openejb-core/src:
main/java/org/apache/openejb/config/
main/java/org/apache/openejb/resource/jdbc/
main/resources/META-INF/org.apache.openejb.cli/
main/resources/org/apache/openejb/config/ test...
Author: jlmonteiro
Date: Fri Oct 9 20:44:06 2009
New Revision: 823678
URL: http://svn.apache.org/viewvc?rev=823678&view=rev
Log:
OPENEJB-1076 Allow end users to use ciphered passwords in datasource configuration (openejb.xml).
--> Avoid code duplication --> refactoring
--> Add a command line tool to use a PasswordCodec
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/Codec.java
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/codec
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java
openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties
Added: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/Codec.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/Codec.java?rev=823678&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/Codec.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/Codec.java Fri Oct 9 20:44:06 2009
@@ -0,0 +1,125 @@
+/**
+ * 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.config;
+
+import java.sql.SQLException;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.apache.openejb.cli.SystemExitException;
+import org.apache.openejb.resource.jdbc.BasicDataSourceUtil;
+import org.apache.openejb.resource.jdbc.PasswordCodec;
+import org.apache.openejb.util.Messages;
+
+/**
+ * Command line tool on top of the {@link PasswordCodec} interface. Basically,
+ * it allows end user to encode/decode a string (ie. a password) using a codec
+ * implementation.
+ */
+public class Codec {
+
+ private static Messages messages = new Messages(Codec.class);
+
+ public static void main(String[] args) throws SystemExitException {
+
+ CommandLineParser parser = new PosixParser();
+
+ // create the Options
+ Options options = new Options();
+ options.addOption(option("h", "help", "cmd.codec.opt.help"));
+ options.addOption(option("i", "codec", "i", "cmd.codec.opt.impl"));
+ options.addOption(option("d", "decode", "cmd.codec.opt.decode"));
+ options.addOption(option("e", "encode", "cmd.codec.opt.encode"));
+
+ CommandLine line;
+ try {
+ // parse the command line arguments
+ line = parser.parse(options, args);
+ } catch (ParseException exp) {
+ help(options);
+ throw new SystemExitException(-1);
+ }
+
+ if (line.hasOption("help")) {
+ help(options);
+ return;
+ }
+
+ if (!line.hasOption("codec")) {
+ System.out.println("Must specify the PasswordCodec implementation to use.");
+ help(options);
+ return;
+ }
+
+ if (line.getArgList().size() != 1) {
+ System.out.println("Must specify either a plain text to encode, either a ciphered value to decode.");
+ help(options);
+ return;
+ }
+
+ try {
+ PasswordCodec codec = BasicDataSourceUtil.getPasswordCodec(line
+ .getOptionValue("codec"));
+
+ if (line.hasOption("decode")) {
+ String pwdArg = (String) line.getArgList().get(0);
+ char[] encodedPassword = pwdArg.toCharArray();
+ System.out.println(
+ "The plain text value for " + pwdArg
+ + " is " + codec.decode(encodedPassword));
+
+ } else { // if option neither encode/decode is specified, we assume
+ // it is encode.
+ String plainPassword = (String) line.getArgList().get(0);
+ System.out.println(
+ "The encode value for " + plainPassword
+ + " is " + new String(codec.encode(plainPassword)));
+ }
+
+ } catch (SQLException e) {
+ System.out.println("Could not load password codec implementation class. Check your classpath.");
+ throw new SystemExitException(-1);
+ }
+
+ }
+
+ private static void help(Options options) {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp("codec [options] <value>", "\n" + i18n("cmd.codec.description"), options, "\n");
+ }
+
+ private static Option option(String shortOpt, String longOpt,
+ String description) {
+ return OptionBuilder.withLongOpt(longOpt).withDescription(i18n(description)).create(shortOpt);
+ }
+
+ private static Option option(String shortOpt, String longOpt,
+ String argName, String description) {
+ return OptionBuilder.withLongOpt(longOpt).withArgName(argName).hasArg().withDescription(i18n(description)).create(shortOpt);
+ }
+
+ private static String i18n(String key) {
+ return messages.format(key);
+ }
+
+}
Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java?rev=823678&r1=823677&r2=823678&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java Fri Oct 9 20:44:06 2009
@@ -97,7 +97,7 @@
// check password codec if available
if (null != passwordCodecClass) {
- PasswordCodec codec = getPasswordCodec();
+ PasswordCodec codec = BasicDataSourceUtil.getPasswordCodec(passwordCodecClass);
String plainPwd = codec.decode(password.toCharArray());
// override previous password value
@@ -131,44 +131,4 @@
}
}
- /**
- * Create a {@link PasswordCodec} instance from the
- * {@link #passwordCodecClass}.
- *
- * @return the password codec from the {@link #passwordCodecClass}
- * optionally set.
- * @throws SQLException
- * if the driver can not be found.
- */
- private PasswordCodec getPasswordCodec() throws SQLException {
- // Load the password codec class
- Class pwdCodec = null;
- try {
- try {
- pwdCodec = Class.forName(passwordCodecClass);
-
- } catch (ClassNotFoundException cnfe) {
- pwdCodec = Thread.currentThread().getContextClassLoader().loadClass(passwordCodecClass);
- }
- } catch (Throwable t) {
- String message = "Cannot load password codec class '" + passwordCodecClass + "'";
- logWriter.println(message);
- t.printStackTrace(logWriter);
- throw new SQLNestedException(message, t);
- }
-
- // Create an instance
- PasswordCodec codec = null;
- try {
- codec = (PasswordCodec) pwdCodec.newInstance();
-
- } catch (Throwable t) {
- String message = "Cannot create password codec instance";
- logWriter.println(message);
- t.printStackTrace(logWriter);
- throw new SQLNestedException(message, t);
- }
-
- return codec;
- }
}
Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java?rev=823678&r1=823677&r2=823678&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java Fri Oct 9 20:44:06 2009
@@ -17,6 +17,7 @@
*/
package org.apache.openejb.resource.jdbc;
+import org.apache.commons.dbcp.SQLNestedException;
import org.apache.xbean.finder.ResourceFinder;
import java.sql.SQLException;
@@ -81,4 +82,42 @@
return jdbcUrl;
}
+
+ /**
+ * Create a {@link PasswordCodec} instance from the
+ * {@link #passwordCodecClass}.
+ *
+ * @param passwordCodecClass the password codec to look for
+ * @return the password codec from the {@link #passwordCodecClass}
+ * optionally set.
+ * @throws SQLException
+ * if the driver can not be found.
+ */
+ public static PasswordCodec getPasswordCodec(String passwordCodecClass) throws SQLException {
+ // Load the password codec class
+ Class pwdCodec = null;
+ try {
+ try {
+ pwdCodec = Class.forName(passwordCodecClass);
+
+ } catch (ClassNotFoundException cnfe) {
+ pwdCodec = Thread.currentThread().getContextClassLoader().loadClass(passwordCodecClass);
+ }
+ } catch (Throwable t) {
+ String message = "Cannot load password codec class '" + passwordCodecClass + "'";
+ throw new SQLNestedException(message, t);
+ }
+
+ // Create an instance
+ PasswordCodec codec = null;
+ try {
+ codec = (PasswordCodec) pwdCodec.newInstance();
+
+ } catch (Throwable t) {
+ String message = "Cannot create password codec instance";
+ throw new SQLNestedException(message, t);
+ }
+
+ return codec;
+ }
}
Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java?rev=823678&r1=823677&r2=823678&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java Fri Oct 9 20:44:06 2009
@@ -23,7 +23,6 @@
import javax.sql.DataSource;
-import org.apache.commons.dbcp.SQLNestedException;
import org.apache.openejb.loader.SystemInstance;
public class BasicManagedDataSource extends org.apache.commons.dbcp.managed.BasicManagedDataSource {
@@ -96,7 +95,7 @@
// check password codec if available
if (null != passwordCodecClass) {
- PasswordCodec codec = getPasswordCodec();
+ PasswordCodec codec = BasicDataSourceUtil.getPasswordCodec(passwordCodecClass);
String plainPwd = codec.decode(password.toCharArray());
// override previous password value
@@ -134,45 +133,4 @@
protected void wrapTransactionManager() {
}
- /**
- * Create a {@link PasswordCodec} instance from the
- * {@link #passwordCodecClass}.
- *
- * @return the password codec from the {@link #passwordCodecClass}
- * optionally set.
- * @throws SQLException
- * if the driver can not be found.
- */
- private PasswordCodec getPasswordCodec() throws SQLException {
- // Load the password codec class
- Class pwdCodec = null;
- try {
- try {
- pwdCodec = Class.forName(passwordCodecClass);
-
- } catch (ClassNotFoundException cnfe) {
- pwdCodec = Thread.currentThread().getContextClassLoader().loadClass(passwordCodecClass);
- }
- } catch (Throwable t) {
- String message = "Cannot load password codec class '" + passwordCodecClass + "'";
- logWriter.println(message);
- t.printStackTrace(logWriter);
- throw new SQLNestedException(message, t);
- }
-
- // Create an instance
- PasswordCodec codec = null;
- try {
- codec = (PasswordCodec) pwdCodec.newInstance();
-
- } catch (Throwable t) {
- String message = "Cannot create password codec instance";
- logWriter.println(message);
- t.printStackTrace(logWriter);
- throw new SQLNestedException(message, t);
- }
-
- return codec;
- }
-
}
Added: openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/codec
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/codec?rev=823678&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/codec (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.cli/codec Fri Oct 9 20:44:06 2009
@@ -0,0 +1,20 @@
+# 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.
+#
+main.class = org.apache.openejb.config.Codec
+name = codec
+description = Encode/Decode using the PasswordCodec.
+
+
Modified: openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties?rev=823678&r1=823677&r2=823678&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/Messages.properties Fri Oct 9 20:44:06 2009
@@ -17,6 +17,22 @@
#
# $Rev$ $Date$
#
+# codec
+# formatter.printHelp("codec [options] <value>", "\n"+i18n("cmd.codec.description"), options, "\n");
+cmd.codec.description = Uses a password codec implementation to encode/decode a value.
+
+#options.addOption(option("h", "help", "cmd.codec.opt.help"));
+cmd.codec.opt.help = Lists these options and exits.
+
+#options.addOption(option("i", "codec", "cmd.codec.opt.impl"));
+cmd.codec.opt.impl = Specifies the password codec implementation to use.
+
+#options.addOption(option("d", "decode", "cmd.codec.opt.decode"));
+cmd.codec.opt.decode = Switches command line tool to decode.
+
+#options.addOption(option("e", "encode", "cmd.codec.opt.encode"));
+cmd.codec.opt.encode = Switches command line tool to encode (default).
+
# Undeploy
#formatter.printHelp("undeploy [options] <moduleId> [<moduleId>...]", "\n"+ Undeploy.i18n("cmd.deploy.description"), options, "\n");
cmd.undeploy.description= Connects to the server and Undeploys the module(s)
Added: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java?rev=823678&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/PasswordCodecTest.java Fri Oct 9 20:44:06 2009
@@ -0,0 +1,38 @@
+/**
+ *
+ * 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;
+
+import junit.framework.TestCase;
+
+public class PasswordCodecTest extends TestCase {
+
+ private static final String PLAIN_PWD = "david";
+
+ public void testPlainCodec() {
+ PasswordCodec codec = new PlainTextPasswordCodec();
+ assertEquals(PLAIN_PWD, new String(codec.encode(PLAIN_PWD)));
+ assertEquals(PLAIN_PWD, codec.decode(PLAIN_PWD.toCharArray()));
+ }
+
+ public void testStaticDesCodec() {
+ PasswordCodec codec = new StaticDESPasswordCodec();
+ char[] tmp = codec.encode(PLAIN_PWD);
+ assertEquals(PLAIN_PWD, codec.decode(tmp));
+ }
+
+}
\ No newline at end of file