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.mpt;
21
22 import java.net.InetSocketAddress;
23 import java.nio.channels.SocketChannel;
24
25 import org.apache.james.mpt.HostSystem.Continuation;
26
27 /**
28 * Session factory creates session which connection to a server port.
29 */
30 public class ExternalSessionFactory implements SessionFactory {
31
32 public static final String IMAP_SHABANG = "* OK IMAP4rev1 Server ready";
33 protected final InetSocketAddress address;
34 protected final Monitor monitor;
35 protected final String shabang;
36
37 public ExternalSessionFactory(final String host, final int port, final Monitor monitor, final String shabang) {
38 super();
39 this.address = new InetSocketAddress(host, port);
40 this.monitor = monitor;
41 this.shabang = shabang;
42 }
43
44 public Session newSession(Continuation continuation) throws Exception {
45 monitor.note("Connecting to " + address.getHostName() + ":" + address.getPort());
46 final SocketChannel channel = SocketChannel.open(address);
47 channel.configureBlocking(false);
48 final ExternalSession result = new ExternalSession(channel, monitor, shabang);
49 return result;
50 }
51
52 public void reset() throws Exception {
53 monitor.note("Please reset system.");
54 }
55
56 /**
57 * Constructs a <code>String</code> with all attributes
58 * in name = value format.
59 *
60 * @return a <code>String</code> representation
61 * of this object.
62 */
63 public String toString()
64 {
65 final String TAB = " ";
66
67 String retValue = "ExternalSessionFactory ( "
68 + "address = " + this.address + TAB
69 + "monitor = " + this.monitor + TAB
70 + "shabang = " + this.shabang + TAB
71 + " )";
72
73 return retValue;
74 }
75 }