View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mailrepository.javamail;
21  
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  
25  import javax.mail.Folder;
26  import javax.mail.Message;
27  import javax.mail.MessagingException;
28  import javax.mail.UIDFolder;
29  
30  /**
31   * Simple 1:1 wrapper for original JavaMail Folder and UIDFolder. uses
32   * reflection to call UIDPlusFolder methods on a JavaMail Folder.
33   */
34  public class UIDPlusFolderAdapter extends FolderAdapter implements UIDPlusFolder {
35      
36      private Method addUIDMessagesMethod;
37      private Method addMessagesMethod;
38      
39      public UIDPlusFolderAdapter(Folder folder) {
40          super(folder);
41          try {
42              addUIDMessagesMethod=folder.getClass().getMethod("addUIDMessages",
43                      new Class[] { Message[].class });
44              addMessagesMethod=folder.getClass().getMethod("addMessages",
45                      new Class[] { Message[].class });
46          } catch (SecurityException e) {
47              throw new RuntimeException(e);
48          } catch (NoSuchMethodException e) {
49              throw new RuntimeException(e);
50          } 
51      }
52  
53      /**
54       * @see org.apache.james.mailrepository.javamail.UIDPlusFolder#addUIDMessages(javax.mail.Message[])
55       */
56      public long[] addUIDMessages(Message[] msgs) throws MessagingException {
57          try {
58              return (long[]) addUIDMessagesMethod.invoke(folder, new Object[] { msgs });
59          } catch (IllegalArgumentException e) {
60              throw new RuntimeException(e);
61          } catch (IllegalAccessException e) {
62              throw new RuntimeException(e);
63          } catch (InvocationTargetException e) {
64              Throwable cause=e.getCause();
65              if (cause instanceof RuntimeException) {
66                  throw (RuntimeException)cause;
67              }
68              throw new RuntimeException(e);
69          }
70      }
71  
72      /**
73       * @see org.apache.james.mailrepository.javamail.UIDPlusFolder#addMessages(javax.mail.Message[])
74       */
75      public Message[] addMessages(Message[] msgs) throws MessagingException {
76          try {
77              return (Message[]) addMessagesMethod.invoke(folder, new Object[] { msgs });
78          } catch (IllegalArgumentException e) {
79              throw new RuntimeException(e);
80          } catch (IllegalAccessException e) {
81              throw new RuntimeException(e);
82          } catch (InvocationTargetException e) {
83              Throwable cause=e.getCause();
84              if (cause instanceof RuntimeException) {
85                  throw (RuntimeException)cause;
86              }
87              throw new RuntimeException(e);
88          }
89      }
90  
91      /**
92       * @see javax.mail.UIDFolder#getUIDValidity()
93       */
94      public long getUIDValidity() throws MessagingException {
95          return ((UIDFolder)folder).getUIDValidity();
96      }
97  
98      /**
99       * @see javax.mail.UIDFolder#getMessageByUID(long)
100      */
101     public Message getMessageByUID(long uid) throws MessagingException {
102         return ((UIDFolder)folder).getMessageByUID(uid);
103     }
104 
105     /**
106      * @see javax.mail.UIDFolder#getMessagesByUID(long, long)
107      */
108     public Message[] getMessagesByUID(long start, long end) throws MessagingException {
109         return ((UIDFolder)folder).getMessagesByUID(start, end);
110     }
111 
112     /**
113      * @see javax.mail.UIDFolder#getMessagesByUID(long[])
114      */
115     public Message[] getMessagesByUID(long[] uids) throws MessagingException {
116         return ((UIDFolder)folder).getMessagesByUID(uids);
117     }
118 
119     /**
120      * @see javax.mail.UIDFolder#getUID(javax.mail.Message)
121      */
122     public long getUID(Message message) throws MessagingException {
123         return ((UIDFolder)folder).getUID(message);
124     }
125 }