Author: imario
Date: Wed Nov 15 14:02:03 2006
New Revision: 475451

URL: http://svn.apache.org/viewvc?view=rev&rev=475451
Log:
allow access to get/is methods of underlaying access through the attributes

Added:
    
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java
   (with props)
Modified:
    
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileObject.java
    
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileSystem.java

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=475451&r1=475450&r2=475451
==============================================================================
--- 
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:02:03 2006
@@ -28,7 +28,6 @@
 import org.apache.commons.vfs.util.FileObjectUtils;
 import org.apache.commons.vfs.util.SharedRandomContentInputStream;
 
-import javax.mail.Header;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Multipart;
@@ -37,14 +36,12 @@
 import javax.mail.internet.MimeMultipart;
 import java.io.InputStream;
 import java.io.IOException;
-import java.io.StringReader;
 import java.io.ByteArrayInputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
-import java.util.TreeMap;
 
 /**
  * A part of a MIME message.
@@ -57,15 +54,15 @@
        implements FileObject
 {
        private Part part;
+       private Map attributeMap = Collections.EMPTY_MAP;
 
        protected MimeFileObject(final FileName name,
                                                        final Part part,
                                                        final 
AbstractFileSystem fileSystem) throws FileSystemException
        {
                super(name, fileSystem);
-               this.part = part;
+               setPart(part);
        }
-
        /**
      * Attaches this file object to its file resource.
      */
@@ -76,7 +73,7 @@
                        if (!getName().equals(getFileSystem().getRootName()))
                        {
                                MimeFileObject foParent = (MimeFileObject) 
FileObjectUtils.getAbstractFileObject(getParent());
-                               part = 
foParent.findPart(getName().getBaseName());
+                               
setPart(foParent.findPart(getName().getBaseName()));
                                return;
                        }
 
@@ -97,7 +94,7 @@
                                {
                                        is = 
getFileSystem().getParentLayer().getContent().getInputStream();
                                }
-                               part = new MimeMessage(null, is);
+                               setPart(new MimeMessage(null, is));
                        }
                        finally
                        {
@@ -233,6 +230,14 @@
        private void setPart(Part part)
        {
                this.part = part;
+               if (part != null)
+               {
+                       attributeMap = new MimeLazyMap(part);
+               }
+               else
+               {
+                       attributeMap = Collections.EMPTY_MAP;
+               }
        }
 
        /**
@@ -285,7 +290,7 @@
                        String preamble = ((MimeMultipart) 
part.getContent()).getPreamble();
                        return new 
ByteArrayInputStream(preamble.getBytes(MimeFileSystem.PREAMBLE_CHARSET));
                }
-               
+
                return part.getInputStream();
        }
 
@@ -315,24 +320,28 @@
         */
        protected Map doGetAttributes() throws Exception
        {
+               return attributeMap;
+               /*
                Map ret = new TreeMap();
 
-               Enumeration headers = getAllHeaders();
+               Enumeration headers = part.getAllHeaders();
                while (headers.hasMoreElements())
                {
                        Header header = (Header) headers.nextElement();
-                       Object values = ret.get(header.getName());
+                       String headerName = header.getName();
+
+                       Object values = ret.get(headerName);
 
                        if (values == null)
                        {
-                               ret.put(header.getName(), header.getValue());
+                               ret.put(headerName, header.getValue());
                        }
                        else if (values instanceof String)
                        {
                                List newValues = new ArrayList();
                                newValues.add(values);
                                newValues.add(header.getValue());
-                               ret.put(header.getName(), newValues);
+                               ret.put(headerName, newValues);
                        }
                        else if (values instanceof List)
                        {
@@ -341,6 +350,7 @@
                }
 
                return ret;
+               */
        }
 
        protected Enumeration getAllHeaders() throws MessagingException

Modified: 
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileSystem.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileSystem.java?view=diff&rev=475451&r1=475450&r2=475451
==============================================================================
--- 
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileSystem.java
 (original)
+++ 
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeFileSystem.java
 Wed Nov 15 14:02:03 2006
@@ -22,7 +22,11 @@
 import org.apache.commons.vfs.FileSystemOptions;
 import org.apache.commons.vfs.provider.AbstractFileSystem;
 
+import javax.mail.internet.MimeMessage;
 import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
+import java.lang.reflect.Method;
 
 /**
  * An MIME file system.

Added: 
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/MimeLazyMap.java?view=auto&rev=475451
==============================================================================
--- 
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java
 (added)
+++ 
jakarta/commons/proper/vfs/trunk/sandbox/src/main/java/org/apache/commons/vfs/provider/mime/MimeLazyMap.java
 Wed Nov 15 14:02:03 2006
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * Licensed 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.commons.vfs.provider.mime;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.mail.Header;
+import javax.mail.MessagingException;
+import javax.mail.Part;
+import javax.mail.internet.MimeMessage;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+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
+ */
+public class MimeLazyMap implements Map
+{
+       private Log log = LogFactory.getLog(MimeLazyMap.class);
+
+       private final static String OBJECT_PREFIX = "obj.";
+
+       private final Part part;
+       private Map backingMap;
+
+       private final static Map mimeMessageGetters = new TreeMap();
+
+       static
+       {
+               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);
+               }
+       }
+
+       private static void addMimeMessageMethod(Method method)
+       {
+               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);
+               }
+       }
+
+       public MimeLazyMap(Part part)
+       {
+               this.part = part;
+       }
+
+       private Map getMap()
+       {
+               if (backingMap == null)
+               {
+                       backingMap = createMap();
+               }
+
+               return backingMap;
+       }
+
+       private Map createMap()
+       {
+               Map ret = new TreeMap();
+
+               Enumeration headers = null;
+               try
+               {
+                       headers = part.getAllHeaders();
+               }
+               catch (MessagingException e)
+               {
+                       throw (RuntimeException) new RuntimeException(e);
+               }
+               while (headers.hasMoreElements())
+               {
+                       Header header = (Header) headers.nextElement();
+                       String headerName = header.getName();
+
+                       Object values = ret.get(headerName);
+
+                       if (values == null)
+                       {
+                               ret.put(headerName, header.getValue());
+                       }
+                       else if (values instanceof String)
+                       {
+                               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());
+                       }
+               }
+
+               Iterator iterEntries = mimeMessageGetters.entrySet().iterator();
+               while (iterEntries.hasNext())
+               {
+                       Map.Entry entry = (Map.Entry) iterEntries.next();
+                       String name = (String) entry.getKey();
+                       Method method = (Method) entry.getValue();
+
+                       Object value;
+                       try
+                       {
+                               value = method.invoke(part, null);
+                       }
+                       catch (IllegalAccessException e)
+                       {
+                               log.warn(e.getLocalizedMessage(), e);
+                               continue;
+                       }
+                       catch (InvocationTargetException e)
+                       {
+                               log.warn(e.getLocalizedMessage(), e);
+                               continue;
+                       }
+
+                       ret.put(OBJECT_PREFIX+name, value);
+               }
+
+               return ret;
+       }
+
+       public int size()
+       {
+               return getMap().size();
+       }
+
+       public boolean isEmpty()
+       {
+               return getMap().size() < 1;
+       }
+
+       public boolean containsKey(Object key)
+       {
+               return getMap().containsKey(key);
+       }
+
+       public boolean containsValue(Object value)
+       {
+               return getMap().containsValue(value);
+       }
+
+       public Object get(Object key)
+       {
+               if (backingMap != null)
+               {
+                       return backingMap.get(key);
+               }
+
+               return null;
+       }
+
+       public Object put(Object key, Object value)
+       {
+               throw new UnsupportedOperationException();
+       }
+
+       public Object remove(Object key)
+       {
+               throw new UnsupportedOperationException();
+       }
+
+       public void putAll(Map t)
+       {
+               throw new UnsupportedOperationException();
+       }
+
+       public void clear()
+       {
+               throw new UnsupportedOperationException();
+       }
+
+       public Set keySet()
+       {
+               return Collections.unmodifiableSet(getMap().keySet());
+       }
+
+       public Collection values()
+       {
+               return Collections.unmodifiableCollection(getMap().values());
+       }
+
+       public Set entrySet()
+       {
+               return Collections.unmodifiableSet(getMap().entrySet());
+       }
+}

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

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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to