KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > CorbaORBService


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.iiop;
23
24 import java.io.InputStream JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.ejb.spi.HandleDelegate JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.naming.spi.ObjectFactory JavaDoc;
35
36 import org.jboss.iiop.naming.ORBInitialContextFactory;
37 import org.jboss.metadata.MetaData;
38 import org.jboss.proxy.ejb.handle.HandleDelegateImpl;
39 import org.jboss.security.SecurityDomain;
40 import org.jboss.system.Registry;
41 import org.jboss.system.ServiceMBeanSupport;
42 import org.jboss.system.server.ServerConfigUtil;
43 import org.omg.CORBA.ORB JavaDoc;
44 import org.omg.CORBA.Policy JavaDoc;
45 import org.omg.PortableServer.IdAssignmentPolicy JavaDoc;
46 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
47 import org.omg.PortableServer.IdUniquenessPolicyValue JavaDoc;
48 import org.omg.PortableServer.LifespanPolicy JavaDoc;
49 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
50 import org.omg.PortableServer.POA JavaDoc;
51 import org.omg.PortableServer.POAHelper JavaDoc;
52 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
53 import org.omg.PortableServer.ServantRetentionPolicyValue JavaDoc;
54 import org.w3c.dom.Element JavaDoc;
55
56 /**
57  * This is a JMX service that provides the default CORBA ORB
58  * for JBoss to use.
59  *
60  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
61  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
62  * @version $Revision: 37459 $
63  */

64 public class CorbaORBService
65       extends ServiceMBeanSupport
66       implements CorbaORBServiceMBean, ObjectFactory JavaDoc
67 {
68    // Constants -----------------------------------------------------
69

70    public static String JavaDoc ORB_NAME = "JBossCorbaORB";
71    public static String JavaDoc POA_NAME = "JBossCorbaPOA";
72    public static String JavaDoc IR_POA_NAME = "JBossCorbaInterfaceRepositoryPOA";
73    public static String JavaDoc SSL_DOMAIN = "JBossCorbaSSLDomain";
74     
75    // Attributes ----------------------------------------------------
76

77    private String JavaDoc orbClass = null;
78    private String JavaDoc orbSingletonClass = null;
79    private String JavaDoc orbSingletonDelegate = null;
80    private String JavaDoc orbPropertiesFileName = "orb-properties-file-not-defined";
81    private Element JavaDoc portableInterceptorInitializers = null;
82    private int port = 0;
83    private int sslPort = 0;
84    private String JavaDoc sslDomain = null;
85    private boolean sunJDK14IsLocalBugFix = true;
86
87    // Static --------------------------------------------------------
88

89    private static ORB JavaDoc orb;
90    private static POA JavaDoc poa;
91    private static POA JavaDoc otsPoa;
92    private static POA JavaDoc otsResourcePoa;
93    private static POA JavaDoc irPoa;
94    private static HandleDelegate JavaDoc hd;
95    private static int oaSslPort;
96
97    /**
98     * Addition of SSL components to IORs is disabled by default.
99     * This must remain off for interoperation with IONA's ASP 6.0,
100     * which does not accept IORs with SSL components.
101     */

102    private static boolean sslComponentsEnabledFlag = false;
103
104    /**
105     * Sending an IIOP reply that carries both a CompleteEstablishContext
106     * (SAS accept) reply and an exception is enabled by default, because
107     * the CSIv2 spect does not explicitly disallow an SAS accept in an
108     * IIOP exception reply. This flag must be turned off for interoperation
109     * with IONA's ASP 6.0, which throws an ArrayIndexOutOfBoundsException
110     * when it receives an IIOP reply carrying both an application exception
111     * and a SAS reply CompleteEstablishContext.
112     */

113    private static boolean sendSasAcceptWithExceptionEnabledFlag = true;
114
115    /**
116     * True if the OTS context should be sent along with outgoing requests.
117     */

118    private static boolean otsContextPropagationEnabledFlag = false;
119
120    /**
121     * Returns the actual SSL port. This method is intended to be called
122     * by the CSIv2 IOR interceptor, which needs to know the SSL port.
123     */

124    public static int getTheActualSSLPort()
125    {
126       return oaSslPort;
127    }
128
129    /**
130     * Returns true if addition of SSL components to IORs is enabled.
131     * This method is intended to be called by the CSIv2 IOR interceptor.
132     */

133    public static boolean getSSLComponentsEnabledFlag()
134    {
135       return sslComponentsEnabledFlag;
136    }
137    
138    /**
139     * Returns true if sending an SAS accept reply together with an IIOP
140     * exception reply is enabled. This method is intended to be called by
141     * the CSIv2 server request interceptor.
142     */

143    public static boolean getSendSASAcceptWithExceptionEnabledFlag()
144    {
145       return sendSasAcceptWithExceptionEnabledFlag;
146    }
147    
148    /**
149     * Returns true if the OTS context should be sent along with outgoing
150     * requests. This method is intended to be called by the OTS client request
151     * interceptor installed in the JBoss server.
152     */

153    public static boolean getOTSContextPropagationEnabledFlag()
154    {
155       return otsContextPropagationEnabledFlag;
156    }
157    
158    /**
159     * Returns the CORBA transaction service main POA. Our OTS implementation
160     * uses this POA for all OTS objects that are not Resource instances.
161     */

162    public static POA JavaDoc getOtsPoa()
163    {
164       return otsPoa;
165    }
166    
167    /**
168     * Returns the CORBA transaction service secondary POA. Our OTS
169     * implementation uses this POA for OTS Resources, so that they can have
170     * their own default servant. (The name clash between Terminator::rollback
171     * and Resource::rollback disallows us to use a single default servant for
172     * all OTS objects.)
173     */

174    public static POA JavaDoc getOtsResourcePoa()
175    {
176       return otsResourcePoa;
177    }
178
179    // ServiceMBeanSupport overrides ---------------------------------
180

181    protected void startService()
182       throws Exception JavaDoc
183    {
184
185       Properties JavaDoc props = new Properties JavaDoc();
186
187       // Read orb properties file into props
188
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
189       InputStream JavaDoc is = cl.getResourceAsStream(orbPropertiesFileName);
190       props.load(is);
191       String JavaDoc oaiAddr = props.getProperty("OAIAddr");
192       if (oaiAddr == null)
193          oaiAddr = ServerConfigUtil.getSpecificBindAddress();
194       if (oaiAddr != null)
195          props.setProperty("OAIAddr", oaiAddr);
196       log.debug("Using OAIAddr=" + oaiAddr);
197
198       // Set ORB initialization properties
199
Properties JavaDoc systemProps = System.getProperties();
200       if (orbClass != null) {
201          props.put("org.omg.CORBA.ORBClass", orbClass);
202          systemProps.put("org.omg.CORBA.ORBClass", orbClass);
203       }
204       if (orbSingletonClass != null) {
205          props.put("org.omg.CORBA.ORBSingletonClass", orbSingletonClass);
206          systemProps.put("org.omg.CORBA.ORBSingletonClass", orbSingletonClass);
207       }
208       if (orbSingletonDelegate != null)
209          systemProps.put(org.jboss.system.ORBSingleton.DELEGATE_CLASS_KEY,
210                          orbSingletonDelegate);
211
212       // JacORB-specific hack: add jacorb.config.log.verbosity to the system
213
// properties so that it is seen by JacORB at configuration time.
214
// This allows us (by setting jacorb.config.log.verbosity=0) to get rid
215
// of the messages "jacorb.home unset! Will use '.'" and
216
// "File ./jacorb.properties for configuration jacorb not found", which
217
// would otherwise be sent to the standard output.
218
String JavaDoc str = props.getProperty("jacorb.config.log.verbosity");
219       if (str != null)
220          systemProps.put("jacorb.config.log.verbosity", str);
221
222       // This is for SUN JDK bug with isLocal
223
if (sunJDK14IsLocalBugFix)
224       {
225          /* Validate that the class can actually be loaded. It cannot be used
226          under java 5 for example because the SunJDK14IsLocalBugFix base class
227          does not exist
228          */

229          try
230          {
231             Class JavaDoc SunJDK14IsLocalBugFix = cl.loadClass("org.jboss.iiop.SunJDK14IsLocalBugFix");
232             log.debug("Was able to load SunJDK14IsLocalBugFix, class="+SunJDK14IsLocalBugFix);
233             // Its loadable to use it
234
systemProps.put("javax.rmi.CORBA.UtilClass", "org.jboss.iiop.SunJDK14IsLocalBugFix");
235          }
236          catch(Throwable JavaDoc t)
237          {
238             log.debug("Ignoring sunJDK14IsLocalBugFix=true due to inability to load org.jboss.iiop.SunJDK14IsLocalBugFix", t);
239          }
240       }
241
242       System.setProperties(systemProps);
243       
244       // Add portable interceptor initializers
245
Iterator JavaDoc initializerElements =
246             MetaData.getChildrenByTagName(portableInterceptorInitializers,
247                                           "initializer");
248       if (initializerElements != null)
249       {
250          while (initializerElements.hasNext())
251          {
252             Element JavaDoc initializerElement = (Element JavaDoc) initializerElements.next();
253             String JavaDoc portableInterceptorInitializerClassName =
254                   MetaData.getElementContent(initializerElement);
255                   log.debug("Adding portable interceptor initializer: " +
256                            portableInterceptorInitializerClassName);
257             if (portableInterceptorInitializerClassName != null
258                   && !portableInterceptorInitializerClassName.equals(""))
259                props.put("org.omg.PortableInterceptor.ORBInitializerClass."
260                          + portableInterceptorInitializerClassName, "");
261          }
262       }
263
264       // Allows overriding of OAPort/OASSLPort
265
// from config file through MBean config
266
if (port != 0)
267          props.put("OAPort", Integer.toString(this.port));
268       if (sslPort != 0)
269          props.put("OASSLPort", Integer.toString(this.sslPort));
270
271       // Keep the actual SSL port in a static field
272
// (the CSIv2 IOR interceptor needs this value)
273
String JavaDoc oaSslPortString = props.getProperty("OASSLPort");
274       if (oaSslPortString != null)
275          oaSslPort = Integer.parseInt(oaSslPortString);
276
277       // Allow SSL domain to be specified through MBean config
278
if (sslDomain != null)
279       {
280          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
281          log.debug("sslDomain: " + sslDomain);
282          try
283          {
284             SecurityDomain domain = (SecurityDomain) ctx.lookup(sslDomain);
285
286             // Make SSL domain available to socket factories
287
Registry.bind(SSL_DOMAIN, domain);
288          }
289          catch (Exception JavaDoc e)
290          {
291             log.warn("Security domain " + sslDomain + " not found");
292             log.debug("Exception looking up " + sslDomain + ": ", e);
293          }
294       }
295
296       // Initialize the ORB
297
orb = ORB.init(new String JavaDoc[0], props);
298       bind(ORB_NAME, "org.omg.CORBA.ORB");
299       CorbaORB.setInstance(orb);
300       ORBInitialContextFactory.setORB(orb);
301
302       // Initialize the POA
303
poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
304       bind(POA_NAME, "org.omg.PortableServer.POA");
305
306       // We will now create POAs for the CORBA transaction service. Those POAs
307
// will remain in the holding state until the CORBA transaction service
308
// MBean gets deployed.
309
//
310
// The early creation of OTS POAs serve the purpose of avoiding spurious
311
// rollbacks that could happen between the ORB activation and the CORBA
312
// transaction service activation. Such a rollback could happen if a
313
// resource sent us a reply_completion request in that time interval.
314
// The resource would get an OBJECT_NOT_EXIST exception and would presume
315
// that the transaction had been rolled back. To avoid this problem, we
316
// use the fact that CORBA ORBs do not throw OBJECT_NOT_EXIST while the
317
// target POA is in the holding state. We also use the fact that JacORB
318
// release 2.2.2 (JBoss patch 3) does not throw OBJECT_NOT_EXIST if the
319
// target POA was not yet created and root POA is in the holding state.
320
// So we create the OTS POAs while the root POA is in the holding state.
321
// Then we activate the root POA, but keep the OTS POAs in the holding
322
// state. The OTS MBean will activate those POAs when it is started.
323

324       Policy JavaDoc[] policies = { // Will create OTS POAs with these policies.
325
poa.create_lifespan_policy(
326                            LifespanPolicyValue.PERSISTENT),
327          poa.create_id_assignment_policy(
328                            IdAssignmentPolicyValue.USER_ID),
329          poa.create_servant_retention_policy(
330                            ServantRetentionPolicyValue.NON_RETAIN),
331          poa.create_request_processing_policy(
332                            RequestProcessingPolicyValue.USE_DEFAULT_SERVANT),
333          poa.create_id_uniqueness_policy(
334                            IdUniquenessPolicyValue.MULTIPLE_ID),
335       };
336       
337       // Create a POA for OTS objects that are not Resource instances
338
otsPoa = poa.create_POA("OTS", null, policies);
339       
340       // Create another POA for OTS Resources, so that they can have their own
341
// default servant. (The name clash between Terminator::rollback and
342
// Resource::rollback disallows us to use a single default servant for
343
// all OTS objects.)
344
otsResourcePoa =
345          poa.create_POA("OTSResources", otsPoa.the_POAManager(), policies);
346
347       // OTS POAs already exist, we can activate the root POA.
348
poa.the_POAManager().activate();
349
350       // Make the ORB work
351
new Thread JavaDoc(
352          new Runnable JavaDoc() {
353             public void run() {
354                orb.run();
355             }
356          }, "ORB thread"
357       ).start();
358
359       // Create a POA for interface repositories
360
try {
361          LifespanPolicy JavaDoc lifespanPolicy =
362             poa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
363          IdAssignmentPolicy JavaDoc idAssignmentPolicy =
364             poa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
365
366          irPoa = poa.create_POA("IR", null,
367                                 new Policy JavaDoc[]{lifespanPolicy,
368                                              idAssignmentPolicy});
369          bind(IR_POA_NAME, "org.omg.PortableServer.POA");
370          
371          // Activate the poa
372
irPoa.the_POAManager().activate();
373          
374       } catch (Exception JavaDoc ex) {
375          getLog().error("Error in IR POA initialization", ex);
376       }
377       
378       // Keep a HandleDelegate
379
hd = new HandleDelegateImpl();
380    }
381
382    protected void stopService() throws Exception JavaDoc
383    {
384       /*
385       if( mJSR77 != null )
386       {
387           RMI_IIOPResource.destroy(
388              getServer(),
389              ORB_NAME
390           );
391           mJSR77 = null;
392       }
393       */

394       try {
395          // Unbind from JNDI
396
unbind(ORB_NAME);
397          unbind(POA_NAME);
398          unbind(IR_POA_NAME);
399
400          // Stop ORB
401
orb.shutdown(false);
402          
403          // Unbind SSL domain
404
Registry.unbind(SSL_DOMAIN);
405       } catch (Exception JavaDoc e) {
406          log.error("Exception while stopping ORB service", e);
407       }
408    }
409
410    // CorbaORBServiceMBean implementation ---------------------------
411

412    public ORB JavaDoc getORB()
413    {
414       return orb;
415    }
416
417    public HandleDelegate JavaDoc getHandleDelegate()
418    {
419       return hd;
420    }
421    
422    public String JavaDoc getORBClass()
423    {
424       return orbClass;
425    }
426
427    public void setORBClass(String JavaDoc orbClass)
428    {
429       this.orbClass = orbClass;
430    }
431
432    public String JavaDoc getORBSingletonClass()
433    {
434       return orbSingletonClass;
435    }
436
437    public void setORBSingletonClass(String JavaDoc orbSingletonClass)
438    {
439       this.orbSingletonClass = orbSingletonClass;
440    }
441
442    public String JavaDoc getORBSingletonDelegate()
443    {
444       return orbSingletonDelegate;
445    }
446
447    public void setORBSingletonDelegate(String JavaDoc orbSingletonDelegate)
448    {
449       this.orbSingletonDelegate = orbSingletonDelegate;
450    }
451
452    public void setORBPropertiesFileName(String JavaDoc orbPropertiesFileName)
453    {
454       this.orbPropertiesFileName = orbPropertiesFileName;
455    }
456
457    public String JavaDoc getORBPropertiesFileName()
458    {
459       return orbPropertiesFileName;
460    }
461
462    public Element JavaDoc getPortableInterceptorInitializers()
463    {
464       return portableInterceptorInitializers;
465    }
466
467    public void setPortableInterceptorInitializers(
468                                        Element JavaDoc portableInterceptorInitializers)
469    {
470       this.portableInterceptorInitializers = portableInterceptorInitializers;
471    }
472
473    public void setPort(int port)
474    {
475       this.port = port;
476    }
477
478    public int getPort()
479    {
480       return this.port;
481    }
482
483    public void setSSLPort(int sslPort)
484    {
485       this.sslPort = sslPort;
486    }
487
488    public int getSSLPort()
489    {
490       return this.sslPort;
491    }
492
493    public void setSecurityDomain(String JavaDoc sslDomain)
494    {
495       this.sslDomain = sslDomain;
496    }
497
498    public String JavaDoc getSecurityDomain()
499    {
500       return this.sslDomain;
501    }
502
503    public boolean getSSLComponentsEnabled()
504    {
505       return CorbaORBService.sslComponentsEnabledFlag;
506    }
507
508    public void setSSLComponentsEnabled(boolean sslComponentsEnabled)
509    {
510       CorbaORBService.sslComponentsEnabledFlag = sslComponentsEnabled;
511    }
512
513    public boolean getSendSASAcceptWithExceptionEnabled()
514    {
515       return CorbaORBService.sendSasAcceptWithExceptionEnabledFlag;
516    }
517
518    public void setSendSASAcceptWithExceptionEnabled(boolean value)
519    {
520       CorbaORBService.sendSasAcceptWithExceptionEnabledFlag = value;
521    }
522    
523    public boolean getOTSContextPropagationEnabled()
524    {
525       return CorbaORBService.otsContextPropagationEnabledFlag;
526    }
527
528    public void setOTSContextPropagationEnabled(boolean value)
529    {
530       CorbaORBService.otsContextPropagationEnabledFlag = value;
531    }
532
533    public boolean getSunJDK14IsLocalBugFix()
534    {
535       return sunJDK14IsLocalBugFix;
536    }
537
538    public void setSunJDK14IsLocalBugFix(boolean sunJDK14IsLocalBugFix)
539    {
540       this.sunJDK14IsLocalBugFix = sunJDK14IsLocalBugFix;
541    }
542
543    // ObjectFactory implementation ----------------------------------
544

545    public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name,
546                                    Context JavaDoc nameCtx, Hashtable JavaDoc environment)
547       throws Exception JavaDoc
548    {
549       String JavaDoc s = name.toString();
550       if (getLog().isTraceEnabled())
551          getLog().trace("getObjectInstance: obj.getClass().getName=\"" +
552                         obj.getClass().getName() +
553                         "\n name=" + s);
554       if (ORB_NAME.equals(s))
555          return orb;
556       if (POA_NAME.equals(s))
557          return poa;
558       if (IR_POA_NAME.equals(s))
559          return irPoa;
560       return null;
561    }
562
563
564    // Private -------------------------------------------------------
565

566    private void bind(String JavaDoc name, String JavaDoc className)
567       throws Exception JavaDoc
568    {
569       Reference JavaDoc ref = new Reference JavaDoc(className, getClass().getName(), null);
570       new InitialContext JavaDoc().bind("java:/"+name, ref);
571    }
572
573    private void unbind(String JavaDoc name)
574       throws Exception JavaDoc
575    {
576       new InitialContext JavaDoc().unbind("java:/"+name);
577    }
578
579 }
580
581
Popular Tags