View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.mailets;
19  
20  import javax.net.SocketFactory;
21  
22  import java.io.IOException;
23  import java.net.InetAddress;
24  import java.net.InetSocketAddress;
25  import java.net.Socket;
26  import java.net.UnknownHostException;
27  
28  /***
29   * It is used by RemoteDelivery in order to make possible to bind the client
30   * socket to a specific ip address.
31   *
32   * This is not a nice solution because the ip address must be shared by all 
33   * RemoteDelivery instances. It would be better to modify JavaMail 
34   * (current version 1.3) to support a corresonding property, e.g.
35   * mail.smtp.bindAdress.
36   * 
37   * This used to not extend javax.net.SocketFactory descendant, because 
38   * 1. it was not necessary because JavaMail 1.2 uses reflection when accessing
39   * this class;
40   * 2. it was not desirable because it would require java 1.4.
41   * 
42   * But since James 2.3.0a1:
43   * 1. we require Java 1.4 so the dependency on SocketFactory is
44   * not really an issue;
45   * 2. Javamail 1.4 cast the object returned by getDefault to SocketFactory and
46   * fails to create the socket if we don't extend SocketFactory.
47   * 
48   * Note: Javamail 1.4 should correctly support mail.smtp.localaddr so we could
49   * probably get rid of this class and simply add that property to the Session.
50   */
51  public class RemoteDeliverySocketFactory extends SocketFactory {
52      
53      /***
54       * @param addr the ip address or host name the delivery socket will bind to
55       */
56      static void setBindAdress(String addr) throws UnknownHostException {
57          if (addr == null) bindAddress = null;
58          else bindAddress = InetAddress.getByName(addr);
59      }
60      
61      /***
62       * the same as the similarly named javax.net.SocketFactory operation.
63       */
64      public static SocketFactory getDefault() {
65          return new RemoteDeliverySocketFactory();
66      }
67      
68      /***
69       * the same as the similarly named javax.net.SocketFactory operation.
70       * Just to be safe, it is not used by JavaMail 1.3.
71       * This is the only method used by JavaMail 1.4.
72       */
73      public Socket createSocket() throws IOException {
74          Socket s = new Socket();
75          s.bind(new InetSocketAddress(bindAddress, 0));
76          return s;
77      }
78      
79      /***
80       * the same as the similarly named javax.net.SocketFactory operation.
81       * This is the one which is used by JavaMail 1.3.
82       * This is not used by JavaMail 1.4.
83       */
84      public Socket createSocket(String host, int port)
85                              throws IOException, UnknownHostException {
86          return new Socket(host, port, bindAddress, 0);
87      }
88      
89      /***
90       * the same as the similarly named javax.net.SocketFactory operation.
91       * Just to be safe, it is not used by JavaMail 1.3.
92       * This is not used by JavaMail 1.4.
93       */
94      public Socket createSocket(String host,
95                                      int port,
96                                      InetAddress clientHost,
97                                      int clientPort)
98                                      throws IOException,
99                                      UnknownHostException {
100         return new Socket(host, port, 
101                 clientHost == null ? bindAddress : clientHost, clientPort);
102     }
103     
104     /***
105      * the same as the similarly named javax.net.SocketFactory operation.
106      * Just to be safe, it is not used by JavaMail 1.3.
107      * This is not used by JavaMail 1.4.
108      */
109     public Socket createSocket(InetAddress host, int port) throws IOException {
110         return new Socket(host, port, bindAddress, 0);
111     }
112     
113     /***
114      * the same as the similarly named javax.net.SocketFactory operation.
115      * Just to be safe, it is not used by JavaMail 1.3.
116      * This is not used by JavaMail 1.4.
117      */
118     public Socket createSocket(InetAddress address,
119                                     int port,
120                                     InetAddress clientAddress,
121                                     int clientPort)
122                              throws IOException {
123         return new Socket(address, port, 
124                 clientAddress == null ? bindAddress : clientAddress, 
125                 clientPort);
126     }
127     
128     /***
129      * it should be set by setBindAdress(). Null means the socket is bind to 
130      * the default address.
131      */
132     private static InetAddress bindAddress;
133 }