/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.
 */

/*
 * Licensed under the X license (see http://www.x.org/terms.htm)
 */
package org.opentools.minerva.connector.jdbc;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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

/**
 * Abstract base class for ManagedConnections.
 * @author Aaron Mulder <ammulder@alumni.princeton.edu>
 * @version $Revision: 245 $
 */
public abstract class BaseManagedConnection implements ManagedConnection {
    protected PrintWriter logger;
    protected List<ConnectionEventListener> listeners;
    private String user;

    public BaseManagedConnection(String user) {
        this.user = user;
        listeners = new ArrayList<ConnectionEventListener>();
    }

    public void associateConnection(Object Tx) throws ResourceException {
        throw new ResourceException("associateConnection not supported");
    }

    public void addConnectionEventListener(ConnectionEventListener listener) {
        listeners.add(listener);
    }

    public void removeConnectionEventListener(ConnectionEventListener listener) {
        listeners.remove(listener);
    }

    public void setLogWriter(PrintWriter writer) throws ResourceException {
        logger = writer;
    }

    public PrintWriter getLogWriter() throws ResourceException {
        return logger;
    }

    public void destroy() throws ResourceException {
        listeners.clear();
        listeners = null;
        logger = null;
        user = null;
    }

    String getUser() {
        return user;
    }

    protected void fireConnectionEvent(ConnectionEvent evt) {
        List<ConnectionEventListener> local = new ArrayList<ConnectionEventListener>(listeners);
        for(int i=local.size()-1; i >= 0; i--) {
            if(evt.getId() == ConnectionEvent.CONNECTION_CLOSED) {
                local.get(i).connectionClosed(evt);
            }
            else if(evt.getId() == ConnectionEvent.CONNECTION_ERROR_OCCURRED) {
                local.get(i).connectionErrorOccurred(evt);
            }
        }
    }

}
@@ -1882,32 +1882,62 @@
 ner listener) %7B%0A
+        synchronized (this) %7B%0A
         listener
@@ -1950,32 +1950,38 @@
 listener);%0A    %7D
+%0A    %7D
 %0A%0A    public voi
@@ -2040,32 +2040,62 @@
 ner listener) %7B%0A
+        synchronized (this) %7B%0A
         listener
@@ -2111,32 +2111,38 @@
 listener);%0A    %7D
+%0A    %7D
 %0A%0A    public voi
@@ -2375,32 +2375,62 @@
 urceException %7B%0A
+        synchronized (this) %7B%0A
         listener
@@ -2458,32 +2458,42 @@
 steners = null;%0A
+        %7D%0A
         logger =
@@ -2677,16 +2677,65 @@
 istener%3E
+ local;%0A        synchronized (this) %7B%0A           
  local =
@@ -2786,16 +2786,26 @@
 eners);%0A
+        %7D%0A
         

Fix potential NPE due to concurrent race