1 /*
   2  * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.oa.poa ;
  27 
  28 import org.omg.PortableServer.Servant ;
  29 
  30 import org.omg.PortableServer.POAPackage.WrongPolicy ;
  31 import org.omg.PortableServer.POAPackage.ServantNotActive ;
  32 import org.omg.PortableServer.POAPackage.ServantAlreadyActive ;
  33 import org.omg.PortableServer.POAPackage.ObjectNotActive ;
  34 import org.omg.PortableServer.POAPackage.ObjectAlreadyActive ;
  35 
  36 import com.sun.corba.se.impl.orbutil.concurrent.SyncUtil ;
  37 
  38 import com.sun.corba.se.impl.orbutil.ORBUtility ;
  39 import com.sun.corba.se.impl.orbutil.ORBConstants ;
  40 
  41 import com.sun.corba.se.impl.javax.rmi.CORBA.Util ;
  42 
  43 import com.sun.corba.se.impl.oa.NullServantImpl ;
  44 
  45 public abstract class POAPolicyMediatorBase_R extends POAPolicyMediatorBase {
  46     protected ActiveObjectMap activeObjectMap ;
  47 
  48     POAPolicyMediatorBase_R( Policies policies, POAImpl poa )
  49     {
  50         super( policies, poa ) ;
  51 
  52         // assert policies.retainServants() && policies.useActiveObjectMapOnly()
  53         if (!policies.retainServants())
  54             throw poa.invocationWrapper().policyMediatorBadPolicyInFactory() ;
  55 
  56         activeObjectMap = ActiveObjectMap.create(poa, !isUnique);
  57     }
  58 
  59     public void returnServant()
  60     {
  61         // NO-OP
  62     }
  63 
  64     public void clearAOM()
  65     {
  66         activeObjectMap.clear() ;
  67         activeObjectMap = null ;
  68     }
  69 
  70     protected Servant internalKeyToServant( ActiveObjectMap.Key key )
  71     {
  72         AOMEntry entry = activeObjectMap.get(key);
  73         if (entry == null)
  74             return null ;
  75 
  76         return activeObjectMap.getServant( entry ) ;
  77     }
  78 
  79     protected Servant internalIdToServant( byte[] id )
  80     {
  81         ActiveObjectMap.Key key = new ActiveObjectMap.Key( id ) ;
  82         return internalKeyToServant( key ) ;
  83     }
  84 
  85     protected void activateServant( ActiveObjectMap.Key key, AOMEntry entry, Servant servant )
  86     {
  87         setDelegate(servant, key.id );
  88 
  89         if (orb.shutdownDebugFlag) {
  90             System.out.println("Activating object " + servant +
  91                 " with POA " + poa);
  92         }
  93 
  94         activeObjectMap.putServant( servant, entry ) ;
  95 
  96         if (Util.isInstanceDefined()) {
  97             POAManagerImpl pm = (POAManagerImpl)poa.the_POAManager() ;
  98             POAFactory factory = pm.getFactory() ;
  99             factory.registerPOAForServant(poa, servant);
 100         }
 101     }
 102 
 103     public final void activateObject(byte[] id, Servant servant)
 104         throws WrongPolicy, ServantAlreadyActive, ObjectAlreadyActive
 105     {
 106         if (isUnique && activeObjectMap.contains(servant))
 107             throw new ServantAlreadyActive();
 108         ActiveObjectMap.Key key = new ActiveObjectMap.Key( id ) ;
 109 
 110         AOMEntry entry = activeObjectMap.get( key ) ;
 111 
 112         // Check for an ObjectAlreadyActive error
 113         entry.activateObject() ;
 114         activateServant( key, entry, servant ) ;
 115     }
 116 
 117     public Servant deactivateObject( byte[] id )
 118         throws ObjectNotActive, WrongPolicy
 119     {
 120         ActiveObjectMap.Key key = new ActiveObjectMap.Key( id ) ;
 121         return deactivateObject( key ) ;
 122     }
 123 
 124     protected void deactivateHelper( ActiveObjectMap.Key key, AOMEntry entry,
 125         Servant s ) throws ObjectNotActive, WrongPolicy
 126     {
 127         // Default does nothing, but the USE_SERVANT_MANAGER case
 128         // must handle etherealization
 129 
 130         activeObjectMap.remove(key);
 131 
 132         if (Util.isInstanceDefined()) {
 133             POAManagerImpl pm = (POAManagerImpl)poa.the_POAManager() ;
 134             POAFactory factory = pm.getFactory() ;
 135             factory.unregisterPOAForServant(poa, s);
 136         }
 137     }
 138 
 139     public Servant deactivateObject( ActiveObjectMap.Key key )
 140         throws ObjectNotActive, WrongPolicy
 141     {
 142         if (orb.poaDebugFlag) {
 143             ORBUtility.dprint( this,
 144                 "Calling deactivateObject for key " + key ) ;
 145         }
 146 
 147         try {
 148             AOMEntry entry = activeObjectMap.get(key);
 149             if (entry == null)
 150                 throw new ObjectNotActive();
 151 
 152             Servant s = activeObjectMap.getServant( entry ) ;
 153             if (s == null)
 154                 throw new ObjectNotActive();
 155 
 156             if (orb.poaDebugFlag) {
 157                 System.out.println("Deactivating object " + s + " with POA " + poa);
 158             }
 159 
 160             deactivateHelper( key, entry, s ) ;
 161 
 162             return s ;
 163         } finally {
 164             if (orb.poaDebugFlag) {
 165                 ORBUtility.dprint( this,
 166                     "Exiting deactivateObject" ) ;
 167             }
 168         }
 169     }
 170 
 171     public byte[] servantToId( Servant servant ) throws ServantNotActive, WrongPolicy
 172     {
 173         // XXX needs to handle call from an invocation on this POA
 174 
 175         if (!isUnique && !isImplicit)
 176             throw new WrongPolicy();
 177 
 178         if (isUnique) {
 179             ActiveObjectMap.Key key = activeObjectMap.getKey(servant);
 180             if (key != null)
 181                 return key.id ;
 182         }
 183 
 184         // assert !isUnique || (servant not in activateObjectMap)
 185 
 186         if (isImplicit)
 187             try {
 188                 byte[] id = newSystemId() ;
 189                 activateObject( id, servant ) ;
 190                 return id ;
 191             } catch (ObjectAlreadyActive oaa) {
 192                 // This can't occur here, since id is always brand new.
 193                 throw poa.invocationWrapper().servantToIdOaa( oaa ) ;
 194             } catch (ServantAlreadyActive s) {
 195                 throw poa.invocationWrapper().servantToIdSaa( s ) ;
 196             } catch (WrongPolicy w) {
 197                 throw poa.invocationWrapper().servantToIdWp( w ) ;
 198             }
 199 
 200         throw new ServantNotActive();
 201     }
 202 }