Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve file formatting (consistency) #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,35 @@
* @author K Jonathan Harker
*/
@Deprecated
public class LogstashBuildWrapper extends BuildWrapper
{
public class LogstashBuildWrapper extends BuildWrapper {

/**
* Create a new {@link LogstashBuildWrapper}.
*/
@DataBoundConstructor
public LogstashBuildWrapper()
{}
public LogstashBuildWrapper() {}

/**
* {@inheritDoc}
*/
@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener)
throws IOException, InterruptedException
{
return new Environment()
{
};
throws IOException, InterruptedException {
return new Environment() {};
}

@Override
public DescriptorImpl getDescriptor()
{
public DescriptorImpl getDescriptor() {
return (DescriptorImpl)super.getDescriptor();
}

/**
* Registers {@link LogstashBuildWrapper} as a {@link BuildWrapper}.
*/
@Extension
public static class DescriptorImpl extends BuildWrapperDescriptor
{
public static class DescriptorImpl extends BuildWrapperDescriptor {

public DescriptorImpl()
{
public DescriptorImpl() {
super(LogstashBuildWrapper.class);
load();
}
Expand All @@ -90,17 +82,15 @@ public DescriptorImpl()
* {@inheritDoc}
*/
@Override
public String getDisplayName()
{
public String getDisplayName() {
return Messages.DisplayName();
}

/**
* {@inheritDoc}
*/
@Override
public boolean isApplicable(AbstractProject<?, ?> item)
{
public boolean isApplicable(AbstractProject<?, ?> item) {
return false;
}
}
Expand Down
107 changes: 34 additions & 73 deletions src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import net.sf.json.JSONObject;

@Extension
public class LogstashConfiguration extends GlobalConfiguration
{
public class LogstashConfiguration extends GlobalConfiguration {

private static final Logger LOGGER = Logger.getLogger(LogstashConfiguration.class.getName());
private static final FastDateFormat MILLIS_FORMATTER = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
private static final FastDateFormat LEGACY_FORMATTER = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZ");
Expand All @@ -47,61 +47,46 @@ public class LogstashConfiguration extends GlobalConfiguration
// a flag indicating if we're currently in the configure method.
private transient boolean configuring = false;

public LogstashConfiguration()
{
public LogstashConfiguration() {
load();
if (enabled == null)
{
if (logstashIndexer == null)
{
if (enabled == null) {
if (logstashIndexer == null) {
enabled = false;
}
else
{
} else {
enabled = true;
}
}
activeIndexer = logstashIndexer;
}

public boolean isEnabled()
{
public boolean isEnabled() {
return enabled == null ? false: enabled;
}

public void setEnabled(boolean enabled)
{
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public boolean isEnableGlobally()
{
public boolean isEnableGlobally() {
return enableGlobally;
}

public void setEnableGlobally(boolean enableGlobally)
{
public void setEnableGlobally(boolean enableGlobally) {
this.enableGlobally = enableGlobally;
}

public boolean isMilliSecondTimestamps()
{
public boolean isMilliSecondTimestamps() {
return milliSecondTimestamps;
}

public void setMilliSecondTimestamps(boolean milliSecondTimestamps)
{
public void setMilliSecondTimestamps(boolean milliSecondTimestamps) {
this.milliSecondTimestamps = milliSecondTimestamps;
}

public FastDateFormat getDateFormatter()
{
if (milliSecondTimestamps)
{
public FastDateFormat getDateFormatter() {
if (milliSecondTimestamps) {
return MILLIS_FORMATTER;
}
else
{
} else {
return LEGACY_FORMATTER;
}
}
Expand All @@ -111,16 +96,13 @@ public FastDateFormat getDateFormatter()
*
* @return configuration instance
*/
public LogstashIndexer<?> getLogstashIndexer()
{
public LogstashIndexer<?> getLogstashIndexer() {
return logstashIndexer;
}

public void setLogstashIndexer(LogstashIndexer<?> logstashIndexer)
{
public void setLogstashIndexer(LogstashIndexer<?> logstashIndexer) {
this.logstashIndexer = logstashIndexer;
if (!configuring && !Objects.equals(logstashIndexer, activeIndexer))
{
if (!configuring && !Objects.equals(logstashIndexer, activeIndexer)) {
activeIndexer = logstashIndexer;
}
}
Expand All @@ -130,33 +112,25 @@ public void setLogstashIndexer(LogstashIndexer<?> logstashIndexer)
* @return dao instance
*/
@CheckForNull
public LogstashIndexerDao getIndexerInstance()
{
if (activeIndexer != null)
{
public LogstashIndexerDao getIndexerInstance() {
if (activeIndexer != null) {
return activeIndexer.getInstance();
}
return null;
}


public List<?> getIndexerTypes()
{
public List<?> getIndexerTypes() {
return LogstashIndexer.all();
}

@SuppressWarnings("deprecation")
@Initializer(after = InitMilestone.JOB_LOADED)
public void migrateData()
{
if (!dataMigrated)
{
public void migrateData() {
if (!dataMigrated) {
Descriptor descriptor = LogstashInstallation.getLogstashDescriptor();
if (descriptor.getType() != null)
{
if (descriptor.getType() != null) {
IndexerType type = descriptor.getType();
switch (type)
{
switch (type) {
case REDIS:
LOGGER.log(Level.INFO, "Migrating logstash configuration for Redis");
Redis redis = new Redis();
Expand All @@ -170,8 +144,7 @@ public void migrateData()
case ELASTICSEARCH:
LOGGER.log(Level.INFO, "Migrating logstash configuration for Elastic Search");
URI uri;
try
{
try {
uri = (new URIBuilder(descriptor.getHost()))
.setPort(descriptor.getPort())
.setPath("/" + descriptor.getKey()).build();
Expand All @@ -181,9 +154,7 @@ public void migrateData()
es.setPassword(Secret.fromString(descriptor.getPassword()));
logstashIndexer = es;
enabled = true;
}
catch (URISyntaxException e)
{
} catch (URISyntaxException e) {
enabled = false;
LOGGER.log(Level.INFO, "Migrating logstash configuration for Elastic Search failed: " + e.toString());
}
Expand All @@ -205,8 +176,7 @@ public void migrateData()
syslog.setHost(descriptor.getHost());
syslog.setPort(descriptor.getPort());
syslog.setSyslogProtocol(descriptor.getSyslogProtocol());
switch (descriptor.getSyslogFormat())
{
switch (descriptor.getSyslogFormat()) {
case RFC3164:
syslog.setMessageFormat(MessageFormat.RFC_3164);
break;
Expand Down Expand Up @@ -234,15 +204,12 @@ public void migrateData()
}

@Override
public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws FormException
{

public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws FormException {
// When not enabling the plugin we just save the enabled state
// without binding the JSON and then return. This avoids problems with missing configuration
// like URLs which can't be parsed when empty, which would lead to errors in the UI.
Boolean e = json.getBoolean("enabled");
if (!e)
{
if (!e) {
enabled = false;
save();
return true;
Expand All @@ -254,8 +221,7 @@ public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws
// logstashIndexer is holder for the dao instance.
// To avoid that we get a new dao instance in case there was no change in configuration
// we compare it to the currently active configuration.
try
{
try {
staplerRequest.bindJSON(this, json);

try {
Expand All @@ -270,23 +236,18 @@ public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws
throw new IllegalArgumentException(ex);
}

if (!Objects.equals(logstashIndexer, activeIndexer))
{
if (!Objects.equals(logstashIndexer, activeIndexer)) {
activeIndexer = logstashIndexer;
}

save();
return true;
}
finally
{
} finally {
configuring = false;
}
}

public static LogstashConfiguration getInstance()
{
public static LogstashConfiguration getInstance() {
return GlobalConfiguration.all().get(LogstashConfiguration.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,50 @@
import hudson.model.Run;

@Extension(ordinal = 1000)
public class LogstashConsoleLogFilter extends ConsoleLogFilter implements Serializable
{
public class LogstashConsoleLogFilter extends ConsoleLogFilter implements Serializable {

private static final Logger LOGGER = Logger.getLogger(LogstashConsoleLogFilter.class.getName());

public LogstashConsoleLogFilter() {}

private static final long serialVersionUID = 1L;

public LogstashConsoleLogFilter() {}

@Override
public OutputStream decorateLogger(Run build, OutputStream logger) throws IOException, InterruptedException
{
public OutputStream decorateLogger(Run build, OutputStream logger) throws IOException, InterruptedException {
LogstashConfiguration configuration = LogstashConfiguration.getInstance();
if (!configuration.isEnabled())
{
if (!configuration.isEnabled()) {
LOGGER.log(Level.FINE, "Logstash is disabled. Logs will not be forwarded.");
return logger;
}

if (build != null && build instanceof AbstractBuild<?, ?>)
{
if (isLogstashEnabled(build))
{
if (build != null && build instanceof AbstractBuild<?, ?>) {
if (isLogstashEnabled(build)) {
LogstashWriter logstash = getLogStashWriter(build, logger);
return new LogstashOutputStream(logger, logstash);
}
else
{
} else {
return logger;
}
}

return logger;
}

LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream)
{
LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream) {
return new LogstashWriter(build, errorStream, null, build.getCharset());
}

private boolean isLogstashEnabled(Run<?, ?> build)
{
private boolean isLogstashEnabled(Run<?, ?> build) {
LogstashConfiguration configuration = LogstashConfiguration.getInstance();
if (configuration.isEnableGlobally())
{
if (configuration.isEnableGlobally()) {
return true;
}

if (build.getParent() instanceof AbstractProject)
{
if (build.getParent() instanceof AbstractProject) {
AbstractProject<?, ?> project = (AbstractProject<?, ?>)build.getParent();
if (project.getProperty(LogstashJobProperty.class) != null)
{
if (project.getProperty(LogstashJobProperty.class) != null) {
return true;
}
}
return false;
}

}
Loading