From 254ee6f47eebfc00462c10756a92066e82cc1a96 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 21 Jun 2011 15:46:02 +0200 Subject: Initial commit --- source/com/c2kernel/entity/CorbaServer.java | 191 ++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100755 source/com/c2kernel/entity/CorbaServer.java (limited to 'source/com/c2kernel/entity/CorbaServer.java') diff --git a/source/com/c2kernel/entity/CorbaServer.java b/source/com/c2kernel/entity/CorbaServer.java new file mode 100755 index 0000000..948b1a6 --- /dev/null +++ b/source/com/c2kernel/entity/CorbaServer.java @@ -0,0 +1,191 @@ +package com.c2kernel.entity; + +import java.util.Map; + +import org.omg.PortableServer.POA; +import org.omg.PortableServer.POAManager; +import org.omg.PortableServer.Servant; +import org.omg.PortableServer.POAManagerPackage.AdapterInactive; + +import com.c2kernel.common.CannotManageException; +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectAlreadyExistsException; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.entity.agent.ActiveEntity; +import com.c2kernel.entity.agent.ActiveLocator; +import com.c2kernel.lookup.AgentPath; +import com.c2kernel.lookup.EntityPath; +import com.c2kernel.lookup.InvalidEntityPathException; +import com.c2kernel.process.Gateway; +import com.c2kernel.utils.Logger; +import com.c2kernel.utils.SoftCache; + +/************************************************************************** + * + * $Revision: 1.8 $ + * $Date: 2005/10/13 08:13:44 $ + * + * Copyright (C) 2003 CERN - European Organization for Nuclear Research + * All rights reserved. + **************************************************************************/ + + +public class CorbaServer { + private Map mEntityCache; + private POA mRootPOA; + private POA mItemPOA; + private POA mAgentPOA; + private POAManager mPOAManager; + + public CorbaServer() throws InvalidDataException { + mEntityCache = new SoftCache(50); + + // init POA + try { + setupPOA(); + mPOAManager.activate(); + } catch (Exception ex) { + Logger.error(ex); + throw new InvalidDataException("Error initialising POA", ""); + } + + new Thread(new Runnable() { + public void run() { + Thread.currentThread().setName("ORB Invoker"); + Gateway.getORB().run(); + } + }).start(); + } + + public void close() { + try { + mPOAManager.deactivate(true, true); + } catch (AdapterInactive ex) { + Logger.error(ex); + } + } + + /************************************************************************** + * Initialises the C2KRootPOA with policies which are suitable for Factory objects + **************************************************************************/ + public void setupPOA() throws Exception { + + //Initialise the RootPOA + mRootPOA = org.omg.PortableServer.POAHelper.narrow( + Gateway.getORB().resolve_initial_references("RootPOA")); + + //Initilaise the default POAManager + + mPOAManager = mRootPOA.the_POAManager(); + + org.omg.CORBA.Policy[] poaPolicies; + + // Create POA for use by the entities + org.omg.CORBA.Policy[] policies = new org.omg.CORBA.Policy[6]; + + policies[0] = mRootPOA.create_id_assignment_policy( + org.omg.PortableServer.IdAssignmentPolicyValue.USER_ID); + policies[1] = mRootPOA.create_lifespan_policy( + org.omg.PortableServer.LifespanPolicyValue.PERSISTENT); + policies[2] = mRootPOA.create_servant_retention_policy( + org.omg.PortableServer.ServantRetentionPolicyValue.NON_RETAIN); + policies[3] = mRootPOA.create_id_uniqueness_policy( + org.omg.PortableServer.IdUniquenessPolicyValue.UNIQUE_ID); + policies[4] = mRootPOA.create_request_processing_policy( + org.omg.PortableServer.RequestProcessingPolicyValue. + USE_SERVANT_MANAGER); + policies[5] = mRootPOA.create_implicit_activation_policy( + org.omg.PortableServer.ImplicitActivationPolicyValue. + NO_IMPLICIT_ACTIVATION); + + mItemPOA = mRootPOA.create_POA( "Item", + mRootPOA.the_POAManager(), + policies ); + mAgentPOA = mRootPOA.create_POA( "Agent", + mRootPOA.the_POAManager(), + policies ); + + //Create the locators + TraceableLocator itemLocator = new TraceableLocator( mItemPOA ); + mItemPOA.set_servant_manager( itemLocator._this( Gateway.getORB() ) ); + + ActiveLocator agentLocator = new ActiveLocator( mAgentPOA ); + mAgentPOA.set_servant_manager( agentLocator._this( Gateway.getORB() ) ); + + } + + + /************************************************************************** + * Returns a CORBA servant for a pre-existing entity + **************************************************************************/ + private Servant getEntity(int sysKey, org.omg.PortableServer.POA poa) throws ObjectNotFoundException { + try { + EntityPath entityPath = new EntityPath(sysKey); + Servant entity = null; + synchronized (mEntityCache) { + entity = (Servant)mEntityCache.get(entityPath); + if (entity == null) { + Logger.msg(7, "Creating new servant for "+sysKey); + + Class entityClass = Gateway.getLDAPLookup().getEntityClass(entityPath); + + if (entityClass == TraceableEntity.class) { + if (poa == null) poa = mItemPOA; + entity = new TraceableEntity(sysKey, poa); + } + else if (entityClass == ActiveEntity.class) { + if (poa == null) poa = mAgentPOA; + entity = new ActiveEntity(sysKey, poa); + } + mEntityCache.put(entityPath, entity); + } + } + return entity; + + } catch (InvalidEntityPathException ex) { + throw new ObjectNotFoundException("Invalid Entity Key", ""); + } + } + + /************************************************************************** + * Wrapper for fetching Items + **************************************************************************/ + public TraceableEntity getItem(int sysKey, org.omg.PortableServer.POA poa) throws ObjectNotFoundException { + return (TraceableEntity)getEntity(sysKey, poa); + } + + /************************************************************************** + * Wrapper for fetching Agents + **************************************************************************/ + public ActiveEntity getAgent(int sysKey, org.omg.PortableServer.POA poa) throws ObjectNotFoundException { + return (ActiveEntity)getEntity(sysKey, poa); + } + + /** + * @param entityPath + * @return + */ + public Servant createEntity(EntityPath entityPath) throws CannotManageException, ObjectAlreadyExistsException { + try { + if (entityPath == null) + entityPath = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey(); + } catch (Exception ex) { + Logger.error(ex); + throw new CannotManageException("Cannot generate next entity key"); + } + boolean isAgent = entityPath instanceof AgentPath; + POA myPOA = isAgent?mAgentPOA:mItemPOA; + org.omg.CORBA.Object obj = myPOA.create_reference_with_id(entityPath.getOID(), isAgent?AgentHelper.id():ItemHelper.id()); + entityPath.setIOR(obj); + Servant entity; + if (isAgent) + entity = new ActiveEntity(entityPath.getSysKey(), myPOA); + else + entity = new TraceableEntity(entityPath.getSysKey(), myPOA); + synchronized (mEntityCache) { + mEntityCache.put(entityPath, entity); + } + return entity; + + } +} -- cgit v1.2.3