Skip to content

Commit

Permalink
Merge pull request #105 from TouK/configurationRefactor
Browse files Browse the repository at this point in the history
Configuration refactor
  • Loading branch information
pjagielski committed Jul 3, 2015
2 parents 7a72a5d + 7432aef commit 1bd60fd
Show file tree
Hide file tree
Showing 59 changed files with 448 additions and 354 deletions.
9 changes: 4 additions & 5 deletions src/main/java/pl/touk/sputnik/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ public static void main(String[] args) {
System.exit(1);
}

ConfigurationHolder.initFromFile(commandLine.getOptionValue(CliOption.CONF.getCommandLineParam()));
Configuration configuration = ConfigurationHolder.instance();
Configuration configuration = ConfigurationBuilder.initFromFile(commandLine.getOptionValue(CliOption.CONF.getCommandLineParam()));
configuration.updateWithCliOptions(commandLine);

ConnectorFacade facade = getConnectorFacade(configuration);
new Engine(facade).run();
new Engine(facade, configuration).run();
}

private static ConnectorFacade getConnectorFacade(Configuration configuration) {
ConnectorType connectorType = ConnectorType.getValidConnectorType(configuration.getProperty(GeneralOption.CONNECTOR_TYPE));
ConnectorFacade facade = null;
try {
facade = ConnectorFacadeFactory.INSTANCE.build(connectorType);
facade.validate(ConfigurationHolder.instance());
facade = ConnectorFacadeFactory.INSTANCE.build(connectorType, configuration);
facade.validate(configuration);
} catch (GeneralOptionNotSupportedException e) {
System.out.println(e.getMessage());
System.exit(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,40 @@
import com.google.common.io.Resources;

@Slf4j
public class ConfigurationHolder {
public class ConfigurationBuilder {

private static Configuration INSTANCE;

public static void initFromFile(String configurationFilename) {
public static Configuration initFromFile(String configurationFilename) {
notBlank(configurationFilename, "You need to provide filename with configuration properties");
log.info("Initializing configuration properties from file {}", configurationFilename);

try (FileReader resourceReader = new FileReader(configurationFilename)){
Properties properties = new Properties();
properties.load(resourceReader);
initFromProperties(properties);
return initFromProperties(properties);
} catch (IOException e) {
log.error("Configuration initialization failed", e);
throw new RuntimeException("Configuration file " + configurationFilename + " cannot be loaded");
}
}

public static void initFromResource(String configurationResource) {
public static Configuration initFromResource(String configurationResource) {
notBlank(configurationResource, "You need to provide url with configuration properties");
log.info("Initializing configuration properties from url {}", configurationResource);

CharSource charSource = Resources.asCharSource(getResource(configurationResource), Charsets.UTF_8);
try (Reader resourceStream = charSource.openStream()) {
Properties properties = new Properties();
properties.load(resourceStream);
initFromProperties(properties);
return initFromProperties(properties);
} catch (IOException e) {
log.error("Configuration initialization failed", e);
throw new RuntimeException(e);
}
}

public static void initFromProperties(Properties properties) {
setInstance(new Configuration(properties));
}

public static void reset() {
INSTANCE = null;
public static Configuration initFromProperties(Properties properties) {
return new Configuration(properties);
}

public static Configuration instance() {
return INSTANCE;
}

private static void setInstance(Configuration instance) {
INSTANCE = instance;
}

}
20 changes: 10 additions & 10 deletions src/main/java/pl/touk/sputnik/connector/ConnectorDetails.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.touk.sputnik.connector;

import lombok.Getter;
import pl.touk.sputnik.configuration.ConfigurationHolder;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOption;

import static org.apache.commons.lang3.Validate.notBlank;
Expand All @@ -17,18 +17,18 @@ public class ConnectorDetails {
private String useHttps;
private boolean isHttps;

public ConnectorDetails() {
buildFromConfiguration();
public ConnectorDetails(Configuration configuration) {
buildFromConfiguration(configuration);
validate();
}

private void buildFromConfiguration() {
host = ConfigurationHolder.instance().getProperty(GeneralOption.HOST);
port = Integer.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.PORT));
path = ConfigurationHolder.instance().getProperty(GeneralOption.PATH);
username = ConfigurationHolder.instance().getProperty(GeneralOption.USERNAME);
password = ConfigurationHolder.instance().getProperty(GeneralOption.PASSWORD);
useHttps = ConfigurationHolder.instance().getProperty(GeneralOption.USE_HTTPS);
private void buildFromConfiguration(Configuration configuration) {
host = configuration.getProperty(GeneralOption.HOST);
port = Integer.valueOf(configuration.getProperty(GeneralOption.PORT));
path = configuration.getProperty(GeneralOption.PATH);
username = configuration.getProperty(GeneralOption.USERNAME);
password = configuration.getProperty(GeneralOption.PASSWORD);
useHttps = configuration.getProperty(GeneralOption.USE_HTTPS);
isHttps = Boolean.parseBoolean(useHttps);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.NotNull;

import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException;
import pl.touk.sputnik.connector.gerrit.GerritFacadeBuilder;
import pl.touk.sputnik.connector.stash.StashFacadeBuilder;
Expand All @@ -13,12 +14,12 @@ public class ConnectorFacadeFactory {
StashFacadeBuilder stashFacadeBuilder = new StashFacadeBuilder();

@NotNull
public ConnectorFacade build(@NotNull ConnectorType type) {
public ConnectorFacade build(@NotNull ConnectorType type, Configuration configuration) {
switch (type) {
case STASH:
return stashFacadeBuilder.build();
return stashFacadeBuilder.build(configuration);
case GERRIT:
return gerritFacadeBuilder.build();
return gerritFacadeBuilder.build(configuration);
default:
throw new GeneralOptionNotSupportedException("Connector " + type.getName() + " is not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.configuration.ConfigurationHolder;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.CliOption;
import pl.touk.sputnik.connector.ConnectorDetails;
import pl.touk.sputnik.connector.http.HttpHelper;
Expand All @@ -21,9 +21,9 @@ public class GerritFacadeBuilder {
private HttpHelper httpHelper = new HttpHelper();

@NotNull
public GerritFacade build() {
ConnectorDetails connectorDetails = new ConnectorDetails();
GerritPatchset gerritPatchset = buildGerritPatchset();
public GerritFacade build(Configuration configuration) {
ConnectorDetails connectorDetails = new ConnectorDetails(configuration);
GerritPatchset gerritPatchset = buildGerritPatchset(configuration);

GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
String hostUri = httpHelper.buildHttpHostUri(connectorDetails);
Expand All @@ -46,9 +46,9 @@ public HttpClientBuilder extend(HttpClientBuilder httpClientBuilder, GerritAuthD
}

@NotNull
private GerritPatchset buildGerritPatchset() {
String changeId = ConfigurationHolder.instance().getProperty(CliOption.CHANGE_ID);
String revisionId = ConfigurationHolder.instance().getProperty(CliOption.REVISION_ID);
private GerritPatchset buildGerritPatchset(Configuration configuration) {
String changeId = configuration.getProperty(CliOption.CHANGE_ID);
String revisionId = configuration.getProperty(CliOption.REVISION_ID);

notBlank(changeId, "You must provide non blank Gerrit change Id");
notBlank(revisionId, "You must provide non blank Gerrit revision Id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minidev.json.JSONObject;
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.ConfigurationHolder;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException;
import pl.touk.sputnik.connector.ConnectorFacade;
Expand All @@ -32,9 +31,11 @@
public class StashFacade implements ConnectorFacade {
private StashConnector stashConnector;
private ObjectMapper objectMapper = new ObjectMapper();
private final Configuration configuration;

public StashFacade(@NotNull StashConnector stashConnector) {
public StashFacade(@NotNull StashConnector stashConnector, Configuration configuration) {
this.stashConnector = stashConnector;
this.configuration = configuration;
}

@Override
Expand Down Expand Up @@ -84,7 +85,7 @@ public void setReview(@NotNull Review review) {
}

private void sendFileComments(Review review) {
boolean commentOnlyChangedLines = Boolean.parseBoolean(ConfigurationHolder.instance().getProperty(GeneralOption.COMMENT_ONLY_CHANGED_LINES));
boolean commentOnlyChangedLines = Boolean.parseBoolean(configuration.getProperty(GeneralOption.COMMENT_ONLY_CHANGED_LINES));

for (ReviewFile reviewFile : review.getFiles()) {
SingleFileChanges changes = changesForSingleFile(reviewFile.getReviewFilename());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.configuration.CliOption;
import pl.touk.sputnik.configuration.ConfigurationHolder;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.connector.ConnectorDetails;
import pl.touk.sputnik.connector.http.HttpConnector;
Expand All @@ -19,22 +19,22 @@ public class StashFacadeBuilder {
private HttpHelper httpHelper = new HttpHelper();

@NotNull
public StashFacade build() {
ConnectorDetails connectorDetails = new ConnectorDetails();
StashPatchset stashPatchset = buildStashPatchset();
public StashFacade build(Configuration configuration) {
ConnectorDetails connectorDetails = new ConnectorDetails(configuration);
StashPatchset stashPatchset = buildStashPatchset(configuration);

HttpHost httpHost = httpHelper.buildHttpHost(connectorDetails);
HttpClientContext httpClientContext = httpHelper.buildClientContext(httpHost, new BasicScheme());
CloseableHttpClient closeableHttpClient = httpHelper.buildClient(httpHost, connectorDetails);

return new StashFacade(new StashConnector(new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath()), stashPatchset));
return new StashFacade(new StashConnector(new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath()), stashPatchset), configuration);
}

@NotNull
public StashPatchset buildStashPatchset() {
String pullRequestId = ConfigurationHolder.instance().getProperty(CliOption.PULL_REQUEST_ID);
String repositorySlug = ConfigurationHolder.instance().getProperty(GeneralOption.REPOSITORY_SLUG);
String projectKey = ConfigurationHolder.instance().getProperty(GeneralOption.PROJECT_KEY);
public StashPatchset buildStashPatchset(Configuration configuration) {
String pullRequestId = configuration.getProperty(CliOption.PULL_REQUEST_ID);
String repositorySlug = configuration.getProperty(GeneralOption.REPOSITORY_SLUG);
String projectKey = configuration.getProperty(GeneralOption.PROJECT_KEY);

notBlank(pullRequestId, "You must provide non blank Stash pull request id");
notBlank(repositorySlug, "You must provide non blank Stash repository slug");
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/pl/touk/sputnik/engine/Engine.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package pl.touk.sputnik.engine;

import lombok.extern.slf4j.Slf4j;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.connector.ConnectorFacade;
import pl.touk.sputnik.engine.visitor.AfterReviewVisitor;
import pl.touk.sputnik.engine.visitor.BeforeReviewVisitor;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewFile;
import pl.touk.sputnik.review.ReviewFormatterFactory;
import pl.touk.sputnik.review.ReviewProcessor;

import java.util.List;
Expand All @@ -14,26 +16,28 @@
public class Engine {

private final ConnectorFacade facade;
private final Configuration config;

public Engine(ConnectorFacade facade) {
public Engine(ConnectorFacade facade, Configuration configuration) {
this.facade = facade;
this.config = configuration;
}

public void run() {
List<ReviewFile> reviewFiles = facade.listFiles();
Review review = new Review(reviewFiles);
Review review = new Review(reviewFiles, ReviewFormatterFactory.get(config));

for (BeforeReviewVisitor beforeReviewVisitor : new VisitorBuilder().buildBeforeReviewVisitors()) {
for (BeforeReviewVisitor beforeReviewVisitor : new VisitorBuilder().buildBeforeReviewVisitors(config)) {
beforeReviewVisitor.beforeReview(review);
}

List<ReviewProcessor> processors = new ProcessorBuilder().buildProcessors();
List<ReviewProcessor> processors = ProcessorBuilder.buildProcessors(config);
ReviewRunner reviewRunner = new ReviewRunner(review);
for (ReviewProcessor processor : processors) {
reviewRunner.review(processor);
}

for (AfterReviewVisitor afterReviewVisitor : new VisitorBuilder().buildAfterReviewVisitors()) {
for (AfterReviewVisitor afterReviewVisitor : new VisitorBuilder().buildAfterReviewVisitors(config)) {
afterReviewVisitor.afterReview(review);
}

Expand Down
36 changes: 18 additions & 18 deletions src/main/java/pl/touk/sputnik/engine/ProcessorBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.touk.sputnik.engine;

import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.configuration.ConfigurationHolder;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.processor.checkstyle.CheckstyleProcessor;
import pl.touk.sputnik.processor.codenarc.CodeNarcProcessor;
Expand All @@ -19,31 +19,31 @@
public class ProcessorBuilder {

@NotNull
public List<ReviewProcessor> buildProcessors() {
public static List<ReviewProcessor> buildProcessors(Configuration configuration) {
List<ReviewProcessor> processors = new ArrayList<>();
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.CHECKSTYLE_ENABLED))) {
processors.add(new CheckstyleProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.CHECKSTYLE_ENABLED))) {
processors.add(new CheckstyleProcessor(configuration));
}
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.PMD_ENABLED))) {
processors.add(new PmdProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.PMD_ENABLED))) {
processors.add(new PmdProcessor(configuration));
}
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.FINDBUGS_ENABLED))) {
processors.add(new FindBugsProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.FINDBUGS_ENABLED))) {
processors.add(new FindBugsProcessor(configuration));
}
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.SCALASTYLE_ENABLED))) {
processors.add(new ScalastyleProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.SCALASTYLE_ENABLED))) {
processors.add(new ScalastyleProcessor(configuration));
}
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.CODE_NARC_ENABLED))) {
processors.add(new CodeNarcProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.CODE_NARC_ENABLED))) {
processors.add(new CodeNarcProcessor(configuration));
}
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.JSLINT_ENABLED))) {
processors.add(new JsLintProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.JSLINT_ENABLED))) {
processors.add(new JsLintProcessor(configuration));
}
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.JSHINT_ENABLED))) {
processors.add(new JsHintProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.JSHINT_ENABLED))) {
processors.add(new JsHintProcessor(configuration));
}
if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.SONAR_ENABLED))) {
processors.add(new SonarProcessor());
if (Boolean.valueOf(configuration.getProperty(GeneralOption.SONAR_ENABLED))) {
processors.add(new SonarProcessor(configuration));
}
return processors;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/pl/touk/sputnik/engine/ReviewRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewException;
import pl.touk.sputnik.review.ReviewProcessor;
Expand Down
Loading

0 comments on commit 1bd60fd

Please sign in to comment.