/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. package com.hazelcast.jca;
  17. import com.hazelcast.core.Hazelcast;
  18. import com.hazelcast.impl.ThreadContext;
  19. import javax.resource.ResourceException;
  20. import javax.resource.spi.*;
  21. import javax.security.auth.Subject;
  22. import javax.transaction.xa.XAResource;
  23. import java.io.PrintWriter;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.concurrent.atomic.AtomicInteger;
  27. public class ManagedConnectionImpl extends JcaBase implements ManagedConnection,
  28. javax.resource.cci.LocalTransaction, LocalTransaction {
  29. private final ConnectionImpl conn;
  30. private List<ConnectionEventListener> lsListeners = null;
  31. private PrintWriter printWriter = null;
  32. private static AtomicInteger idGen = new AtomicInteger();
  33. private transient final int id;
  34. public ManagedConnectionImpl() {
  35. conn = new ConnectionImpl(this);
  36. id = idGen.incrementAndGet();
  37. }
  38. public void associateConnection(Object arg0) throws ResourceException {
  39. log(this, "associateConnection: " + arg0);
  40. }
  41. public void cleanup() throws ResourceException {
  42. log(this, "cleanup");
  43. }
  44. public void destroy() throws ResourceException {
  45. log(this, "destroy");
  46. }
  47. public Object getConnection(Subject arg0, ConnectionRequestInfo arg1) throws ResourceException {
  48. log(this, "getConnection");
  49. return conn;
  50. }
  51. public LocalTransaction getLocalTransaction() throws ResourceException {
  52. log(this, "getLocalTransaction");
  53. return this;
  54. }
  55. public PrintWriter getLogWriter() throws ResourceException {
  56. return printWriter;
  57. }
  58. public void setLogWriter(PrintWriter printWriter) throws ResourceException {
  59. this.printWriter = printWriter;
  60. }
  61. public ManagedConnectionMetaData getMetaData() throws ResourceException {
  62. return null;
  63. }
  64. public XAResource getXAResource() throws ResourceException {
  65. log(this, "getXAResource");
  66. return null;
  67. }
  68. public void addConnectionEventListener(ConnectionEventListener listener) {
  69. log(this, "addConnectionEventListener");
  70. if (lsListeners == null)
  71. lsListeners = new ArrayList<ConnectionEventListener>();
  72. lsListeners.add(listener);
  73. }
  74. public void removeConnectionEventListener(ConnectionEventListener listener) {
  75. if (lsListeners == null)
  76. return;
  77. lsListeners.remove(listener);
  78. }
  79. public void begin() throws ResourceException {
  80. log(this, "txn.begin");
  81. Hazelcast.getTransaction().begin();
  82. fireConnectionEvent(ConnectionEvent.LOCAL_TRANSACTION_STARTED);
  83. }
  84. public void commit() throws ResourceException {
  85. log(this, "txn.commit");
  86. ThreadContext.get().getTransaction().commit();
  87. fireConnectionEvent(ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
  88. }
  89. public void rollback() throws ResourceException {
  90. log(this, "txn.rollback");
  91. ThreadContext.get().getTransaction().rollback();
  92. fireConnectionEvent(ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
  93. }
  94. public void fireConnectionEvent(int event) {
  95. if (lsListeners == null)
  96. return;
  97. ConnectionEvent connnectionEvent = new ConnectionEvent(this, event);
  98. connnectionEvent.setConnectionHandle(conn);
  99. for (ConnectionEventListener listener : lsListeners) {
  100. if (event == ConnectionEvent.LOCAL_TRANSACTION_STARTED) {
  101. listener.localTransactionStarted(connnectionEvent);
  102. } else if (event == ConnectionEvent.LOCAL_TRANSACTION_COMMITTED) {
  103. listener.localTransactionCommitted(connnectionEvent);
  104. } else if (event == ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK) {
  105. listener.localTransactionRolledback(connnectionEvent);
  106. } else if (event == ConnectionEvent.CONNECTION_CLOSED) {
  107. listener.connectionClosed(connnectionEvent);
  108. }
  109. }
  110. }
  111. @Override
  112. public String toString() {
  113. return "hazelcast.ManagedConnectionImpl [" + id + "]";
  114. }
  115. }