You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2010/01/22 17:32:38 UTC
svn commit: r902158 - in /james/imap/trunk: maildir/
maildir/src/main/java/org/apache/james/imap/maildir/mail/
maildir/src/main/java/org/apache/james/imap/maildir/mail/model/ parent/
Author: norman
Date: Fri Jan 22 16:32:37 2010
New Revision: 902158
URL: http://svn.apache.org/viewvc?rev=902158&view=rev
Log:
more work on maildir impl .. far from useful
Added:
james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/
james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirHeader.java
james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirMailbox.java
Modified:
james/imap/trunk/maildir/pom.xml
james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/MaildirMailboxMapper.java
james/imap/trunk/parent/pom.xml
Modified: james/imap/trunk/maildir/pom.xml
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/pom.xml?rev=902158&r1=902157&r2=902158&view=diff
==============================================================================
--- james/imap/trunk/maildir/pom.xml (original)
+++ james/imap/trunk/maildir/pom.xml Fri Jan 22 16:32:37 2010
@@ -28,6 +28,23 @@
<groupId>org.apache.james</groupId>
<artifactId>apache-james-imap-maildir</artifactId>
<name>Apache James IMAP Maildir Mailbox Manager</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.1</version>
+ <!-- commons-transaction 2 needs java6 -->
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+
<dependencies>
<dependency>
<groupId>${javax.mail.groupId}</groupId>
@@ -69,7 +86,13 @@
<groupId>commons-transaction</groupId>
<artifactId>commons-transaction</artifactId>
</dependency>
-
+
+ <dependency>
+ <groupId>com.thoughtworks.xstream</groupId>
+ <artifactId>xstream</artifactId>
+ </dependency>
+
+
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
Modified: james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/MaildirMailboxMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/MaildirMailboxMapper.java?rev=902158&r1=902157&r2=902158&view=diff
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/MaildirMailboxMapper.java (original)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/MaildirMailboxMapper.java Fri Jan 22 16:32:37 2010
@@ -19,7 +19,9 @@
package org.apache.james.imap.maildir.mail;
+import java.io.IOException;
import java.util.List;
+import java.util.Properties;
import org.apache.commons.transaction.file.TxFileResourceManager;
import org.apache.commons.transaction.file.FileResourceManager.FileResource;
@@ -27,24 +29,34 @@
import org.apache.james.imap.api.display.HumanReadableText;
import org.apache.james.imap.mailbox.MailboxNotFoundException;
import org.apache.james.imap.mailbox.StorageException;
+import org.apache.james.imap.maildir.mail.model.MaildirMailbox;
import org.apache.james.imap.store.mail.MailboxMapper;
import org.apache.james.imap.store.mail.model.Mailbox;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
/**
* MailboxMapper which use Maildir layout
*
- *
+ *
*/
-public class MaildirMailboxMapper extends MaildirTransactionalMapper implements MailboxMapper{
+public class MaildirMailboxMapper extends MaildirTransactionalMapper implements
+ MailboxMapper {
+
+ private final static String PREFIX = "james-imap";
+ private final XStream xstream = new XStream(new DomDriver());
public MaildirMailboxMapper(final TxFileResourceManager manager) {
- super(manager);
+ super(manager);
}
-
-
+
/*
* (non-Javadoc)
- * @see org.apache.james.imap.store.mail.MailboxMapper#countMailboxesWithName(java.lang.String)
+ *
+ * @see
+ * org.apache.james.imap.store.mail.MailboxMapper#countMailboxesWithName
+ * (java.lang.String)
*/
public long countMailboxesWithName(String name) throws StorageException {
try {
@@ -54,32 +66,39 @@
}
return 0;
} catch (ResourceException e) {
- throw new StorageException(HumanReadableText.COUNT_FAILED, e);
+ throw new StorageException(HumanReadableText.COUNT_FAILED, e);
}
}
/*
* (non-Javadoc)
- * @see org.apache.james.imap.store.mail.MailboxMapper#delete(org.apache.james.imap.store.mail.model.Mailbox)
+ *
+ * @see
+ * org.apache.james.imap.store.mail.MailboxMapper#delete(org.apache.james
+ * .imap.store.mail.model.Mailbox)
*/
public void delete(Mailbox mailbox) throws StorageException {
try {
manager.getResource(getPathForMailbox(mailbox)).delete();
- } catch (ResourceException e) {
+ deleteMailboxId(mailbox.getMailboxId());
+ } catch (Exception e) {
throw new StorageException(HumanReadableText.DELETED_FAILED, e);
}
}
/*
* (non-Javadoc)
+ *
* @see org.apache.james.imap.store.mail.MailboxMapper#deleteAll()
*/
public void deleteAll() throws StorageException {
try {
- List<? extends FileResource> mailboxes = manager.getResource(manager.getRootPath()).getChildren();
- for(int i = 0 ; i < mailboxes.size(); i++) {
+ List<? extends FileResource> mailboxes = manager.getResource(
+ manager.getRootPath()).getChildren();
+ for (int i = 0; i < mailboxes.size(); i++) {
mailboxes.get(i).delete();
}
+ manager.getResource(manager.getRootPath()).getChild(PREFIX + "-ids.properties").delete();
} catch (ResourceException e) {
throw new StorageException(HumanReadableText.DELETED_FAILED, e);
}
@@ -87,7 +106,10 @@
/*
* (non-Javadoc)
- * @see org.apache.james.imap.store.mail.MailboxMapper#existsMailboxStartingWith(java.lang.String)
+ *
+ * @see
+ * org.apache.james.imap.store.mail.MailboxMapper#existsMailboxStartingWith
+ * (java.lang.String)
*/
public boolean existsMailboxStartingWith(String mailboxName)
throws StorageException {
@@ -97,14 +119,14 @@
return true;
}
return false;
-
+
} catch (ResourceException e) {
throw new StorageException(HumanReadableText.SEARCH_FAILED, e);
}
}
/**
- * Convert the give name to the right path
+ * Convert the give name to the right path
*
* @param name
* @return path
@@ -112,29 +134,89 @@
private String convertToPath(String name) {
return name.substring("#mail".length()).replaceAll("\\.", "/");
}
-
-
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.MailboxMapper#findMailboxById(long)
+ */
public Mailbox findMailboxById(long mailboxId) throws StorageException,
MailboxNotFoundException {
- // TODO Auto-generated method stub
- return null;
+
+ try {
+ String name = convertToPath(getMailboxName(mailboxId));
+ MaildirMailbox mailbox = (MaildirMailbox) xstream.fromXML(manager
+ .getResource(name).getChild(PREFIX + "-metadata.xml")
+ .readStream());
+ return mailbox;
+ } catch (Exception e) {
+ throw new StorageException(HumanReadableText.SEARCH_FAILED, e);
+ }
}
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.MailboxMapper#findMailboxByName(java.lang.String)
+ */
public Mailbox findMailboxByName(String name) throws StorageException,
MailboxNotFoundException {
- // TODO Auto-generated method stub
- return null;
+ try {
+
+ FileResource fr = manager.getResource(convertToPath(name));
+ if (fr.exists()) {
+ return (MaildirMailbox) xstream.fromXML(fr.getChild(
+ PREFIX + "-metadata.xml").readStream());
+
+ }
+ } catch (ResourceException e) {
+ throw new StorageException(HumanReadableText.SEARCH_FAILED, e);
+ }
+ throw new MailboxNotFoundException(name);
}
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.MailboxMapper#findMailboxWithNameLike(java.lang.String)
+ */
public List<Mailbox> findMailboxWithNameLike(String name)
throws StorageException {
// TODO Auto-generated method stub
return null;
}
+ private String getMailboxName(long id) throws IOException,
+ ResourceException {
+ Properties props = new Properties();
+ props.load(manager.getResource(manager.getRootPath()).getChild(
+ PREFIX + "-ids.properties").readStream());
+ return (String) props.get(id);
+ }
+
+ private void deleteMailboxId(long id) throws IOException, ResourceException {
+ FileResource fr = manager.getResource(manager.getRootPath()).getChild(
+ PREFIX + "-ids.properties");
+ Properties props = new Properties();
+ props.load(fr.readStream());
+ props.remove(id);
+ props.store(manager.getResource(manager.getRootPath()).getChild(
+ PREFIX + "-ids.properties").writeStream(true), "");
+ }
+
+ private void saveMailboxIdMapping(long id, String name) throws IOException, ResourceException {
+ FileResource fr = manager.getResource(manager.getRootPath()).getChild(
+ PREFIX + "-ids.properties");
+ Properties props = new Properties();
+ props.load(fr.readStream());
+ props.put(id,name);
+ props.store(manager.getResource(manager.getRootPath()).getChild(
+ PREFIX + "-ids.properties").writeStream(true), "");
+ }
+
/*
* (non-Javadoc)
- * @see org.apache.james.imap.store.mail.MailboxMapper#save(org.apache.james.imap.store.mail.model.Mailbox)
+ *
+ * @see
+ * org.apache.james.imap.store.mail.MailboxMapper#save(org.apache.james.
+ * imap.store.mail.model.Mailbox)
*/
public void save(Mailbox mailbox) throws StorageException {
try {
@@ -142,31 +224,36 @@
fr.getChild("new").createAsDirectory();
fr.getChild("cur").createAsDirectory();
fr.getChild("tmp").createAsDirectory();
- } catch (ResourceException e) {
+ saveMailboxIdMapping(mailbox.getMailboxId(), mailbox.getName());
+ xstream.toXML(mailbox,fr.getChild(PREFIX + "-metadata.xml").writeStream(true));
+
+ } catch (Exception e) {
throw new StorageException(HumanReadableText.SAVE_FAILED, e);
}
}
/**
- * Return the path for the given mailbox
+ * Return the path for the given mailbox
*
* @param mailbox
* @return path
*/
private String getPathForMailbox(Mailbox mailbox) {
- String path ="";
+ String path = "";
String name = mailbox.getName();
String parts[] = name.split("\\.");
String userParts[] = parts[1].split("@");
-
- path += constructPath(userParts, 0);
+
+ // reverse order to domain, user
+ path += constructPath(new String[] { userParts[1], userParts[2] }, 0);
+ path += "/Maildir";
path += constructPath(parts, 2);
return path;
}
-
+
/**
- * Construct path for the given array, starting from start index.
+ * Construct path for the given array, starting from start index.
*
* @param parts
* @param start
@@ -175,7 +262,8 @@
private String constructPath(String[] parts, int start) {
String path = "";
for (int i = start; i < parts.length; i++) {
- path += "/" + parts[i];
+ // suffix with "."
+ path += "/." + parts[i];
}
return path;
}
Added: james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirHeader.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirHeader.java?rev=902158&view=auto
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirHeader.java (added)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirHeader.java Fri Jan 22 16:32:37 2010
@@ -0,0 +1,79 @@
+/****************************************************************
+ * 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.imap.maildir.mail.model;
+
+import org.apache.james.imap.store.mail.model.Header;
+
+public class MaildirHeader implements Header {
+
+ private int lineNumber;
+ private String field;
+ private String value;
+
+ /**
+ * Copies the content of an existing header.
+ * @param header
+ */
+ public MaildirHeader(Header header) {
+ this(header.getLineNumber(), header.getFieldName(), header.getValue());
+ }
+
+ public MaildirHeader(int lineNumber, String field, String value) {
+ super();
+ this.lineNumber = lineNumber;
+ this.field = field;
+ this.value = value;
+ }
+
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Header#getFieldName()
+ */
+ public String getFieldName() {
+ return field;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Header#getLineNumber()
+ */
+ public int getLineNumber() {
+ return lineNumber;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Header#getValue()
+ */
+ public String getValue() {
+ return value;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.lang.Comparable#compareTo(java.lang.Object)
+ */
+ public int compareTo(Header header) {
+ final int result = lineNumber - header.getLineNumber();
+ return result;
+ }
+
+}
Added: james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirMailbox.java?rev=902158&view=auto
==============================================================================
--- james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirMailbox.java (added)
+++ james/imap/trunk/maildir/src/main/java/org/apache/james/imap/maildir/mail/model/MaildirMailbox.java Fri Jan 22 16:32:37 2010
@@ -0,0 +1,113 @@
+/****************************************************************
+ * 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.imap.maildir.mail.model;
+
+import org.apache.james.imap.store.mail.model.Mailbox;
+
+import com.thoughtworks.xstream.annotations.XStreamAlias;
+
+@XStreamAlias("mailbox")
+public class MaildirMailbox implements Mailbox {
+
+ private String name;
+
+ private long id;
+ private long lastUid;
+
+ private int uidValidity;
+
+ public MaildirMailbox(String name, int uidValidity, long lastUid) {
+ this.name = name;
+ this.uidValidity = uidValidity;
+ this.lastUid = lastUid;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Mailbox#consumeUid()
+ */
+ public void consumeUid() {
+ lastUid++;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Mailbox#getLastUid()
+ */
+ public long getLastUid() {
+ return lastUid;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Mailbox#getMailboxId()
+ */
+ public long getMailboxId() {
+ return id;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Mailbox#getName()
+ */
+ public String getName() {
+ return name;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Mailbox#getUidValidity()
+ */
+ public long getUidValidity() {
+ return uidValidity;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.james.imap.store.mail.model.Mailbox#setName(java.lang.String)
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof MaildirMailbox) {
+ if (id == ((MaildirMailbox) obj).getMailboxId()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + (int) (id ^ (id >>> 32));
+ return result;
+ }
+
+}
Modified: james/imap/trunk/parent/pom.xml
URL: http://svn.apache.org/viewvc/james/imap/trunk/parent/pom.xml?rev=902158&r1=902157&r2=902158&view=diff
==============================================================================
--- james/imap/trunk/parent/pom.xml (original)
+++ james/imap/trunk/parent/pom.xml Fri Jan 22 16:32:37 2010
@@ -510,6 +510,14 @@
<artifactId>commons-transaction</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>com.thoughtworks.xstream</groupId>
+ <artifactId>xstream</artifactId>
+ <version>1.3.1</version>
+ </dependency>
+
+
+
<!--
END Maildir
-->
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org