commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r475467 - in /jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime: MimeAttributesMap.java MimeFileObject.java MimeLazyMap.java
Date Wed, 15 Nov 2006 22:22:49 GMT
Author: imario
Date: Wed Nov 15 14:22:49 2006
New Revision: 475467

URL: http://svn.apache.org/viewvc?view=rev&rev=475467
Log:
reverted lazy strategy, now build map on the first access

Added:
    jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java
  (contents, props changed)
      - copied, changed from r475452, jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java
Removed:
    jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java
Modified:
    jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileObject.java

Copied: jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java
(from r475452, jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java)
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java?view=diff&rev=475467&p1=jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java&r1=475452&p2=jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java&r2=475467
==============================================================================
--- jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java
(original)
+++ jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java
Wed Nov 15 14:22:49 2006
@@ -21,65 +21,65 @@
 import javax.mail.Header;
 import javax.mail.MessagingException;
 import javax.mail.Part;
+import javax.mail.Address;
 import javax.mail.internet.MimeMessage;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
-import java.util.Iterator;
-import java.util.Collections;
-import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
 
 /**
- * A map which tries to avoid building the real content as long as possible.
- * This makes quick lookups with known keys faster
+ * A map which tries to allow access to the various aspects of the mail
  */
-public class MimeLazyMap implements Map
+public class MimeAttributesMap implements Map
 {
-	private Log log = LogFactory.getLog(MimeLazyMap.class);
+	private Log log = LogFactory.getLog(MimeAttributesMap.class);
 
 	private final static String OBJECT_PREFIX = "obj.";
 
 	private final Part part;
 	private Map backingMap;
 
-	private final static Map mimeMessageGetters = new TreeMap();
+	private final Map mimeMessageGetters = new TreeMap();
 
-	static
+	public MimeAttributesMap(Part part)
 	{
-		Method[] methods = MimeMessage.class.getMethods();
-		for (int i = 0; i<methods.length; i++)
-		{
-			Method method = methods[i];
-			addMimeMessageMethod(method);
-		}
-		methods = MimeMessage.class.getDeclaredMethods();
-		for (int i = 0; i<methods.length; i++)
-		{
-			Method method = methods[i];
-			addMimeMessageMethod(method);
-		}
+		this.part = part;
+		addMimeMessageMethod(part.getClass().getMethods());
+		addMimeMessageMethod(part.getClass().getDeclaredMethods());
 	}
 
-	private static void addMimeMessageMethod(Method method)
+	private void addMimeMessageMethod(Method[] methods)
 	{
-		if (method.getName().startsWith("get"))
+		for (int i = 0; i < methods.length; i++)
 		{
-			mimeMessageGetters.put(method.getName().substring(3), method);
-		}
-		else if (method.getName().startsWith("is"))
-		{
-			mimeMessageGetters.put(method.getName().substring(2), method);
-		}
-	}
+			Method method = methods[i];
+			if (!Modifier.isPublic(method.getModifiers()))
+			{
+				continue;
+			}
+			if (method.getParameterTypes().length > 0)
+			{
+				continue;
+			}
 
-	public MimeLazyMap(Part part)
-	{
-		this.part = part;
+			if (method.getName().startsWith("get"))
+			{
+				mimeMessageGetters.put(method.getName().substring(3), method);
+			}
+			else if (method.getName().startsWith("is"))
+			{
+				mimeMessageGetters.put(method.getName().substring(2), method);
+			}
+		}
 	}
 
 	private Map getMap()
@@ -96,15 +96,17 @@
 	{
 		Map ret = new TreeMap();
 
-		Enumeration headers = null;
+		Enumeration headers;
 		try
 		{
 			headers = part.getAllHeaders();
 		}
 		catch (MessagingException e)
 		{
-			throw (RuntimeException) new RuntimeException(e);
+			throw new RuntimeException(e);
 		}
+
+		// add all headers
 		while (headers.hasMoreElements())
 		{
 			Header header = (Header) headers.nextElement();
@@ -129,6 +131,7 @@
 			}
 		}
 
+		// add all simple get/is results (with obj. prefix)
 		Iterator iterEntries = mimeMessageGetters.entrySet().iterator();
 		while (iterEntries.hasNext())
 		{
@@ -136,23 +139,61 @@
 			String name = (String) entry.getKey();
 			Method method = (Method) entry.getValue();
 
-			Object value;
 			try
 			{
-				value = method.invoke(part, null);
+				Object value = method.invoke(part, null);
+				ret.put(OBJECT_PREFIX + name, value);
 			}
 			catch (IllegalAccessException e)
 			{
-				log.warn(e.getLocalizedMessage(), e);
-				continue;
+				log.debug(e.getLocalizedMessage(), e);
 			}
 			catch (InvocationTargetException e)
 			{
-				log.warn(e.getLocalizedMessage(), e);
-				continue;
+				log.debug(e.getLocalizedMessage(), e);
 			}
+		}
 
-			ret.put(OBJECT_PREFIX+name, value);
+		// add extended fields (with obj. prefix too)
+		if (part instanceof MimeMessage)
+		{
+			MimeMessage message = (MimeMessage) part;
+			try
+			{
+				Address[] address = message.getRecipients(MimeMessage.RecipientType.BCC);
+				ret.put(OBJECT_PREFIX + "Recipients.BCC", address);
+			}
+			catch (MessagingException e)
+			{
+				log.debug(e.getLocalizedMessage(), e);
+			}
+			try
+			{
+				Address[] address = message.getRecipients(MimeMessage.RecipientType.CC);
+				ret.put(OBJECT_PREFIX + "Recipients.CC", address);
+			}
+			catch (MessagingException e)
+			{
+				log.debug(e.getLocalizedMessage(), e);
+			}
+			try
+			{
+				Address[] address = message.getRecipients(MimeMessage.RecipientType.TO);
+				ret.put(OBJECT_PREFIX + "Recipients.TO", address);
+			}
+			catch (MessagingException e)
+			{
+				log.debug(e.getLocalizedMessage(), e);
+			}
+			try
+			{
+				Address[] address = message.getRecipients(MimeMessage.RecipientType.NEWSGROUPS);
+				ret.put(OBJECT_PREFIX + "Recipients.NEWSGROUPS", address);
+			}
+			catch (MessagingException e)
+			{
+				log.debug(e.getLocalizedMessage(), e);
+			}
 		}
 
 		return ret;
@@ -180,12 +221,7 @@
 
 	public Object get(Object key)
 	{
-		if (backingMap != null)
-		{
-			return backingMap.get(key);
-		}
-
-		return null;
+		return getMap().get(key);
 	}
 
 	public Object put(Object key, Object value)

Propchange: jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeAttributesMap.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileObject.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileObject.java?view=diff&rev=475467&r1=475466&r2=475467
==============================================================================
--- jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileObject.java
(original)
+++ jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileObject.java
Wed Nov 15 14:22:49 2006
@@ -54,7 +54,7 @@
 	implements FileObject
 {
 	private Part part;
-	private Map attributeMap = Collections.EMPTY_MAP;
+	private Map attributeMap;
 
 	protected MimeFileObject(final FileName name,
 							final Part part,
@@ -230,14 +230,7 @@
 	private void setPart(Part part)
 	{
 		this.part = part;
-		if (part != null)
-		{
-			attributeMap = new MimeLazyMap(part);
-		}
-		else
-		{
-			attributeMap = Collections.EMPTY_MAP;
-		}
+		this.attributeMap = null;
 	}
 
 	/**
@@ -320,37 +313,19 @@
 	 */
 	protected Map doGetAttributes() throws Exception
 	{
-		return attributeMap;
-		/*
-		Map ret = new TreeMap();
-
-		Enumeration headers = part.getAllHeaders();
-		while (headers.hasMoreElements())
+		if (attributeMap == null)
 		{
-			Header header = (Header) headers.nextElement();
-			String headerName = header.getName();
-
-			Object values = ret.get(headerName);
-
-			if (values == null)
+			if (part != null)
 			{
-				ret.put(headerName, header.getValue());
+				attributeMap = new MimeAttributesMap(part);
 			}
-			else if (values instanceof String)
+			else
 			{
-				List newValues = new ArrayList();
-				newValues.add(values);
-				newValues.add(header.getValue());
-				ret.put(headerName, newValues);
-			}
-			else if (values instanceof List)
-			{
-				((List) values).add(header.getValue());
+				attributeMap = Collections.EMPTY_MAP;
 			}
 		}
 
-		return ret;
-		*/
+		return attributeMap;
 	}
 
 	protected Enumeration getAllHeaders() throws MessagingException



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message