PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-ra/src/main/java/com/hazelcast/jca/ManagedConnectionImpl.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 136 lines | 100 code | 21 blank | 15 comment | 18 complexity | 1a008d6949f70d59d0292c69ac5c55f2 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.jca;
 18
 19import com.hazelcast.core.Hazelcast;
 20import com.hazelcast.impl.ThreadContext;
 21
 22import javax.resource.ResourceException;
 23import javax.resource.spi.*;
 24import javax.security.auth.Subject;
 25import javax.transaction.xa.XAResource;
 26import java.io.PrintWriter;
 27import java.util.ArrayList;
 28import java.util.List;
 29import java.util.concurrent.atomic.AtomicInteger;
 30
 31public class ManagedConnectionImpl extends JcaBase implements ManagedConnection,
 32        javax.resource.cci.LocalTransaction, LocalTransaction {
 33    private final ConnectionImpl conn;
 34    private List<ConnectionEventListener> lsListeners = null;
 35    private PrintWriter printWriter = null;
 36    private static AtomicInteger idGen = new AtomicInteger();
 37    private transient final int id;
 38
 39    public ManagedConnectionImpl() {
 40        conn = new ConnectionImpl(this);
 41        id = idGen.incrementAndGet();
 42    }
 43
 44    public void associateConnection(Object arg0) throws ResourceException {
 45        log(this, "associateConnection: " + arg0);
 46    }
 47
 48    public void cleanup() throws ResourceException {
 49        log(this, "cleanup");
 50    }
 51
 52    public void destroy() throws ResourceException {
 53        log(this, "destroy");
 54    }
 55
 56    public Object getConnection(Subject arg0, ConnectionRequestInfo arg1) throws ResourceException {
 57        log(this, "getConnection");
 58        return conn;
 59    }
 60
 61    public LocalTransaction getLocalTransaction() throws ResourceException {
 62        log(this, "getLocalTransaction");
 63        return this;
 64    }
 65
 66    public PrintWriter getLogWriter() throws ResourceException {
 67        return printWriter;
 68    }
 69
 70    public void setLogWriter(PrintWriter printWriter) throws ResourceException {
 71        this.printWriter = printWriter;
 72    }
 73
 74    public ManagedConnectionMetaData getMetaData() throws ResourceException {
 75        return null;
 76    }
 77
 78    public XAResource getXAResource() throws ResourceException {
 79        log(this, "getXAResource");
 80        return null;
 81    }
 82
 83    public void addConnectionEventListener(ConnectionEventListener listener) {
 84        log(this, "addConnectionEventListener");
 85        if (lsListeners == null)
 86            lsListeners = new ArrayList<ConnectionEventListener>();
 87        lsListeners.add(listener);
 88    }
 89
 90    public void removeConnectionEventListener(ConnectionEventListener listener) {
 91        if (lsListeners == null)
 92            return;
 93        lsListeners.remove(listener);
 94    }
 95
 96    public void begin() throws ResourceException {
 97        log(this, "txn.begin");
 98        Hazelcast.getTransaction().begin();
 99        fireConnectionEvent(ConnectionEvent.LOCAL_TRANSACTION_STARTED);
100    }
101
102    public void commit() throws ResourceException {
103        log(this, "txn.commit");
104        ThreadContext.get().getTransaction().commit();
105        fireConnectionEvent(ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
106    }
107
108    public void rollback() throws ResourceException {
109        log(this, "txn.rollback");
110        ThreadContext.get().getTransaction().rollback();
111        fireConnectionEvent(ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
112    }
113
114    public void fireConnectionEvent(int event) {
115        if (lsListeners == null)
116            return;
117        ConnectionEvent connnectionEvent = new ConnectionEvent(this, event);
118        connnectionEvent.setConnectionHandle(conn);
119        for (ConnectionEventListener listener : lsListeners) {
120            if (event == ConnectionEvent.LOCAL_TRANSACTION_STARTED) {
121                listener.localTransactionStarted(connnectionEvent);
122            } else if (event == ConnectionEvent.LOCAL_TRANSACTION_COMMITTED) {
123                listener.localTransactionCommitted(connnectionEvent);
124            } else if (event == ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK) {
125                listener.localTransactionRolledback(connnectionEvent);
126            } else if (event == ConnectionEvent.CONNECTION_CLOSED) {
127                listener.connectionClosed(connnectionEvent);
128            }
129        }
130    }
131
132    @Override
133    public String toString() {
134        return "hazelcast.ManagedConnectionImpl [" + id + "]";
135    }
136}