com.sonyericsson.hudson.plugins.gerrit.gerritevents
Class GerritHandler

java.lang.Object
  extended by java.lang.Thread
      extended by com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler
All Implemented Interfaces:
Coordinator, Runnable

public class GerritHandler
extends Thread
implements Coordinator

Main class for this module. Contains the main loop for connecting and reading streamed events from Gerrit.

Author:
Robert Sandell <robert.sandell@sonyericsson.com>

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
protected static int BRUTE_FORCE_TRIES
          How many times to try and set the shutdown flag to true.
static int CONNECT_SLEEP
          Time to wait between connection attempts.
protected static int PAUSE_SECOND
          The amount of milliseconds to pause when brute forcing the shutdown flag to true.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
GerritHandler()
          Creates a GerritHandler with all the default values set.
GerritHandler(GerritConnectionConfig config)
          Creates a GerritHandler with the specified values.
GerritHandler(String gerritHostName, int gerritSshPort, Authentication authentication)
          Creates a GerritHandler with the specified values and default number of worker threads.
GerritHandler(String gerritHostName, int gerritSshPort, Authentication authentication, int numberOfWorkerThreads)
          Creates a GerritHandler with the specified values.
 
Method Summary
 void addConnectionListeners(Collection<? extends ConnectionListener> listeners)
          Add all ConnectionListeners to the list of listeners.
 void addConnectionListeners(Map<Integer,ConnectionListener> listeners)
          Deprecated.  
 void addEventListeners(Collection<? extends GerritEventListener> listeners)
          Adds all the provided listeners to the internal list of listeners.
 void addEventListeners(Map<Integer,GerritEventListener> listeners)
          Deprecated. 
 boolean addListener(ConnectionListener listener)
          Add a ConnectionListener to the list of listeners.
 void addListener(GerritEventListener listener)
          Add a GerritEventListener to the list of listeners.
 Authentication getAuthentication()
          The authentication credentials for ssh connection.
 int getEventListenersCount()
          The number of added eGerritEventListeners.
 String getGerritHostName()
          gets the hostname where Gerrit is running.
 int getGerritSshPort()
          Gets the port for gerrit ssh commands.
 String getGerritVersion()
          The gerrit version we are connected to.
 int getNumberOfWorkerThreads()
          Gets the number of event worker threads.
 BlockingQueue<Work> getWorkQueue()
          Retrieves the work queue for workers to poll.
 boolean isShutdownInProgress()
          If the system is shutting down.
protected  void notifyConnectionDown()
          Notifies all ConnectionListeners that the connection is down.
protected  void notifyConnectionEstablished()
          Notifies all ConnectionListeners that the connection is established.
 void notifyListeners(GerritEvent event)
          Notifies all listeners of a Gerrit event.
 Collection<ConnectionListener> removeAllConnectionListeners()
          Removes all connection listeners and returns those who where removed.
 Collection<GerritEventListener> removeAllEventListeners()
          Removes all event listeners and returns those that where removed.
 void removeListener(ConnectionListener listener)
          Removes a ConnectionListener from the list of listeners.
 void removeListener(GerritEventListener listener)
          Removes a GerritEventListener from the list of listeners.
 void run()
          Main loop for connecting and reading Gerrit JSON Events and dispatching them to Workers.
 void setAuthentication(Authentication authentication)
          The authentication credentials for ssh connection.
 void setGerritHostName(String gerritHostName)
          Sets the hostname where Gerrit is running.
 void setGerritSshPort(int gerritSshPort)
          Sets the port for gerrit ssh commands.
 void setNumberOfWorkerThreads(int numberOfWorkerThreads)
          Sets the number of worker event threads.
 void shutdown(boolean join)
          Closes the connection.
 void triggerEvent(GerritEvent event)
          "Triggers" an event by adding it to the internal queue and be taken by one of the worker threads.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONNECT_SLEEP

public static final int CONNECT_SLEEP
Time to wait between connection attempts.

See Also:
Constant Field Values

PAUSE_SECOND

protected static final int PAUSE_SECOND
The amount of milliseconds to pause when brute forcing the shutdown flag to true.

See Also:
Constant Field Values

BRUTE_FORCE_TRIES

protected static final int BRUTE_FORCE_TRIES
How many times to try and set the shutdown flag to true. Noticed during unit tests there seems to be a timing issue or something so sometimes the flag is not set properly. Setting it a number of times with some delay helped.

See Also:
shutdown(boolean), PAUSE_SECOND, Constant Field Values
Constructor Detail

GerritHandler

public GerritHandler()
Creates a GerritHandler with all the default values set.

See Also:
GerritDefaultValues.DEFAULT_GERRIT_HOSTNAME, GerritDefaultValues.DEFAULT_GERRIT_SSH_PORT, GerritDefaultValues.DEFAULT_GERRIT_USERNAME, GerritDefaultValues.DEFAULT_GERRIT_AUTH_KEY_FILE, GerritDefaultValues.DEFAULT_GERRIT_AUTH_KEY_FILE_PASSWORD, GerritDefaultValues.DEFAULT_NR_OF_RECEIVING_WORKER_THREADS

GerritHandler

public GerritHandler(String gerritHostName,
                     int gerritSshPort,
                     Authentication authentication)
Creates a GerritHandler with the specified values and default number of worker threads.

Parameters:
gerritHostName - the hostName
gerritSshPort - the ssh port that the gerrit server listens to.
authentication - the authentication credentials.

GerritHandler

public GerritHandler(GerritConnectionConfig config)
Creates a GerritHandler with the specified values.

Parameters:
config - the configuration containing the connection values.

GerritHandler

public GerritHandler(String gerritHostName,
                     int gerritSshPort,
                     Authentication authentication,
                     int numberOfWorkerThreads)
Creates a GerritHandler with the specified values.

Parameters:
gerritHostName - the hostName for gerrit.
gerritSshPort - the ssh port that the gerrit server listens to.
authentication - the authentication credentials.
numberOfWorkerThreads - the number of eventthreads.
Method Detail

getGerritVersion

public String getGerritVersion()
The gerrit version we are connected to.

Returns:
the gerrit version.

run

public void run()
Main loop for connecting and reading Gerrit JSON Events and dispatching them to Workers.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

addListener

public void addListener(GerritEventListener listener)
Add a GerritEventListener to the list of listeners.

Parameters:
listener - the listener to add.

addEventListeners

@Deprecated
public void addEventListeners(Map<Integer,GerritEventListener> listeners)
Deprecated. 

Adds all the provided listeners to the internal list of listeners.

Parameters:
listeners - the listeners to add.

addEventListeners

public void addEventListeners(Collection<? extends GerritEventListener> listeners)
Adds all the provided listeners to the internal list of listeners.

Parameters:
listeners - the listeners to add.

removeListener

public void removeListener(GerritEventListener listener)
Removes a GerritEventListener from the list of listeners.

Parameters:
listener - the listener to remove.

removeAllEventListeners

public Collection<GerritEventListener> removeAllEventListeners()
Removes all event listeners and returns those that where removed.

Returns:
the former list of listeners.

getEventListenersCount

public int getEventListenersCount()
The number of added eGerritEventListeners.

Returns:
the size.

addListener

public boolean addListener(ConnectionListener listener)
Add a ConnectionListener to the list of listeners. Return the current connection status so that listeners that are added later than a connectionestablished/ connectiondown will get the current connection status.

Parameters:
listener - the listener to add.
Returns:
the connection status

addConnectionListeners

@Deprecated
public void addConnectionListeners(Map<Integer,ConnectionListener> listeners)
Deprecated. 

Adds all ConnectionListeners.

Parameters:
listeners - the listener.

addConnectionListeners

public void addConnectionListeners(Collection<? extends ConnectionListener> listeners)
Add all ConnectionListeners to the list of listeners.

Parameters:
listeners - the listeners to add.

removeListener

public void removeListener(ConnectionListener listener)
Removes a ConnectionListener from the list of listeners.

Parameters:
listener - the listener to remove.

removeAllConnectionListeners

public Collection<ConnectionListener> removeAllConnectionListeners()
Removes all connection listeners and returns those who where removed.

Returns:
the list of former listeners.

getAuthentication

public Authentication getAuthentication()
The authentication credentials for ssh connection.

Returns:
the credentials.

setAuthentication

public void setAuthentication(Authentication authentication)
The authentication credentials for ssh connection.

Parameters:
authentication - the credentials.

getGerritHostName

public String getGerritHostName()
gets the hostname where Gerrit is running.

Returns:
the hostname.

setGerritHostName

public void setGerritHostName(String gerritHostName)
Sets the hostname where Gerrit is running.

Parameters:
gerritHostName - the hostname.

getGerritSshPort

public int getGerritSshPort()
Gets the port for gerrit ssh commands.

Returns:
the port nr.

setGerritSshPort

public void setGerritSshPort(int gerritSshPort)
Sets the port for gerrit ssh commands.

Parameters:
gerritSshPort - the port nr.

getNumberOfWorkerThreads

public int getNumberOfWorkerThreads()
Gets the number of event worker threads.

Returns:
the number of threads.

setNumberOfWorkerThreads

public void setNumberOfWorkerThreads(int numberOfWorkerThreads)
Sets the number of worker event threads.

Parameters:
numberOfWorkerThreads - the number of threads

getWorkQueue

public BlockingQueue<Work> getWorkQueue()
Description copied from interface: Coordinator
Retrieves the work queue for workers to poll.

Specified by:
getWorkQueue in interface Coordinator
Returns:
the queue

notifyListeners

public void notifyListeners(GerritEvent event)
Notifies all listeners of a Gerrit event. This method is meant to be called by one of the Worker Threads EventThread and not on this Thread which would defeat the purpose of having workers.

Specified by:
notifyListeners in interface Coordinator
Parameters:
event - the event.

isShutdownInProgress

public boolean isShutdownInProgress()
If the system is shutting down. I.e. the shutdown method has been called.

Returns:
true if so.

shutdown

public void shutdown(boolean join)
Closes the connection.

Parameters:
join - if the method should wait for the thread to finish before returning.

notifyConnectionDown

protected void notifyConnectionDown()
Notifies all ConnectionListeners that the connection is down.


notifyConnectionEstablished

protected void notifyConnectionEstablished()
Notifies all ConnectionListeners that the connection is established.


triggerEvent

public void triggerEvent(GerritEvent event)
"Triggers" an event by adding it to the internal queue and be taken by one of the worker threads. This way it will be put into the normal flow of events as if it was coming from the stream-events command.

Parameters:
event - the event to trigger.


Copyright © 2004-2012 Hudson. All Rights Reserved.