~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/detection/jndi/startup/JNDIDetectorServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
package org.jboss.test.remoting.detection.jndi.startup;
 
23
 
 
24
import org.apache.log4j.Level;
 
25
import org.jboss.jrunit.extensions.ServerTestCase;
 
26
import org.jboss.remoting.InvokerLocator;
 
27
import org.jboss.remoting.detection.jndi.JNDIDetector;
 
28
import org.jboss.remoting.network.NetworkRegistry;
 
29
import org.jboss.remoting.samples.detection.jndi.SimpleJNDIServer;
 
30
import org.jboss.remoting.transport.Connector;
 
31
import org.jboss.remoting.util.SecurityUtility;
 
32
import org.jboss.test.remoting.TestUtil;
 
33
import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler;
 
34
import org.jnp.server.Main;
 
35
import javax.management.MBeanServer;
 
36
import javax.management.MBeanServerFactory;
 
37
import javax.management.ObjectName;
 
38
 
 
39
import java.lang.reflect.Method;
 
40
import java.net.InetAddress;
 
41
import java.security.AccessController;
 
42
import java.security.PrivilegedActionException;
 
43
import java.security.PrivilegedExceptionAction;
 
44
 
 
45
/**
 
46
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
47
 */
 
48
public class JNDIDetectorServer extends ServerTestCase
 
49
{
 
50
   private int detectorPort = 1099;
 
51
   private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
 
52
   private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
 
53
 
 
54
   private JNDIDetector detector;
 
55
   private Connector connector;
 
56
   private NetworkRegistry registry;
 
57
 
 
58
   public void setUp() throws Exception
 
59
   {
 
60
      // start JNDI server
 
61
      Object namingBean = null;
 
62
      Class namingBeanImplClass = null;
 
63
      try
 
64
      {
 
65
         namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
 
66
         namingBean = namingBeanImplClass.newInstance();
 
67
         Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
 
68
         setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
 
69
         startMethod.invoke(namingBean, new Object[] {});
 
70
      }
 
71
      catch (Exception e)
 
72
      {
 
73
         SimpleJNDIServer.println("Cannot find NamingBeanImpl: must be running jdk 1.4");
 
74
      }
 
75
      
 
76
      int port = 1099;
 
77
      String host = InetAddress.getLocalHost().getHostName();
 
78
      Main JNDIServer = new Main();
 
79
      if (namingBean != null)
 
80
      {
 
81
         Class namingBeanClass = Class.forName("org.jnp.server.NamingBean");
 
82
         Method setNamingInfoMethod = JNDIServer.getClass().getMethod("setNamingInfo", new Class[] {namingBeanClass});
 
83
         setNamingInfoMethod.invoke(JNDIServer, new Object[] {namingBean});
 
84
      }
 
85
      JNDIServer.setPort(port);
 
86
      JNDIServer.setBindAddress(host);
 
87
      JNDIServer.start();
 
88
      System.out.println("Started JNDI server on " + host + ":" + port);
 
89
 
 
90
      String detectorHost = InetAddress.getLocalHost().getHostName();
 
91
 
 
92
      detector = new JNDIDetector();
 
93
      detector.setCleanDetectionNumber(2);
 
94
 
 
95
      System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
 
96
      System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
 
97
 
 
98
      MBeanServer server = MBeanServerFactory.createMBeanServer();
 
99
 
 
100
      registry = NetworkRegistry.getInstance();
 
101
      server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
 
102
 
 
103
      port = TestUtil.getRandomPort();
 
104
      System.out.println("port = " + port);
 
105
 
 
106
      String bindAddr = System.getProperty("jrunit.bind_addr", host);
 
107
      InvokerLocator locator = new InvokerLocator("socket://" + bindAddr + ":" + port);
 
108
 
 
109
      System.out.println("Starting remoting server with locator uri of: " + locator.getLocatorURI());
 
110
      connector = new Connector();
 
111
      connector.setInvokerLocator(locator.getLocatorURI());
 
112
      connector.create();
 
113
 
 
114
      MockServerInvocationHandler handler = new MockServerInvocationHandler();
 
115
      connector.addInvocationHandler("mock", handler);
 
116
 
 
117
      ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
 
118
      server.registerMBean(connector, obj);
 
119
      //connector.create();
 
120
      connector.start();
 
121
 
 
122
      //Need to set new domain for identity
 
123
      server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
 
124
 
 
125
      // set config info for detector and start it.
 
126
      detector.setPort(detectorPort);
 
127
      detector.setHost(detectorHost);
 
128
      detector.setContextFactory(contextFactory);
 
129
      detector.setURLPackage(urlPackage);
 
130
      detector.start();
 
131
      Thread.sleep(10000);
 
132
   }
 
133
 
 
134
   public void tearDown() throws Exception
 
135
   {
 
136
      if (detector != null)
 
137
      {
 
138
         detector.stop();
 
139
      }
 
140
      if (connector != null)
 
141
      {
 
142
         connector.stop();
 
143
         connector.destroy();
 
144
      }
 
145
   }
 
146
 
 
147
   public static void main(String[] args)
 
148
   {
 
149
      org.apache.log4j.BasicConfigurator.configure();
 
150
      org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
 
151
 
 
152
      try
 
153
      {
 
154
         JNDIDetectorServer test = new JNDIDetectorServer();
 
155
         test.setUp();
 
156
      }
 
157
      catch (Exception e)
 
158
      {
 
159
         e.printStackTrace();
 
160
      }
 
161
 
 
162
   }
 
163
 
 
164
   
 
165
   static private void setSystemProperty(final String name, final String value)
 
166
   {
 
167
      if (SecurityUtility.skipAccessControl())
 
168
      {
 
169
         System.setProperty(name, value);
 
170
         return;
 
171
      }
 
172
      
 
173
      try
 
174
      {
 
175
         AccessController.doPrivileged( new PrivilegedExceptionAction()
 
176
         {
 
177
            public Object run() throws Exception
 
178
            {
 
179
               return System.setProperty(name, value);
 
180
            }
 
181
         });
 
182
      }
 
183
      catch (PrivilegedActionException e)
 
184
      {
 
185
         throw (RuntimeException) e.getCause();
 
186
      }
 
187
   }
 
188
}
 
 
b'\\ No newline at end of file'