KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > RemoteDeliverySocketFactory


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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 java.net.InetAddress JavaDoc;
21 import java.net.Socket JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * It is used by RemoteDelivery in order to make possible to bind the client
27  * socket to a specific ip address.
28  *
29  * This is not a nice solution because the ip address must be shared by all
30  * RemoteDelivery instances. It would be better to modify JavaMail
31  * (current version 1.3) to support a corresonding property, e.g.
32  * mail.smtp.bindAdress.
33  *
34  * It should be a javax.net.SocketFactory descendant, but
35  * 1. it is not necessary because JavaMail 1.2 uses reflection when accessing
36  * this class;
37  * 2. it is not desirable because it would require java 1.4.
38  */

39 public class RemoteDeliverySocketFactory {
40     
41     /**
42      * @param addr the ip address or host name the delivery socket will bind to
43      */

44     static void setBindAdress(String JavaDoc addr) throws UnknownHostException JavaDoc {
45         if (addr == null) bindAddress = null;
46         else bindAddress = InetAddress.getByName(addr);
47     }
48     
49     /**
50      * the same as the similarly named javax.net.SocketFactory operation.
51      */

52     public static RemoteDeliverySocketFactory getDefault() {
53         return new RemoteDeliverySocketFactory();
54     }
55     
56     /**
57      * the same as the similarly named javax.net.SocketFactory operation.
58      * Just to be safe, it is not used by JavaMail 1.3.
59      */

60     public Socket JavaDoc createSocket() throws IOException JavaDoc {
61         throw new IOException JavaDoc("Incompatible JavaMail version, " +
62                 "cannot bound socket");
63     }
64     
65     /**
66      * the same as the similarly named javax.net.SocketFactory operation.
67      * This is the one which is used by JavaMail 1.3.
68      */

69     public Socket JavaDoc createSocket(String JavaDoc host, int port)
70                             throws IOException JavaDoc, UnknownHostException JavaDoc {
71         return new Socket JavaDoc(host, port, bindAddress, 0);
72     }
73     
74     /**
75      * the same as the similarly named javax.net.SocketFactory operation.
76      * Just to be safe, it is not used by JavaMail 1.3.
77      */

78     public Socket JavaDoc createSocket(String JavaDoc host,
79                                     int port,
80                                     InetAddress JavaDoc clientHost,
81                                     int clientPort)
82                                     throws IOException JavaDoc,
83                                     UnknownHostException JavaDoc {
84         return new Socket JavaDoc(host, port,
85                 clientHost == null ? bindAddress : clientHost, clientPort);
86     }
87     
88     /**
89      * the same as the similarly named javax.net.SocketFactory operation.
90      * Just to be safe, it is not used by JavaMail 1.3.
91      */

92     public Socket JavaDoc createSocket(InetAddress JavaDoc host, int port) throws IOException JavaDoc {
93         return new Socket JavaDoc(host, port, bindAddress, 0);
94     }
95     
96     /**
97      * the same as the similarly named javax.net.SocketFactory operation.
98      * Just to be safe, it is not used by JavaMail 1.3.
99      */

100     public Socket JavaDoc createSocket(InetAddress JavaDoc address,
101                                     int port,
102                                     InetAddress JavaDoc clientAddress,
103                                     int clientPort)
104                              throws IOException JavaDoc {
105         return new Socket JavaDoc(address, port,
106                 clientAddress == null ? bindAddress : clientAddress,
107                 clientPort);
108     }
109     
110     /**
111      * it should be set by setBindAdress(). Null means the socket is bind to
112      * the default address.
113      */

114     private static InetAddress JavaDoc bindAddress;
115 }
116
Popular Tags