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

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/detection/util/DetectorUtil.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

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.remoting.detection.util;
 
23
 
 
24
import java.security.AccessController;
 
25
import java.security.PrivilegedActionException;
 
26
import java.security.PrivilegedExceptionAction;
 
27
 
 
28
import javax.management.MBeanServer;
 
29
import javax.management.MBeanServerFactory;
 
30
import javax.management.ObjectName;
 
31
import org.apache.log4j.Level;
 
32
import org.jboss.logging.Logger;
 
33
import org.jboss.remoting.InvokerLocator;
 
34
import org.jboss.remoting.detection.Detector;
 
35
import org.jboss.remoting.detection.jndi.JNDIDetector;
 
36
import org.jboss.remoting.detection.multicast.MulticastDetector;
 
37
import org.jboss.remoting.network.NetworkInstance;
 
38
import org.jboss.remoting.network.NetworkRegistry;
 
39
import org.jboss.remoting.transport.Connector;
 
40
import org.jboss.remoting.util.SecurityUtility;
 
41
 
 
42
 
 
43
/**
 
44
 * A simple utility class that will periodically print out remoting servers
 
45
 * running based on detections.  Can specify to either use multicast or jndi
 
46
 * detection type.
 
47
 *
 
48
 * @author <a href="mailto:telrod@vocalocity.net">Tom Elrod</a>
 
49
 * @version $Revision: 5001 $
 
50
 */
 
51
public class DetectorUtil
 
52
{
 
53
   /**
 
54
    * multicast *
 
55
    */
 
56
   public static final String TYPE_MULTICAST = "multicast";
 
57
   /**
 
58
    * jndi *
 
59
    */
 
60
   public static final String TYPE_JNDI = "jndi";
 
61
   /**
 
62
    * 2410 *
 
63
    */
 
64
   public static final int DEFAULT_PORT = 2410;
 
65
   /**
 
66
    * localhost *
 
67
    */
 
68
   public static final String DEFAULT_HOST = "localhost";
 
69
 
 
70
   private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
 
71
   private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
 
72
 
 
73
   private String type = TYPE_MULTICAST;
 
74
   private int port = DEFAULT_PORT;
 
75
   private String host = DEFAULT_HOST;
 
76
 
 
77
   public DetectorUtil()
 
78
   {
 
79
   }
 
80
 
 
81
   public DetectorUtil(String type)
 
82
   {
 
83
      this(type, DEFAULT_PORT, DEFAULT_HOST);
 
84
   }
 
85
 
 
86
   public DetectorUtil(String type, int port)
 
87
   {
 
88
      this(type, port, DEFAULT_HOST);
 
89
   }
 
90
 
 
91
   public DetectorUtil(String type, int port, String host)
 
92
   {
 
93
      this.type = type;
 
94
      this.port = port;
 
95
      this.host = host;
 
96
   }
 
97
 
 
98
   public void start()
 
99
   {
 
100
      try
 
101
      {
 
102
 
 
103
         org.apache.log4j.BasicConfigurator.configure();
 
104
         org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
 
105
         Logger log = Logger.getLogger(getClass());
 
106
 
 
107
         setSystemProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
 
108
 
 
109
         MBeanServer server = createMBeanServer();
 
110
         NetworkRegistry registry = NetworkRegistry.getInstance();
 
111
         registerMBean(server, registry, new ObjectName("remoting:type=NetworkRegistry"));
 
112
        
 
113
         InvokerLocator locator = new InvokerLocator("socket://localhost");
 
114
         //ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
 
115
         //SocketServerInvoker invokerSvr = new SocketServerInvoker(clsLoader, locator);
 
116
         //ObjectName objName = new ObjectName("jboss.remoting:type=SocketServerInvoker");
 
117
         //server.registerMBean(invokerSvr, objName);
 
118
         //invokerSvr.start();
 
119
 
 
120
         Connector connector = new Connector();
 
121
         connector.setInvokerLocator(locator.getLocatorURI());
 
122
         ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
 
123
         registerMBean(server, connector, obj);
 
124
         //connector.create();
 
125
         connector.start();
 
126
 
 
127
         Detector detector = null;
 
128
         ObjectName objName = null;
 
129
 
 
130
         if(type.equals(TYPE_MULTICAST))
 
131
         {
 
132
            MulticastDetector mdet = new MulticastDetector();
 
133
            mdet.setPort(port);
 
134
            detector = mdet;
 
135
            objName = new ObjectName("remoting:type=Detector,transport=multicast");
 
136
         }
 
137
         else if(type.equals(TYPE_JNDI))
 
138
         {
 
139
            JNDIDetector jdet = new JNDIDetector();
 
140
            jdet.setPort(port);
 
141
            jdet.setHost(host);
 
142
            jdet.setContextFactory(contextFactory);
 
143
            jdet.setURLPackage(urlPackage);
 
144
            detector = jdet;
 
145
            objName = new ObjectName("remoting:type=Detector,transport=jndi");
 
146
         }
 
147
         
 
148
         registerMBean(server, detector, objName);
 
149
         detector.start();
 
150
         System.err.println("Starting Detector");
 
151
 
 
152
         while(true)
 
153
         {
 
154
            Thread.currentThread().sleep(3000);
 
155
            NetworkInstance[] instances = registry.getServers();
 
156
            for(int x = 0; x < instances.length; x++)
 
157
            {
 
158
               log.debug(instances[x]);
 
159
            }
 
160
         }
 
161
 
 
162
 
 
163
      }
 
164
      catch(Exception ex)
 
165
      {
 
166
         System.err.println("Error creating and starting DetectorUtil.");
 
167
         ex.printStackTrace();
 
168
      }
 
169
   }
 
170
 
 
171
   public static void main(String[] args)
 
172
   {
 
173
      DetectorUtil test = null;
 
174
 
 
175
      if(args.length == 1)
 
176
      {
 
177
         String type = args[0];
 
178
         test = new DetectorUtil(type);
 
179
      }
 
180
      else if(args.length == 2)
 
181
      {
 
182
         String type = args[0];
 
183
         int port = Integer.parseInt(args[1]);
 
184
         test = new DetectorUtil(type, port);
 
185
      }
 
186
      else if(args.length == 3)
 
187
      {
 
188
         String type = args[0];
 
189
         int port = Integer.parseInt(args[1]);
 
190
         String host = args[2];
 
191
         test = new DetectorUtil(type, port, host);
 
192
      }
 
193
      else
 
194
      {
 
195
         test = new DetectorUtil();
 
196
         System.out.println("Using default type (" + test.getTransport() +
 
197
                            ") and default port (" + test.getPort() + ") and " +
 
198
                            "default host (" + test.getHost() + ")" +
 
199
                            "\nCan enter type (multicast, jndi), port, and/or host via command line.");
 
200
      }
 
201
 
 
202
      test.start();
 
203
 
 
204
 
 
205
   }
 
206
 
 
207
   private String getHost()
 
208
   {
 
209
      return host;
 
210
   }
 
211
 
 
212
   private int getPort()
 
213
   {
 
214
      return port;
 
215
   }
 
216
 
 
217
   private String getTransport()
 
218
   {
 
219
      return type;
 
220
   }
 
221
 
 
222
   static private MBeanServer createMBeanServer() throws Exception
 
223
   {
 
224
      if (SecurityUtility.skipAccessControl())
 
225
      {
 
226
         return MBeanServerFactory.createMBeanServer();
 
227
      }
 
228
      
 
229
      try
 
230
      {
 
231
         return (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
 
232
         {
 
233
            public Object run() throws Exception
 
234
            {
 
235
               return MBeanServerFactory.createMBeanServer();
 
236
            }
 
237
         });
 
238
      }
 
239
      catch (PrivilegedActionException e)
 
240
      {
 
241
         throw (Exception) e.getCause();
 
242
      }   
 
243
   }
 
244
   
 
245
   static private void registerMBean(final MBeanServer server, final Object o, final ObjectName name)
 
246
   throws Exception
 
247
   {
 
248
      if (SecurityUtility.skipAccessControl())
 
249
      {
 
250
         server.registerMBean(o, name);
 
251
         return;
 
252
      }
 
253
      
 
254
      try
 
255
      {
 
256
         AccessController.doPrivileged( new PrivilegedExceptionAction()
 
257
         {
 
258
            public Object run() throws Exception
 
259
            {
 
260
               server.registerMBean(o, name);
 
261
               return null;
 
262
            }
 
263
         });
 
264
      }
 
265
      catch (PrivilegedActionException e)
 
266
      {
 
267
         throw (Exception) e.getCause();
 
268
      }
 
269
   }
 
270
   
 
271
   static private void setSystemProperty(final String name, final String value)
 
272
   {
 
273
      if (SecurityUtility.skipAccessControl())
 
274
      {
 
275
         System.setProperty(name, value);
 
276
         return;
 
277
      }
 
278
      
 
279
      try
 
280
      {
 
281
         AccessController.doPrivileged( new PrivilegedExceptionAction()
 
282
         {
 
283
            public Object run() throws Exception
 
284
            {
 
285
               return System.setProperty(name, value);
 
286
            }
 
287
         });
 
288
      }
 
289
      catch (PrivilegedActionException e)
 
290
      {
 
291
         throw (RuntimeException) e.getCause();
 
292
      }
 
293
   }
 
294
}