corba/src/share/classes/com/sun/corba/se/spi/orb/ParserImplBase.java
author skoppar
Fri, 24 Sep 2010 22:42:14 -0700
changeset 7059 79caba0caf4f
parent 5555 b2b5ed3f0d0d
permissions -rw-r--r--
6891766: Vulnerabilities in use of reflection in CORBA Reviewed-by: hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
7059
79caba0caf4f 6891766: Vulnerabilities in use of reflection in CORBA
skoppar
parents: 5555
diff changeset
     2
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
package com.sun.corba.se.spi.orb ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
import java.util.Map ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import java.util.Set ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
import java.util.Iterator ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import java.util.Properties ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedExceptionAction ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedActionException ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Field ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import org.omg.CORBA.INTERNAL ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import com.sun.corba.se.spi.logging.CORBALogDomains ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
import com.sun.corba.se.impl.orbutil.ObjectUtility ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
// XXX This could probably be further extended by using more reflection and
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
// a dynamic proxy that satisfies the interfaces that are inherited by the
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
// more derived class.  Do we want to go that far?
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
public abstract class ParserImplBase {
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
    private ORBUtilSystemException wrapper ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
    protected abstract PropertyParser makeParser() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
    /** Override this method if there is some needed initialization
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
    * that takes place after argument parsing.  It is always called
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
    * at the end of setFields.
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
    */
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
    protected void complete()
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    public ParserImplBase()
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
        // Do nothing in this case: no parsing takes place
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        wrapper = ORBUtilSystemException.get(
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
            CORBALogDomains.ORB_LIFECYCLE ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
    public void init( DataCollector coll )
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
        PropertyParser parser = makeParser() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
        coll.setParser( parser ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        Properties props = coll.getProperties() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        Map map = parser.parse( props ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        setFields( map ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
    private Field getAnyField( String name )
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
        Field result = null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
            Class cls = this.getClass() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
            result = cls.getDeclaredField( name ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
            while (result == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
                cls = cls.getSuperclass() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
                if (cls == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
                    break ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
                result = cls.getDeclaredField( name ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
        } catch (Exception exc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
            throw wrapper.fieldNotFound( exc, name ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
        if (result == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
            throw wrapper.fieldNotFound( name ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
        return result ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    protected void setFields( Map map )
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
        Set entries = map.entrySet() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
        Iterator iter = entries.iterator() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
        while (iter.hasNext()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
            java.util.Map.Entry entry = (java.util.Map.Entry)(iter.next()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
            final String name = (String)(entry.getKey()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
            final Object value = entry.getValue() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
                AccessController.doPrivileged(
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
                    new PrivilegedExceptionAction() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
                        public Object run() throws IllegalAccessException,
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
                            IllegalArgumentException
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
                        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
                            Field field = getAnyField( name ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
                            field.setAccessible( true ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
                            field.set( ParserImplBase.this, value ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
                            return null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
                ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
            } catch (PrivilegedActionException exc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
                // Since exc wraps the actual exception, use exc.getCause()
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
                // instead of exc.
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
                throw wrapper.errorSettingField( exc.getCause(), name,
7059
79caba0caf4f 6891766: Vulnerabilities in use of reflection in CORBA
skoppar
parents: 5555
diff changeset
   128
                    value.toString() ) ;
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
        // Make sure that any extra initialization takes place after all the
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
        // fields are set from the map.
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
        complete() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
}