Author: iocanel
Date: Fri Oct 29 14:01:14 2010
New Revision: 1028750

URL: http://svn.apache.org/viewvc?rev=1028750&view=rev
Log:
[KARAF-188] Added a BackingEngine and BackingEngineFactory per LoginModule.

Added:
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineFactory.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineService.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngineFactory.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCUtils.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngineFactory.java
Modified:
    karaf/trunk/jaas/modules/pom.xml
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/AbstractKarafLoginModule.java
    
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCLoginModule.java
    
karaf/trunk/jaas/modules/src/main/resources/OSGI-INF/blueprint/karaf-jaas-module.xml

Modified: karaf/trunk/jaas/modules/pom.xml
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/pom.xml?rev=1028750&r1=1028749&r2=1028750&view=diff
==============================================================================
--- karaf/trunk/jaas/modules/pom.xml (original)
+++ karaf/trunk/jaas/modules/pom.xml Fri Oct 29 14:01:14 2010
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
 
     <!--
 
@@ -44,6 +45,10 @@
             <groupId>org.apache.karaf.jaas</groupId>
             <artifactId>org.apache.karaf.jaas.config</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.jaas</groupId>
+            <artifactId>org.apache.karaf.jaas.boot</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>org.apache.karaf</groupId>

Modified: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/AbstractKarafLoginModule.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/AbstractKarafLoginModule.java?rev=1028750&r1=1028749&r2=1028750&view=diff
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/AbstractKarafLoginModule.java
 (original)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/AbstractKarafLoginModule.java
 Fri Oct 29 14:01:14 2010
@@ -14,30 +14,24 @@
  */
 package org.apache.karaf.jaas.modules;
 
-import java.security.Principal;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import org.apache.karaf.jaas.modules.encryption.EncryptionSupport;
+import org.osgi.framework.BundleContext;
 
 import javax.security.auth.Subject;
 import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.login.LoginException;
 import javax.security.auth.spi.LoginModule;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
+import java.security.Principal;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 
 
 /**
  * <p>
  * Abstract JAAS login module extended by all Karaf Login Modules.
  * </p>
- * 
+ *
  * @author iocanel, jbonofre
  */
 public abstract class AbstractKarafLoginModule implements LoginModule {
@@ -51,15 +45,13 @@ public abstract class AbstractKarafLogin
 
     protected String rolePolicy;
     protected String roleDiscriminator;
-    
-    /** the bundle context is required to use the encryption service */
-    protected BundleContext bundleContext;
 
-    private Encryption encryption;
-    private String encryptionPrefix;
-    private String encryptionSuffix;
+    /**
+     * the bundle context is required to use the encryption service
+     */
+    protected BundleContext bundleContext;
 
-    private static final Log LOG = 
LogFactory.getLog(AbstractKarafLoginModule.class);
+    private EncryptionSupport encryptionSupport;
 
     public boolean commit() throws LoginException {
         RolePolicy policy = RolePolicy.getPolicy(rolePolicy);
@@ -84,76 +76,15 @@ public abstract class AbstractKarafLogin
         this.debug = Boolean.parseBoolean((String) options.get("debug"));
         // the bundle context is set in the Config JaasRealm by default
         this.bundleContext = (BundleContext) 
options.get(BundleContext.class.getName());
+        encryptionSupport = new EncryptionSupport(options);
     }
 
-    public Encryption getEncryption() {
-        if (encryption == null) {
-            Map<String,String> encOpts = new HashMap<String,String>();
-            for (String key : options.keySet()) {
-                if (key.startsWith("encryption.")) {
-                    encOpts.put(key.substring("encryption.".length()), 
options.get(key).toString());
-                }
-            }
-            encryptionPrefix = encOpts.remove("prefix");
-            encryptionSuffix = encOpts.remove("suffix");
-            boolean enabled = Boolean.parseBoolean(encOpts.remove("enabled"));
-            if (!enabled) {
-                if (debug) {
-                    LOG.debug("Encryption is disabled.");
-                }
-            } else {
-                String name = encOpts.remove("name");
-                if (debug) {
-                    if (name != null && name.length() > 0) {
-                        LOG.debug("Encryption is enabled. Using service " + 
name + " with options " + encOpts);
-                    } else {
-                        LOG.debug("Encryption is enabled. Using options " + 
encOpts);
-                    }
-                }
-                // lookup the encryption service reference
-                ServiceReference[] encryptionServiceReferences;
-                try {
-                    encryptionServiceReferences = 
bundleContext.getServiceReferences(
-                                EncryptionService.class.getName(),
-                                name != null && name.length() > 0 ? "(name=" + 
name + ")" : null);
-                } catch (InvalidSyntaxException e) {
-                    throw new IllegalStateException("The encryption service 
filter is not well formed.", e);
-                }
-                if (encryptionServiceReferences.length == 0) {
-                    if (name != null && name.length() > 0) {
-                        throw new IllegalStateException("Encryption service " 
+ name + " not found. Please check that the encryption service is correctly set 
up.");
-                    } else {
-                        throw new IllegalStateException("No encryption service 
found. Please install the Karaf encryption feature and check that the 
encryption algorithm is supported..");
-                    }
-                }
-                Arrays.sort(encryptionServiceReferences);
-                for (ServiceReference ref : encryptionServiceReferences) {
-                    try {
-                        EncryptionService encryptionService = 
(EncryptionService) bundleContext.getService(ref);
-                        if (encryptionService != null) {
-                            try {
-                                encryption = 
encryptionService.createEncryption(encOpts);
-                                if (encryption != null) {
-                                    break;
-                                }
-                            } finally {
-                                bundleContext.ungetService(ref);
-                            }
-                        }
-                    } catch (IllegalStateException e) {
-                         // continue
-                    }
-                }
-                if (encryption == null) {
-                    throw new IllegalStateException("No EncryptionService 
supporting the required options could be found.");
-                }
-            }
-        }
-        return encryption;
-    }
 
     public String getEncryptedPassword(String password) {
-        Encryption encryption = getEncryption();
+        Encryption encryption = encryptionSupport.getEncryption();
+        String encryptionPrefix = encryptionSupport.getEncryptionPrefix();
+        String encryptionSuffix = encryptionSupport.getEncryptionSuffix();
+
         if (encryption == null) {
             return password;
         } else {
@@ -172,11 +103,13 @@ public abstract class AbstractKarafLogin
                 return p;
             }
         }
-
     }
 
     public boolean checkPassword(String plain, String encrypted) {
-        Encryption encryption = getEncryption();
+        Encryption encryption = encryptionSupport.getEncryption();
+        String encryptionPrefix = encryptionSupport.getEncryptionPrefix();
+        String encryptionSuffix = encryptionSupport.getEncryptionSuffix();
+
         if (encryption == null) {
             return plain.equals(encrypted);
         } else {

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngine.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,54 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.modules;
+
+/**
+ * @author iocanel
+ */
+public interface BackingEngine {
+
+
+    /**
+     * Create a new User.
+     *
+     * @param username
+     * @param password
+     */
+    void addUser(String username, String password);
+
+    /**
+     * Delete User
+     *
+     * @param username
+     */
+    void deleteUser(String username);
+
+    /**
+     * Add a role to the user
+     *
+     * @param username
+     * @param role
+     */
+    void addRole(String username, String role);
+
+    /**
+     * Remove a role from a user.
+     *
+     * @param username
+     * @param role
+     */
+    void deleteRole(String username, String role);
+
+}

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineFactory.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineFactory.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineFactory.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineFactory.java
 Fri Oct 29 14:01:14 2010
@@ -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.karaf.jaas.modules;
+
+import java.util.Map;
+
+/**
+ * @author iocanel
+ */
+public interface BackingEngineFactory {
+
+    /**
+     * Returns the corresponding module class.
+     *
+     * @return
+     */
+    String getModuleClass();
+
+
+    /**
+     * Backing engine factory method.
+     *
+     * @param options
+     * @return
+     */
+    BackingEngine build(Map options);
+}

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineService.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineService.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineService.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/BackingEngineService.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.karaf.jaas.modules;
+
+import org.apache.karaf.jaas.boot.ProxyLoginModule;
+
+import javax.security.auth.login.AppConfigurationEntry;
+import java.util.List;
+
+/**
+ * @author iocanel
+ */
+public class BackingEngineService {
+
+    private List<BackingEngineFactory> engineFactories;
+
+    public BackingEngine get(AppConfigurationEntry entry) {
+
+        if (engineFactories != null) {
+            for (BackingEngineFactory factory : engineFactories) {
+                String loginModuleClass = (String) 
entry.getOptions().get(ProxyLoginModule.PROPERTY_MODULE);
+                if (factory.getModuleClass().equals(loginModuleClass)) {
+                    return factory.build(entry.getOptions());
+                }
+            }
+        }
+        return null;
+    }
+
+    public List<BackingEngineFactory> getEngineFactories() {
+        return engineFactories;
+    }
+
+    public void setEngineFactories(List<BackingEngineFactory> engineFactories) 
{
+        this.engineFactories = engineFactories;
+    }
+}

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,135 @@
+/*
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.karaf.jaas.modules.encryption;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.karaf.jaas.modules.Encryption;
+import org.apache.karaf.jaas.modules.EncryptionService;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author iocanel
+ */
+public class EncryptionSupport {
+
+    private static final Log LOG = LogFactory.getLog(EncryptionSupport.class);
+
+    private BundleContext bundleContext;
+
+    private Encryption encryption;
+    private String encryptionPrefix;
+    private String encryptionSuffix;
+    protected Map<String, ?> options;
+
+    protected boolean debug;
+
+
+    public EncryptionSupport(Map<String, ?> options) {
+        this.options = options;
+        this.debug = Boolean.parseBoolean((String) options.get("debug"));
+        // the bundle context is set in the Config JaasRealm by default
+        this.bundleContext = (BundleContext) 
options.get(BundleContext.class.getName());
+    }
+
+    public Encryption getEncryption() {
+        if (encryption == null) {
+            Map<String, String> encOpts = new HashMap<String, String>();
+            for (String key : options.keySet()) {
+                if (key.startsWith("encryption.")) {
+                    encOpts.put(key.substring("encryption.".length()), 
options.get(key).toString());
+                }
+            }
+            encryptionPrefix = encOpts.remove("prefix");
+            encryptionSuffix = encOpts.remove("suffix");
+            boolean enabled = Boolean.parseBoolean(encOpts.remove("enabled"));
+
+            if (!enabled) {
+                if (debug) {
+                    LOG.debug("Encryption is disabled.");
+                }
+            } else {
+                String name = encOpts.remove("name");
+                if (debug) {
+                    if (name != null && name.length() > 0) {
+                        LOG.debug("Encryption is enabled. Using service " + 
name + " with options " + encOpts);
+                    } else {
+                        LOG.debug("Encryption is enabled. Using options " + 
encOpts);
+                    }
+                }
+                // lookup the encryption service reference
+                ServiceReference[] encryptionServiceReferences;
+                try {
+                    encryptionServiceReferences = 
bundleContext.getServiceReferences(
+                            EncryptionService.class.getName(),
+                            name != null && name.length() > 0 ? "(name=" + 
name + ")" : null);
+                } catch (InvalidSyntaxException e) {
+                    throw new IllegalStateException("The encryption service 
filter is not well formed.", e);
+                }
+                if (encryptionServiceReferences.length == 0) {
+                    if (name != null && name.length() > 0) {
+                        throw new IllegalStateException("Encryption service " 
+ name + " not found. Please check that the encryption service is correctly set 
up.");
+                    } else {
+                        throw new IllegalStateException("No encryption service 
found. Please install the Karaf encryption feature and check that the 
encryption algorithm is supported..");
+                    }
+                }
+                Arrays.sort(encryptionServiceReferences);
+                for (ServiceReference ref : encryptionServiceReferences) {
+                    try {
+                        EncryptionService encryptionService = 
(EncryptionService) bundleContext.getService(ref);
+                        if (encryptionService != null) {
+                            try {
+                                encryption = 
encryptionService.createEncryption(encOpts);
+                                if (encryption != null) {
+                                    break;
+                                }
+                            } finally {
+                                bundleContext.ungetService(ref);
+                            }
+                        }
+                    } catch (IllegalStateException e) {
+                        // continue
+                    }
+                }
+                if (encryption == null) {
+                    throw new IllegalStateException("No EncryptionService 
supporting the required options could be found.");
+                }
+            }
+        }
+        return encryption;
+    }
+
+    public String getEncryptionSuffix() {
+        return encryptionSuffix;
+    }
+
+    public void setEncryptionSuffix(String encryptionSuffix) {
+        this.encryptionSuffix = encryptionSuffix;
+    }
+
+    public String getEncryptionPrefix() {
+        return encryptionPrefix;
+    }
+
+    public void setEncryptionPrefix(String encryptionPrefix) {
+        this.encryptionPrefix = encryptionPrefix;
+    }
+}

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngine.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.karaf.jaas.modules.jdbc;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.karaf.jaas.modules.BackingEngine;
+import org.apache.karaf.jaas.modules.encryption.EncryptionSupport;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+/**
+ * @author iocanel
+ */
+public class JDBCBackingEngine implements BackingEngine {
+
+    private static final Log LOG = LogFactory.getLog(JDBCBackingEngine.class);
+
+    private DataSource dataSource;
+    private EncryptionSupport encryptionSupport;
+
+    private static final String MSG_CONNECTION_CLOSE_FAILED = "Failed to 
clearly close connection to the database:";
+
+    private String addUserStatement = "INSERT INTO USERS VALUES(?,?)";
+    private String addRoleStatement = "INSERT INTO ROLES VALUES(?,?)";
+    private String deleteRoleStatement = "DELETE FROM ROLES WHERE USERNAME=? 
AND ROLE=?";
+    private String deleteAllUserRolesStatement = "DELETE FROM ROLES WHERE 
USERNAME=?";
+    private String deleteUserStatement = "DELETE FROM USERS WHERE USERNAME=?";
+
+
+    /**
+     * Constructor
+     *
+     * @param dataSource
+     */
+    public JDBCBackingEngine(DataSource dataSource) {
+        this.dataSource = dataSource;
+    }
+
+    public JDBCBackingEngine(DataSource dataSource, EncryptionSupport 
encryptionSupport) {
+        this.dataSource = dataSource;
+        this.encryptionSupport = encryptionSupport;
+    }
+
+    /**
+     * Adds a new user.
+     *
+     * @param username
+     * @param password
+     */
+    public void addUser(String username, String password) {
+        Connection connection = null;
+        PreparedStatement statement = null;
+
+        String newPassword = password;
+
+        //If encryption support is enabled, encrypt password
+        if (encryptionSupport != null && encryptionSupport.getEncryption() != 
null) {
+            newPassword = 
encryptionSupport.getEncryption().encryptPassword(password);
+        }
+
+        if (dataSource != null) {
+
+            try {
+                connection = dataSource.getConnection();
+                statement = connection.prepareStatement(addUserStatement);
+                statement.setString(1, username);
+                statement.setString(2, newPassword);
+                int rows = statement.executeUpdate();
+
+                if (!connection.getAutoCommit()) {
+                    connection.commit();
+                }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(String.format("Executiong [%s], USERNAME=%s, 
PASSWORD=%s. %i rows affected.", addUserStatement, username, newPassword, 
rows));
+                }
+            } catch (SQLException e) {
+                LOG.error("Error executiong statement", e);
+            } finally {
+                try {
+                    if (statement != null) {
+                        statement.close();
+                    }
+                    if (connection != null) {
+                        connection.close();
+                    }
+                } catch (SQLException e) {
+                    LOG.warn(MSG_CONNECTION_CLOSE_FAILED, e);
+                }
+            }
+        }
+    }
+
+    /**
+     * Delete user by username.
+     *
+     * @param username
+     */
+    public void deleteUser(String username) {
+        Connection connection = null;
+        PreparedStatement userStatement = null;
+        PreparedStatement roleStatement = null;
+
+        if (dataSource != null) {
+
+            try {
+                connection = dataSource.getConnection();
+
+                //Remove from roles
+                roleStatement = 
connection.prepareStatement(deleteAllUserRolesStatement);
+                roleStatement.setString(1, username);
+                roleStatement.executeUpdate();
+
+                //Remove from users
+                userStatement = 
connection.prepareStatement(deleteUserStatement);
+                userStatement.setString(1, username);
+                int userRows = userStatement.executeUpdate();
+
+                if (!connection.getAutoCommit()) {
+                    connection.commit();
+                }
+
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(String.format("Executiong [%s], USERNAME=%s. %i 
userRows affected.", deleteUserStatement, username, userRows));
+                }
+            } catch (SQLException e) {
+                LOG.error("Error executiong statement", e);
+            } finally {
+                try {
+                    if (userStatement != null) {
+                        userStatement.close();
+                    }
+                    if (roleStatement != null) {
+                        roleStatement.close();
+                    }
+                    if (connection != null) {
+                        connection.close();
+                    }
+                } catch (SQLException e) {
+                    LOG.warn(MSG_CONNECTION_CLOSE_FAILED, e);
+                }
+            }
+        }
+    }
+
+    /**
+     * Add a role to a user.
+     *
+     * @param username
+     * @param role
+     */
+    public void addRole(String username, String role) {
+        Connection connection = null;
+        PreparedStatement statement = null;
+
+        if (dataSource != null) {
+
+            try {
+                connection = dataSource.getConnection();
+                statement = connection.prepareStatement(addRoleStatement);
+                statement.setString(1, username);
+                statement.setString(2, role);
+                int rows = statement.executeUpdate();
+
+                if (!connection.getAutoCommit()) {
+                    connection.commit();
+                }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(String.format("Executiong [%s], USERNAME=%s, 
ROLE=%s. %i rows affected.", addRoleStatement, username, role, rows));
+                }
+            } catch (SQLException e) {
+                LOG.error("Error executiong statement", e);
+            } finally {
+                try {
+                    if (statement != null) {
+                        statement.close();
+                    }
+                    if (connection != null) {
+                        connection.close();
+                    }
+                } catch (SQLException e) {
+                    LOG.warn(MSG_CONNECTION_CLOSE_FAILED, e);
+                }
+            }
+        }
+    }
+
+    /**
+     * Remove role from user.
+     *
+     * @param username
+     * @param role
+     */
+    public void deleteRole(String username, String role) {
+        Connection connection = null;
+        PreparedStatement statement = null;
+
+        if (dataSource != null) {
+
+            try {
+                connection = dataSource.getConnection();
+                statement = connection.prepareStatement(deleteRoleStatement);
+                statement.setString(1, username);
+                statement.setString(2, role);
+                int rows = statement.executeUpdate();
+
+                if (!connection.getAutoCommit()) {
+                    connection.commit();
+                }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(String.format("Executiong [%s], USERNAME=%s, 
ROLE=%s. %i rows affected.", deleteRoleStatement, username, role, rows));
+                }
+            } catch (SQLException e) {
+                LOG.error("Error executing statement", e);
+            } finally {
+                try {
+                    if (statement != null) {
+                        statement.close();
+                    }
+                    if (connection != null) {
+                        connection.close();
+                    }
+                } catch (SQLException e) {
+                    LOG.warn(MSG_CONNECTION_CLOSE_FAILED, e);
+                }
+            }
+        }
+    }
+}

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngineFactory.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngineFactory.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngineFactory.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCBackingEngineFactory.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.karaf.jaas.modules.jdbc;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.karaf.jaas.modules.BackingEngine;
+import org.apache.karaf.jaas.modules.BackingEngineFactory;
+import org.apache.karaf.jaas.modules.encryption.EncryptionSupport;
+import org.osgi.framework.BundleContext;
+
+import javax.sql.DataSource;
+import java.util.Map;
+
+/**
+ * @author iocanel
+ */
+public class JDBCBackingEngineFactory implements BackingEngineFactory {
+
+    private static final Log LOG = 
LogFactory.getLog(JDBCBackingEngineFactory.class);
+
+    /**
+     * Build a Backing engine for the JDBCLoginModule.
+     *
+     * @param options
+     * @return
+     */
+    public BackingEngine build(Map options) {
+        JDBCBackingEngine instance = null;
+        String datasourceURL = (String) options.get(JDBCUtils.DATASOURCE);
+        BundleContext bundleContext = (BundleContext) 
options.get(BundleContext.class.getName());
+        try {
+            DataSource dataSource = (DataSource) 
JDBCUtils.createDatasource(bundleContext, datasourceURL);
+            EncryptionSupport encryptionSupport = new 
EncryptionSupport(options);
+            instance = new JDBCBackingEngine(dataSource, encryptionSupport);
+        } catch (Exception e) {
+            LOG.error("Error creating JDBCBackingEngine.", e);
+        }
+        return instance;
+    }
+
+    /**
+     * Returns the login module class, that this factory can build.
+     *
+     * @return
+     */
+    public String getModuleClass() {
+        return JDBCLoginModule.class.getName();
+    }
+}

Modified: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCLoginModule.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCLoginModule.java?rev=1028750&r1=1028749&r2=1028750&view=diff
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCLoginModule.java
 (original)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCLoginModule.java
 Fri Oct 29 14:01:14 2010
@@ -15,6 +15,18 @@
  */
 package org.apache.karaf.jaas.modules.jdbc;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.karaf.jaas.modules.AbstractKarafLoginModule;
+import org.apache.karaf.jaas.modules.RolePrincipal;
+import org.apache.karaf.jaas.modules.UserPrincipal;
+import org.apache.karaf.jaas.modules.properties.PropertiesLoginModule;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.*;
+import javax.security.auth.login.LoginException;
+import javax.sql.DataSource;
+import javax.sql.XADataSource;
 import java.io.IOException;
 import java.security.Principal;
 import java.sql.Connection;
@@ -23,94 +35,33 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.HashSet;
 import java.util.Map;
-import javax.naming.InitialContext;
-import javax.security.auth.Subject;
-import javax.security.auth.callback.Callback;
-import javax.security.auth.callback.CallbackHandler;
-import javax.security.auth.callback.NameCallback;
-import javax.security.auth.callback.PasswordCallback;
-import javax.security.auth.callback.UnsupportedCallbackException;
-import javax.security.auth.login.LoginException;
-import javax.sql.DataSource;
-import javax.sql.XADataSource;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.karaf.jaas.modules.AbstractKarafLoginModule;
-import org.apache.karaf.jaas.modules.RolePrincipal;
-import org.apache.karaf.jaas.modules.UserPrincipal;
-import org.apache.karaf.jaas.modules.properties.PropertiesLoginModule;
-import org.osgi.framework.ServiceReference;
 
 /**
- *
  * @author iocanel
  */
 public class JDBCLoginModule extends AbstractKarafLoginModule {
 
     private static final Log LOG = 
LogFactory.getLog(PropertiesLoginModule.class);
-    private static final String DATASOURCE = "datasource";
+
     private static final String PASSWORD_QUERY = "query.password";
     private static final String ROLE_QUERY = "query.role";
-    private static final String JNDI = "jndi:";
-    private static final String OSGI = "osgi:";
+
     private String datasourceURL;
     protected String passwordQuery = "SELECT PASSWORD FROM USERS WHERE 
USERNAME=?";
     protected String roleQuery = "SELECT ROLE FROM ROLES WHERE USERNAME=?";
 
     public void initialize(Subject subject, CallbackHandler callbackHandler, 
Map<String, ?> sharedState, Map<String, ?> options) {
         super.initialize(subject, callbackHandler, options);
-        datasourceURL = (String) options.get(DATASOURCE);
+        datasourceURL = (String) options.get(JDBCUtils.DATASOURCE);
         passwordQuery = (String) options.get(PASSWORD_QUERY);
         roleQuery = (String) options.get(ROLE_QUERY);
         if (datasourceURL == null || datasourceURL.trim().length() == 0) {
             LOG.error("No datasource was specified ");
-        } else if (!datasourceURL.startsWith(JNDI) && 
!datasourceURL.startsWith(OSGI)) {
+        } else if (!datasourceURL.startsWith(JDBCUtils.JNDI) && 
!datasourceURL.startsWith(JDBCUtils.OSGI)) {
             LOG.error("Invalid datasource lookup protocol");
         }
     }
 
-    /**
-     * Looks up a datasource from the url. The datasource can be passed either 
as jndi name or osgi ldap filter.
-     * @param url
-     * @return
-     * @throws Exception
-     */
-    public Object createDatasource(String url) throws Exception {
-        if (url == null) {
-            throw new Exception("Illegal datasource url format. Datasource URL 
cannot be null.");
-        } else if (url.trim().length() == 0) {
-            throw new Exception("Illegal datasource url format. Datasource URL 
cannot be empty.");
-        } else if (url.startsWith(JNDI)) {
-            String jndiName = url.substring(JNDI.length());
-            InitialContext ic = new InitialContext();
-            Object ds =  ic.lookup(jndiName);
-            return ds;
-        } else if (url.startsWith(OSGI)) {
-            String osgiFilter = url.substring(OSGI.length());
-            String clazz = null;
-            String filter = null;
-            String[] tokens = osgiFilter.split("/", 2);
-            if (tokens != null) {
-                if (tokens.length > 0) {
-                    clazz = tokens[0];
-                }
-                if (tokens.length > 1) {
-                    filter = tokens[1];
-                }
-            }
-            ServiceReference[] references = 
bundleContext.getServiceReferences(clazz, filter);
-            if (references != null) {
-                ServiceReference ref = references[0];
-                Object ds = bundleContext.getService(ref);
-                bundleContext.ungetService(ref);
-                return ds;
-            } else {
-                throw new Exception("Unable to find service reference for 
datasource: " + clazz + "/" + filter);
-            }
-        } else {
-            throw new Exception("Illegal datasource url format");
-        }
-    }
 
     public boolean login() throws LoginException {
         Connection connection = null;
@@ -144,7 +95,7 @@ public class JDBCLoginModule extends Abs
         principals = new HashSet<Principal>();
 
         try {
-            Object credentialsDatasource = createDatasource(datasourceURL);
+            Object credentialsDatasource = 
JDBCUtils.createDatasource(bundleContext, datasourceURL);
 
             if (credentialsDatasource == null) {
                 throw new LoginException("Cannot obtain data source:" + 
datasourceURL);

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCUtils.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCUtils.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCUtils.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/jdbc/JDBCUtils.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.karaf.jaas.modules.jdbc;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+import javax.naming.InitialContext;
+
+/**
+ * @author iocanel
+ */
+public final class JDBCUtils {
+
+    public static final String DATASOURCE = "datasource";
+    public static final String JNDI = "jndi:";
+    public static final String OSGI = "osgi:";
+
+    private JDBCUtils() {
+
+    }
+
+    /**
+     * Looks up a datasource from the url. The datasource can be passed either 
as jndi name or osgi ldap filter.
+     *
+     * @param url
+     * @return
+     * @throws Exception
+     */
+    public static Object createDatasource(BundleContext bc, String url) throws 
Exception {
+        if (url == null) {
+            throw new Exception("Illegal datasource url format. Datasource URL 
cannot be null.");
+        } else if (url.trim().length() == 0) {
+            throw new Exception("Illegal datasource url format. Datasource URL 
cannot be empty.");
+        } else if (url.startsWith(JNDI)) {
+            String jndiName = url.substring(JNDI.length());
+            InitialContext ic = new InitialContext();
+            return ic.lookup(jndiName);
+        } else if (url.startsWith(OSGI)) {
+            String osgiFilter = url.substring(OSGI.length());
+            String clazz = null;
+            String filter = null;
+            String[] tokens = osgiFilter.split("/", 2);
+            if (tokens != null) {
+                if (tokens.length > 0) {
+                    clazz = tokens[0];
+                }
+                if (tokens.length > 1) {
+                    filter = tokens[1];
+                }
+            }
+            ServiceReference[] references = bc.getServiceReferences(clazz, 
filter);
+            if (references != null) {
+                ServiceReference ref = references[0];
+                Object ds = bc.getService(ref);
+                bc.ungetService(ref);
+                return ds;
+            } else {
+                throw new Exception("Unable to find service reference for 
datasource: " + clazz + "/" + filter);
+            }
+        } else {
+            throw new Exception("Illegal datasource url format");
+        }
+    }
+}

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngine.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.karaf.jaas.modules.properties;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.karaf.jaas.modules.BackingEngine;
+import org.apache.karaf.jaas.modules.encryption.EncryptionSupport;
+import org.apache.karaf.util.Properties;
+
+
+/**
+ * @author iocanel
+ */
+public class PropertiesBackingEngine implements BackingEngine {
+
+    private static final Log LOG = 
LogFactory.getLog(PropertiesBackingEngine.class);
+
+    private Properties users;
+    private EncryptionSupport encryptionSupport;
+
+    /**
+     * Constructor
+     *
+     * @param users
+     */
+    public PropertiesBackingEngine(Properties users) {
+        this.users = users;
+    }
+
+    public PropertiesBackingEngine(Properties users, EncryptionSupport 
encryptionSupport) {
+        this.users = users;
+        this.encryptionSupport = encryptionSupport;
+    }
+
+    /**
+     * Add a user.
+     *
+     * @param username
+     * @param password
+     */
+    public void addUser(String username, String password) {
+        String[] infos = null;
+        StringBuffer userInfoBuffer = new StringBuffer();
+
+        String newPassword = password;
+
+        //If encryption support is enabled, encrypt password
+        if (encryptionSupport != null && encryptionSupport.getEncryption() != 
null) {
+            newPassword = 
encryptionSupport.getEncryption().encryptPassword(password);
+        }
+
+        String userInfos = users.get(username);
+
+        //If user already exists, update password
+        if (userInfos != null && userInfos.length() > 0) {
+            infos = userInfos.split(",");
+            userInfoBuffer.append(newPassword);
+
+            for (int i = 1; i < infos.length; i++) {
+                userInfoBuffer.append(",");
+                userInfoBuffer.append(infos[i]);
+            }
+            String newUserInfo = userInfoBuffer.toString();
+            users.put(username, newUserInfo);
+        } else {
+            users.put(username, newPassword);
+        }
+
+        try {
+            users.save();
+        } catch (Exception ex) {
+            LOG.error("Cannot update users file,", ex);
+        }
+    }
+
+    /**
+     * Delete a User.
+     *
+     * @param username
+     */
+    public void deleteUser(String username) {
+        users.remove(username);
+
+    }
+
+    /**
+     * Add a role to a User.
+     *
+     * @param username
+     * @param role
+     */
+    public void addRole(String username, String role) {
+        String userInfos = users.get(username);
+        if (userInfos != null) {
+            String newUserInfos = userInfos + "," + role;
+            users.put(username, newUserInfos);
+        }
+        try {
+            users.save();
+        } catch (Exception ex) {
+            LOG.error("Cannot update users file,", ex);
+        }
+    }
+
+    /**
+     * Delete a Role form User.
+     *
+     * @param username
+     * @param role
+     */
+    public void deleteRole(String username, String role) {
+        String[] infos = null;
+        StringBuffer userInfoBuffer = new StringBuffer();
+
+        String userInfos = users.get(username);
+
+        //If user already exists, remove the role
+        if (userInfos != null && userInfos.length() > 0) {
+            infos = userInfos.split(",");
+            String password = infos[0];
+            userInfoBuffer.append(password);
+
+            for (int i = 1; i < infos.length; i++) {
+                if (infos[i] != null && !infos[i].equals(role)) {
+                    userInfoBuffer.append(",");
+                    userInfoBuffer.append(infos[i]);
+                }
+            }
+            String newUserInfo = userInfoBuffer.toString();
+            users.put(username, newUserInfo);
+        }
+
+        try {
+            users.save();
+        } catch (Exception ex) {
+            LOG.error("Cannot update users file,", ex);
+        }
+    }
+}

Added: 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngineFactory.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngineFactory.java?rev=1028750&view=auto
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngineFactory.java
 (added)
+++ 
karaf/trunk/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesBackingEngineFactory.java
 Fri Oct 29 14:01:14 2010
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.karaf.jaas.modules.properties;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.karaf.jaas.modules.BackingEngine;
+import org.apache.karaf.jaas.modules.BackingEngineFactory;
+import org.apache.karaf.jaas.modules.encryption.EncryptionSupport;
+import org.apache.karaf.util.Properties;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * @author iocanel
+ */
+public class PropertiesBackingEngineFactory implements BackingEngineFactory {
+
+    private static final String USER_FILE = "users";
+    private static final Log LOG = 
LogFactory.getLog(PropertiesBackingEngineFactory.class);
+
+    /**
+     * Builds the Backing Engine
+     *
+     * @param options
+     * @return
+     */
+    public BackingEngine build(Map options) {
+        PropertiesBackingEngine engine = null;
+        String usersFile = (String) options.get(USER_FILE);
+
+        File f = new File(usersFile);
+        Properties users;
+        try {
+            users = new Properties(f);
+            EncryptionSupport encryptionSupport = new 
EncryptionSupport(options);
+            engine = new PropertiesBackingEngine(users, encryptionSupport);
+        } catch (IOException ioe) {
+            LOG.warn("Cannot open users file:" + usersFile);
+        } finally {
+            return engine;
+        }
+    }
+
+    /**
+     * Returns the login module class, that this factory can build.
+     *
+     * @return
+     */
+    public String getModuleClass() {
+        return PropertiesLoginModule.class.getName();
+    }
+}

Modified: 
karaf/trunk/jaas/modules/src/main/resources/OSGI-INF/blueprint/karaf-jaas-module.xml
URL: 
http://svn.apache.org/viewvc/karaf/trunk/jaas/modules/src/main/resources/OSGI-INF/blueprint/karaf-jaas-module.xml?rev=1028750&r1=1028749&r2=1028750&view=diff
==============================================================================
--- 
karaf/trunk/jaas/modules/src/main/resources/OSGI-INF/blueprint/karaf-jaas-module.xml
 (original)
+++ 
karaf/trunk/jaas/modules/src/main/resources/OSGI-INF/blueprint/karaf-jaas-module.xml
 Fri Oct 29 14:01:14 2010
@@ -23,21 +23,21 @@
            
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0";>
 
     <type-converters>
-        <bean 
class="org.apache.karaf.jaas.modules.properties.PropertiesConverter" />
+        <bean 
class="org.apache.karaf.jaas.modules.properties.PropertiesConverter"/>
     </type-converters>
 
     <!-- Allow usage of System properties, especially the karaf.base property 
-->
     <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]"/>
-    
+
     <!-- AdminConfig property place holder for the org.apache.karaf.jaas  -->
     <cm:property-placeholder persistent-id="org.apache.karaf.jaas" 
update-strategy="reload">
         <cm:default-properties>
-            <cm:property name="encryption.name" value="" />
-            <cm:property name="encryption.enabled" value="false" />
-            <cm:property name="encryption.prefix" value="{CRYPT}" />
-            <cm:property name="encryption.suffix" value="{CRYPT}" />
-            <cm:property name="encryption.algorithm" value="MD5" />
-            <cm:property name="encryption.encoding" value="hexadeciman" />
+            <cm:property name="encryption.name" value=""/>
+            <cm:property name="encryption.enabled" value="false"/>
+            <cm:property name="encryption.prefix" value="{CRYPT}"/>
+            <cm:property name="encryption.suffix" value="{CRYPT}"/>
+            <cm:property name="encryption.algorithm" value="MD5"/>
+            <cm:property name="encryption.encoding" value="hexadecimal"/>
         </cm:default-properties>
     </cm:property-placeholder>
 
@@ -53,6 +53,12 @@
         </jaas:module>
     </jaas:config>
 
+
+    <!-- The Backing Engine Factory Service for the PropertiesLoginModule -->
+    <service interface="org.apache.karaf.jaas.modules.BackingEngineFactory">
+        <bean 
class="org.apache.karaf.jaas.modules.properties.PropertiesBackingEngineFactory"/>
+    </service>
+
     <service interface="org.apache.karaf.jaas.modules.EncryptionService" 
ranking="-1">
         <service-properties>
             <entry key="name" value="basic"/>


Reply via email to