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 java.io.File;
 import java.net.URL;
 
 import static com.sonyericsson.hudson.plugins.gerrit.trigger.utils.StringUtil.PLUGIN_IMAGES_URL;

Management link for configuring the global configuration of this trigger.

Author(s):
Robert Sandell <robert.sandell@sonyericsson.com>
 
 public class GerritManagement extends ManagementLink implements StaplerProxyDescribable<GerritManagement>, Saveable {
 
     private static final Logger logger = LoggerFactory.getLogger(GerritManagement.class);
 
     @Override
     public String getIconFileName() {
         return  + "icon.png";
     }
 
     @Override
     public String getUrlName() {
         return "gerrit-trigger";
     }
 
     @Override
     public String getDisplayName() {
         return Messages.DisplayName();
     }
 
     @Override
     public String getDescription() {
         return Messages.PluginDescription();
     }
 
     @Override
     public DescriptorImpl getDescriptor() {
         return Hudson.getInstance().getDescriptorByType(DescriptorImpl.class);
     }

    
Descriptor is only used for UI form bindings.
 
     @Extension
     public static final class DescriptorImpl extends Descriptor<GerritManagement> {
 
         @Override
        public String getDisplayName() {
            return null// unused
        }

        
Starts the event connection to Gerrit.

Returns:
ok or error.
        public FormValidation doStartConnection() {
            .debug("check permisson of doStartConnection().");
            Hudson.getInstance().checkPermission(.);
            try {
                PluginImpl.getInstance().startConnection();
                //TODO wait for the connection to actually be established.
                return FormValidation.ok();
            } catch (Exception ex) {
                .error("Could not start connection. "ex);
                return FormValidation.error(ex.getMessage());
            }
        }

        
Stops the event connection to Gerrit.

Returns:
ok or error.
        public FormValidation doStopConnection() {
            .debug("check permisson of doStopConnection().");
            Hudson.getInstance().checkPermission(.);
            try {
                PluginImpl.getInstance().stopConnection();
                //TODO wait for the connection to actually be shut down.
                return FormValidation.ok();
            } catch (Exception ex) {
                .error("Could not stop connection. "ex);
                return FormValidation.error(ex.getMessage());
            }
        }

        
Stops the event connection to Gerrit.

Returns:
ok or error.
        public FormValidation doRestartConnection() {
            .debug("check permisson of doRestartConnection().");
            Hudson.getInstance().checkPermission(.);
            try {
                PluginImpl.getInstance().restartConnection();
                //TODO wait for the connection to actually be shut down and connected again.
                return FormValidation.ok();
            } catch (Exception ex) {
                .error("Could not restart connection. "ex);
                return FormValidation.error(ex.getMessage());
            }
        }

        
Tests if the provided parameters can connect to Gerrit.

Parameters:
gerritHostName the hostname
gerritSshPort the ssh-port
gerritUserName the username
gerritAuthKeyFile the private key file
gerritAuthKeyFilePassword the password for the keyfile or null if there is none.
Returns:
if can be done, otherwise.
        public FormValidation doTestConnection(
                @QueryParameter("gerritHostName"final String gerritHostName,
                @QueryParameter("gerritSshPort"final int gerritSshPort,
                @QueryParameter("gerritUserName"final String gerritUserName,
                @QueryParameter("gerritAuthKeyFile"final String gerritAuthKeyFile,
                @QueryParameter("gerritAuthKeyFilePassword"final String gerritAuthKeyFilePassword) {
            if (.isDebugEnabled()) {
                .debug("gerritHostName = {}\n"
                        + "gerritSshPort = {}\n"
                        + "gerritUserName = {}\n"
                        + "gerritAuthKeyFile = {}\n"
                        + "gerritAuthKeyFilePassword = {}",
                        new Object[]{gerritHostName,
                            gerritSshPort,
                            gerritUserName,
                            gerritAuthKeyFile,
                            gerritAuthKeyFilePassword,  });
            }
            File file = new File(gerritAuthKeyFile);
            String password = null;
            if (gerritAuthKeyFilePassword != null && gerritAuthKeyFilePassword.length() > 0) {
                password = gerritAuthKeyFilePassword;
            }
            if (SshUtil.checkPassPhrase(filepassword)) {
                if (file.exists() && file.isFile()) {
                    try {
                        SshConnection sshConnection = SshConnectionFactory.getConnection(
                                gerritHostName,
                                gerritSshPort,
                                new Authentication(filegerritUserNamepassword));
                        sshConnection.disconnect();
                        return FormValidation.ok(Messages.Success());
                    } catch (SshConnectException ex) {
                        return FormValidation.error(Messages.SshConnectException());
                    } catch (SshAuthenticationException ex) {
                        return FormValidation.error(Messages.SshAuthenticationException(ex.getMessage()));
                    } catch (Exception e) {
                        return FormValidation.error(Messages.ConnectionError(e.getMessage()));
                    }
                } else {
                    return FormValidation.error(Messages.SshKeyFileNotFoundError(gerritAuthKeyFile));
                }
            } else {
                return FormValidation.error(Messages.BadSshkeyOrPasswordError());
            }
        }
    }

    
Saves the form to the configuration and disk.

Parameters:
req StaplerRequest
rsp StaplerResponse
Throws:
javax.servlet.ServletException if something unfortunate happens.
java.io.IOException if something unfortunate happens.
java.lang.InterruptedException if something unfortunate happens.
    public void doConfigSubmit(StaplerRequest reqStaplerResponse rspthrows ServletException,
            IOException,
            InterruptedException {
        if (.isDebugEnabled()) {
            .debug("submit {}"req.toString());
        }
        JSONObject form = req.getSubmittedForm();
        try {
            getConfig().setValues(form);
            PluginImpl.getInstance().save();
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
        rsp.sendRedirect(".");
    }

    
Checks that the provided parameter is an integer and not negative.

            @QueryParameter("value")
            final String value) {
        return FormValidation.validatePositiveInteger(value);
    }

    
Checks that the provided parameter is an integer, not negative, that is larger than the minimum value.

            @QueryParameter("value")
            final String value) {
        FormValidation validatePositive = FormValidation.validatePositiveInteger(value);
        if (!validatePositive.kind.equals(..)) {
            return validatePositive;
        } else {
            int intValue = Integer.parseInt(value);
            if (intValue < .) {
                return FormValidation.error(Messages.DynamicConfRefreshTooLowError(
                        .));
            }
        }
        return FormValidation.ok();
    }



    
Checks that the provided parameter is an integer.

            @QueryParameter("value")
            final String value) {
        try {
            Integer.parseInt(value);
            return FormValidation.ok();
        } catch (NumberFormatException e) {
            return FormValidation.error(hudson.model.Messages.Hudson_NotANumber());
        }
    }

    
Checks that the provided parameter is an empty string or an integer.

            @QueryParameter("value")
            final String value) {
        if (value == null || value.length() <= 0) {
            return FormValidation.ok();
        } else {
            try {
                Integer.parseInt(value);
                return FormValidation.ok();
            } catch (NumberFormatException e) {
                return FormValidation.error(hudson.model.Messages.Hudson_NotANumber());
            }
        }
    }

    
Checks if the value is a valid URL. It does not check if the URL is reachable.

Parameters:
value the value
Returns:
if it is so.
    public FormValidation doUrlCheck(
            @QueryParameter("value")
            final String value) {
        if (value == null || value.length() <= 0) {
            return FormValidation.error(Messages.EmptyError());
        } else {
            try {
                new URL(value);
                return FormValidation.ok();
            } catch (MalformedURLException ex) {
                return FormValidation.error(Messages.BadUrlError());
            }
        }
    }

    
Checks to see if the provided value is a file path to a valid private key file.

Parameters:
value the value.
Returns:
if it is so.
            @QueryParameter("value")
            final String value) {
        File f = new File(value);
        if (!f.exists()) {
            return FormValidation.error(Messages.FileNotFoundError(value));
        } else if (!f.isFile()) {
            return FormValidation.error(Messages.NotFileError(value));
        } else {
            if (SshUtil.isPrivateKeyFileValid(f)) {
                return FormValidation.ok();
            } else {
                return FormValidation.error(Messages.InvalidKeyFileError(value));
            }
        }
    }
    @Override
    public Object getTarget() {
        Hudson.getInstance().checkPermission(.);
        return this;
    }
    @Override
    public void save() throws IOException {
        .debug("SAVE!!!");
    }

    
Returns this singleton.

Returns:
the single loaded instance if this class.
    public static GerritManagement get() {
        return ManagementLink.all().get(GerritManagement.class);
    }

    
Gets the global config.

Returns:
the config.
See also:
PluginImpl.getConfig()
    public static IGerritHudsonTriggerConfig getConfig() {
        if (PluginImpl.getInstance() != null) {
            return PluginImpl.getInstance().getConfig();
        }
        return null;
    }

    
The AdministrativeMonitor related to Gerrit. convenience method for the jelly page.

Returns:
the monitor if it could be found.
    @SuppressWarnings("unused"//Called from Jelly
        for (AdministrativeMonitor monitor : AdministrativeMonitor.all()) {
            if (monitor instanceof GerritAdministrativeMonitor) {
                return (GerritAdministrativeMonitor)monitor;
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X