Skip to content

Commit

Permalink
Turned Guice dependencies into singletons to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavkarlsson committed Mar 10, 2018
1 parent 2cb6049 commit 0760945
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package se.gustavkarlsson.rocketchat.jira_trigger.configuration;

import com.google.inject.Singleton;
import se.gustavkarlsson.rocketchat.jira_trigger.server.Server;

import javax.inject.Inject;

import static org.apache.commons.lang3.Validate.inclusiveBetween;
import static org.apache.commons.lang3.Validate.notNull;

@Singleton
public class AppConfiguration {
private static final String KEY_PREFIX = "app.";
static final String PORT_KEY = KEY_PREFIX + "port";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.moandjiezana.toml.Toml;
import se.gustavkarlsson.rocketchat.jira_trigger.di.qualifiers.Default;

Expand Down Expand Up @@ -30,16 +31,19 @@ protected void configure() {

@Provides
@Default
@Singleton
Toml provideDefaultToml() {
return new Toml().read(ConfigurationModule.class.getClassLoader().getResourceAsStream(DEFAULTS_FILE_NAME));
}

@Provides
@Singleton
Toml provideTomlFromFile() {
return Optional.ofNullable(configFile).map(configFile -> new Toml().read(configFile)).orElse(null);
}

@Provides
@Singleton
ConfigMap provideConfigMap(@Default Toml defaultToml, @Nullable Toml configFileToml) {
List<ConfigMap> configMaps = new ArrayList<>();
configMaps.add(new EnvVarConfigMap(System.getenv()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package se.gustavkarlsson.rocketchat.jira_trigger.configuration;

import com.google.inject.Singleton;

import javax.inject.Inject;
import java.net.URI;

import static org.apache.commons.lang3.Validate.notNull;

@Singleton
public class JiraConfiguration {
private static final String KEY_PREFIX = "jira.";
static final String URI_KEY = KEY_PREFIX + "uri";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package se.gustavkarlsson.rocketchat.jira_trigger.configuration;

import com.google.inject.Singleton;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

Expand All @@ -13,6 +14,7 @@
import static java.util.stream.Collectors.toSet;
import static org.apache.commons.lang3.Validate.notNull;

@Singleton
public class MessageConfiguration {
private static final String KEY_PREFIX = "message.";
static final String USERNAME_KEY = KEY_PREFIX + "username";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package se.gustavkarlsson.rocketchat.jira_trigger.configuration;

import com.google.inject.Singleton;

import javax.inject.Inject;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -8,6 +10,7 @@

import static org.apache.commons.lang3.Validate.notNull;

@Singleton
public class RocketChatConfiguration {
private static final String KEY_PREFIX = "rocketchat.";
static final String TOKENS_KEY = KEY_PREFIX + "tokens";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.JiraConfiguration;

Expand All @@ -13,6 +14,7 @@

import static org.slf4j.LoggerFactory.getLogger;

@Singleton
class AuthHandlerProvider implements Provider<AuthenticationHandler> {
private static final Logger log = getLogger(AuthHandlerProvider.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.JiraConfiguration;

import java.io.Console;
Expand All @@ -18,22 +19,26 @@ protected void configure() {
}

@Provides
@Singleton
JiraRestClient provideJiraRestClient(JiraConfiguration jiraConfig, AuthenticationHandler authHandler) {
AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
return factory.create(jiraConfig.getUri(), authHandler);
}

@Provides
@Singleton
MetadataRestClient provideMetadataRestClient(JiraRestClient jiraClient) {
return jiraClient.getMetadataClient();
}

@Provides
@Singleton
IssueRestClient provideIssueRestClient(JiraRestClient jiraClient) {
return jiraClient.getIssueClient();
}

@Provides
@Singleton
Console provideConsole() {
return System.console();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.MessageConfiguration;
import se.gustavkarlsson.rocketchat.jira_trigger.messages.field_creators.FieldCreator;

import java.util.List;

import static org.apache.commons.lang3.Validate.notNull;

@Singleton
class DefaultFieldCreatorsProvider implements Provider<List<FieldCreator>> {
private final FieldCreatorMapper fieldCreatorMapper;
private final MessageConfiguration messageConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.MessageConfiguration;
import se.gustavkarlsson.rocketchat.jira_trigger.messages.field_creators.FieldCreator;

import java.util.List;

import static org.apache.commons.lang3.Validate.notNull;

@Singleton
class ExtendedFieldCreatorsProvider implements Provider<List<FieldCreator>> {
private final FieldCreatorMapper fieldCreatorMapper;
private final MessageConfiguration messageConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.JiraConfiguration;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.MessageConfiguration;
Expand All @@ -20,16 +21,19 @@ protected void configure() {
}

@Provides
@Singleton
ToRocketChatMessageFactory provideToRocketChatMessageFactory(MessageConfiguration messageConfig) {
return new ToRocketChatMessageFactory(messageConfig.getUsername(), messageConfig.getIconUrl());
}

@Provides
@Singleton
FieldCreatorMapper provideFieldCreatorMapper(MessageConfiguration messageConfig) {
return new FieldCreatorMapper(messageConfig.isUseRealNames(), messageConfig.getDateFormatter());
}

@Provides
@Singleton
AttachmentCreator provideAttachmentConverter(MessageConfiguration messageConfig, JiraConfiguration jiraConfig, @Default List<FieldCreator> defaultFieldCreators, List<FieldCreator> extendedFieldCreators) {
return new AttachmentCreator(messageConfig.isPriorityColors(), messageConfig.getDefaultColor(), defaultFieldCreators, extendedFieldCreators, jiraConfig.getUri());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.atlassian.jira.rest.client.api.IssueRestClient;
import com.atlassian.jira.rest.client.api.RestClientException;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import se.gustavkarlsson.rocketchat.jira_trigger.messages.AttachmentCreator;
import se.gustavkarlsson.rocketchat.jira_trigger.messages.ToRocketChatMessageFactory;
Expand All @@ -26,6 +27,7 @@
import static org.eclipse.jetty.http.HttpStatus.NOT_FOUND_404;
import static org.slf4j.LoggerFactory.getLogger;

@Singleton
public class DetectIssueRoute extends RocketChatMessageRoute {
private static final Logger log = getLogger(DetectIssueRoute.class);

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

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.MessageConfiguration;

public class RouteModule extends AbstractModule {
Expand All @@ -10,6 +11,7 @@ protected void configure() {
}

@Provides
@Singleton
JiraKeyParser provideJiraKeyParser(MessageConfiguration messageConfig) {
return new JiraKeyParser(messageConfig.getWhitelistedJiraKeyPrefixes(), messageConfig.getWhitelistedJiraKeySuffixes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.atlassian.jira.rest.client.api.MetadataRestClient;
import com.atlassian.jira.rest.client.api.domain.ServerInfo;
import com.atlassian.util.concurrent.Promise;
import com.google.inject.Singleton;
import org.slf4j.Logger;

import javax.inject.Inject;

import static org.apache.commons.lang3.Validate.notNull;
import static org.slf4j.LoggerFactory.getLogger;

@Singleton
class JiraServerInfoLogger {
private static final Logger log = getLogger(JiraServerInfoLogger.class);

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

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.AppConfiguration;
import se.gustavkarlsson.rocketchat.jira_trigger.routes.DetectIssueRoute;

Expand All @@ -11,6 +12,7 @@ protected void configure() {
}

@Provides
@Singleton
Server provideServer(AppConfiguration appConfig, DetectIssueRoute detectIssueRoute, JiraServerInfoLogger jiraServerInfoLogger) {
return new Server(appConfig.getMaxThreads(), appConfig.getPort(), detectIssueRoute, jiraServerInfoLogger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import se.gustavkarlsson.rocketchat.jira_trigger.configuration.RocketChatConfiguration;

import java.util.Arrays;
Expand All @@ -14,21 +15,25 @@ protected void configure() {
}

@Provides
@Singleton
TokenValidator provideTokenValidator(RocketChatConfiguration rocketChatConfig) {
return new TokenValidator(rocketChatConfig.getTokens());
}

@Provides
@Singleton
UserValidator provideUserValidator(RocketChatConfiguration rocketChatConfig) {
return new UserValidator(rocketChatConfig.getBlacklistedUsers(), rocketChatConfig.getWhitelistedUsers());
}

@Provides
@Singleton
ChannelValidator provideChannelValidator(RocketChatConfiguration rocketChatConfig) {
return new ChannelValidator(rocketChatConfig.getBlacklistedChannels(), rocketChatConfig.getWhitelistedChannels());
}

@Provides
@Singleton
List<Validator> provideValidators(TokenValidator tokenValidator, UserValidator userValidator, ChannelValidator channelValidator) {
return Arrays.asList(tokenValidator, userValidator, channelValidator);
}
Expand Down

0 comments on commit 0760945

Please sign in to comment.