KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Hashtable JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.Name JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.Reference JavaDoc;
30 import javax.naming.spi.ObjectFactory JavaDoc;
31
32 import org.apache.avalon.framework.configuration.Configuration;
33 import org.jboss.logging.Logger;
34 import org.jboss.system.ServiceMBeanSupport;
35 import org.omg.CORBA.ORB JavaDoc;
36 import org.omg.CORBA.Policy JavaDoc;
37 import org.omg.CosNaming.Binding JavaDoc;
38 import org.omg.CosNaming.BindingHolder JavaDoc;
39 import org.omg.CosNaming.BindingIteratorHolder JavaDoc;
40 import org.omg.CosNaming.BindingListHolder JavaDoc;
41 import org.omg.CosNaming.BindingType JavaDoc;
42 import org.omg.CosNaming.NameComponent JavaDoc;
43 import org.omg.CosNaming.NamingContext JavaDoc;
44 import org.omg.CosNaming.NamingContextExt JavaDoc;
45 import org.omg.CosNaming.NamingContextHelper JavaDoc;
46 import org.omg.CosNaming.NamingContextExtHelper JavaDoc;
47 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
48 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
49 import org.omg.PortableServer.POA JavaDoc;
50
51 /**
52  * This is a JMX service that provides the default CORBA naming service
53  * for JBoss to use.
54  *
55  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
56  * @version $Revision: 37459 $
57  */

58 public class CorbaNamingService
59    extends ServiceMBeanSupport
60    implements CorbaNamingServiceMBean, ObjectFactory JavaDoc
61 {
62    // Constants -----------------------------------------------------
63
public static String JavaDoc NAMING_NAME = "JBossCorbaNaming";
64     
65    // Attributes ----------------------------------------------------
66

67    /** The POA used by the CORBA naming service. */
68    private POA JavaDoc namingPOA;
69
70    // Static --------------------------------------------------------
71

72    /** Root naming context (returned by <code>getObjectInstance()</code>). */
73    private static NamingContextExt JavaDoc namingService;
74
75    /** List the CORBA naming service contents.
76     *
77     * @return
78     */

79    public String JavaDoc list()
80    {
81       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
82       rlist(namingService, new NameComponent JavaDoc[0], buf);
83       return buf.toString();
84    }
85
86    // ServiceMBeanSupport overrides ---------------------------------
87

88    protected void startService()
89       throws Exception JavaDoc
90    {
91       Context JavaDoc ctx;
92       ORB JavaDoc orb;
93       POA JavaDoc rootPOA;
94
95       try {
96          ctx = new InitialContext JavaDoc();
97       }
98       catch (NamingException JavaDoc e) {
99          throw new RuntimeException JavaDoc("Cannot get intial JNDI context: " + e);
100       }
101       try {
102          orb = (ORB JavaDoc)ctx.lookup("java:/" + CorbaORBService.ORB_NAME);
103       }
104       catch (NamingException JavaDoc e) {
105          throw new RuntimeException JavaDoc("Cannot lookup java:/"
106                                     + CorbaORBService.ORB_NAME + ": " + e);
107       }
108       try {
109          rootPOA = (POA JavaDoc)ctx.lookup("java:/" + CorbaORBService.POA_NAME);
110       }
111       catch (NamingException JavaDoc e) {
112          throw new RuntimeException JavaDoc("Cannot lookup java:/"
113                                     + CorbaORBService.POA_NAME + ": " + e);
114       }
115
116       // Create the naming server POA as a child of the root POA
117
Policy JavaDoc[] policies = new Policy JavaDoc[2];
118       policies[0] =
119          rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
120       policies[1] =
121          rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
122       namingPOA = rootPOA.create_POA("Naming", null, policies);
123       namingPOA.the_POAManager().activate();
124
125       // Create the naming service
126
org.jacorb.naming.NamingContextImpl.init(orb, rootPOA);
127       NamingContextImpl ns = new NamingContextImpl(namingPOA);
128       Configuration config = ((org.jacorb.orb.ORB)orb).getConfiguration();
129       ns.configure(config); // configure the name service using the ORB config
130
byte[] rootContextId = "root".getBytes();
131       namingPOA.activate_object_with_id(rootContextId, ns);
132       namingService = NamingContextExtHelper.narrow(
133                   namingPOA.create_reference_with_id(rootContextId,
134                                 "IDL:omg.org/CosNaming/NamingContextExt:1.0"));
135       bind(NAMING_NAME, "org.omg.CosNaming.NamingContextExt");
136       getLog().info("CORBA Naming Started");
137       getLog().debug("Naming: ["+orb.object_to_string(namingService)+"]");
138    }
139     
140    protected void stopService()
141    {
142       // Unbind from JNDI
143
try {
144          unbind(NAMING_NAME);
145       } catch (Exception JavaDoc e) {
146          log.error("Exception while stopping CORBA naming service", e);
147       }
148
149       // Destroy naming POA
150
try {
151          namingPOA.destroy(false, false);
152       } catch (Exception JavaDoc e) {
153          log.error("Exception while stopping CORBA naming service", e);
154       }
155    }
156     
157    // ObjectFactory implementation ----------------------------------
158

159    public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name,
160                                    Context JavaDoc nameCtx, Hashtable JavaDoc environment)
161       throws Exception JavaDoc
162    {
163       String JavaDoc s = name.toString();
164       if (getLog().isTraceEnabled())
165          getLog().trace("getObjectInstance: obj.getClass().getName=\"" +
166                         obj.getClass().getName() +
167                         "\n name=" + s);
168       if (NAMING_NAME.equals(s))
169          return namingService;
170       else
171          return null;
172    }
173
174    // Private -------------------------------------------------------
175

176    private void bind(String JavaDoc name, String JavaDoc className)
177       throws Exception JavaDoc
178    {
179       Reference JavaDoc ref = new Reference JavaDoc(className, getClass().getName(), null);
180       new InitialContext JavaDoc().bind("java:/"+name, ref);
181    }
182
183    private void unbind(String JavaDoc name)
184       throws Exception JavaDoc
185    {
186       new InitialContext JavaDoc().unbind("java:/"+name);
187    }
188
189    private static void rlist(NamingContext JavaDoc ctx, NameComponent JavaDoc[] base,
190       StringBuffer JavaDoc buf)
191    {
192       BindingListHolder JavaDoc listHolder = new BindingListHolder JavaDoc(new Binding JavaDoc[0]);
193       BindingIteratorHolder JavaDoc iterHolder = new BindingIteratorHolder JavaDoc();
194       ctx.list(0, listHolder, iterHolder);
195       BindingHolder JavaDoc bindingHolder = new BindingHolder JavaDoc();
196
197       if (iterHolder.value == null )
198          return;
199
200       NameComponent JavaDoc[] name = new NameComponent JavaDoc[base.length + 1];
201       for (int i = 0; i < base.length; i++)
202          name[i] = base[i];
203
204       while (iterHolder.value.next_one(bindingHolder))
205       {
206          Binding JavaDoc binding = bindingHolder.value;
207          name[name.length - 1] = binding.binding_name[0];
208          try
209          {
210             String JavaDoc stringName = namingService.to_string(name);
211             buf.append(stringName);
212          }
213          catch(Exception JavaDoc e)
214          {
215             buf.append(e.getMessage());
216          }
217
218          if (binding.binding_type.value() == BindingType._ncontext)
219          {
220             // this entry is for a subcontext
221
// add trailing '/' just to distinguish
222
// a subcontext from a regular object
223
buf.append('/');
224             buf.append('\n');
225
226             // recursively list the subcontext contents
227
try
228             {
229                NamingContext JavaDoc subCtx =
230                   NamingContextHelper.narrow(ctx.resolve(binding.binding_name));
231                rlist(subCtx, name, buf);
232             }
233             catch(Exception JavaDoc e)
234             {
235                buf.append(e.getMessage());
236             }
237          }
238          else
239          {
240             buf.append('\n');
241          }
242       }
243    }
244
245    // Static inner class --------------------------------------------
246

247    /**
248     * This subclass of <code>org.jacorb.naming.NamingContextImpl</code>
249     * overrides the method <code>new_context()</code>, because its
250     * implementation in <code>org.jacorb.naming.NamingContextImpl</code>
251     * is not suitable for our in-VM naming server. The superclass
252     * implementation of <code>new_context()</code> assumes that naming context
253     * states are persistently stored and requires a servant activator that
254     * reads context states from persistent storage.
255     */

256    static class NamingContextImpl
257       extends org.jacorb.naming.NamingContextImpl
258    {
259       private POA JavaDoc poa;
260       private int childCount = 0;
261       private static final Logger logger =
262                               Logger.getLogger(NamingContextImpl.class);
263
264       NamingContextImpl(POA JavaDoc poa)
265       {
266          this.poa = poa;
267       }
268
269       public NamingContext JavaDoc new_context()
270       {
271          try {
272             NamingContextImpl newContextImpl = new NamingContextImpl(poa);
273             byte[] oid = (new String JavaDoc(poa.servant_to_id(this)) +
274                           "/ctx" + (++childCount)).getBytes();
275             poa.activate_object_with_id(oid, newContextImpl);
276             return NamingContextExtHelper.narrow(
277                         poa.create_reference_with_id(oid,
278                                 "IDL:omg.org/CosNaming/NamingContextExt:1.0"));
279          }
280          catch (Exception JavaDoc e) {
281             logger.error("Cannot create CORBA naming context", e);
282             return null;
283          }
284       }
285    }
286
287 }
288
Popular Tags