/**
 * Copyright (C) 2011, FuseSource Corp.  All rights reserved.
 * http://fusesource.com
 *
 * The software in this package is published under the terms of the
 * CDDL license a copy of which has been included with this distribution
 * in the license.txt file.
 */
package org.fusesource.fabric.jaas;

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.jaas.modules.properties.PropertiesBackingEngine;
import org.fusesource.fabric.api.FabricService;
import org.fusesource.fabric.service.FabricServiceImpl;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class ZookeeperBackingEngineFactory implements BackingEngineFactory {

    protected BundleContext bundleContext;

    protected FabricServiceImpl service;

    private static final transient Logger LOGGER = LoggerFactory.getLogger(ZookeeperBackingEngineFactory.class);

    @Override
    public String getModuleClass() {
        return ZookeeperLoginModule.class.getName();
    }

    @Override
    public BackingEngine build(Map options) {
        ZookeeperBackingEngine engine = null;
        this.bundleContext = (BundleContext) options.get(BundleContext.class.getName());
        ServiceReference ref = bundleContext.getServiceReference(FabricService.class.getName());
        service = (FabricServiceImpl) bundleContext.getService(ref);
        String path = (String)options.get("path");
        if (path == null) {
            path = ZookeeperBackingEngine.USERS_NODE;
        }
        try {
            ZookeeperProperties users = new ZookeeperProperties(service.getZooKeeper(), path);
            EncryptionSupport encryptionSupport = new EncryptionSupport(options);
            engine = new ZookeeperBackingEngine(users, encryptionSupport);
        } catch (Exception e) {
            LOGGER.warn("Cannot initialize engine", e);
        } finally {
            return engine;
        }
    }
}

@@ -967,20 +967,16 @@
 cService
-Impl
  service
@@ -977,16 +977,16 @@
 ervice;%0A
+
 %0A    pri
@@ -1390,32 +1390,73 @@
 ass.getName());%0A
+        if (bundleContext != null) %7B%0A    
         ServiceR
@@ -1544,16 +1544,20 @@
         
+    
 service 
@@ -1609,16 +1609,26 @@
 e(ref);%0A
+        %7D%0A
         
@@ -1840,23 +1840,44 @@
 perties(
+((FabricServiceImpl)
 service
+)
 .getZooK
@@ -2184,16 +2184,16 @@
 engine;%0A
-
         
@@ -2199,11 +2199,103 @@
  %7D%0A    %7D
+%0A%0A    public void setService(FabricService service) %7B%0A        this.service = service;%0A%0A    %7D
 %0A%7D%0A

allow backing engine to be used outside karaf