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 ****************************************************************/ 019 020package org.apache.james.mpt.session; 021 022import java.net.InetSocketAddress; 023import java.nio.channels.SocketChannel; 024 025import org.apache.james.mpt.api.Continuation; 026import org.apache.james.mpt.api.Monitor; 027import org.apache.james.mpt.api.Session; 028import org.apache.james.mpt.api.SessionFactory; 029 030/** 031 * Session factory creates session which connection to a server port. 032 */ 033public class ExternalSessionFactory implements SessionFactory { 034 035 public static final String IMAP_SHABANG = "* OK IMAP4rev1 Server ready"; 036 protected final InetSocketAddress address; 037 protected final Monitor monitor; 038 protected final String shabang; 039 040 public ExternalSessionFactory(Monitor monitor, String shabang) { 041 this(null, monitor, shabang); 042 } 043 044 public ExternalSessionFactory(String host, int port, Monitor monitor, String shabang) { 045 this(new InetSocketAddress(host, port), monitor, shabang); 046 } 047 048 public ExternalSessionFactory(InetSocketAddress address, Monitor monitor, String shabang) { 049 super(); 050 this.monitor = monitor; 051 this.shabang = shabang; 052 this.address = address; 053 } 054 055 public Session newSession(Continuation continuation) throws Exception { 056 InetSocketAddress address = getAddress(); 057 monitor.note("Connecting to " + address.getHostName() + ":" + address.getPort()); 058 final SocketChannel channel = SocketChannel.open(address); 059 channel.configureBlocking(false); 060 return new ExternalSession(channel, monitor, shabang); 061 } 062 063 protected InetSocketAddress getAddress() { 064 return address; 065 } 066 067 /** 068 * Constructs a <code>String</code> with all attributes 069 * in name = value format. 070 * 071 * @return a <code>String</code> representation 072 * of this object. 073 */ 074 public String toString() { 075 final String TAB = " "; 076 077 return "ExternalSessionFactory ( " 078 + "address = " + this.getAddress() + TAB 079 + "monitor = " + this.monitor + TAB 080 + "shabang = " + this.shabang + TAB 081 + " )"; 082 } 083}