KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > util > ImapServerLauncher


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.imapserver.util;
21
22 import java.io.IOException JavaDoc;
23 import java.net.ServerSocket JavaDoc;
24 import java.net.Socket JavaDoc;
25
26 import javax.mail.MessagingException JavaDoc;
27
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.configuration.DefaultConfiguration;
30 import org.apache.avalon.framework.container.ContainerUtil;
31 import org.apache.james.imapserver.ImapHandler;
32 import org.apache.james.imapserver.TestConstants;
33 import org.apache.james.imapserver.mock.MockImapHandlerConfigurationData;
34 import org.apache.james.imapserver.mock.MockWatchdog;
35 import org.apache.james.mailboxmanager.MailboxManagerException;
36 import org.apache.james.services.DNSServer;
37 import org.apache.james.test.mock.avalon.MockLogger;
38
39 public class ImapServerLauncher implements TestConstants
40 {
41
42
43     private DNSServer dnsServer;
44
45     public void go() throws IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException
46     {
47         ServerSocket JavaDoc ss = new ServerSocket JavaDoc(HOST_PORT);
48         final MockImapHandlerConfigurationData theConfigData=new MockImapHandlerConfigurationData();
49         while (true) {
50             
51             final Socket JavaDoc s=ss.accept();
52             new Thread JavaDoc() {
53                 public void run() {
54                     try {
55                         ImapHandler imapHandler=new ImapHandler();
56                         imapHandler.enableLogging(new MockLogger());
57                         imapHandler.setConfigurationData(theConfigData);
58                         imapHandler.setDnsServer(getDNSServer());
59                         imapHandler.setStreamDumpDir("streamdump");
60                         imapHandler.setWatchdog(new MockWatchdog());
61                         System.out.println("Handle connection "+s);
62                         imapHandler.handleConnection(s);
63                         System.out.println("Handle connection finished."+s);
64     
65                     } catch (Exception JavaDoc e) {
66                         throw new RuntimeException JavaDoc(e);
67                     }
68                 }
69             }.start();
70             
71             
72         }
73
74     }
75     
76     public DNSServer getDNSServer() throws Exception JavaDoc {
77         dnsServer=new org.apache.james.dnsserver.DNSServer();
78         ContainerUtil.enableLogging(dnsServer, new MockLogger());
79         ContainerUtil.configure(dnsServer, new DefaultConfiguration("dnsserver"));
80         ContainerUtil.initialize(dnsServer);
81         return dnsServer;
82     }
83
84     public static void main(String JavaDoc[] args)
85     {
86         try {
87             new ImapServerLauncher().go();
88         } catch (Exception JavaDoc e) {
89             throw new RuntimeException JavaDoc(e);
90         }
91
92     }
93
94 }
95
Popular Tags