1 /*
   2  * Copyright (c) 1999, 2004, 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 package com.sun.corba.se.impl.oa.poa;
  26 
  27 import java.util.EmptyStackException;
  28 
  29 import org.omg.PortableServer.*;
  30 
  31 import com.sun.corba.se.spi.orb.ORB ;
  32 
  33 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  34 
  35 import com.sun.corba.se.impl.logging.POASystemException ;
  36 
  37 public class DelegateImpl implements org.omg.PortableServer.portable.Delegate
  38 {
  39     private ORB orb ;
  40     private POASystemException wrapper ;
  41     private POAFactory factory;
  42 
  43     public DelegateImpl(ORB orb, POAFactory factory){
  44         this.orb = orb ;
  45         this.wrapper = POASystemException.get( orb,
  46             CORBALogDomains.OA ) ;
  47         this.factory = factory;
  48     }
  49 
  50     public org.omg.CORBA.ORB orb(Servant self)
  51     {
  52         return orb;
  53     }
  54 
  55     public org.omg.CORBA.Object this_object(Servant self)
  56     {
  57         byte[] oid;
  58         POA poa;
  59         try {
  60             oid = orb.peekInvocationInfo().id();
  61             poa = (POA)orb.peekInvocationInfo().oa();
  62             String repId = self._all_interfaces(poa,oid)[0] ;
  63             return poa.create_reference_with_id(oid, repId);
  64         } catch (EmptyStackException notInInvocationE) {
  65             //Not within an invocation context
  66             POAImpl defaultPOA = null;
  67             try {
  68                 defaultPOA = (POAImpl)self._default_POA();
  69             } catch (ClassCastException exception){
  70                 throw wrapper.defaultPoaNotPoaimpl( exception ) ;
  71             }
  72 
  73             try {
  74                 if (defaultPOA.getPolicies().isImplicitlyActivated() ||
  75                     (defaultPOA.getPolicies().isUniqueIds() &&
  76                      defaultPOA.getPolicies().retainServants())) {
  77                     return defaultPOA.servant_to_reference(self);
  78                 } else {
  79                     throw wrapper.wrongPoliciesForThisObject() ;
  80                 }
  81             } catch ( org.omg.PortableServer.POAPackage.ServantNotActive e) {
  82                 throw wrapper.thisObjectServantNotActive( e ) ;
  83             } catch ( org.omg.PortableServer.POAPackage.WrongPolicy e) {
  84                 throw wrapper.thisObjectWrongPolicy( e ) ;
  85             }
  86         } catch (ClassCastException e) {
  87             throw wrapper.defaultPoaNotPoaimpl( e ) ;
  88         }
  89     }
  90 
  91     public POA poa(Servant self)
  92     {
  93         try {
  94             return (POA)orb.peekInvocationInfo().oa();
  95         } catch (EmptyStackException exception){
  96             POA returnValue = factory.lookupPOA(self);
  97             if (returnValue != null) {
  98                 return returnValue;
  99             }
 100 
 101             throw wrapper.noContext( exception ) ;
 102         }
 103     }
 104 
 105     public byte[] object_id(Servant self)
 106     {
 107         try{
 108             return orb.peekInvocationInfo().id();
 109         } catch (EmptyStackException exception){
 110             throw wrapper.noContext(exception) ;
 111         }
 112     }
 113 
 114     public POA default_POA(Servant self)
 115     {
 116         return factory.getRootPOA();
 117     }
 118 
 119     public boolean is_a(Servant self, String repId)
 120     {
 121         String[] repositoryIds = self._all_interfaces(poa(self),object_id(self));
 122         for ( int i=0; i<repositoryIds.length; i++ )
 123             if ( repId.equals(repositoryIds[i]) )
 124                 return true;
 125 
 126         return false;
 127     }
 128 
 129     public boolean non_existent(Servant self)
 130     {
 131         //REVISIT
 132         try{
 133             byte[] oid = orb.peekInvocationInfo().id();
 134             if( oid == null) return true;
 135             else return false;
 136         } catch (EmptyStackException exception){
 137             throw wrapper.noContext(exception) ;
 138         }
 139     }
 140 
 141     // The get_interface() method has been replaced by get_interface_def()
 142 
 143     public org.omg.CORBA.Object get_interface_def(Servant Self)
 144     {
 145         throw wrapper.methodNotImplemented() ;
 146     }
 147 }