KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > naming > EmbeddedNameServer


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * EmbeddedNameServer.java
20  *
21  * This object is responsible for starting the JacORB naming service. It starts
22  * it in process and not as another process. This means this class copies a lot
23  * from JacORB's own naming service.
24  */

25
26 package org.jacorb.naming;
27
28 // java imports
29
import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36
37 // apache imports
38
import org.apache.avalon.framework.configuration.Configuration;
39 import org.apache.avalon.framework.configuration.ConfigurationException;
40 import org.apache.avalon.framework.logger.Logger;
41
42 // JacORB imports
43
import org.jacorb.imr.util.ImRManager;
44 import org.jacorb.util.ObjectUtil;
45
46 // omg impors
47
import org.omg.CORBA.ORB JavaDoc;
48 import org.omg.PortableServer.ForwardRequest JavaDoc;
49 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
50 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
51 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
52 import org.omg.PortableServer.POA JavaDoc;
53 import org.omg.PortableServer.Servant JavaDoc;
54 import org.omg.PortableServer._ServantActivatorLocalBase;
55
56
57 /**
58  * This object is responsible for starting the JacORB naming service. It starts
59  * it in process and not as another process. This means this class copies a lot
60  * from JacORB's own naming service.
61  *
62  * @author Brett Chaldecott
63  */

64 public class EmbeddedNameServer {
65     
66     /**
67     * Creates a new instance of EmbeddedNameServer
68     */

69     public EmbeddedNameServer(ORB JavaDoc orb, POA JavaDoc poa) throws NameServerException {
70         try {
71             // retrieve the orb configuration object
72
Configuration config =
73                     ((org.jacorb.orb.ORB)orb).getConfiguration();
74             
75             /* configure the name service using the ORB configuration */
76             NameServer.configure(config);
77             
78             /* create a user defined poa for the naming contexts */
79             org.omg.CORBA.Policy JavaDoc [] policies = new org.omg.CORBA.Policy JavaDoc[3];
80             policies[0] =
81                     poa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
82             policies[1] =
83                     poa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
84             policies[2] =
85                     poa.create_request_processing_policy(
86                     RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
87             POA JavaDoc nsPOA = poa.create_POA("NameServer-POA",
88                     poa.the_POAManager(),
89                     policies);
90             
91             NamingContextImpl.init(orb, poa);
92             NameServer.NameServantActivatorImpl servantActivator =
93                     new NameServer.NameServantActivatorImpl( orb );
94             servantActivator.configure(config);
95             NamingContextImpl namingContext = new NamingContextImpl();
96             nsPOA.set_servant_manager( servantActivator );
97             nsPOA.the_POAManager().activate();
98             
99             byte[] oid = ( new String JavaDoc("_root").getBytes() );
100             org.omg.CORBA.Object JavaDoc obj =
101                     nsPOA.create_reference_with_id( oid,
102                     "IDL:omg.org/CosNaming/NamingContextExt:1.0");
103             
104             System.out.println("NS SERVER IOR: " + orb.object_to_string(obj));
105             
106             // free up the policies
107
for (int i = 0; i < policies.length; i++)
108                 policies[i].destroy();
109             
110         } catch( ConfigurationException ex ) {
111             throw new NameServerException("Failed to init the " +
112                     "EmbeddedNameServer : " + ex.getMessage(),ex);
113         } catch( Exception JavaDoc ex ) {
114             throw new NameServerException("Failed to init the " +
115                     "EmbeddedNameServer : " + ex.getMessage(),ex);
116         }
117     }
118     
119 }
120
Popular Tags