001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * EmbeddedNameServer.java
020: *
021: * This object is responsible for starting the JacORB naming service. It starts
022: * it in process and not as another process. This means this class copies a lot
023: * from JacORB's own naming service.
024: */
025:
026: package org.jacorb.naming;
027:
028: // java imports
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.FileOutputStream;
032: import java.io.IOException;
033: import java.io.ObjectInputStream;
034: import java.io.ObjectOutputStream;
035: import java.io.PrintWriter;
036:
037: // apache imports
038: import org.apache.avalon.framework.configuration.Configuration;
039: import org.apache.avalon.framework.configuration.ConfigurationException;
040: import org.apache.avalon.framework.logger.Logger;
041:
042: // JacORB imports
043: import org.jacorb.imr.util.ImRManager;
044: import org.jacorb.util.ObjectUtil;
045:
046: // omg impors
047: import org.omg.CORBA.ORB;
048: import org.omg.PortableServer.ForwardRequest;
049: import org.omg.PortableServer.IdAssignmentPolicyValue;
050: import org.omg.PortableServer.LifespanPolicyValue;
051: import org.omg.PortableServer.RequestProcessingPolicyValue;
052: import org.omg.PortableServer.POA;
053: import org.omg.PortableServer.Servant;
054: import org.omg.PortableServer._ServantActivatorLocalBase;
055:
056: /**
057: * This object is responsible for starting the JacORB naming service. It starts
058: * it in process and not as another process. This means this class copies a lot
059: * from JacORB's own naming service.
060: *
061: * @author Brett Chaldecott
062: */
063: public class EmbeddedNameServer {
064:
065: /**
066: * Creates a new instance of EmbeddedNameServer
067: */
068: public EmbeddedNameServer(ORB orb, POA poa)
069: throws NameServerException {
070: try {
071: // retrieve the orb configuration object
072: Configuration config = ((org.jacorb.orb.ORB) orb)
073: .getConfiguration();
074:
075: /* configure the name service using the ORB configuration */
076: NameServer.configure(config);
077:
078: /* create a user defined poa for the naming contexts */
079: org.omg.CORBA.Policy[] policies = new org.omg.CORBA.Policy[3];
080: policies[0] = poa
081: .create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
082: policies[1] = poa
083: .create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
084: policies[2] = poa
085: .create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
086: POA nsPOA = poa.create_POA("NameServer-POA", poa
087: .the_POAManager(), policies);
088:
089: NamingContextImpl.init(orb, poa);
090: NameServer.NameServantActivatorImpl servantActivator = new NameServer.NameServantActivatorImpl(
091: orb);
092: servantActivator.configure(config);
093: NamingContextImpl namingContext = new NamingContextImpl();
094: nsPOA.set_servant_manager(servantActivator);
095: nsPOA.the_POAManager().activate();
096:
097: byte[] oid = (new String("_root").getBytes());
098: org.omg.CORBA.Object obj = nsPOA.create_reference_with_id(
099: oid, "IDL:omg.org/CosNaming/NamingContextExt:1.0");
100:
101: System.out.println("NS SERVER IOR: "
102: + orb.object_to_string(obj));
103:
104: // free up the policies
105: for (int i = 0; i < policies.length; i++)
106: policies[i].destroy();
107:
108: } catch (ConfigurationException ex) {
109: throw new NameServerException("Failed to init the "
110: + "EmbeddedNameServer : " + ex.getMessage(), ex);
111: } catch (Exception ex) {
112: throw new NameServerException("Failed to init the "
113: + "EmbeddedNameServer : " + ex.getMessage(), ex);
114: }
115: }
116:
117: }
|