Skip to content

Commit

Permalink
JAMES-1842: Rename GuiceJamesServer to GuiceJmapJamesServer, Abstract…
Browse files Browse the repository at this point in the history
…JamesServerTest to AbstractJmapJamesServerTest, add more GuiceJamesServer
  • Loading branch information
Quynh Nguyen committed Nov 18, 2016
1 parent b9a9402 commit 11771d0
Show file tree
Hide file tree
Showing 28 changed files with 707 additions and 143 deletions.
Expand Up @@ -25,7 +25,7 @@ public class JPAId implements MailboxId {
public static class Factory implements MailboxId.Factory {
@Override
public JPAId fromString(String serialized) {
return of(Long.parseLong(serialized));
return of(Long.valueOf(serialized));
}
}

Expand Down
3 changes: 0 additions & 3 deletions mailbox/jpa/src/main/resources/META-INF/persistence.xml
Expand Up @@ -30,9 +30,6 @@
<class>org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMailboxMessage</class>
<class>org.apache.james.mailbox.jpa.mail.model.JPAProperty</class>
<class>org.apache.james.mailbox.jpa.user.model.JPASubscription</class>
<class>org.apache.james.domainlist.jpa.model.JPADomain</class>
<class>org.apache.james.user.jpa.model.JPAUser</class>
<class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.jdbc.MappingDefaults" value="ForeignKeyDeleteAction=cascade, JoinForeignKeyDeleteAction=cascade"/>
Expand Down
Expand Up @@ -35,7 +35,7 @@
import com.google.inject.Provides;
import com.google.inject.Singleton;

public class CassandraJamesServerTest extends AbstractJamesServerTest {
public class CassandraJamesServerTest extends AbstractJmapJamesServerTest {

private TemporaryFolder temporaryFolder = new TemporaryFolder();
private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder);
Expand Down
@@ -0,0 +1,99 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james;

import java.util.Arrays;
import java.util.Optional;

import javax.annotation.PreDestroy;

import org.apache.james.modules.CommonServicesModule;
import org.apache.james.modules.MailetProcessingModule;
import org.apache.james.modules.ProtocolsModuleWithoutJMAP;
import org.apache.james.onami.lifecycle.Stager;
import org.apache.james.utils.ConfigurationsPerformer;
import org.apache.james.utils.ExtendedServerProbe;
import org.apache.james.utils.GuiceServerProbe;
import org.apache.james.webadmin.Port;
import org.apache.james.webadmin.WebAdminServer;

import com.google.common.collect.Iterables;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Modules;

public class GuiceJamesServer {
private final Module module;
private Stager<PreDestroy> preDestroy;
private GuiceServerProbe serverProbe;
private Optional<Port> webadminPort;

public GuiceJamesServer() {
this(Modules.combine(
new CommonServicesModule(),
new ProtocolsModuleWithoutJMAP(),
new MailetProcessingModule()));
}

private GuiceJamesServer(Module module) {
this.module = module;
}

public GuiceJamesServer combineWith(Module... modules) {
return new GuiceJamesServer(Modules.combine(Iterables.concat(Arrays.asList(module), Arrays.asList(modules))));
}

public GuiceJamesServer overrideWith(Module... overrides) {
return new GuiceJamesServer(Modules.override(module).with(overrides));
}

public void start() throws Exception {
Injector injector = Guice.createInjector(module);
preDestroy = injector.getInstance(Key.get(new TypeLiteral<Stager<PreDestroy>>() {}));
injector.getInstance(ConfigurationsPerformer.class).initModules();
serverProbe = injector.getInstance(GuiceServerProbe.class);
webadminPort =locateWebAdminPort(injector);
}

private Optional<Port> locateWebAdminPort(Injector injector) {
try {
return Optional.of(injector.getInstance(WebAdminServer.class).getPort());
} catch(Exception e) {
return Optional.empty();
}
}

public void stop() {
if (preDestroy != null) {
preDestroy.stage();
}
}

public ExtendedServerProbe serverProbe() {
return serverProbe;
}

public Optional<Port> getWebadminPort() {
return webadminPort;
}

}
Expand Up @@ -57,7 +57,7 @@ public GuiceJmapJamesServer() {
new MailetProcessingModule()));
}

public GuiceJmapJamesServer(Module module) {
private GuiceJmapJamesServer(Module module) {
this.module = module;
}

Expand Down
Expand Up @@ -38,8 +38,6 @@
import org.apache.james.mailbox.MailboxManager.SearchCapabilities;
import org.apache.james.mailetcontainer.impl.MatcherMailetPair;
import org.apache.james.modules.server.CamelMailetContainerModule;
import org.apache.james.transport.mailets.RemoveMimeHeader;
import org.apache.james.transport.matchers.All;
import org.apache.james.transport.matchers.RecipientIsLocal;
import org.apache.james.utils.ConfigurationPerformer;
import org.apache.james.utils.PropertiesProvider;
Expand Down Expand Up @@ -71,7 +69,6 @@ protected void configure() {

Multibinder<CamelMailetContainerModule.TransportProcessorCheck> transportProcessorChecks = Multibinder.newSetBinder(binder(), CamelMailetContainerModule.TransportProcessorCheck.class);
transportProcessorChecks.addBinding().to(VacationMailetCheck.class);
transportProcessorChecks.addBinding().to(BccMailetCheck.class);

bind(SystemMailboxesProvider.class).to(SystemMailboxesProviderImpl.class);
}
Expand Down Expand Up @@ -134,16 +131,4 @@ public void check(List<MatcherMailetPair> pairs) throws ConfigurationException {
}
}

public static class BccMailetCheck implements CamelMailetContainerModule.TransportProcessorCheck {
@Override
public void check(List<MatcherMailetPair> pairs) throws ConfigurationException {
Preconditions.checkNotNull(pairs);
pairs.stream()
.filter(pair -> pair.getMailet().getClass().equals(RemoveMimeHeader.class))
.filter(pair -> pair.getMatcher().getClass().equals(All.class))
.filter(pair -> pair.getMailet().getMailetConfig().getInitParameter("name").equals("bcc"))
.findAny()
.orElseThrow(() -> new ConfigurationException("Missing RemoveMimeHeader in mailets configuration (mailetcontainer -> processors -> transport). Should be configured to remove Bcc header"));
}
}
}
Expand Up @@ -33,7 +33,6 @@
import org.apache.james.modules.server.ConfigurationProviderModule;
import org.apache.james.modules.server.DNSServiceModule;
import org.apache.james.onami.lifecycle.PreDestroyModule;
import org.apache.james.utils.JmapGuiceServerProbe;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
Expand All @@ -55,7 +54,6 @@ protected void configure() {
install(new AsyncTasksExecutorModule());

bind(FileSystemImpl.class).in(Scopes.SINGLETON);
bind(JmapGuiceServerProbe.class).in(Scopes.SINGLETON);

bind(FileSystem.class).to(FileSystemImpl.class);
}
Expand Down
Expand Up @@ -26,8 +26,10 @@
import org.apache.james.modules.protocols.ProtocolHandlerModule;
import org.apache.james.modules.protocols.SMTPServerModule;
import org.apache.james.modules.server.WebAdminServerModule;
import org.apache.james.utils.JmapGuiceServerProbe;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;

public class ProtocolsModule extends AbstractModule {

Expand All @@ -41,6 +43,8 @@ protected void configure() {
install(new LMTPServerModule());
install(new ManageSieveServerModule());
install(new WebAdminServerModule());

bind(JmapGuiceServerProbe.class).in(Scopes.SINGLETON);
}

}
Expand Up @@ -7,8 +7,10 @@
import org.apache.james.modules.protocols.ProtocolHandlerModule;
import org.apache.james.modules.protocols.SMTPServerModule;
import org.apache.james.modules.server.WebAdminServerModule;
import org.apache.james.utils.GuiceServerProbe;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;

public class ProtocolsModuleWithoutJMAP extends AbstractModule {
@Override
Expand All @@ -20,6 +22,9 @@ protected void configure() {
install(new LMTPServerModule());
install(new ManageSieveServerModule());
install(new WebAdminServerModule());

bind(GuiceServerProbe.class).in(Scopes.SINGLETON);

}

}
Expand Up @@ -39,6 +39,8 @@
import org.apache.james.mailetcontainer.impl.camel.CamelCompositeProcessor;
import org.apache.james.mailetcontainer.impl.camel.CamelMailetProcessor;
import org.apache.james.queue.api.MailQueueFactory;
import org.apache.james.transport.mailets.RemoveMimeHeader;
import org.apache.james.transport.matchers.All;
import org.apache.james.user.api.UsersRepository;
import org.apache.james.utils.ConfigurationPerformer;
import org.apache.james.utils.ConfigurationProvider;
Expand All @@ -48,6 +50,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.inject.AbstractModule;
Expand Down Expand Up @@ -75,6 +78,9 @@ protected void configure() {
bind(MatcherLoader.class).to(GuiceMatcherLoader.class);

Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(MailetModuleConfigurationPerformer.class);

Multibinder<CamelMailetContainerModule.TransportProcessorCheck> transportProcessorChecks = Multibinder.newSetBinder(binder(), CamelMailetContainerModule.TransportProcessorCheck.class);
transportProcessorChecks.addBinding().to(BccMailetCheck.class);
}

@Provides
Expand Down Expand Up @@ -164,4 +170,18 @@ public List<Class<? extends Configurable>> forClasses() {
public interface TransportProcessorCheck {
void check(List<MatcherMailetPair> pairs) throws ConfigurationException;
}

public static class BccMailetCheck implements CamelMailetContainerModule.TransportProcessorCheck {
@Override
public void check(List<MatcherMailetPair> pairs) throws ConfigurationException {
Preconditions.checkNotNull(pairs);
pairs.stream()
.filter(pair -> pair.getMailet().getClass().equals(RemoveMimeHeader.class))
.filter(pair -> pair.getMatcher().getClass().equals(All.class))
.filter(pair -> pair.getMailet().getMailetConfig().getInitParameter("name").equals("bcc"))
.findAny()
.orElseThrow(() -> new ConfigurationException("Missing RemoveMimeHeader in mailets configuration (mailetcontainer -> processors -> transport). Should be configured to remove Bcc header"));
}
}

}

0 comments on commit 11771d0

Please sign in to comment.