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

Issue #62 - mostly fixed #84

Open
wants to merge 12 commits 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
15 changes: 15 additions & 0 deletions greenmail-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
33 changes: 27 additions & 6 deletions greenmail-core/src/main/java/com/icegreen/greenmail/Managers.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.icegreen.greenmail.imap.ImapHostManagerImpl;
import com.icegreen.greenmail.smtp.SmtpManager;
import com.icegreen.greenmail.store.InMemoryStore;
import com.icegreen.greenmail.store.Store;
import com.icegreen.greenmail.user.UserManager;

/**
Expand All @@ -16,19 +17,39 @@
* @since Jan 27, 2006
*/
public class Managers {
private ImapHostManager imapHostManager = new ImapHostManagerImpl(new InMemoryStore());
private UserManager userManager = new UserManager(imapHostManager);
private SmtpManager smtpManager = new SmtpManager(imapHostManager, userManager);
private ImapHostManager imapHostManager;
private UserManager userManager;
private SmtpManager smtpManager;

public SmtpManager getSmtpManager() {
public Managers() {
this(new InMemoryStore());
}

protected Managers(final Store imapHostManagerStore) {
imapHostManager = new ImapHostManagerImpl(imapHostManagerStore);
userManager = new UserManager(imapHostManager);
smtpManager = new SmtpManager(imapHostManager, userManager);
}

public final SmtpManager getSmtpManager() {
return smtpManager;
}

public UserManager getUserManager() {
public final UserManager getUserManager() {
return userManager;
}

public ImapHostManager getImapHostManager() {
public final ImapHostManager getImapHostManager() {
return imapHostManager;
}

public final void reset() {
imapHostManager = new ImapHostManagerImpl(createNewStore());
userManager = new UserManager(imapHostManager);
smtpManager = new SmtpManager(imapHostManager, userManager);
}

protected Store createNewStore() {
return new InMemoryStore();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* -------------------------------------------------------------------
* This software is released under the Apache license 2.0
* -------------------------------------------------------------------
* /
*/

package com.icegreen.greenmail;

import com.icegreen.greenmail.store.InMemoryStore;
import com.icegreen.greenmail.store.Store;
import com.icegreen.greenmail.store.StoredMessageCollectionFactory;

/**
* {@link Managers} which uses the {@link StoredMessageCollectionFactory#MAP_BASED_FACTORY MAP_BASED_FACTORY} for
* instantiating the {@link InMemoryStore}.
*
* @author Raimund Klein <raimund.klein@gmx.de>
*/
public class MemorySafeManagers extends Managers {
public MemorySafeManagers() {
super(new InMemoryStore(StoredMessageCollectionFactory.MAP_BASED_FACTORY));
}

@Override
protected Store createNewStore() {
return new InMemoryStore(StoredMessageCollectionFactory.MAP_BASED_FACTORY);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
package com.icegreen.greenmail.configuration;

import com.icegreen.greenmail.Managers;
import com.icegreen.greenmail.base.GreenMailOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A version of GreenMailOperations that implements the configure() method.
*/
public abstract class ConfiguredGreenMail implements GreenMailOperations {
protected final Logger log = LoggerFactory.getLogger(getClass());
private GreenMailConfiguration config;

protected final Managers createManagers() {
if (config == null)
config = GreenMailConfiguration.aConfig();
Class<? extends Managers> configuredManagersClass = config.getManagersClass();
try {
return configuredManagersClass.newInstance();
} catch (InstantiationException e) {
log.warn(String.format("Could not instantiate Managers class '%s'. Will run with default class '%s'.",
configuredManagersClass, Managers.class), e);
} catch (IllegalAccessException e) {
log.warn(String.format("Illegal access while instantiating Managers class '%s'. Will run with default " +
"class '%s'.", configuredManagersClass, Managers.class), e);
} catch (Exception e) {
log.warn(String.format("General exception while instantiating Managers class '%s'. Will run with default " +
"class '%s'.", configuredManagersClass, Managers.class), e);
}
return new Managers();
}

@Override
public ConfiguredGreenMail withConfiguration(GreenMailConfiguration config) {
this.config = config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.icegreen.greenmail.configuration;

import com.icegreen.greenmail.Managers;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -8,6 +10,14 @@
*/
public class GreenMailConfiguration {
private final List<UserBean> usersToCreate = new ArrayList<UserBean>();
private Class<? extends Managers> managersClass = Managers.class;

/**
* @return New GreenMail configuration
*/
public static GreenMailConfiguration aConfig() {
return new GreenMailConfiguration();
}

/**
* The given {@link com.icegreen.greenmail.user.GreenMailUser} will be created when servers will start
Expand All @@ -33,11 +43,12 @@ public GreenMailConfiguration withUser(final String email, final String login, f
return this;
}

/**
* @return New GreenMail configuration
*/
public static GreenMailConfiguration aConfig() {
return new GreenMailConfiguration();
public GreenMailConfiguration withManagersClass(final Class<? extends Managers> managersClass) {
if (managersClass == null) {
throw new NullPointerException("managersClass may not be null.");
}
this.managersClass = managersClass;
return this;
}

/**
Expand All @@ -46,4 +57,11 @@ public static GreenMailConfiguration aConfig() {
public List<UserBean> getUsersToCreate() {
return usersToCreate;
}

/**
* @return The {@link Managers} class to should be used
*/
public Class<? extends Managers> getManagersClass() {
return managersClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class ImapHostManagerImpl
* Hack constructor which creates an in-memory store, and creates a console logger.
*/
public ImapHostManagerImpl() {
store = new InMemoryStore();
subscriptions = new MailboxSubscriptions();
this(new InMemoryStore());
}

public ImapHostManagerImpl(Store store) {
Expand Down Expand Up @@ -294,6 +293,10 @@ private String getQualifiedMailboxName(GreenMailUser user, String mailboxName) {
}
}

public Store getStore() {
return store;
}

/**
* Handles all user subscriptions.
* TODO make this a proper class
Expand Down Expand Up @@ -348,9 +351,4 @@ private List<String> getUserSubs(GreenMailUser user) {
return subs;
}
}

@Override
public Store getStore() {
return store;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public class MovingMessage {
private MimeMessage message;
private int _references = 0;

public List<MailAddress> getToAddresses() {
return toAddresses;
}

public MovingMessage(Workspace workspace) {
_workspace = workspace;
}

public List<MailAddress> getToAddresses() {
return toAddresses;
}

public MimeMessage getMessage() {
return message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
public abstract class AbstractServer extends Thread implements Service {
protected final Logger log = LoggerFactory.getLogger(getClass());
protected final InetAddress bindTo;
private final List<ProtocolHandler> handlers = Collections.synchronizedList(new ArrayList<ProtocolHandler>());
private final Object startupMonitor = new Object();
protected ServerSocket serverSocket = null;
protected Managers managers;
protected ServerSetup setup;
private final List<ProtocolHandler> handlers = Collections.synchronizedList(new ArrayList<ProtocolHandler>());
private volatile boolean keepRunning = false;
private volatile boolean running = false;
private final Object startupMonitor = new Object();

protected AbstractServer(ServerSetup setup, Managers managers) {
this.setup = setup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class HierarchicalFolder implements MailFolder, UIDFolder {
PERMANENT_FLAGS.add(Flags.Flag.SEEN);
}

private final StoredMessageCollection mailMessages = new ListBasedStoredMessageCollection();
private final StoredMessageCollectionFactory storedMessageCollectionFactory;
private final StoredMessageCollection mailMessages;
private final List<FolderListener> _mailboxListeners = Collections.synchronizedList(new ArrayList<FolderListener>());
protected String name;
private Collection<HierarchicalFolder> children;
Expand All @@ -39,14 +40,20 @@ class HierarchicalFolder implements MailFolder, UIDFolder {
private long nextUid = 1;
private long uidValidity;

public HierarchicalFolder(HierarchicalFolder parent,
String name) {
protected HierarchicalFolder(final StoredMessageCollectionFactory storedMessageCollectionFactory,
HierarchicalFolder parent, String name) {
this.storedMessageCollectionFactory = storedMessageCollectionFactory;
mailMessages = storedMessageCollectionFactory.createCollection();
this.name = name;
this.children = new ArrayList<HierarchicalFolder>();
this.parent = parent;
this.uidValidity = System.currentTimeMillis();
}

public HierarchicalFolder(final HierarchicalFolder castParent, final String mailboxName) {
this(castParent.storedMessageCollectionFactory, castParent, mailboxName);
}

public Collection<HierarchicalFolder> getChildren() {
return children;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@
*/
public class InMemoryStore
implements Store, ImapConstants {
private final RootFolder rootMailbox;
boolean quotaSupported = true;
private RootFolder rootMailbox = new RootFolder();
private Map<String, Set<Quota>> quotaMap = new HashMap<String, Set<Quota>>();

public InMemoryStore() {
this(StoredMessageCollectionFactory.LIST_BASED_FACTORY);
}

public InMemoryStore(final StoredMessageCollectionFactory storedMessageCollectionFactory) {
this.rootMailbox = new RootFolder(storedMessageCollectionFactory);
}

@Override
public MailFolder getMailbox(String absoluteMailboxName) {
StringTokenizer tokens = new StringTokenizer(absoluteMailboxName, HIERARCHY_DELIMITER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* @author Raimund Klein <raimund.klein@gmx.de>
*/
class RootFolder extends HierarchicalFolder {
public RootFolder() {
super(null, ImapConstants.USER_NAMESPACE);
RootFolder(final StoredMessageCollectionFactory storedMessageCollectionFactory) {
super(storedMessageCollectionFactory, null, ImapConstants.USER_NAMESPACE);
}

@Override
Expand Down
Loading