Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  The MIT License
   *
   *  Copyright 2010 Sony Ericsson Mobile Communications.
   *
   *  Permission is hereby granted, free of charge, to any person obtaining a copy
   *  of this software and associated documentation files (the "Software"), to deal
   *  in the Software without restriction, including without limitation the rights
   *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  *  copies of the Software, and to permit persons to whom the Software is
  *  furnished to do so, subject to the following conditions:
  *
  *  The above copyright notice and this permission notice shall be included in
  *  all copies or substantial portions of the Software.
  *
  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  *  THE SOFTWARE.
  */
 package com.sonyericsson.hudson.plugins.gerrit.trigger;
 
 import java.io.Reader;
 import java.util.List;
Class responsible for providing the Config object with a list of all available Gerrit projects. Executed periodically on a timely basis and every time the connection to the Gerrit server has been restored.

Author(s):
Gustaf Lundh <Gustaf.Lundh@sonyericsson.com>
 
 public class GerritProjectListUpdater extends Thread implements ConnectionListener {
    
The command for fetching projects.
 
     public static final String GERRIT_LS_PROJECTS = "gerrit ls-projects";

    
Time to wait between refresh attempts
 
     private static final int UPDATE_DELAY = 3600 * 1000;
     private boolean connected = false;
     private boolean shutdown = false;
     private static final Logger logger = LoggerFactory.getLogger(GerritProjectListUpdater.class);
     private List<StringgerritProjects;

    
Standard constructor.
 
     public GerritProjectListUpdater() {
         this.setName(this.getClass().getName() + " Thread");
         this.setDaemon(true);
 
          = PluginImpl.getInstance().addListener(this);
     }
 
     @Override
     public synchronized void connectionEstablished() {
         setConnected(true);
         notify();
     }
 
     @Override
     public synchronized void connectionDown() {
         setConnected(false);
     }

    
Shutdown the thread.
 
     public synchronized void shutdown() {
          = true;
         notify();
     }
 
     @Override
     public void run() {
         while (!) {
             try {
                 if (PluginImpl.getInstance() != null && PluginImpl.getInstance().getConfig() != null && isConnected()) {
                     IGerritHudsonTriggerConfig activeConfig = PluginImpl.getInstance().getConfig();
                     SshConnection sshConnection = SshConnectionFactory.getConnection(
                             activeConfig.getGerritHostName(),
                             activeConfig.getGerritSshPort(),
                             activeConfig.getGerritAuthentication()
                    );
                    setGerritProjects(readProjects(sshConnection.executeCommandReader()));
                    sshConnection.disconnect();
                }
            } catch (SshException ex) {
                 .warn("Could not connect to Gerrit server when updating Gerrit project list: "ex);
            } catch (IOException ex) {
                .error("Could not read stream with Gerrit projects: "ex);
            }
            try {
                synchronized (this) {
                    wait();
                }
            } catch (InterruptedException ex) {
                .warn("InterruptedException: "ex);
                break;
            }
        }
        PluginImpl.getInstance().removeListener(this);
    }

    
Reads a list of Gerrit projects from a Reader, one project per line. As from command: gerrit ls-projects.

Parameters:
commandReader the Reader.
Returns:
a list of projects
Throws:
java.io.IOException if something unfortunate happens.
    public static List<StringreadProjects(Reader commandReaderthrows IOException {
        List<Stringprojects = new ArrayList<String>();
        BufferedReader br = new BufferedReader(commandReader);
        String line = br.readLine();
        while (line != null) {
            projects.add(line);
            line = br.readLine();
        }
        return projects;
    }

    

Returns:
if connected to Gerrit.
    public synchronized boolean isConnected() {
        return ;
    }

    

Parameters:
connected the connected to set.
    public synchronized void setConnected(boolean connected) {
        this. = connected;
    }

    
Sets the internal Gerrit project list.

Parameters:
projects The list of projects
    public synchronized void setGerritProjects(List<Stringprojects) {
         = projects;
    }

    
Returns a string list of Gerrit projects.

Returns:
list of gerrit projects
    public synchronized List<StringgetGerritProjects() {
        if ( == null) {
             = new ArrayList<String>();
        }
        return ;
    }
New to GrepCode? Check out our FAQ X