/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. 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.wso2.carbon.transaction.manager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.transaction.manager.exception.TransactionManagerException;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.util.ArrayList;
import java.util.List;

/**
 * @scr.component name="transactionmanager.component" immediate="true"
 * @scr.reference name="transactionmanager" interface="javax.transaction.TransactionManager"
 * cardinality="1..1" policy="dynamic" bind="setTransactionManager"  unbind="unsetTransactionManager"
 * @scr.reference name="usertransaction" interface="javax.transaction.UserTransaction"
 * cardinality="1..1" policy="dynamic" bind="setUserTransaction"  unbind="unsetUserTransaction"
 * @scr.reference name="user.realmservice.default"
 * interface="org.wso2.carbon.user.core.service.RealmService" cardinality="0..1" policy="dynamic"
 * bind="setRealmService" unbind="unsetRealmService"
 */
public class TransactionManagerComponent {

    private static Log log = LogFactory.getLog(TransactionManagerComponent.class);

    private static TransactionManager txManager;

    private static UserTransaction userTransaction;

    private static RealmService realmService;

    /* class level lock for controlling synchronized access to static variables */
    private static Object txManagerComponentLock = new Object();

    protected void activate(ComponentContext ctxt) throws TransactionManagerException {
        BundleContext bundleContext = ctxt.getBundleContext();
        bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(),
                new TransactionManagerAxis2ConfigurationContextObserver(), null);

        //Register transaction-manager with JNDI for all available tenants.
        List<Integer> tenants = this.getAllTenantIds();
        for (int tid : tenants) {
            bindTransactionManagerWithJNDIForTenant(tid);
        }
        if (log.isDebugEnabled()) {
            log.debug("Transaction Manager bundle is activated ");
        }
        bundleContext.registerService(TransactionManagerDummyService.class.getName(),
                new TransactionManagerDummyService(), null);
    }

    protected void deactivate(ComponentContext ctxt) {
        if (log.isDebugEnabled()) {
            log.debug("Transaction Manager bundle is deactivated ");
        }
    }

    protected void setTransactionManager(TransactionManager txManager) {
        synchronized (txManagerComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Setting the Transaction Manager Service");
            }
            TransactionManagerComponent.txManager = txManager;
        }
    }

    protected void unsetTransactionManager(TransactionManager txManager) {
        synchronized (txManagerComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Unsetting the Transaction Manager Service");
            }
            TransactionManagerComponent.txManager = null;
        }
    }

    public static TransactionManager getTransactionManager() {
        return txManager;
    }

    protected void setUserTransaction(UserTransaction userTransaction) {
        synchronized (txManagerComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Setting the UserTransaction Service");
            }
            TransactionManagerComponent.userTransaction = userTransaction;
        }
    }

    protected void unsetUserTransaction(UserTransaction userTransaction) {
        synchronized (txManagerComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Unsetting the UserTransaction Service");
            }
            TransactionManagerComponent.userTransaction = null;
        }
    }

    public static UserTransaction getUserTransaction() {
        return userTransaction;
    }

    private List<Integer> getAllTenantIds() throws TransactionManagerException {
        try {
            RealmService realmService = TransactionManagerComponent.getRealmService();
            if (realmService != null) {
                Tenant[] tenants = TransactionManagerComponent.getRealmService().getTenantManager().
                        getAllTenants();
                List<Integer> tids = new ArrayList<Integer>();
                for (Tenant tenant : tenants) {
                    tids.add(tenant.getId());
                }
                tids.add(MultitenantConstants.SUPER_TENANT_ID);
                return tids;
            }
            return new ArrayList<Integer>();
        } catch (UserStoreException e) {
            log.error(e);
            throw new TransactionManagerException("Error in listing all the tenants", e);
        }
    }

    protected void setRealmService(RealmService realmService) {
    	TransactionManagerComponent.realmService = realmService;
    }

    protected void unsetRealmService(RealmService realmService) {
    	TransactionManagerComponent.realmService = null;
    }

    public static RealmService getRealmService() {
    	return TransactionManagerComponent.realmService;
    }
    
    protected static void bindTransactionManagerWithJNDIForTenant(int tid) {
    	PrivilegedCarbonContext.startTenantFlow();
    	PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tid);

        try {
            Context currentCtx = PrivilegedCarbonContext.getThreadLocalCarbonContext().getJNDIContext();
            Context javaCtx = null;
            try {
                javaCtx = (Context) currentCtx.lookup("java:comp");
            } catch (NameNotFoundException ignore) {
                //ignore
            }
            if (javaCtx == null) {
                currentCtx = currentCtx.createSubcontext("java:comp");
            }

            Object txManager = null, userTx = null;
            try {
             txManager = currentCtx.lookup("java:comp/TransactionManager");
            } catch (NameNotFoundException ignore) {
                //ignore
            }
            try {
             userTx = currentCtx.lookup("java:comp/UserTransaction");
            } catch (NameNotFoundException ignore) {
                //ignore
            }
            if(txManager == null) {
                currentCtx.bind("TransactionManager", getTransactionManager());
            }
            if(userTx == null) {
                currentCtx.bind("UserTransaction", getUserTransaction());
            }
        } catch (Exception e) {
           log.error("Error in binding transaction manager for tenant: " + tid, e);
        } finally {
        	PrivilegedCarbonContext.endTenantFlow();
        }
    }
}

@@ -5185,32 +5185,91 @@
 %7B%0A        try %7B%0A
+            List%3CInteger%3E tids = new ArrayList%3CInteger%3E();%0A
             Real
@@ -5335,24 +5335,24 @@
 mService();%0A
-
             
@@ -5525,71 +5525,8 @@
 ();%0A
-                List%3CInteger%3E tids = new ArrayList%3CInteger%3E();%0A
     
@@ -5731,32 +5731,39 @@
 s;%0A            %7D
+ else %7B
 %0A            ret
@@ -5763,40 +5763,102 @@
     
-return new ArrayList%3CInteger%3E();
+    tids.add(MultitenantConstants.SUPER_TENANT_ID);%0A                return tids;%0A            %7D
 %0A   

Fix Transaction Manager (#314)