001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets;
019:
020: import javax.net.SocketFactory;
021:
022: import java.io.IOException;
023: import java.net.InetAddress;
024: import java.net.InetSocketAddress;
025: import java.net.Socket;
026: import java.net.UnknownHostException;
027:
028: /**
029: * It is used by RemoteDelivery in order to make possible to bind the client
030: * socket to a specific ip address.
031: *
032: * This is not a nice solution because the ip address must be shared by all
033: * RemoteDelivery instances. It would be better to modify JavaMail
034: * (current version 1.3) to support a corresonding property, e.g.
035: * mail.smtp.bindAdress.
036: *
037: * This used to not extend javax.net.SocketFactory descendant, because
038: * 1. it was not necessary because JavaMail 1.2 uses reflection when accessing
039: * this class;
040: * 2. it was not desirable because it would require java 1.4.
041: *
042: * But since James 2.3.0a1:
043: * 1. we require Java 1.4 so the dependency on SocketFactory is
044: * not really an issue;
045: * 2. Javamail 1.4 cast the object returned by getDefault to SocketFactory and
046: * fails to create the socket if we don't extend SocketFactory.
047: *
048: * Note: Javamail 1.4 should correctly support mail.smtp.localaddr so we could
049: * probably get rid of this class and simply add that property to the Session.
050: */
051: public class RemoteDeliverySocketFactory extends SocketFactory {
052:
053: /**
054: * @param addr the ip address or host name the delivery socket will bind to
055: */
056: static void setBindAdress(String addr) throws UnknownHostException {
057: if (addr == null)
058: bindAddress = null;
059: else
060: bindAddress = InetAddress.getByName(addr);
061: }
062:
063: /**
064: * the same as the similarly named javax.net.SocketFactory operation.
065: */
066: public static SocketFactory getDefault() {
067: return new RemoteDeliverySocketFactory();
068: }
069:
070: /**
071: * the same as the similarly named javax.net.SocketFactory operation.
072: * Just to be safe, it is not used by JavaMail 1.3.
073: * This is the only method used by JavaMail 1.4.
074: */
075: public Socket createSocket() throws IOException {
076: Socket s = new Socket();
077: s.bind(new InetSocketAddress(bindAddress, 0));
078: return s;
079: }
080:
081: /**
082: * the same as the similarly named javax.net.SocketFactory operation.
083: * This is the one which is used by JavaMail 1.3.
084: * This is not used by JavaMail 1.4.
085: */
086: public Socket createSocket(String host, int port)
087: throws IOException, UnknownHostException {
088: return new Socket(host, port, bindAddress, 0);
089: }
090:
091: /**
092: * the same as the similarly named javax.net.SocketFactory operation.
093: * Just to be safe, it is not used by JavaMail 1.3.
094: * This is not used by JavaMail 1.4.
095: */
096: public Socket createSocket(String host, int port,
097: InetAddress clientHost, int clientPort) throws IOException,
098: UnknownHostException {
099: return new Socket(host, port, clientHost == null ? bindAddress
100: : clientHost, clientPort);
101: }
102:
103: /**
104: * the same as the similarly named javax.net.SocketFactory operation.
105: * Just to be safe, it is not used by JavaMail 1.3.
106: * This is not used by JavaMail 1.4.
107: */
108: public Socket createSocket(InetAddress host, int port)
109: 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, int port,
119: InetAddress clientAddress, int clientPort)
120: throws IOException {
121: return new Socket(address, port,
122: clientAddress == null ? bindAddress : clientAddress,
123: clientPort);
124: }
125:
126: /**
127: * it should be set by setBindAdress(). Null means the socket is bind to
128: * the default address.
129: */
130: private static InetAddress bindAddress;
131: }
|