cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [2/5] cayenne git commit: CAY-2166 Auto-loading of Cayenne modules
Date Tue, 30 May 2017 07:23:37 GMT
CAY-2166 Auto-loading of Cayenne modules

* renaming module builder to extender, as the base module is autoloaded and is never changed


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

Branch: refs/heads/master
Commit: 78102cf383fa8677afe41ac486a1180ffdf63c55
Parents: 543a8f7
Author: Andrus Adamchik <andrus@objectstyle.com>
Authored: Tue May 30 09:43:26 2017 +0300
Committer: Andrus Adamchik <andrus@objectstyle.com>
Committed: Tue May 30 09:43:26 2017 +0300

----------------------------------------------------------------------
 .../org/apache/cayenne/crypto/CryptoModule.java |  22 +-
 .../cayenne/crypto/CryptoModuleBuilder.java     | 330 -------------------
 .../cayenne/crypto/CryptoModuleExtender.java    | 330 +++++++++++++++++++
 .../cayenne/crypto/CryptoModuleBuilderTest.java |   4 +-
 .../cayenne/crypto/Runtime_AES128_Base.java     |   4 +-
 5 files changed, 348 insertions(+), 342 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/78102cf3/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
index 69c134a..f12f22a 100644
--- a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
+++ b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
@@ -73,28 +73,34 @@ public class CryptoModule implements Module {
     private static final char[] DEFAULT_KEY_PASSWORD = "changeit".toCharArray();
     private static final String DEFAULT_COLUMN_MAPPER_PATTERN = "^CRYPTO_";
 
-    public static CryptoModuleBuilder builder() {
-        return new CryptoModuleBuilder();
+    /**
+     * Returns a new extender that helps to build a custom module that provides required
configuration for {@link CryptoModule}
+     * as well as custom strategies overriding the defaults defined here.
+     *
+     * @return a new extender that helps to build a custom module that provides required
configuration for {@link CryptoModule}.
+     */
+    public static CryptoModuleExtender extend() {
+        return new CryptoModuleExtender();
     }
 
-    public static MapBuilder<String> contributeProperties(Binder binder) {
+    static MapBuilder<String> contributeProperties(Binder binder) {
         return binder.bindMap(String.class, CryptoConstants.PROPERTIES_MAP);
     }
 
-    public static MapBuilder<char[]> contributeCredentials(Binder binder) {
+    static MapBuilder<char[]> contributeCredentials(Binder binder) {
         return binder.bindMap(char[].class, CryptoConstants.CREDENTIALS_MAP);
     }
 
     @SuppressWarnings("unchecked")
-    public static MapBuilder<BytesConverter<?>> contributeDbToByteConverters(Binder
binder) {
+    static MapBuilder<BytesConverter<?>> contributeDbToByteConverters(Binder
binder) {
         MapBuilder mapBuilder = binder.bindMap(BytesConverter.class, DefaultValueTransformerFactory.DB_TO_BYTE_CONVERTERS_KEY);
-        return (MapBuilder<BytesConverter<?>>)mapBuilder;
+        return (MapBuilder<BytesConverter<?>>) mapBuilder;
     }
 
     @SuppressWarnings("unchecked")
-    public static MapBuilder<BytesConverter<?>> contributeObjectToByteConverters(Binder
binder) {
+    static MapBuilder<BytesConverter<?>> contributeObjectToByteConverters(Binder
binder) {
         MapBuilder mapBuilder = binder.bindMap(BytesConverter.class, DefaultValueTransformerFactory.OBJECT_TO_BYTE_CONVERTERS_KEY);
-        return (MapBuilder<BytesConverter<?>>)mapBuilder;
+        return (MapBuilder<BytesConverter<?>>) mapBuilder;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cayenne/blob/78102cf3/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
deleted file mode 100644
index d05ecba..0000000
--- a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleBuilder.java
+++ /dev/null
@@ -1,330 +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.cayenne.crypto;
-
-import org.apache.cayenne.crypto.cipher.CipherFactory;
-import org.apache.cayenne.crypto.key.KeySource;
-import org.apache.cayenne.crypto.map.ColumnMapper;
-import org.apache.cayenne.crypto.map.PatternColumnMapper;
-import org.apache.cayenne.crypto.transformer.bytes.BytesTransformerFactory;
-import org.apache.cayenne.crypto.transformer.value.BytesConverter;
-import org.apache.cayenne.crypto.transformer.value.ValueTransformerFactory;
-import org.apache.cayenne.di.Binder;
-import org.apache.cayenne.di.MapBuilder;
-import org.apache.cayenne.di.Module;
-
-import java.io.File;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-
-/**
- * A builder that allows to customize {@link CryptoModule} module.
- *
- * @since 4.0
- */
-public class CryptoModuleBuilder {
-
-    private Class<? extends ValueTransformerFactory> valueTransformerFactoryType;
-    private Class<? extends BytesTransformerFactory> bytesTransformerFactoryType;
-
-    private Map<String, BytesConverter<?>> extraObjectToBytes;
-    private Map<Integer, BytesConverter<?>> extraDbToBytes;
-
-    private String columnMapperPattern;
-    private ColumnMapper columnMapper;
-    private Class<? extends ColumnMapper> columnMapperType;
-
-    private String cipherAlgoritm;
-    private String cipherMode;
-    private Class<? extends CipherFactory> cipherFactoryType;
-
-    private URL keyStoreUrl;
-    private String keyStoreUrlString;
-    private File keyStoreFile;
-    private Class<? extends KeySource> keySourceType;
-    private KeySource keySource;
-
-    private String encryptionKeyAlias;
-    private char[] keyPassword;
-
-    private boolean compress;
-    private boolean useHMAC;
-
-    // use CryptoModule.builder() to create the builder...
-    protected CryptoModuleBuilder() {
-        this.extraDbToBytes = new HashMap<>();
-        this.extraObjectToBytes = new HashMap<>();
-    }
-
-    public CryptoModuleBuilder cipherAlgorithm(String algorithm) {
-        this.cipherAlgoritm = Objects.requireNonNull(algorithm);
-        return this;
-    }
-
-    public CryptoModuleBuilder cipherMode(String mode) {
-        this.cipherMode = Objects.requireNonNull(mode);
-        return this;
-    }
-
-    public CryptoModuleBuilder cipherFactory(Class<? extends CipherFactory> factoryType)
{
-        this.cipherFactoryType = Objects.requireNonNull(factoryType);
-        return this;
-    }
-
-    public CryptoModuleBuilder valueTransformer(Class<? extends ValueTransformerFactory>
factoryType) {
-        this.valueTransformerFactoryType = Objects.requireNonNull(factoryType);
-        return this;
-    }
-
-    public <T> CryptoModuleBuilder objectToBytesConverter(Class<T> objectType,
BytesConverter<T> converter) {
-        extraObjectToBytes.put(objectType.getName(), Objects.requireNonNull(converter));
-        return this;
-    }
-
-    public CryptoModuleBuilder dbToBytesConverter(int sqlType, BytesConverter<?> converter)
{
-        extraDbToBytes.put(sqlType, Objects.requireNonNull(converter));
-        return this;
-    }
-
-    public CryptoModuleBuilder bytesTransformer(Class<? extends BytesTransformerFactory>
factoryType) {
-        this.bytesTransformerFactoryType = Objects.requireNonNull(factoryType);
-        return this;
-    }
-
-    public CryptoModuleBuilder columnMapper(Class<? extends ColumnMapper> columnMapperType)
{
-        this.columnMapperPattern = null;
-        this.columnMapperType = Objects.requireNonNull(columnMapperType);
-        this.columnMapper = null;
-        return this;
-    }
-
-    public CryptoModuleBuilder columnMapper(ColumnMapper columnMapper) {
-        this.columnMapperPattern = null;
-        this.columnMapperType = null;
-        this.columnMapper = Objects.requireNonNull(columnMapper);
-        return this;
-    }
-
-    public CryptoModuleBuilder columnMapper(String pattern) {
-        this.columnMapperPattern = Objects.requireNonNull(pattern);
-        this.columnMapperType = null;
-        this.columnMapper = null;
-        return this;
-    }
-
-    /**
-     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
-     *                           encryption by default.
-     */
-    public CryptoModuleBuilder encryptionKeyAlias(String encryptionKeyAlias) {
-        this.encryptionKeyAlias = Objects.requireNonNull(encryptionKeyAlias);
-        return this;
-    }
-
-    /**
-     * Configures keystore parameters. The KeyStore must be of "jceks" type and
-     * contain all needed secret keys for the target database. Currently all
-     * keys must be protected with the same password.
-     *
-     * @param file               A file to load keystore from.
-     * @param passwordForAllKeys A password that unlocks all keys in the keystore.
-     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
-     *                           encryption by default.
-     */
-    public CryptoModuleBuilder keyStore(File file, char[] passwordForAllKeys, String encryptionKeyAlias)
{
-        this.encryptionKeyAlias = encryptionKeyAlias;
-        this.keyPassword = passwordForAllKeys;
-        this.keyStoreUrl = null;
-        this.keyStoreUrlString = null;
-        this.keyStoreFile = Objects.requireNonNull(file);
-        return this;
-    }
-
-    /**
-     * Configures keystore parameters. The KeyStore must be of "jceks" type and
-     * contain all needed secret keys for the target database. Currently all
-     * keys must be protected with the same password.
-     *
-     * @param url                A URL to load keystore from.
-     * @param passwordForAllKeys A password that unlocks all keys in the keystore.
-     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
-     *                           encryption by default.
-     */
-    public CryptoModuleBuilder keyStore(String url, char[] passwordForAllKeys, String encryptionKeyAlias)
{
-        this.encryptionKeyAlias = encryptionKeyAlias;
-        this.keyPassword = passwordForAllKeys;
-        this.keyStoreUrl = null;
-        this.keyStoreUrlString = Objects.requireNonNull(url);
-        this.keyStoreFile = null;
-        return this;
-    }
-
-    /**
-     * Configures keystore parameters. The KeyStore must be of "jceks" type and
-     * contain all needed secret keys for the target database. Currently all
-     * keys must be protected with the same password.
-     *
-     * @param url                A URL to load keystore from.
-     * @param passwordForAllKeys A password that unlocks all keys in the keystore.
-     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
-     *                           encryption by default.
-     */
-    public CryptoModuleBuilder keyStore(URL url, char[] passwordForAllKeys, String encryptionKeyAlias)
{
-        this.encryptionKeyAlias = encryptionKeyAlias;
-        this.keyPassword = passwordForAllKeys;
-        this.keyStoreUrl = Objects.requireNonNull(url);
-        this.keyStoreUrlString = null;
-        this.keyStoreFile = null;
-        return this;
-    }
-
-    public CryptoModuleBuilder keySource(Class<? extends KeySource> type) {
-        this.keySourceType = Objects.requireNonNull(type);
-        this.keySource = null;
-        return this;
-    }
-
-    public CryptoModuleBuilder keySource(KeySource keySource) {
-        this.keySourceType = null;
-        this.keySource = Objects.requireNonNull(keySource);
-        return this;
-    }
-
-    public CryptoModuleBuilder compress() {
-        this.compress = true;
-        return this;
-    }
-
-    /**
-     * Enable authentication codes
-     */
-    public CryptoModuleBuilder useHMAC() {
-        this.useHMAC = true;
-        return this;
-    }
-
-    /**
-     * Produces a module that can be used to start Cayenne runtime.
-     */
-    public Module build() {
-
-        return new Module() {
-
-            @Override
-            public void configure(Binder binder) {
-
-                MapBuilder<String> props = CryptoModule.contributeProperties(binder);
-
-                if (cipherAlgoritm != null) {
-                    props.put(CryptoConstants.CIPHER_ALGORITHM, cipherAlgoritm);
-                }
-
-                if (cipherMode != null) {
-                    props.put(CryptoConstants.CIPHER_MODE, cipherMode);
-                }
-
-                String keyStoreUrl = keyStoreUrl();
-                if (keyStoreUrl != null) {
-                    props.put(CryptoConstants.KEYSTORE_URL, keyStoreUrl);
-                }
-
-                if (encryptionKeyAlias != null) {
-                    props.put(CryptoConstants.ENCRYPTION_KEY_ALIAS, encryptionKeyAlias);
-                }
-
-                if (compress) {
-                    props.put(CryptoConstants.COMPRESSION, "true");
-                }
-
-                if (useHMAC) {
-                    props.put(CryptoConstants.USE_HMAC, "true");
-                }
-
-                if (keyPassword != null) {
-                    CryptoModule.contributeCredentials(binder).put(CryptoConstants.KEY_PASSWORD,
keyPassword);
-                }
-
-                if (cipherFactoryType != null) {
-                    binder.bind(CipherFactory.class).to(cipherFactoryType);
-                }
-
-                if (valueTransformerFactoryType != null) {
-                    binder.bind(ValueTransformerFactory.class).to(valueTransformerFactoryType);
-                }
-
-                if (!extraDbToBytes.isEmpty()) {
-                    MapBuilder<BytesConverter<?>> dbToBytesBinder = CryptoModule.contributeDbToByteConverters(binder);
-                    for (Map.Entry<Integer, BytesConverter<?>> extraConverter
: extraDbToBytes.entrySet()) {
-                        dbToBytesBinder.put(extraConverter.getKey().toString(), extraConverter.getValue());
-                    }
-                }
-
-                if (!extraObjectToBytes.isEmpty()) {
-                    MapBuilder<BytesConverter<?>> objectToBytesBinder = CryptoModule.contributeObjectToByteConverters(binder);
-                    for (Map.Entry<String, BytesConverter<?>> extraConverter
: extraObjectToBytes.entrySet()) {
-                        objectToBytesBinder.put(extraConverter.getKey(), extraConverter.getValue());
-                    }
-                }
-
-                if (bytesTransformerFactoryType != null) {
-                    binder.bind(BytesTransformerFactory.class).to(bytesTransformerFactoryType);
-                }
-
-                if (keySource != null) {
-                    binder.bind(KeySource.class).toInstance(keySource);
-                } else if (keySourceType != null) {
-                    binder.bind(KeySource.class).to(keySourceType);
-                }
-
-                if (columnMapperPattern != null) {
-                    binder.bind(ColumnMapper.class).toInstance(new PatternColumnMapper(columnMapperPattern));
-                } else if (columnMapperType != null) {
-                    binder.bind(ColumnMapper.class).to(columnMapperType);
-                } else if (columnMapper != null) {
-                    binder.bind(ColumnMapper.class).toInstance(columnMapper);
-                }
-            }
-        };
-    }
-
-    protected String keyStoreUrl() {
-        if (this.keyStoreUrl != null) {
-            return this.keyStoreUrl.toExternalForm();
-        }
-
-        if (this.keyStoreUrlString != null) {
-            return this.keyStoreUrlString;
-        }
-
-        if (keyStoreFile != null) {
-            try {
-                return keyStoreFile.toURI().toURL().toExternalForm();
-            } catch (MalformedURLException e) {
-                throw new IllegalStateException("Invalid keyStore file", e);
-            }
-        }
-
-        return null;
-    }
-
-
-}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/78102cf3/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleExtender.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleExtender.java
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleExtender.java
new file mode 100644
index 0000000..98bef8c
--- /dev/null
+++ b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModuleExtender.java
@@ -0,0 +1,330 @@
+/*****************************************************************
+ *   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.cayenne.crypto;
+
+import org.apache.cayenne.crypto.cipher.CipherFactory;
+import org.apache.cayenne.crypto.key.KeySource;
+import org.apache.cayenne.crypto.map.ColumnMapper;
+import org.apache.cayenne.crypto.map.PatternColumnMapper;
+import org.apache.cayenne.crypto.transformer.bytes.BytesTransformerFactory;
+import org.apache.cayenne.crypto.transformer.value.BytesConverter;
+import org.apache.cayenne.crypto.transformer.value.ValueTransformerFactory;
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.MapBuilder;
+import org.apache.cayenne.di.Module;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * A builder of custom extensions and required configuration for {@link CryptoModule} module.
+ *
+ * @since 4.0
+ */
+public class CryptoModuleExtender {
+
+    private Class<? extends ValueTransformerFactory> valueTransformerFactoryType;
+    private Class<? extends BytesTransformerFactory> bytesTransformerFactoryType;
+
+    private Map<String, BytesConverter<?>> extraObjectToBytes;
+    private Map<Integer, BytesConverter<?>> extraDbToBytes;
+
+    private String columnMapperPattern;
+    private ColumnMapper columnMapper;
+    private Class<? extends ColumnMapper> columnMapperType;
+
+    private String cipherAlgoritm;
+    private String cipherMode;
+    private Class<? extends CipherFactory> cipherFactoryType;
+
+    private URL keyStoreUrl;
+    private String keyStoreUrlString;
+    private File keyStoreFile;
+    private Class<? extends KeySource> keySourceType;
+    private KeySource keySource;
+
+    private String encryptionKeyAlias;
+    private char[] keyPassword;
+
+    private boolean compress;
+    private boolean useHMAC;
+
+    // use CryptoModule.builder() to create the builder...
+    protected CryptoModuleExtender() {
+        this.extraDbToBytes = new HashMap<>();
+        this.extraObjectToBytes = new HashMap<>();
+    }
+
+    public CryptoModuleExtender cipherAlgorithm(String algorithm) {
+        this.cipherAlgoritm = Objects.requireNonNull(algorithm);
+        return this;
+    }
+
+    public CryptoModuleExtender cipherMode(String mode) {
+        this.cipherMode = Objects.requireNonNull(mode);
+        return this;
+    }
+
+    public CryptoModuleExtender cipherFactory(Class<? extends CipherFactory> factoryType)
{
+        this.cipherFactoryType = Objects.requireNonNull(factoryType);
+        return this;
+    }
+
+    public CryptoModuleExtender valueTransformer(Class<? extends ValueTransformerFactory>
factoryType) {
+        this.valueTransformerFactoryType = Objects.requireNonNull(factoryType);
+        return this;
+    }
+
+    public <T> CryptoModuleExtender objectToBytesConverter(Class<T> objectType,
BytesConverter<T> converter) {
+        extraObjectToBytes.put(objectType.getName(), Objects.requireNonNull(converter));
+        return this;
+    }
+
+    public CryptoModuleExtender dbToBytesConverter(int sqlType, BytesConverter<?> converter)
{
+        extraDbToBytes.put(sqlType, Objects.requireNonNull(converter));
+        return this;
+    }
+
+    public CryptoModuleExtender bytesTransformer(Class<? extends BytesTransformerFactory>
factoryType) {
+        this.bytesTransformerFactoryType = Objects.requireNonNull(factoryType);
+        return this;
+    }
+
+    public CryptoModuleExtender columnMapper(Class<? extends ColumnMapper> columnMapperType)
{
+        this.columnMapperPattern = null;
+        this.columnMapperType = Objects.requireNonNull(columnMapperType);
+        this.columnMapper = null;
+        return this;
+    }
+
+    public CryptoModuleExtender columnMapper(ColumnMapper columnMapper) {
+        this.columnMapperPattern = null;
+        this.columnMapperType = null;
+        this.columnMapper = Objects.requireNonNull(columnMapper);
+        return this;
+    }
+
+    public CryptoModuleExtender columnMapper(String pattern) {
+        this.columnMapperPattern = Objects.requireNonNull(pattern);
+        this.columnMapperType = null;
+        this.columnMapper = null;
+        return this;
+    }
+
+    /**
+     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
+     *                           encryption by default.
+     */
+    public CryptoModuleExtender encryptionKeyAlias(String encryptionKeyAlias) {
+        this.encryptionKeyAlias = Objects.requireNonNull(encryptionKeyAlias);
+        return this;
+    }
+
+    /**
+     * Configures keystore parameters. The KeyStore must be of "jceks" type and
+     * contain all needed secret keys for the target database. Currently all
+     * keys must be protected with the same password.
+     *
+     * @param file               A file to load keystore from.
+     * @param passwordForAllKeys A password that unlocks all keys in the keystore.
+     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
+     *                           encryption by default.
+     */
+    public CryptoModuleExtender keyStore(File file, char[] passwordForAllKeys, String encryptionKeyAlias)
{
+        this.encryptionKeyAlias = encryptionKeyAlias;
+        this.keyPassword = passwordForAllKeys;
+        this.keyStoreUrl = null;
+        this.keyStoreUrlString = null;
+        this.keyStoreFile = Objects.requireNonNull(file);
+        return this;
+    }
+
+    /**
+     * Configures keystore parameters. The KeyStore must be of "jceks" type and
+     * contain all needed secret keys for the target database. Currently all
+     * keys must be protected with the same password.
+     *
+     * @param url                A URL to load keystore from.
+     * @param passwordForAllKeys A password that unlocks all keys in the keystore.
+     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
+     *                           encryption by default.
+     */
+    public CryptoModuleExtender keyStore(String url, char[] passwordForAllKeys, String encryptionKeyAlias)
{
+        this.encryptionKeyAlias = encryptionKeyAlias;
+        this.keyPassword = passwordForAllKeys;
+        this.keyStoreUrl = null;
+        this.keyStoreUrlString = Objects.requireNonNull(url);
+        this.keyStoreFile = null;
+        return this;
+    }
+
+    /**
+     * Configures keystore parameters. The KeyStore must be of "jceks" type and
+     * contain all needed secret keys for the target database. Currently all
+     * keys must be protected with the same password.
+     *
+     * @param url                A URL to load keystore from.
+     * @param passwordForAllKeys A password that unlocks all keys in the keystore.
+     * @param encryptionKeyAlias The name of the key in the keystore that should be used
for
+     *                           encryption by default.
+     */
+    public CryptoModuleExtender keyStore(URL url, char[] passwordForAllKeys, String encryptionKeyAlias)
{
+        this.encryptionKeyAlias = encryptionKeyAlias;
+        this.keyPassword = passwordForAllKeys;
+        this.keyStoreUrl = Objects.requireNonNull(url);
+        this.keyStoreUrlString = null;
+        this.keyStoreFile = null;
+        return this;
+    }
+
+    public CryptoModuleExtender keySource(Class<? extends KeySource> type) {
+        this.keySourceType = Objects.requireNonNull(type);
+        this.keySource = null;
+        return this;
+    }
+
+    public CryptoModuleExtender keySource(KeySource keySource) {
+        this.keySourceType = null;
+        this.keySource = Objects.requireNonNull(keySource);
+        return this;
+    }
+
+    public CryptoModuleExtender compress() {
+        this.compress = true;
+        return this;
+    }
+
+    /**
+     * Enable authentication codes
+     */
+    public CryptoModuleExtender useHMAC() {
+        this.useHMAC = true;
+        return this;
+    }
+
+    /**
+     * Produces a module that can be used to start Cayenne runtime.
+     */
+    public Module module() {
+
+        return new Module() {
+
+            @Override
+            public void configure(Binder binder) {
+
+                MapBuilder<String> props = CryptoModule.contributeProperties(binder);
+
+                if (cipherAlgoritm != null) {
+                    props.put(CryptoConstants.CIPHER_ALGORITHM, cipherAlgoritm);
+                }
+
+                if (cipherMode != null) {
+                    props.put(CryptoConstants.CIPHER_MODE, cipherMode);
+                }
+
+                String keyStoreUrl = keyStoreUrl();
+                if (keyStoreUrl != null) {
+                    props.put(CryptoConstants.KEYSTORE_URL, keyStoreUrl);
+                }
+
+                if (encryptionKeyAlias != null) {
+                    props.put(CryptoConstants.ENCRYPTION_KEY_ALIAS, encryptionKeyAlias);
+                }
+
+                if (compress) {
+                    props.put(CryptoConstants.COMPRESSION, "true");
+                }
+
+                if (useHMAC) {
+                    props.put(CryptoConstants.USE_HMAC, "true");
+                }
+
+                if (keyPassword != null) {
+                    CryptoModule.contributeCredentials(binder).put(CryptoConstants.KEY_PASSWORD,
keyPassword);
+                }
+
+                if (cipherFactoryType != null) {
+                    binder.bind(CipherFactory.class).to(cipherFactoryType);
+                }
+
+                if (valueTransformerFactoryType != null) {
+                    binder.bind(ValueTransformerFactory.class).to(valueTransformerFactoryType);
+                }
+
+                if (!extraDbToBytes.isEmpty()) {
+                    MapBuilder<BytesConverter<?>> dbToBytesBinder = CryptoModule.contributeDbToByteConverters(binder);
+                    for (Map.Entry<Integer, BytesConverter<?>> extraConverter
: extraDbToBytes.entrySet()) {
+                        dbToBytesBinder.put(extraConverter.getKey().toString(), extraConverter.getValue());
+                    }
+                }
+
+                if (!extraObjectToBytes.isEmpty()) {
+                    MapBuilder<BytesConverter<?>> objectToBytesBinder = CryptoModule.contributeObjectToByteConverters(binder);
+                    for (Map.Entry<String, BytesConverter<?>> extraConverter
: extraObjectToBytes.entrySet()) {
+                        objectToBytesBinder.put(extraConverter.getKey(), extraConverter.getValue());
+                    }
+                }
+
+                if (bytesTransformerFactoryType != null) {
+                    binder.bind(BytesTransformerFactory.class).to(bytesTransformerFactoryType);
+                }
+
+                if (keySource != null) {
+                    binder.bind(KeySource.class).toInstance(keySource);
+                } else if (keySourceType != null) {
+                    binder.bind(KeySource.class).to(keySourceType);
+                }
+
+                if (columnMapperPattern != null) {
+                    binder.bind(ColumnMapper.class).toInstance(new PatternColumnMapper(columnMapperPattern));
+                } else if (columnMapperType != null) {
+                    binder.bind(ColumnMapper.class).to(columnMapperType);
+                } else if (columnMapper != null) {
+                    binder.bind(ColumnMapper.class).toInstance(columnMapper);
+                }
+            }
+        };
+    }
+
+    protected String keyStoreUrl() {
+        if (this.keyStoreUrl != null) {
+            return this.keyStoreUrl.toExternalForm();
+        }
+
+        if (this.keyStoreUrlString != null) {
+            return this.keyStoreUrlString;
+        }
+
+        if (keyStoreFile != null) {
+            try {
+                return keyStoreFile.toURI().toURL().toExternalForm();
+            } catch (MalformedURLException e) {
+                throw new IllegalStateException("Invalid keyStore file", e);
+            }
+        }
+
+        return null;
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/78102cf3/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
index f6c4b20..f8a1358 100644
--- a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
+++ b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/CryptoModuleBuilderTest.java
@@ -38,8 +38,8 @@ public class CryptoModuleBuilderTest {
     public void testBuild_KeySource() {
 
         URL ksUrl = JceksKeySourceTest.class.getResource(JceksKeySourceTest.KS1_JCEKS);
-        Module m = new CryptoModuleBuilder().keyStore(ksUrl, JceksKeySourceTest.TEST_KEY_PASS,
"k1")
-                .valueTransformer(DefaultValueTransformerFactory.class).build();
+        Module m = new CryptoModuleExtender().keyStore(ksUrl, JceksKeySourceTest.TEST_KEY_PASS,
"k1")
+                .valueTransformer(DefaultValueTransformerFactory.class).module();
 
         Injector injector = DIBootstrap.createInjector(new CryptoModule(), m);
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/78102cf3/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Runtime_AES128_Base.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Runtime_AES128_Base.java
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Runtime_AES128_Base.java
index 8c5bfce..38e8daa 100644
--- a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Runtime_AES128_Base.java
+++ b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/Runtime_AES128_Base.java
@@ -62,7 +62,7 @@ public class Runtime_AES128_Base {
     protected Module createCryptoModule(boolean compress, boolean useHMAC) {
         URL keyStoreUrl = JceksKeySourceTest.class.getResource(JceksKeySourceTest.KS1_JCEKS);
 
-        CryptoModuleBuilder builder = CryptoModule
+        CryptoModuleExtender builder = CryptoModule
                 .builder()
                 .keyStore(keyStoreUrl, JceksKeySourceTest.TEST_KEY_PASS, "k3");
 
@@ -73,7 +73,7 @@ public class Runtime_AES128_Base {
             builder.useHMAC();
         }
 
-        return builder.build();
+        return builder.module();
     }
 
 }


Mime
View raw message