Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  The MIT License
   *
   *  Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
   *  Copyright 2012 Sony Mobile Communications AB. All rights reserved.
   *
   *  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 hudson.Plugin;
 
 
 import java.util.List;

Main Plugin entrance.

Author(s):
Robert Sandell <robert.sandell@sonyericsson.com>
 
 public class PluginImpl extends Plugin {

    
What to call this plug-in to humans.
 
     public static final String DISPLAY_NAME = "Gerrit Trigger";

    
Any special permissions needed by this plugin are grouped into this.
 
     public static final PermissionGroup PERMISSION_GROUP =
             new PermissionGroup(PluginImpl.class, Messages._GerritPermissionGroup());
    
 
     public static final Permission MANUAL_TRIGGER = new Permission(,
             "ManualTrigger",
             Messages._ManualTriggerPermissionDescription(),
             .);
 
     private static final Logger logger = LoggerFactory.getLogger(PluginImpl.class);
     private transient GerritHandler gerritEventManager;
     private transient GerritProjectListUpdater projectListUpdater;
     private static PluginImpl instance;
     private transient Collection<GerritEventListenersavedEventListeners;
     private transient Collection<ConnectionListenersavedConnectionListeners;

    
Constructor.
 
     public PluginImpl() {
          = this;
     }

    
Gets the global config.

Returns:
the config.
 
        return ;
    }

    
Returns this singleton instance.

Returns:
the singleton.
    public static PluginImpl getInstance() {
    	return Hudson.getInstance().getPlugin(PluginImpl.class);
    }
    @Override
    public void start() throws Exception {
    	super.start();
        .info("Starting");
        doXStreamRegistrations();
        loadConfig();
        .start();
        //Starts the send-command-queue
        GerritSendCommandQueue.getInstance();
        //do not try to connect to gerrit unless there is a URL or a hostname in the text fields
        List<VerdictCategorycategories = .getCategories();
        if (categories == null) {
            categories = new LinkedList<VerdictCategory>();
        }
        if (categories.isEmpty()) {
            categories.add(new VerdictCategory("CRVW""Code Review"));
            categories.add(new VerdictCategory("VRIF""Verified"));
        }
        if (!.hasDefaultValues()) {
            startManager();
            .info("Started");
        }
    }

    
Registers XStream alias and converters to handle backwards compatibility with old data.
    protected static void doXStreamRegistrations() {
        .trace("doing XStream alias registrations.");
        //Register it in all known XStreams just to be sure.
        //This is where the problems where, reading builds.
        .trace("XStream alias registrations done.");
    }

    
Loads the configuration from disk.

Throws:
java.io.IOException if the unfortunate happens.
    private void loadConfig() throws IOException {
        .trace("loadConfig");
        load();
        if ( == null) {
             = new Config();
        }
    }
    @Override
    public void stop() throws Exception {
        .info("Shutting down...");
        .shutdown();
        .join();
        if ( != null) {
            .shutdown(false);
            //TODO save to register listeners?
             = null;
        }
        GerritSendCommandQueue.shutdown();
    }

    
Starts the GerritEventManager
    private void startManager() {
        .debug("starting Gerrit manager");
        createManager();
        .start();
    }

    
Creates the GerritEventManager
    private void createManager() {
         = new GerritHandler();
        //Add any event/connectionlisteners that were created while the connection was down.
        if ( != null) {
             = null;
        }
        if ( != null) {
             = null;
        }
    }

    
Adds a listener to the EventManager. The listener will receive all events from Gerrit.

    public void addListener(GerritEventListener listener) {
        if ( != null) {
            .addListener(listener);
        } else {
            //If the eventmanager isn't created yet, save the eventlistener so it can be added once
            //the eventmanager is created.
            if ( == null) {
                 = Collections.synchronizedSet(new HashSet<GerritEventListener>());
            }
            .add(listener);
        }
    }

    
    public void removeListener(GerritEventListener listener) {
        if ( != null) {
            .removeListener(listener);
        } else {
            if ( != null) {
                .remove(listener);
            }
        }
    }

    
Removes a connection listener from the manager.

Parameters:
listener the listener to remove.
    public void removeListener(ConnectionListener listener) {
        if ( != null) {
            .removeListener(listener);
        } else {
            if ( != null) {
                .remove(listener);
            }
        }
    }

    
Starts the connection to Gerrit stream of events.

Throws:
java.lang.Exception if it is so unfortunate.
    public synchronized void startConnection() throws Exception {
        if ( == null) {
            createManager();
            if ( != null) {
                 = null;
            }
            if ( != null) {
                 = null;
            }
            .start();
        } else {
            .warn("Already started!");
        }
    }

    
Stops the connection to Gerrit stream of events.

Throws:
java.lang.Exception if it is so unfortunate.
    public synchronized void stopConnection() throws Exception {
        if ( != null) {
             = null;
             = null;
            .shutdown(true);
             = null;
        } else {
            .warn("Was told to shutdown again!?");
        }
    }

    
Restarts the connection to Gerrit stream of events.

Throws:
java.lang.Exception if it is so unfortunate.
    public void restartConnection() throws Exception {
        stopConnection();
        startConnection();
    }

    
Adds a Connection Listener to the manager. 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.
Returns:
the connection status.
    public boolean addListener(ConnectionListener listener) {
        boolean connected = false;
        if ( != null) {
            connected = .addListener(listener);
        } else {
            //If the eventmanager isn't created yet, save the connectionlistener so it can be added once
            //the eventmanager is created.
            if ( == null) {
                 = Collections.synchronizedSet(new HashSet<ConnectionListener>());
            }
            .add(listener);
        }
        return connected;
    }

    
Returns a list of Gerrit projects.

Returns:
list of gerrit projects
    public List<StringgetGerritProjects() {
        if ( != null) {
            return .getGerritProjects();
        } else {
            return new ArrayList<String>();
        }
    }

    
Adds the given event to the stream of events. It gets added to the same event queue as any event coming from the stream-events command in Gerrit.

    public void triggerEvent(GerritEvent event) {
        if ( != null) {
            .triggerEvent(event);
        } else {
            throw new IllegalStateException("Manager not started!");
        }
    }

    
Returns the current Gerrit version. If we are connected to Gerrit, otherwise null is returned.

Returns:
the current gerrit version as a String.
    public String getGerritVersion() {
        if ( != null) {
            return .getGerritVersion();
        } else {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X