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

Provide Project via injection #672

Merged
merged 12 commits into from
Nov 7, 2017
14 changes: 0 additions & 14 deletions app/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import com.commercetools.sunrise.email.EmailSender;
import com.commercetools.sunrise.framework.controllers.metrics.SimpleMetricsSphereClientProvider;
import com.commercetools.sunrise.framework.injection.RequestScoped;
import com.commercetools.sunrise.framework.localization.CountryFromSessionProvider;
import com.commercetools.sunrise.framework.localization.CurrencyFromCountryProvider;
import com.commercetools.sunrise.framework.localization.LocaleFromUrlProvider;
import com.commercetools.sunrise.framework.template.cms.FileBasedCmsServiceProvider;
import com.commercetools.sunrise.framework.template.engine.HandlebarsTemplateEngineProvider;
import com.commercetools.sunrise.framework.template.engine.TemplateEngine;
Expand Down Expand Up @@ -90,17 +87,6 @@ protected void configure() {
.toProvider(EmailSenderProvider.class)
.in(Singleton.class);

// Bindings for all user context related
bind(Locale.class)
.toProvider(LocaleFromUrlProvider.class)
.in(RequestScoped.class);
bind(CountryCode.class)
.toProvider(CountryFromSessionProvider.class)
.in(RequestScoped.class);
bind(CurrencyUnit.class)
.toProvider(CurrencyFromCountryProvider.class)
.in(RequestScoped.class);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed from Module.java, because this file belongs to Starter Project and is therefore never updated with new releases, what makes migration more difficult. Now moved to LocalizationModule, which is enabled by default on reference.conf (default configuration), and can be disabled via configuration as explained in #671 in order to bind your own implementations.

// Bindings for the configured faceted search mappers
bind(TermFacetViewModelFactory.class)
.annotatedWith(Names.named("alphabeticallySorted"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.commercetools.sunrise.framework.viewmodels.SimpleViewModelFactory;
import com.commercetools.sunrise.framework.viewmodels.forms.countries.CountryFormSelectableOptionViewModel;
import com.commercetools.sunrise.framework.viewmodels.forms.countries.CountryFormSelectableOptionViewModelFactory;
import com.commercetools.sunrise.framework.localization.ProjectContext;
import com.commercetools.sunrise.ctp.project.ProjectContext;
import com.commercetools.sunrise.framework.injection.RequestScoped;
import com.neovisionaries.i18n.CountryCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

import com.commercetools.sunrise.sessions.country.CountryInSession;
import com.neovisionaries.i18n.CountryCode;
Expand All @@ -13,7 +13,7 @@ public final class CountryFromSessionProvider implements Provider<CountryCode> {
private final CountryInSession countryInSession;

@Inject
public CountryFromSessionProvider(final ProjectContext projectContext, final CountryInSession countryInSession) {
CountryFromSessionProvider(final ProjectContext projectContext, final CountryInSession countryInSession) {
this.projectContext = projectContext;
this.countryInSession = countryInSession;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

import com.neovisionaries.i18n.CountryCode;

Expand All @@ -14,7 +14,7 @@ public final class CurrencyFromCountryProvider implements Provider<CurrencyUnit>
private final ProjectContext projectContext;

@Inject
public CurrencyFromCountryProvider(final CountryCode country, final ProjectContext projectContext) {
CurrencyFromCountryProvider(final CountryCode country, final ProjectContext projectContext) {
this.country = country;
this.projectContext = projectContext;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

import play.mvc.Http;

Expand All @@ -15,7 +15,7 @@ public final class LocaleFromUrlProvider implements Provider<Locale> {
private final ProjectContext projectContext;

@Inject
public LocaleFromUrlProvider(final ProjectContext projectContext) {
LocaleFromUrlProvider(final ProjectContext projectContext) {
this.projectContext = projectContext;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.commercetools.sunrise.ctp.project;

import com.commercetools.sunrise.framework.injection.RequestScoped;
import com.google.inject.AbstractModule;
import com.neovisionaries.i18n.CountryCode;

import javax.money.CurrencyUnit;
import java.util.Locale;

public final class LocalizationModule extends AbstractModule {

@Override
protected void configure() {
bind(Locale.class)
.toProvider(LocaleFromUrlProvider.class)
.in(RequestScoped.class);
bind(CountryCode.class)
.toProvider(CountryFromSessionProvider.class)
.in(RequestScoped.class);
bind(CurrencyUnit.class)
.toProvider(CurrencyFromCountryProvider.class)
.in(RequestScoped.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

public class NoCountryFoundException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

public class NoCurrencyFoundException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

public class NoLocaleFoundException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

import com.google.inject.ImplementedBy;
import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.projects.Project;
import play.Configuration;

import javax.money.CurrencyUnit;
import java.util.List;
Expand Down Expand Up @@ -64,4 +66,8 @@ default boolean isCountrySupported(final CountryCode countryCode) {
default boolean isCurrencySupported(final CurrencyUnit currency) {
return currencies().contains(currency);
}

static ProjectContext of(final Configuration globalConfig, final String configPath, final Project project) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it looks like this method isn't used or do you plan to use it in tests?

return new ProjectContextImpl(globalConfig, configPath, project);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

import com.commercetools.sunrise.play.configuration.SunriseConfigurationException;
import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.client.SphereRequest;
import io.sphere.sdk.client.SphereTimeoutException;
import io.sphere.sdk.models.Base;
import io.sphere.sdk.projects.Project;
import io.sphere.sdk.projects.queries.ProjectGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.Configuration;
Expand All @@ -16,12 +12,10 @@
import javax.inject.Singleton;
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import java.time.Duration;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;

import static io.sphere.sdk.client.SphereClientUtils.blockingWait;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

Expand All @@ -34,26 +28,22 @@
final class ProjectContextImpl extends Base implements ProjectContext {

private static final Logger LOGGER = LoggerFactory.getLogger(ProjectContext.class);
private static final String CONFIG_LANGUAGES = "application.i18n.languages";
private static final String CONFIG_COUNTRIES = "application.countries";
private static final String CONFIG_CURRENCIES = "application.currencies";

private final List<Locale> locales;
private final List<CountryCode> countryCodes;
private final List<CurrencyUnit> currencies;

@Inject
ProjectContextImpl(final Configuration configuration, final SphereClient client) {
try {
final SphereRequest<Project> request = ProjectGet.of();
final Project project = blockingWait(client.execute(request), Duration.ofMinutes(1));
this.locales = projectLocales(configuration, project);
this.countryCodes = projectCountries(configuration, project);
this.currencies = projectCurrencies(configuration, project);
LOGGER.debug("Project Languages {}, Countries {}, Currencies {}", locales, countryCodes, currencies);
} catch (SphereTimeoutException e) {
throw new RuntimeException("Could not fetch project information", e);
}
ProjectContextImpl(final Configuration globalConfig, final Project project) {
this(globalConfig, "sunrise.ctp.project", project);
}

ProjectContextImpl(final Configuration globalConfig, final String configPath, final Project project) {
final Configuration config = globalConfig.getConfig(configPath);
this.locales = projectLocales(config, project);
this.countryCodes = projectCountries(config, project);
this.currencies = projectCurrencies(config, project);
LOGGER.debug("Initialized ProjectContext: Languages {}, Countries {}, Currencies {}", locales, countryCodes, currencies);
}

@Override
Expand All @@ -72,15 +62,15 @@ public List<CurrencyUnit> currencies() {
}

private static List<Locale> projectLocales(final Configuration configuration, final Project project) {
return getValues(configuration, CONFIG_LANGUAGES, Locale::forLanguageTag, project.getLanguageLocales());
return getValues(configuration, "languages", Locale::forLanguageTag, project.getLanguageLocales());
}

private static List<CountryCode> projectCountries(final Configuration configuration, final Project project) {
return getValues(configuration, CONFIG_COUNTRIES, CountryCode::getByCode, project.getCountries());
return getValues(configuration, "countries", CountryCode::getByCode, project.getCountries());
}

private static List<CurrencyUnit> projectCurrencies(final Configuration configuration, final Project project) {
return getValues(configuration, CONFIG_CURRENCIES, Monetary::getCurrency, project.getCurrencyUnits());
return getValues(configuration, "currencies", Monetary::getCurrency, project.getCurrencyUnits());
}

private static <T> List<T> getValues(final Configuration configuration, final String configKey,
Expand All @@ -94,4 +84,4 @@ private static <T> List<T> getValues(final Configuration configuration, final St
}
return values;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.commercetools.sunrise.ctp.project;

import com.google.inject.AbstractModule;
import io.sphere.sdk.projects.Project;

import javax.inject.Singleton;

public final class ProjectModule extends AbstractModule {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name clearly states that it provides a project, but as an user you have to look at the source code to see what this module provides. What do you think about adding some javadoc here?


@Override
protected void configure() {
bind(Project.class)
.toProvider(ProjectProvider.class)
.in(Singleton.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.commercetools.sunrise.ctp.project;

import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.client.SphereRequest;
import io.sphere.sdk.projects.Project;
import io.sphere.sdk.projects.queries.ProjectGet;

import javax.inject.Inject;
import javax.inject.Provider;
import java.time.Duration;

import static io.sphere.sdk.client.SphereClientUtils.blockingWait;

public final class ProjectProvider implements Provider<Project> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now could even remove the public modifier from this class. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I would like to let developers to bind using this provider, in case they need to override some parts of an injection module and reuse others.


private final SphereClient sphereClient;

@Inject
ProjectProvider(final SphereClient sphereClient) {
this.sphereClient = sphereClient;
}

@Override
public Project get() {
final SphereRequest<Project> request = ProjectGet.of();
return blockingWait(sphereClient.execute(request), Duration.ofSeconds(30));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

import com.google.inject.ImplementedBy;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.commercetools.sunrise.framework.localization;
package com.commercetools.sunrise.ctp.project;

import com.commercetools.sunrise.framework.injection.RequestScoped;
import io.sphere.sdk.models.Base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.commercetools.sunrise.cms.CmsService;
import com.commercetools.sunrise.play.configuration.SunriseConfigurationException;
import com.commercetools.sunrise.framework.localization.ProjectContext;
import com.commercetools.sunrise.ctp.project.ProjectContext;
import com.commercetools.sunrise.framework.template.cms.filebased.FileBasedCmsService;
import com.commercetools.sunrise.framework.template.i18n.composite.CompositeI18nResolver;
import com.commercetools.sunrise.framework.template.i18n.composite.CompositeI18nResolverFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.commercetools.sunrise.cms.CmsPage;
import com.commercetools.sunrise.cms.CmsService;
import com.commercetools.sunrise.framework.localization.UserLanguage;
import com.commercetools.sunrise.ctp.project.UserLanguage;
import com.commercetools.sunrise.framework.viewmodels.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.commercetools.sunrise.framework.template.engine;

import com.commercetools.sunrise.cms.CmsService;
import com.commercetools.sunrise.framework.localization.UserLanguage;
import com.commercetools.sunrise.ctp.project.UserLanguage;
import com.commercetools.sunrise.framework.viewmodels.PageData;
import com.commercetools.sunrise.framework.viewmodels.content.PageContent;
import play.twirl.api.Content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.commercetools.sunrise.cms.CmsService;
import com.commercetools.sunrise.framework.hooks.RequestHookRunner;
import com.commercetools.sunrise.framework.hooks.application.PageDataReadyHook;
import com.commercetools.sunrise.framework.localization.UserLanguage;
import com.commercetools.sunrise.ctp.project.UserLanguage;
import com.commercetools.sunrise.framework.viewmodels.PageData;
import com.commercetools.sunrise.framework.viewmodels.PageDataFactory;
import com.commercetools.sunrise.framework.viewmodels.content.PageContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.commercetools.sunrise.framework.template.i18n;

import com.commercetools.sunrise.play.configuration.SunriseConfigurationException;
import com.commercetools.sunrise.framework.localization.ProjectContext;
import com.commercetools.sunrise.ctp.project.ProjectContext;
import com.commercetools.sunrise.framework.template.i18n.composite.CompositeI18nResolverFactory;
import com.google.inject.Provider;
import play.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.commercetools.sunrise.framework.template.i18n;

import com.commercetools.sunrise.framework.localization.UserLanguage;
import com.commercetools.sunrise.ctp.project.UserLanguage;
import com.commercetools.sunrise.framework.injection.RequestScoped;

import javax.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.commercetools.sunrise.framework.viewmodels.forms.countries;

import com.commercetools.sunrise.framework.localization.ProjectContext;
import com.commercetools.sunrise.ctp.project.ProjectContext;
import com.commercetools.sunrise.framework.injection.RequestScoped;
import com.commercetools.sunrise.framework.viewmodels.forms.FormFieldViewModelFactory;
import com.commercetools.sunrise.framework.viewmodels.forms.FormFieldWithOptions;
Expand Down
Loading