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

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/detection/jndi/JNDIDetector.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.jndi;
23
 
 
24
 
import org.jboss.logging.Logger;
25
 
import org.jboss.remoting.InvokerLocator;
26
 
import org.jboss.remoting.InvokerRegistry;
27
 
import org.jboss.remoting.detection.AbstractDetector;
28
 
import org.jboss.remoting.detection.Detection;
29
 
import org.jboss.remoting.ident.Identity;
30
 
import org.jboss.remoting.transport.PortUtil;
31
 
import org.jboss.remoting.util.SecurityUtility;
32
 
import org.jnp.interfaces.NamingContextFactory;
33
 
import org.jnp.server.Main;
34
 
 
35
 
import javax.naming.Binding;
36
 
import javax.naming.Context;
37
 
import javax.naming.InitialContext;
38
 
import javax.naming.NameAlreadyBoundException;
39
 
import javax.naming.NamingEnumeration;
40
 
import javax.naming.NamingException;
41
 
 
42
 
import java.io.IOException;
43
 
import java.lang.reflect.Method;
44
 
import java.net.InetAddress;
45
 
import java.net.UnknownHostException;
46
 
import java.security.AccessController;
47
 
import java.security.PrivilegedAction;
48
 
import java.security.PrivilegedActionException;
49
 
import java.security.PrivilegedExceptionAction;
50
 
import java.util.Map;
51
 
import java.util.Properties;
52
 
 
53
 
/**
54
 
 * This is a remoting detector for the remoting package which uses a JNDI server to
55
 
 * maintain the registeries for remote invoker servers (stored as Detection messages).
56
 
 * This detector is intended to be used in conjuntion with an external JNDI server that
57
 
 * is already running.  This is done by passing all the information needed to connect
58
 
 * to the remote JNDI server via the setter methods.  This can also be done within
59
 
 * the jboss-service.xml.  An example of the entry is as follows:<p>
60
 
 * &lt;mbean code="org.jboss.remoting.detection.jndi.JNDIDetector" name="jboss.remoting:service=Detector,transport=jndi"&gt;<br>
61
 
 * &lt;attribute name="Port"&gt;5555&lt;/attribute&gt;<br>
62
 
 * &lt;attribute name="Host"&gt;foo.bar.com&lt;/attribute&gt;<br>
63
 
 * &lt;attribute name="ContextFactory"&gt;org.jnp.interfaces.NamingContextFactory&lt;/attribute&gt;<br>
64
 
 * &lt;attribute name="URLPackage"&gt;org.jboss.naming:org.jnp.interfaces&lt;/attribute&gt;<br>
65
 
 * &lt;/mbean&gt;<br><p>
66
 
 * Note: The above xml is for the JBoss JNP JNDI server, and has not be tested (just an example).<p>
67
 
 * Be aware that just because this detector is stopped (and the entry removed from the JNDI server)
68
 
 * remote JNDIDetectors may not recognize that the invoker servers are not available.  This is because
69
 
 * once remote invoker servers (connectors) are detected, they will be pinged directly to determine
70
 
 * if they are no longer available.  However, no new JNDIDetectors will detect your server once stopped.
71
 
 * Also, please note that currently the detection registries are bound at the root context and
72
 
 * not a sub context (which is on the todo list, but you know how that goes).<p>
73
 
 * Important to also note that if any of the above attributes are set once the detector has
74
 
 * started, they will not be used in connecting to the JNDI server until the detector is stopped
75
 
 * and re-started (they do not change the JNDI server connection dynamically).<p>
76
 
 *
77
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
78
 
 */
79
 
public class JNDIDetector extends AbstractDetector implements JNDIDetectorMBean
80
 
{
81
 
   private int port;
82
 
   private String host;
83
 
   private String contextFactory = NamingContextFactory.class.getName();
84
 
   ;
85
 
   private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
86
 
   ;
87
 
 
88
 
   private Identity id;
89
 
   private Context context;
90
 
 
91
 
   public static final String DETECTION_SUBCONTEXT_NAME = "detection";
92
 
 
93
 
   private String subContextName = DETECTION_SUBCONTEXT_NAME;
94
 
 
95
 
   /**
96
 
    * Indicates the number of time will detect before doing check to see if server still alive.
97
 
    */
98
 
   private int detectionNumber = 5;
99
 
   private int cleanDetectionCount = detectionNumber;
100
 
 
101
 
   protected final Logger log = Logger.getLogger(getClass());
102
 
   
103
 
   public JNDIDetector()
104
 
   {
105
 
   }
106
 
   
107
 
   public JNDIDetector(Map config)
108
 
   {
109
 
      super(config);
110
 
   }
111
 
   
112
 
   /**
113
 
    * Gets the port used to connect to the JNDI Server.
114
 
    *
115
 
    * @return
116
 
    */
117
 
   public int getPort()
118
 
   {
119
 
      return port;
120
 
   }
121
 
 
122
 
   /**
123
 
    * Sets the port to use when connecting to JNDI server
124
 
    *
125
 
    * @param port
126
 
    */
127
 
   public void setPort(int port)
128
 
   {
129
 
      this.port = port;
130
 
   }
131
 
 
132
 
   /**
133
 
    * Gets the host to use when connecting to JNDI server
134
 
    *
135
 
    * @return
136
 
    */
137
 
   public String getHost()
138
 
   {
139
 
      return host;
140
 
   }
141
 
 
142
 
   /**
143
 
    * Sets the host to use when connecting to JNDI server
144
 
    *
145
 
    * @param host
146
 
    */
147
 
   public void setHost(String host)
148
 
   {
149
 
      this.host = host;
150
 
   }
151
 
 
152
 
   /**
153
 
    * The context factory string used when connecting to the JNDI server
154
 
    *
155
 
    * @return
156
 
    */
157
 
   public String getContextFactory()
158
 
   {
159
 
      return contextFactory;
160
 
   }
161
 
 
162
 
   /**
163
 
    * Sets the sub context name under which detection messages will be bound
164
 
    * and looked up.
165
 
    * @param subContextName
166
 
    */
167
 
   public void setSubContextName(String subContextName)
168
 
   {
169
 
      this.subContextName = subContextName;
170
 
   }
171
 
 
172
 
   /**
173
 
    * Gets the sub context name under which detection messages will be bound and
174
 
    * looked up.
175
 
    * @return
176
 
    */
177
 
   public String getSubContextName()
178
 
   {
179
 
      return this.subContextName;
180
 
   }
181
 
 
182
 
   /**
183
 
    * The context factory string to use when connecting to the JNDI server.
184
 
    * Should be a qualified class name for JNDI client.
185
 
    *
186
 
    * @param contextFactory
187
 
    */
188
 
   public void setContextFactory(String contextFactory)
189
 
   {
190
 
      this.contextFactory = contextFactory;
191
 
   }
192
 
 
193
 
   /**
194
 
    * The url package string used when connecting to JNDI server
195
 
    *
196
 
    * @return
197
 
    */
198
 
   public String getURLPackage()
199
 
   {
200
 
      return urlPackage;
201
 
   }
202
 
 
203
 
   /**
204
 
    * The url package string to use when connecting to the JNDI server.
205
 
    *
206
 
    * @param urlPackage
207
 
    */
208
 
   public void setURLPackage(String urlPackage)
209
 
   {
210
 
      this.urlPackage = urlPackage;
211
 
   }
212
 
 
213
 
   /**
214
 
    * Will establish the connection to the JNDI server and start detection of other servers.
215
 
    *
216
 
    * @throws Exception
217
 
    */
218
 
   public void start() throws Exception
219
 
   {
220
 
      createContext();
221
 
      id = Identity.get(mbeanserver);
222
 
      super.start();
223
 
   }
224
 
 
225
 
   /**
226
 
    * Creates connection to JNDI server (which should have already happened in start()
227
 
    * method) and will begin checking for remote servers as well as registering itself
228
 
    * so will be visible by remote detectors.
229
 
    */
230
 
   protected void heartbeat()
231
 
   {
232
 
      try
233
 
      {
234
 
         //Need to establish connection to server
235
 
         if(context == null)
236
 
         {
237
 
            createContext();
238
 
         }
239
 
         checkRemoteDetectionMsg();
240
 
      }
241
 
      catch(NamingException nex)
242
 
      {
243
 
         log.error("Can not connect to JNDI server to register local connectors.", nex);
244
 
      }
245
 
   }
246
 
 
247
 
   protected void forceHeartbeat()
248
 
   {
249
 
      heartbeat();
250
 
   }
251
 
 
252
 
   /**
253
 
    * Gets the number of detection iterations before manually pinging remote
254
 
    * server to make sure still alive.
255
 
    *
256
 
    * @return
257
 
    */
258
 
   public int getCleanDetectionNumber()
259
 
   {
260
 
      return detectionNumber;
261
 
   }
262
 
 
263
 
   /**
264
 
    * Sets the number of detection iterations before manually pinging remote
265
 
    * server to make sure still alive.  This is needed since remote server
266
 
    * could crash and yet still have an entry in the JNDI server, thus
267
 
    * making it appear that it is still there.
268
 
    *
269
 
    * @param cleanDetectionNumber
270
 
    */
271
 
   public void setCleanDetectionNumber(int cleanDetectionNumber)
272
 
   {
273
 
      detectionNumber = cleanDetectionNumber;
274
 
      cleanDetectionCount = detectionNumber;
275
 
   }
276
 
 
277
 
   private void checkRemoteDetectionMsg()
278
 
   {
279
 
      try
280
 
      {
281
 
         boolean localFound = false;
282
 
         cleanDetectionCount++;
283
 
         boolean cleanDetect = cleanDetectionCount > detectionNumber;
284
 
         String bindName = "";
285
 
         NamingEnumeration enumeration = listBindings(context, bindName);
286
 
         while(enumeration.hasMore())
287
 
         {
288
 
            Binding binding = (Binding) enumeration.next();
289
 
            Detection regMsg = (Detection) binding.getObject();
290
 
            // No need to detect myself here
291
 
            if(isRemoteDetection(regMsg))
292
 
            {
293
 
               log.debug("Detected id: " + regMsg.getIdentity().getInstanceId() + ", message: " + regMsg);
294
 
 
295
 
               if(cleanDetect)
296
 
               {
297
 
                  if(log.isTraceEnabled())
298
 
                  {
299
 
                     log.trace("Doing clean detection.");
300
 
                  }
301
 
                  // Need to actually detect if servers registered in JNDI server
302
 
                  // are actually there (since could die before unregistering)
303
 
                  ClassLoader cl = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
304
 
                  {
305
 
                     public Object run()
306
 
                     {
307
 
                        return JNDIDetector.class.getClassLoader();
308
 
                     }
309
 
                  });
310
 
                  
311
 
                  if(!checkInvokerServer(regMsg, cl))
312
 
                  {
313
 
                     unregisterDetection(regMsg.getIdentity().getInstanceId());
314
 
                  }
315
 
                  else
316
 
                  {
317
 
                     // Now, let parent handle detection
318
 
                     detect(regMsg);
319
 
                  }
320
 
               }
321
 
               else
322
 
               {
323
 
                  // Now, let parent handle detection
324
 
                  detect(regMsg);
325
 
               }
326
 
            }
327
 
            else
328
 
            {
329
 
               //verify local detection message is correct
330
 
               if(!verifyLocalDetectionMsg(regMsg))
331
 
               {
332
 
                  addLocalDetectionMsg();
333
 
               }
334
 
               localFound = true;
335
 
            }
336
 
         }
337
 
         if(cleanDetect)
338
 
         {
339
 
            // did clean detect, now need to reset.
340
 
            cleanDetectionCount = 0;
341
 
         }
342
 
         if(!localFound)
343
 
         {
344
 
            // never found local detection message in list, so add it
345
 
            addLocalDetectionMsg();
346
 
         }
347
 
      }
348
 
      catch(NamingException e)
349
 
      {
350
 
         log.error("Exception getting detection messages from JNDI server.", e);
351
 
      }
352
 
   }
353
 
 
354
 
   private boolean verifyLocalDetectionMsg(Detection regMsg) throws NamingException
355
 
   {
356
 
      boolean verified = false;
357
 
 
358
 
      InvokerLocator[] locators = InvokerRegistry.getRegisteredServerLocators();
359
 
      Detection msg = createDetection();
360
 
      String sId = id.getInstanceId();
361
 
      InvokerLocator[] invokers = regMsg.getLocators();
362
 
 
363
 
      // first do sanity check to make sure even local detection msg (just in case)
364
 
      if(sId.equals(regMsg.getIdentity().getInstanceId()))
365
 
      {
366
 
 
367
 
         // now see if invoker list changed
368
 
         boolean changed = false;
369
 
         if(locators.length != invokers.length)
370
 
         {
371
 
            changed = true;
372
 
         }
373
 
         else
374
 
         {
375
 
            // now need to make sure all the invokers are same now as in old detection msg
376
 
            // not the most efficient (or elegant) way to do this, but list is short
377
 
            boolean found = false; // flag for if current invoker in list found in old list
378
 
            for(int i = 0; i < locators.length; i++)
379
 
            {
380
 
               found = false;
381
 
               for(int x = 0; x < invokers.length; x++)
382
 
               {
383
 
                  if(locators[i].equals(invokers[x]))
384
 
                  {
385
 
                     found = true;
386
 
                     break;
387
 
                  }
388
 
               }
389
 
               if(!found)
390
 
               {
391
 
                  break;
392
 
               }
393
 
            }
394
 
            if(!found)
395
 
            {
396
 
               changed = true;
397
 
            }
398
 
         }
399
 
         if(changed)
400
 
         {
401
 
            registerDetectionMsg(sId, msg);
402
 
         }
403
 
         // are sure that local detection is correct in JNDI server now
404
 
         verified = true;
405
 
      }
406
 
      return verified;
407
 
   }
408
 
 
409
 
   private void addLocalDetectionMsg() throws NamingException
410
 
   {
411
 
      Detection msg = createDetection();
412
 
      String sId = id.getInstanceId();
413
 
      registerDetectionMsg(sId, msg);
414
 
   }
415
 
 
416
 
   private void registerDetectionMsg(String sId, Detection msg) throws NamingException
417
 
   {
418
 
      if(sId != null && msg != null)
419
 
      {
420
 
         try
421
 
         {
422
 
            rebind(context, sId, msg);
423
 
            log.info("Added " + sId + " to registry.");
424
 
         }
425
 
         catch(NameAlreadyBoundException nabex)
426
 
         {
427
 
            if(log.isTraceEnabled())
428
 
            {
429
 
               log.trace(sId + " already bound to server.");
430
 
            }
431
 
         }
432
 
      }
433
 
   }
434
 
 
435
 
   /**
436
 
    * Convience method to see if given proper configuration to connect to an
437
 
    * existing JNDI server.  If not, will create one via JBoss JNP.  Should
438
 
    * really only be needed for testing.
439
 
    */
440
 
   private void verifyJNDIServer()
441
 
   {
442
 
      if(host == null || host.length() == 0)
443
 
      {
444
 
         try
445
 
         {
446
 
            log.info("JNDI Server configuration information not present so will create a local server.");
447
 
           
448
 
            Object namingBean = null;
449
 
            Class namingBeanImplClass = null;
450
 
            try
451
 
            {
452
 
               namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
453
 
               namingBean = namingBeanImplClass.newInstance();
454
 
               Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
455
 
               setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
456
 
               startMethod.invoke(namingBean, new Object[] {});
457
 
            }
458
 
            catch (Exception e)
459
 
            {
460
 
               log.debug("Cannot find NamingBeanImpl: must be running jdk 1.4");
461
 
            }
462
 
            
463
 
            host = getLocalHostName();
464
 
            port = PortUtil.findFreePort(host);
465
 
 
466
 
            log.info("Remoting JNDI detector starting JNDI server instance since none where specified via configuration.");
467
 
            log.info("Remoting JNDI server started on host + " + host + " and port " + port);
468
 
 
469
 
            //If no server information provided, then start one of our own by default
470
 
            Main server = new Main();
471
 
            if (namingBean != null)
472
 
            {
473
 
               Class namingBeanClass = Class.forName("org.jnp.server.NamingBean");
474
 
               Method setNamingInfoMethod = server.getClass().getMethod("setNamingInfo", new Class[] {namingBeanClass});
475
 
               setNamingInfoMethod.invoke(server, new Object[] {namingBean});
476
 
            }
477
 
            server.setPort(port);
478
 
            server.setBindAddress(host);
479
 
            server.start();
480
 
 
481
 
            contextFactory = NamingContextFactory.class.getName();
482
 
            urlPackage = "org.jboss.naming:org.jnp.interfaces";
483
 
         }
484
 
         catch(Exception e)
485
 
         {
486
 
            log.error("Error starting up JNDI server since none was specified via configuration.", e);
487
 
         }
488
 
      }
489
 
   }
490
 
 
491
 
   /**
492
 
    * Will try to establish the initial context to the JNDI server based
493
 
    * on the configuration properties set.
494
 
    *
495
 
    * @throws NamingException
496
 
    */
497
 
   private void createContext() throws NamingException
498
 
   {
499
 
      verifyJNDIServer();
500
 
 
501
 
      Properties env = new Properties();
502
 
 
503
 
      env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
504
 
      env.put(Context.PROVIDER_URL, host + ":" + port);
505
 
      env.put(Context.URL_PKG_PREFIXES, urlPackage);
506
 
 
507
 
      InitialContext initialContext = createContext(env);
508
 
      try
509
 
      {
510
 
         context = initialContextLookup(initialContext, subContextName);
511
 
      }
512
 
      catch(NamingException e)
513
 
      {
514
 
         try
515
 
         {
516
 
            context = createSubcontext(initialContext, subContextName);
517
 
         }
518
 
         catch(NameAlreadyBoundException e1)
519
 
         {
520
 
            log.debug("The sub context " + subContextName + " was created before we could.");
521
 
            context = initialContextLookup(initialContext, subContextName);
522
 
         }
523
 
      }
524
 
   }
525
 
 
526
 
   public void stop() throws Exception
527
 
   {
528
 
      try
529
 
      {
530
 
         super.stop();
531
 
      }
532
 
      finally // Need to cleanup JNDI, even if super's stop throws exception
533
 
      {
534
 
         String sId = id.getInstanceId();
535
 
         try
536
 
         {
537
 
            unregisterDetection(sId);
538
 
         }
539
 
         catch(NamingException e)
540
 
         {
541
 
            log.warn("Could not unregister " + sId + " before shutdown.  " +
542
 
                     "Root cause is " + e.getMessage());
543
 
         }
544
 
      }
545
 
   }
546
 
 
547
 
   private void unregisterDetection(String sId) throws NamingException
548
 
   {
549
 
      if(log.isTraceEnabled())
550
 
      {
551
 
         log.trace("unregistering detector " + sId);
552
 
      }
553
 
      unbind(context, sId);
554
 
   }
555
 
   
556
 
   static private void setSystemProperty(final String name, final String value)
557
 
   {
558
 
      if (SecurityUtility.skipAccessControl())
559
 
      {
560
 
         System.setProperty(name, value);
561
 
         return;
562
 
      }
563
 
      
564
 
      try
565
 
      {
566
 
         AccessController.doPrivileged( new PrivilegedExceptionAction()
567
 
         {
568
 
            public Object run() throws Exception
569
 
            {
570
 
               return System.setProperty(name, value);
571
 
            }
572
 
         });
573
 
      }
574
 
      catch (PrivilegedActionException e)
575
 
      {
576
 
         throw (RuntimeException) e.getCause();
577
 
      }
578
 
   }
579
 
   
580
 
   static private InetAddress getLocalHost() throws UnknownHostException
581
 
   {
582
 
      if (SecurityUtility.skipAccessControl())
583
 
      {
584
 
         try
585
 
         {
586
 
            return InetAddress.getLocalHost();
587
 
         }
588
 
         catch (IOException e)
589
 
         {
590
 
            return InetAddress.getByName("127.0.0.1");
591
 
         }
592
 
      }
593
 
 
594
 
      try
595
 
      {
596
 
         return (InetAddress) AccessController.doPrivileged( new PrivilegedExceptionAction()
597
 
         {
598
 
            public Object run() throws IOException
599
 
            {
600
 
               try
601
 
               {
602
 
                  return InetAddress.getLocalHost();
603
 
               }
604
 
               catch (IOException e)
605
 
               {
606
 
                  return InetAddress.getByName("127.0.0.1");
607
 
               }
608
 
            }
609
 
         });
610
 
      }
611
 
      catch (PrivilegedActionException e)
612
 
      {
613
 
         throw (UnknownHostException) e.getCause();
614
 
      }
615
 
   }
616
 
   
617
 
   static private String getLocalHostName() throws UnknownHostException
618
 
   {
619
 
      if (SecurityUtility.skipAccessControl())
620
 
      {
621
 
         return getLocalHost().getHostName();
622
 
      }
623
 
 
624
 
      try
625
 
      {
626
 
         return (String) AccessController.doPrivileged( new PrivilegedExceptionAction()
627
 
         {
628
 
            public Object run() throws IOException
629
 
            {
630
 
               InetAddress address = null;
631
 
               try
632
 
               {
633
 
                  address = InetAddress.getLocalHost();
634
 
               }
635
 
               catch (IOException e)
636
 
               {
637
 
                  address = InetAddress.getByName("127.0.0.1");
638
 
               }
639
 
               
640
 
               return address.getHostName();
641
 
            }
642
 
         });
643
 
      }
644
 
      catch (PrivilegedActionException e)
645
 
      {
646
 
         throw (UnknownHostException) e.getCause();
647
 
      }
648
 
   }
649
 
   
650
 
   static private Context createSubcontext(final InitialContext initialContext, final String subContextName)
651
 
   throws NamingException
652
 
   {
653
 
      if (SecurityUtility.skipAccessControl())
654
 
      {
655
 
         return initialContext.createSubcontext(subContextName);
656
 
      }
657
 
 
658
 
      try
659
 
      {
660
 
         return (Context) AccessController.doPrivileged( new PrivilegedExceptionAction() 
661
 
         {
662
 
            public Object run() throws NamingException
663
 
            {
664
 
               return initialContext.createSubcontext(subContextName);
665
 
            }
666
 
         });
667
 
      }
668
 
      catch (PrivilegedActionException e)
669
 
      {
670
 
         throw (NamingException) e.getCause();
671
 
      }
672
 
   }
673
 
   
674
 
   static private Context initialContextLookup(final InitialContext initialContext, final String subContextName)
675
 
   throws NamingException
676
 
   {
677
 
      if (SecurityUtility.skipAccessControl())
678
 
      {
679
 
         return (Context) initialContext.lookup(subContextName);
680
 
      }
681
 
 
682
 
      try
683
 
      {
684
 
         return (Context) AccessController.doPrivileged( new PrivilegedExceptionAction() 
685
 
         {
686
 
            public Object run() throws NamingException
687
 
            {
688
 
               return initialContext.lookup(subContextName);
689
 
            }
690
 
         });
691
 
      }
692
 
      catch (PrivilegedActionException e)
693
 
      {
694
 
         throw (NamingException) e.getCause();
695
 
      }
696
 
   }
697
 
   
698
 
   static private NamingEnumeration listBindings(final Context context, final String bindName)
699
 
   throws NamingException
700
 
   {
701
 
      if (SecurityUtility.skipAccessControl())
702
 
      {
703
 
         return context.listBindings(bindName);
704
 
      }
705
 
 
706
 
      try
707
 
      {
708
 
         return (NamingEnumeration) AccessController.doPrivileged( new PrivilegedExceptionAction() 
709
 
         {
710
 
            public Object run() throws NamingException
711
 
            {
712
 
               return context.listBindings(bindName);
713
 
            }
714
 
         });
715
 
      }
716
 
      catch (PrivilegedActionException e)
717
 
      {
718
 
         throw (NamingException) e.getCause();
719
 
      }
720
 
   }
721
 
   
722
 
   static private void rebind(final Context context, final String name, final Object object)
723
 
   throws NamingException
724
 
   {
725
 
      if (SecurityUtility.skipAccessControl())
726
 
      {
727
 
         context.rebind(name, object);
728
 
         return;
729
 
      }
730
 
 
731
 
      try
732
 
      {
733
 
         AccessController.doPrivileged( new PrivilegedExceptionAction() 
734
 
         {
735
 
            public Object run() throws NamingException
736
 
            {
737
 
               context.rebind(name, object);
738
 
               return null;
739
 
            }
740
 
         });
741
 
      }
742
 
      catch (PrivilegedActionException e)
743
 
      {
744
 
         throw (NamingException) e.getCause();
745
 
      }
746
 
   }
747
 
   
748
 
   static private void unbind(final Context context, final String name)
749
 
   throws NamingException
750
 
   {
751
 
      if (SecurityUtility.skipAccessControl())
752
 
      {
753
 
         context.unbind(name);
754
 
         return;
755
 
      }
756
 
 
757
 
      try
758
 
      {
759
 
         AccessController.doPrivileged( new PrivilegedExceptionAction() 
760
 
         {
761
 
            public Object run() throws NamingException
762
 
            {
763
 
               context.unbind(name);
764
 
               return null;
765
 
            }
766
 
         });
767
 
      }
768
 
      catch (PrivilegedActionException e)
769
 
      {
770
 
         throw (NamingException) e.getCause();
771
 
      }
772
 
   }
773
 
   
774
 
   static private InitialContext createContext(final Properties env) throws NamingException
775
 
   {
776
 
      if (SecurityUtility.skipAccessControl())
777
 
      {
778
 
         return new InitialContext(env);
779
 
      }
780
 
      
781
 
      try
782
 
      {
783
 
         return (InitialContext) AccessController.doPrivileged( new PrivilegedExceptionAction()
784
 
         {
785
 
            public Object run() throws Exception
786
 
            {
787
 
               return new InitialContext(env);
788
 
            }
789
 
         });
790
 
      }
791
 
      catch (PrivilegedActionException e)
792
 
      {
793
 
         throw (RuntimeException) e.getCause();
794
 
      }
795
 
   }
796
 
}