<commit_before>/*
 * IronJacamar, a Java EE Connector Architecture implementation
 * Copyright 2015, Red Hat Inc, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the Eclipse Public License 1.0 as
 * published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Eclipse
 * Public License for more details.
 *
 * You should have received a copy of the Eclipse Public License 
 * along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.ironjacamar.core.connectionmanager.listener;

import org.ironjacamar.common.api.metadata.common.FlushStrategy;
import org.ironjacamar.core.api.connectionmanager.pool.FlushMode;
import org.ironjacamar.core.connectionmanager.ConnectionManager;
import org.ironjacamar.core.connectionmanager.Credential;
import org.ironjacamar.core.connectionmanager.pool.ManagedConnectionPool;
import org.ironjacamar.core.spi.transaction.ConnectableResourceListener;
import org.ironjacamar.core.tracer.Tracer;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import javax.resource.ResourceException;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ManagedConnection;

/**
 * The abstract connection listener
 * 
 * @author <a href="<EMAIL>"><NAME></a>
 */
public abstract class AbstractConnectionListener implements ConnectionListener, ConnectableResourceListener
{
   /** The connection manager */
   protected ConnectionManager cm;
   
   /** The managed connection */
   private ManagedConnection mc;

   /** The managed connection pool*/
   private ManagedConnectionPool mcp;

   /** The credential */
   private Credential credential;
   
   /** The state */
   private AtomicInteger state;

   /** Connection handles */
   protected CopyOnWriteArraySet<Object> connectionHandles;
   
   /** Connection traces */
   protected Map<Object, Exception> connectionTraces;

   /** Last validated timestamp */
   private long validated;

   /** Last fromPool timestamp */
   private long fromPool;

   /** Last toPool timestamp */
   private long toPool;

   /** Flush strategy **/
   private FlushStrategy flushStrategy;

   /**
    * Constructor
    * @param cm The connection manager
    * @param mc The managed connection
    * @param credential The credential
    * @param mcp The ManagedConnectionPool
    * @param flushStrategy The FlushStrategy
    */
   public AbstractConnectionListener(ConnectionManager cm, ManagedConnection mc, Credential credential,
         ManagedConnectionPool mcp, FlushStrategy flushStrategy)
   {
      this.cm = cm;
      this.mc = mc;
      this.mcp = mcp;
      this.flushStrategy = flushStrategy;
      this.credential = credential;
      this.state = new AtomicInteger(FREE);
      this.connectionHandles = new CopyOnWriteArraySet<Object>();

      if (cm.getConnectionManagerConfiguration().isTracking() != null &&
          cm.getConnectionManagerConfiguration().isTracking().booleanValue())
         this.connectionTraces = new HashMap<Object, Exception>();

      long timestamp = System.currentTimeMillis();

      this.validated = timestamp;
      this.fromPool = timestamp;
      this.toPool = timestamp;

      mc.addConnectionEventListener(this);
   }

   /**
    * {@inheritDoc}
    */
   public boolean changeState(int currentState, int newState)
   {
      return state.compareAndSet(currentState, newState);
   }

   /**
    * {@inheritDoc}
    */
   public int getState()
   {
      return state.get();
   }

   /**
    * {@inheritDoc}
    */
   public void setState(int state)
   {
      this.state.set(state);
   }

   /**
    * {@inheritDoc}
    */
   public Credential getCredential()
   {
      return credential;
   }

   /**
    * {@inheritDoc}
    */
   public void connectionClosed(ConnectionEvent event)
   {
      Object connection = event.getConnectionHandle();
      if (removeConnection(connection) && cm.getCachedConnectionManager() != null)
      {
         cm.getCachedConnectionManager().unregisterConnection(cm, this, connection);
      }

      if (connectionHandles.isEmpty() && !isEnlisted())
         cm.returnConnectionListener(this, false);
   }
   
   /**
    * {@inheritDoc}
    */
   public void connectionErrorOccurred(ConnectionEvent event)
   {
      Object connection = event.getConnectionHandle();
      if (removeConnection(connection) && cm.getCachedConnectionManager() != null)
      {
         cm.getCachedConnectionManager().unregisterConnection(cm, this, connection);
      }

      if (cm.getCachedConnectionManager() != null)
      {
         for (Object c : connectionHandles)
            cm.getCachedConnectionManager().unregisterConnection(cm, this, c);
      }

      clearConnections();
      
      haltCatchFire();
      
      cm.returnConnectionListener(this, true);

      if (flushStrategy == FlushStrategy.FAILING_CONNECTION_ONLY)
      {
         mcp.prefill();
      }
      else if (flushStrategy == FlushStrategy.INVALID_IDLE_CONNECTIONS)
      {
         mcp.flush(FlushMode.INVALID);
      }
      else if (flushStrategy == FlushStrategy.IDLE_CONNECTIONS)
      {
         mcp.flush(FlushMode.IDLE);
      }
      else if (flushStrategy == FlushStrategy.GRACEFULLY)
      {<commit_msg>Always remove handle from CCM<commit_after>/*
 * IronJacamar, a Java EE Connector Architecture implementation
 * Copyright 2015, Red Hat Inc, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the Eclipse Public License 1.0 as
 * published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Eclipse
 * Public License for more details.
 *
 * You should have received a copy of the Eclipse Public License 
 * along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.ironjacamar.core.connectionmanager.listener;

import org.ironjacamar.common.api.metadata.common.FlushStrategy;
import org.ironjacamar.core.api.connectionmanager.pool.FlushMode;
import org.ironjacamar.core.connectionmanager.ConnectionManager;
import org.ironjacamar.core.connectionmanager.Credential;
import org.ironjacamar.core.connectionmanager.pool.ManagedConnectionPool;
import org.ironjacamar.core.spi.transaction.ConnectableResourceListener;
import org.ironjacamar.core.tracer.Tracer;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import javax.resource.ResourceException;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ManagedConnection;

/**
 * The abstract connection listener
 * 
 * @author <a href="<EMAIL>"><NAME></a>
 */
public abstract class AbstractConnectionListener implements ConnectionListener, ConnectableResourceListener
{
   /** The connection manager */
   protected ConnectionManager cm;
   
   /** The managed connection */
   private ManagedConnection mc;

   /** The managed connection pool*/
   private ManagedConnectionPool mcp;

   /** The credential */
   private Credential credential;
   
   /** The state */
   private AtomicInteger state;

   /** Connection handles */
   protected CopyOnWriteArraySet<Object> connectionHandles;
   
   /** Connection traces */
   protected Map<Object, Exception> connectionTraces;

   /** Last validated timestamp */
   private long validated;

   /** Last fromPool timestamp */
   private long fromPool;

   /** Last toPool timestamp */
   private long toPool;

   /** Flush strategy **/
   private FlushStrategy flushStrategy;

   /**
    * Constructor
    * @param cm The connection manager
    * @param mc The managed connection
    * @param credential The credential
    * @param mcp The ManagedConnectionPool
    * @param flushStrategy The FlushStrategy
    */
   public AbstractConnectionListener(ConnectionManager cm, ManagedConnection mc, Credential credential,
         ManagedConnectionPool mcp, FlushStrategy flushStrategy)
   {
      this.cm = cm;
      this.mc = mc;
      this.mcp = mcp;
      this.flushStrategy = flushStrategy;
      this.credential = credential;
      this.state = new AtomicInteger(FREE);
      this.connectionHandles = new CopyOnWriteArraySet<Object>();

      if (cm.getConnectionManagerConfiguration().isTracking() != null &&
          cm.getConnectionManagerConfiguration().isTracking().booleanValue())
         this.connectionTraces = new HashMap<Object, Exception>();

      long timestamp = System.currentTimeMillis();

      this.validated = timestamp;
      this.fromPool = timestamp;
      this.toPool = timestamp;

      mc.addConnectionEventListener(this);
   }

   /**
    * {@inheritDoc}
    */
   public boolean changeState(int currentState, int newState)
   {
      return state.compareAndSet(currentState, newState);
   }

   /**
    * {@inheritDoc}
    */
   public int getState()
   {
      return state.get();
   }

   /**
    * {@inheritDoc}
    */
   public void setState(int state)
   {
      this.state.set(state);
   }

   /**
    * {@inheritDoc}
    */
   public Credential getCredential()
   {
      return credential;
   }

   /**
    * {@inheritDoc}
    */
   public void connectionClosed(ConnectionEvent event)
   {
      Object connection = event.getConnectionHandle();

      removeConnection(connection);

      if (cm.getCachedConnectionManager() != null)
      {
         cm.getCachedConnectionManager().unregisterConnection(cm, this, connection);
      }

      if (connectionHandles.isEmpty() && !isEnlisted())
         cm.returnConnectionListener(this, false);
   }
   
   /**
    * {@inheritDoc}
    */
   public void connectionErrorOccurred(ConnectionEvent event)
   {
      Object connection = event.getConnectionHandle();

      removeConnection(connection);
   
      if (cm.getCachedConnectionManager() != null)
      {
         cm.getCachedConnectionManager().unregisterConnection(cm, this, connection);
      }

      if (cm.getCachedConnectionManager() != null)
      {
         for (Object c : connectionHandles)
            cm.getCachedConnectionManager().unregisterConnection(cm, this, c);
      }

      clearConnections();
      
      haltCatchFire();
      
      cm.returnConnectionListener(this, true);

      if (flushStrategy == FlushStrategy.FAILING_CONNECTION_ONLY)
      {
         mcp.prefill();
      }
      else if (flushStrategy == FlushStrategy.INVALID_IDLE_CONNECTIONS)
      {
         mcp.flush(FlushMode.INVALID);
      }
      else if (flushStrategy == FlushStrategy.IDLE_CONNECTIONS)
      {
         mcp.flush(FlushMode.IDLE);
      }
      else if (flushStrategy == FlushStrategy.GRACEFULLY)
      {
{'fraction_non_alphanumeric': 0.0963710024997845, 'fraction_numerical': 0.002930781829152659, 'mean_word_length': 3.2312180889861417, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3026290', 'n_tokens_mistral': 3192, 'n_tokens_neox': 3081, 'n_words': 826}