1 /*
   2  * Copyright (c) 2017, 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.ior;
  27 
  28 import java.util.Set;
  29 
  30 import com.sun.corba.se.impl.orbutil.ORBUtility;
  31 import com.sun.corba.se.spi.ior.IORTypeCheckRegistry;
  32 import com.sun.corba.se.spi.orb.ORB;
  33 
  34 public class IORTypeCheckRegistryImpl implements IORTypeCheckRegistry {
  35 
  36     private final Set<String> iorTypeNames;
  37     private static final Set<String> builtinIorTypeNames;
  38     private ORB theOrb;
  39 
  40     static {
  41         builtinIorTypeNames = initBuiltinIorTypeNames();
  42     }
  43 
  44     public IORTypeCheckRegistryImpl( String filterProperties, ORB orb) {
  45         theOrb = orb;
  46         iorTypeNames = parseIorClassNameList(filterProperties);
  47     }
  48 
  49     /*
  50      *
  51      * A note on the validation flow:
  52      * 1. against the filter class name list
  53      * 2. against the builtin class name list
  54      */
  55 
  56     @Override
  57     public boolean isValidIORType(String iorClassName) {
  58         dprintTransport(".isValidIORType : iorClassName == " + iorClassName);
  59         return validateIorTypeByName(iorClassName);
  60     }
  61 
  62 
  63     private boolean validateIorTypeByName(String iorClassName) {
  64         dprintTransport(".validateIorTypeByName : iorClassName == " + iorClassName);
  65         boolean isValidType;
  66 
  67         isValidType = checkIorTypeNames(iorClassName);
  68 
  69         if (!isValidType) {
  70             isValidType = checkBuiltinClassNames(iorClassName);
  71         }
  72 
  73         dprintTransport(".validateIorTypeByName : isValidType == " + isValidType);
  74         return isValidType;
  75     }
  76 
  77 
  78     /*
  79      * check if the class name corresponding to an IOR Type name
  80      * is in the ior class name list as generated from the filter property.
  81      * So if the IOR type is recorded in the registry then allow the creation of the
  82      * stub factory and let it resolve and load the class. That is if current
  83      * type check deliberation permits.
  84      * IOR Type names are configured by the filter property
  85      */
  86 
  87     private boolean checkIorTypeNames(
  88             String theIorClassName) {
  89         return (iorTypeNames != null) && (iorTypeNames.contains(theIorClassName));
  90     }
  91 
  92     /*
  93      * Check the IOR interface class name against the set of
  94      * class names that correspond to the builtin JDK IDL stub classes.
  95      */
  96 
  97     private boolean  checkBuiltinClassNames(
  98             String theIorClassName) {
  99         return builtinIorTypeNames.contains(theIorClassName);
 100     }
 101 
 102 
 103     private Set<String> parseIorClassNameList(String filterProperty) {
 104         Set<String> _iorTypeNames = null;
 105         if (filterProperty != null) {
 106             String[] tempIorClassNames = filterProperty.split(";");
 107             _iorTypeNames = Set.<String>of(tempIorClassNames);
 108             if (theOrb.orbInitDebugFlag) {
 109                 dprintConfiguredIorTypeNames();
 110             }
 111         }
 112         return _iorTypeNames;
 113     }
 114 
 115 
 116     private static Set<String> initBuiltinIorTypeNames() {
 117         Set<Class<?>> builtInCorbaStubTypes = initBuiltInCorbaStubTypes();
 118         String [] tempBuiltinIorTypeNames = new String[builtInCorbaStubTypes.size()];
 119         int i = 0;
 120         for (Class<?> _stubClass: builtInCorbaStubTypes) {
 121             tempBuiltinIorTypeNames[i++] = _stubClass.getName();
 122         }
 123         return  Set.<String>of(tempBuiltinIorTypeNames);
 124     }
 125 
 126     private static Set<Class<?>> initBuiltInCorbaStubTypes() {
 127         Class<?> tempBuiltinCorbaStubTypes[] = {
 128                 com.sun.corba.se.spi.activation.Activator.class,
 129                 com.sun.corba.se.spi.activation._ActivatorStub.class,
 130                 com.sun.corba.se.spi.activation._InitialNameServiceStub.class,
 131                 com.sun.corba.se.spi.activation._LocatorStub.class,
 132                 com.sun.corba.se.spi.activation._RepositoryStub.class,
 133                 com.sun.corba.se.spi.activation._ServerManagerStub.class,
 134                 com.sun.corba.se.spi.activation._ServerStub.class,
 135                 org.omg.CosNaming.BindingIterator.class,
 136                 org.omg.CosNaming._BindingIteratorStub.class,
 137                 org.omg.CosNaming.NamingContextExt.class,
 138                 org.omg.CosNaming._NamingContextExtStub.class,
 139                 org.omg.CosNaming.NamingContext.class,
 140                 org.omg.CosNaming._NamingContextStub.class,
 141                 org.omg.DynamicAny.DynAnyFactory.class,
 142                 org.omg.DynamicAny._DynAnyFactoryStub.class,
 143                 org.omg.DynamicAny.DynAny.class,
 144                 org.omg.DynamicAny._DynAnyStub.class,
 145                 org.omg.DynamicAny.DynArray.class,
 146                 org.omg.DynamicAny._DynArrayStub.class,
 147                 org.omg.DynamicAny.DynEnum.class,
 148                 org.omg.DynamicAny._DynEnumStub.class,
 149                 org.omg.DynamicAny.DynFixed.class,
 150                 org.omg.DynamicAny._DynFixedStub.class,
 151                 org.omg.DynamicAny.DynSequence.class,
 152                 org.omg.DynamicAny._DynSequenceStub.class,
 153                 org.omg.DynamicAny.DynStruct.class,
 154                 org.omg.DynamicAny._DynStructStub.class,
 155                 org.omg.DynamicAny.DynUnion.class,
 156                 org.omg.DynamicAny._DynUnionStub.class,
 157                 org.omg.DynamicAny._DynValueStub.class,
 158                 org.omg.DynamicAny.DynValue.class,
 159                 org.omg.PortableServer.ServantActivator.class,
 160                 org.omg.PortableServer._ServantActivatorStub.class,
 161                 org.omg.PortableServer.ServantLocator.class,
 162                 org.omg.PortableServer._ServantLocatorStub.class };
 163         return Set.<Class<?>>of(tempBuiltinCorbaStubTypes);
 164     }
 165 
 166     private void dprintConfiguredIorTypeNames() {
 167         if (iorTypeNames != null) {
 168             for (String iorTypeName : iorTypeNames) {
 169                 ORBUtility.dprint(this, ".dprintConfiguredIorTypeNames: " + iorTypeName);
 170             }
 171         }
 172     }
 173 
 174     private void dprintTransport(String msg) {
 175         if (theOrb.transportDebugFlag) {
 176             ORBUtility.dprint(this, msg);
 177         }
 178     }
 179 }