Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: FBEventManager.java 56954 2012-04-09 12:23:23Z mrotteveel $
   * 
   * Firebird Open Source J2EE Connector - JDBC Driver
   * 
   * Copyright (C) All Rights Reserved.
   * 
   * This file was created by members of the firebird development team.
   * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  * 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *  
  *   - Redistributions of source code must retain the above copyright 
  *     notice, this list of conditions and the following disclaimer.
  *   - Redistributions in binary form must reproduce the above 
  *     copyright notice, this list of conditions and the following 
  *     disclaimer in the documentation and/or other materials provided 
  *     with the distribution.
  *   - Neither the name of the firebird development team nor the names
  *     of its contributors may be used to endorse or promote products 
  *     derived from this software without specific prior written 
  *     permission.
  * 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  * SUCH DAMAGE.
  */
 package org.firebirdsql.event;
 
 
 
 import java.util.Map;
 import java.util.Set;
 import java.util.List;
 
An EventListener implementation to listen for database events.

Author(s):
Gabriel Reid
 
 public class FBEventManager implements EventManager {
 
     private GDS gds;
     private IscDbHandle dbHandle;
     private boolean connected = false;
     private String user = "";
     private String password = "";
     private String database = "";
     private int port = 3050;
     private String host = "localhost";
     private Map listenerMap = Collections.synchronizedMap(new HashMap());
     private Map handlerMap = Collections.synchronizedMap(new HashMap());
     private List eventQueue = new ArrayList();
     private EventDispatcher eventDispatcher;
     private Thread dispatchThread;
     private long waitTimeout = 1000;
 
     public FBEventManager() {
     	this(GDSFactory.getDefaultGDSType());
     }
 
     public FBEventManager(GDSType gdsType){
          = GDSFactory.getGDSForType(gdsType);
          = .createIscDbHandle();
     }
 
     public void connect() throws SQLException {
         if (){
             throw new IllegalStateException(
                     "Connect called while already connected");        
         }
        try {
            String connString = GDSFactory.getDatabasePath(.getType(), );
            .iscAttachDatabase(connStringdpb);
        } catch (GDSException e){
            throw new FBSQLException(e);
        }
         = true;
         = new EventDispatcher();
         = new Thread();
        .setDaemon(true);
        .start();
    }
    public void disconnect() throws SQLException {
        if (!){
            throw new IllegalStateException(
                    "Disconnect called while not connected");
        }
        for (Iterator eventItr = new HashSet(.keySet()).iterator(); 
                eventItr.hasNext();){
            String eventName = (String)eventItr.next();
            try {
                unregisterListener(eventName);
            } catch (GDSException e1){
                throw new FBSQLException(e1);
            }
        }
        .clear();
        .clear();
        try {
            .iscDetachDatabase();
        } catch (GDSException e2){
            throw new FBSQLException(e2);
        }
         = false;
        .stop();
        
        // join the thread and wait until it dies
        try {
            .join();
        } catch(InterruptedException ex) {
            throw new FBSQLException(ex);
        }
    }
    public boolean isConnected() {
        return ;
    }
    public void setUser(String user){
        this. = user;
    }
    public String getUser(){
        return this.;
    }
    public void setPassword(String password){
        this. = password;
    }
    public String getPassword(){
        return this.;
    }
    public void setDatabase(String database){
        this. = database;
    }
    public String getDatabase(){
        return this.;
    }
    public String getHost(){
        return this.;
    }
    public void setHost(String host){
        this. = host;
    }
       
    public int getPort(){
        return this.;
    }
    public void setPort(int port){
        this. = port;
    }
    
    
Get the time in milliseconds, after which the async threa will exit from the java.lang.Object.wait(long) method and check whether it was stopped or not.

Default value is 1000 (1 second);

Returns:
wait timeout in milliseconds
    public long getWaitTimeout() {
        return ;
    }

    
    
Set the time in milliseconds, after which the async threa will exit from the java.lang.Object.wait(long) method and check whether it was stopped or not.

Default value is 1000 (1 second);

Parameters:
waitTimeout wait timeout in milliseconds
    public void setWaitTimeout(long waitTimeout) {
        this. = waitTimeout;
    }
    public void addEventListener(
            String eventNameEventListener listenerthrows SQLException {
        if (!){
            throw new IllegalStateException(
                    "Can't add event listeners to disconnected EventManager");
        }
        if (listener == null || eventName == null){
            throw new NullPointerException();
        }
        synchronized (){
            if (!.containsKey(eventName)){
                try {
                    registerListener(eventName);
                } catch (GDSException e){
                    throw new FBSQLException(e);
                }
                .put(eventNamenew HashSet());
            }
            Set listenerSet = (Set).get(eventName);
            listenerSet.add(listener);
        }
    }
    public void removeEventListener(
            String eventNameEventListener listenerthrows SQLException {
        if (eventName == null || listener == null){
            throw new NullPointerException();
        }
        Set listenerSet = (Set).get(eventName);
        if (listenerSet != null){
            listenerSet.remove(listener);
            if (listenerSet.isEmpty()){
                .remove(eventName);
                try {
                    unregisterListener(eventName);
                } catch (GDSException e){
                    throw new FBSQLException(e);
                }
            }
        }
    }
    public int waitForEvent(String eventName
            throws InterruptedExceptionSQLException {
        return waitForEvent(eventName, 0);
    }
    public int waitForEvent(String eventNamefinal int timeout
            throws InterruptedExceptionSQLException {
        if (!){
            throw new IllegalStateException(
                    "Can't wait for events with disconnected EventManager");
        }
        if (eventName == null){
            throw new NullPointerException();
        }
        final Object lock = new Object();
        OneTimeEventListener listener = new OneTimeEventListener(lock);
        synchronized (lock){
            addEventListener(eventNamelistener);
            lock.wait(timeout);
        }
              
        removeEventListener(eventNamelistener);
        return listener.getEventCount();
    }
    private void registerListener(String eventNamethrows GDSException {
        GdsEventHandler handler = new GdsEventHandler(eventName);
        .put(eventNamehandler);
        handler.register();
    }
    private void unregisterListener(String eventNamethrows GDSException {
        GdsEventHandler handler = (GdsEventHandler).get(eventName);
        handler.unregister();
        .remove(eventName);
    }
    class GdsEventHandler implements org.firebirdsql.gds.EventHandler {
        private EventHandle eventHandle;
        private boolean initialized = false;
        private boolean cancelled = false;
        public GdsEventHandler(String eventNamethrows GDSException {
             = .createEventHandle(eventName);
            .iscEventBlock();
        }
        public synchronized void register() throws GDSException {
            if (){
                throw new IllegalStateException(
                        "Trying to register a cancelled event handler");
            }
            .iscQueueEvents(this);
        }
        public synchronized void unregister() throws GDSException {
            if (){
                throw new IllegalStateException(
                        "Trying to cancel a cancelled event handler");
            }
            .iscCancelEvents();
             = true;
        }
        public synchronized void eventOccurred() {
            if (!){
                try {
                    .iscEventCounts();
                } catch (GDSException e1){
                    e1.printStackTrace();
                }
                if ( && !){
                    DatabaseEvent event = new DatabaseEventImpl(
                        .getEventName(),
                        .getEventCount());
                    synchronized (){
                        .add(event);
                        .notify();
                    }
                } else {
                     = true;
                }
                 
                try {
                    register();
                } catch (GDSException e2){
                    e2.printStackTrace();
                }
            }         
        }
    }
    class EventDispatcher implements Runnable {
        
        private volatile boolean running = false;
        public void stop(){
             = false;
        }
        public void run(){
             = true;
            List events = new ArrayList(); 
            while (){
                synchronized (){
                    while (.isEmpty() && ){
                        try {
                            .wait();
                        } catch (InterruptedException ie){ }
                    }
                    events.addAll();
                    .clear();
                }
                
                for (Iterator eItr = events.iterator(); eItr.hasNext();){
                    DatabaseEvent event = (DatabaseEvent)eItr.next();
                    Set listenerSet = null;
                    synchronized (){
                        listenerSet = 
                            (Set).get(event.getEventName());
                        if (listenerSet != null){
                            Iterator listenerItr = listenerSet.iterator();
                            while (listenerItr.hasNext()){
                                EventListener listener = 
                                    (EventListener)listenerItr.next();
                                listener.eventOccurred(event);
                            }
                        }
                    }
                }
                events.clear();
            }
        }
    }
class OneTimeEventListener implements EventListener {
    private int eventCount = -1;
    private Object lock
    public OneTimeEventListener(Object lock){
        this. = lock;
    }
    public  void eventOccurred(DatabaseEvent event){
        if ( == -1){
             = event.getEventCount();
        }
        synchronized (){
            .notify();
        }
    }
    public int getEventCount(){
        return ;
    }
class DatabaseEventImpl implements DatabaseEvent {
    
    private int eventCount;
    private String eventName;
    public DatabaseEventImpl(String eventNameint eventCount){
        this. = eventName;
        this. = eventCount;
    }
    public int getEventCount(){
        return this.;
    }
    public String getEventName(){
        return this.;
    }
    public String toString(){
        return "DatabaseEvent['" +  + " * " +  + "]";
    }
New to GrepCode? Check out our FAQ X