KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > socket > ssl > basic > InvokerServerTest


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.transport.socket.ssl.basic;
8
9 import java.io.IOException JavaDoc;
10 import java.security.KeyManagementException JavaDoc;
11 import java.security.KeyStoreException JavaDoc;
12 import java.security.NoSuchAlgorithmException JavaDoc;
13 import java.security.UnrecoverableKeyException JavaDoc;
14 import java.security.cert.CertificateException JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18 import javax.net.ServerSocketFactory;
19 import org.jboss.jrunit.extensions.ServerTestCase;
20 import org.jboss.logging.Logger;
21 import org.jboss.remoting.InvokerLocator;
22 import org.jboss.remoting.ServerInvocationHandler;
23 import org.jboss.remoting.security.SSLSocketBuilder;
24 import org.jboss.remoting.transport.Connector;
25 import org.jboss.remoting.transport.socket.ssl.SSLSocketServerInvoker;
26 import org.jboss.test.remoting.TestUtil;
27 import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler;
28 import org.jboss.test.remoting.transport.socket.ssl.SSLInvokerConstants;
29
30 /**
31  * This is the concrete test for invoker server.
32  *
33  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
34  */

35 public class InvokerServerTest extends ServerTestCase implements SSLInvokerConstants
36 {
37    private int serverPort = port;
38    private Connector connector = null;
39
40    private static final Logger log = Logger.getLogger(InvokerServerTest.class);
41
42    public void init(Map JavaDoc metatdata) throws Exception JavaDoc
43    {
44       if(serverPort < 0)
45       {
46          serverPort = TestUtil.getRandomPort();
47       }
48       log.debug("port = " + serverPort);
49
50       connector = new Connector();
51       InvokerLocator locator = new InvokerLocator(buildLocatorURI(metatdata));
52       connector.setInvokerLocator(locator.getLocatorURI());
53       connector.create();
54
55       ServerSocketFactory svrSocketFactory = createServerSocketFactory();
56       SSLSocketServerInvoker socketSvrInvoker = (SSLSocketServerInvoker) connector.getServerInvoker();
57       socketSvrInvoker.setServerSocketFactory(svrSocketFactory);
58
59       connector.addInvocationHandler(getSubsystem(), getServerInvocationHandler());
60       connector.start();
61    }
62
63    private ServerSocketFactory createServerSocketFactory()
64          throws NoSuchAlgorithmException JavaDoc, KeyManagementException JavaDoc, IOException JavaDoc,
65                 CertificateException JavaDoc, UnrecoverableKeyException JavaDoc, KeyStoreException JavaDoc
66    {
67       ServerSocketFactory serverSocketFactory = null;
68
69       // since doing basic (using default ssl server socket factory)
70
// need to set the system properties to the keystore and password
71
String JavaDoc keyStoreFilePath = this.getClass().getResource("../.keystore").getFile();
72       System.setProperty("javax.net.ssl.keyStore", keyStoreFilePath);
73       System.setProperty("javax.net.ssl.keyStorePassword", "opensource");
74
75       SSLSocketBuilder server = new SSLSocketBuilder();
76       serverSocketFactory = server.createSSLServerSocketFactory();
77
78       return serverSocketFactory;
79    }
80
81    private String JavaDoc buildLocatorURI(Map JavaDoc metadata)
82    {
83       if(metadata == null || metadata.size() == 0)
84       {
85          return transport + "://" + host + ":" + serverPort;
86       }
87       else
88       {
89          StringBuffer JavaDoc uriBuffer = new StringBuffer JavaDoc(transport + "://localhost:" + serverPort);
90
91          Set JavaDoc keys = metadata.keySet();
92          Iterator JavaDoc itr = keys.iterator();
93          while(itr.hasNext())
94          {
95             String JavaDoc key = (String JavaDoc) itr.next();
96             String JavaDoc value = (String JavaDoc) metadata.get(key);
97             uriBuffer.append(key + "=" + value + ",");
98          }
99          return uriBuffer.substring(0, uriBuffer.length() - 1);
100       }
101    }
102
103    protected String JavaDoc getSubsystem()
104    {
105       return "mock";
106    }
107
108    protected ServerInvocationHandler getServerInvocationHandler()
109    {
110       return new MockServerInvocationHandler();
111    }
112
113    protected void setUp() throws Exception JavaDoc
114    {
115       init(null);
116    }
117
118    protected void tearDown() throws Exception JavaDoc
119    {
120       if(connector != null)
121       {
122          connector.stop();
123          connector.destroy();
124       }
125    }
126
127 }
128
Popular Tags