/**
 * Copyright 2010 CosmoCode GmbH
 *
 * 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.
 */

package de.cosmocode.palava.jta;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.name.Named;

import de.cosmocode.palava.core.lifecycle.Disposable;
import de.cosmocode.palava.core.lifecycle.Initializable;
import de.cosmocode.palava.core.lifecycle.LifecycleException;
import de.cosmocode.palava.jmx.MBeanService;
import de.cosmocode.palava.jndi.JNDIContextBinder;

/**
 * Binds the Bitronix JTA provider as the JTA manager.
 * 
 * @author Tobias Sarnowski
 */
final class JtaLoader implements Initializable, Disposable {    
    private static final Logger LOG = LoggerFactory.getLogger(JtaLoader.class);

    private JNDIContextBinder jndiContextBinder;
    private MBeanService mBeanService;
    private JtaProvider jtaProvider;

    private String manager = JtaConfig.DEFAULT_MANAGER;
    private String user = JtaConfig.DEFAULT_USER;

    private final TransactionCounter counter = new TransactionCounter();

    @Inject
    public JtaLoader(
        JNDIContextBinder jndiContextBinder,
        MBeanService mBeanService,
        JtaProvider jtaProvider) {
        
        this.jndiContextBinder = jndiContextBinder;
        this.mBeanService = mBeanService;
        this.jtaProvider = jtaProvider;
    }

    @Inject(optional = true)
    void setManager(@Named(JtaConfig.MANAGER) String manager) {
        this.manager = Preconditions.checkNotNull(manager, "Manager");
    }

    @Inject(optional = true)
    void setUser(@Named(JtaConfig.USER) String user) {
        this.user = Preconditions.checkNotNull(user, "User");
    }

    @Override
    public void initialize() throws LifecycleException {
        try {
            final TransactionManager tm = new TransactionManagerCounter(jtaProvider.getTransactionManager(), counter);
            final UserTransaction tx = new UserTransactionCounter(jtaProvider.getUserTransaction(), counter);

            LOG.info("Binding TransactionManager to {} [{}]", manager, tm);
            jndiContextBinder.bind(manager, tm, TransactionManager.class);

            LOG.info("Binding UserTransaction to {} [{}]", user, tx);
            jndiContextBinder.bind(user, tx, UserTransaction.class);

            mBeanService.register(counter);

        /* CHECKSTYLE:OFF */
        } catch (Exception e) {
        /* CHECKSTYLE:ON */
            throw new LifecycleException(e);
        }
    }

    @Override
    public void dispose() throws LifecycleException {
        mBeanService.unregister(counter);
    }
    
}

@@ -631,154 +631,8 @@
 a;%0A%0A
-import javax.transaction.TransactionManager;%0Aimport javax.transaction.UserTransaction;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 impo
@@ -672,16 +672,16 @@
 itions;%0A
+
 import c
@@ -742,17 +742,16 @@
 .Named;%0A
-%0A
 import d
@@ -993,19 +993,19 @@
 a.jndi.J
-NDI
+ndi
 ContextB
@@ -1009,16 +1009,168 @@
 xtBinder
+Utility;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport javax.transaction.TransactionManager;%0Aimport javax.transaction.UserTransaction
 ;%0A%0A/**%0A 
@@ -1418,19 +1418,19 @@
 rivate J
-NDI
+ndi
 ContextB
@@ -1426,32 +1426,39 @@
 ndiContextBinder
+Utility
  jndiContextBind
@@ -1455,24 +1455,31 @@
 ontextBinder
+Utility
 ;%0A    privat
@@ -1769,19 +1769,19 @@
        J
-NDI
+ndi
 ContextB
@@ -1785,16 +1785,23 @@
 xtBinder
+Utility
  jndiCon
@@ -1810,16 +1810,23 @@
 xtBinder
+Utility
 ,%0A      
@@ -1889,24 +1889,16 @@
 ider) %7B%0A
-        
 %0A       
@@ -1920,16 +1920,23 @@
 xtBinder
+Utility
  = jndiC
@@ -1947,16 +1947,23 @@
 xtBinder
+Utility
 ;%0A      
@@ -2785,24 +2785,31 @@
 ontextBinder
+Utility
 .bind(manage
@@ -2913,16 +2913,16 @@
 r, tx);%0A
-
         
@@ -2942,16 +2942,23 @@
 xtBinder
+Utility
 .bind(us

use new jndi binder utility