src/share/classes/com/sun/corba/se/impl/orb/PrefixParserAction.java

changeset 0
7ef37b2cdcad
child 748
6845b95cba6b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/corba/se/impl/orb/PrefixParserAction.java	Wed Apr 27 01:21:28 2016 +0800
     1.3 @@ -0,0 +1,122 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.corba.se.impl.orb ;
    1.30 +
    1.31 +import org.omg.CORBA.INITIALIZE ;
    1.32 +
    1.33 +import java.util.Properties ;
    1.34 +import java.util.List ;
    1.35 +import java.util.LinkedList ;
    1.36 +import java.util.Iterator ;
    1.37 +
    1.38 +import java.lang.reflect.Array ;
    1.39 +
    1.40 +import com.sun.corba.se.spi.orb.Operation ;
    1.41 +import com.sun.corba.se.spi.orb.StringPair ;
    1.42 +import com.sun.corba.se.spi.logging.CORBALogDomains ;
    1.43 +
    1.44 +import com.sun.corba.se.impl.orbutil.ObjectUtility ;
    1.45 +import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
    1.46 +
    1.47 +public class PrefixParserAction extends ParserActionBase {
    1.48 +    private Class componentType ;
    1.49 +    private ORBUtilSystemException wrapper ;
    1.50 +
    1.51 +    public PrefixParserAction( String propertyName,
    1.52 +        Operation operation, String fieldName, Class componentType )
    1.53 +    {
    1.54 +        super( propertyName, true, operation, fieldName ) ;
    1.55 +        this.componentType = componentType ;
    1.56 +        this.wrapper = ORBUtilSystemException.get(
    1.57 +            CORBALogDomains.ORB_LIFECYCLE ) ;
    1.58 +    }
    1.59 +
    1.60 +    /** For each String s that matches the prefix given by getPropertyName(),
    1.61 +     * apply getOperation() to { suffix( s ), value }
    1.62 +     * and add the result to an Object[]
    1.63 +     * which forms the result of apply.  Returns null if there are no
    1.64 +     * matches.
    1.65 +     */
    1.66 +    public Object apply( Properties props )
    1.67 +    {
    1.68 +        String prefix = getPropertyName() ;
    1.69 +        int prefixLength = prefix.length() ;
    1.70 +        if (prefix.charAt( prefixLength - 1 ) != '.') {
    1.71 +            prefix += '.' ;
    1.72 +            prefixLength++ ;
    1.73 +        }
    1.74 +
    1.75 +        List matches = new LinkedList() ;
    1.76 +
    1.77 +        // Find all keys in props that start with propertyName
    1.78 +        Iterator iter = props.keySet().iterator() ;
    1.79 +        while (iter.hasNext()) {
    1.80 +            String key = (String)(iter.next()) ;
    1.81 +            if (key.startsWith( prefix )) {
    1.82 +                String suffix = key.substring( prefixLength ) ;
    1.83 +                String value = props.getProperty( key ) ;
    1.84 +                StringPair data = new StringPair( suffix, value ) ;
    1.85 +                Object result = getOperation().operate( data ) ;
    1.86 +                matches.add( result ) ;
    1.87 +            }
    1.88 +        }
    1.89 +
    1.90 +        int size = matches.size() ;
    1.91 +        if (size > 0) {
    1.92 +            // Convert the list into an array of the proper type.
    1.93 +            // An Object[] as a result does NOT work.  Also report
    1.94 +            // any errors carefully, as errors here or in parsers that
    1.95 +            // use this Operation often show up at ORB.init().
    1.96 +            Object result = null ;
    1.97 +            try {
    1.98 +                result = Array.newInstance( componentType, size ) ;
    1.99 +            } catch (Throwable thr) {
   1.100 +                throw wrapper.couldNotCreateArray( thr,
   1.101 +                    getPropertyName(), componentType,
   1.102 +                    new Integer( size ) ) ;
   1.103 +            }
   1.104 +
   1.105 +            Iterator iter2 = matches.iterator() ;
   1.106 +            int ctr = 0 ;
   1.107 +            while (iter2.hasNext()) {
   1.108 +                Object obj = iter2.next() ;
   1.109 +
   1.110 +                try {
   1.111 +                    Array.set( result, ctr, obj ) ;
   1.112 +                } catch (Throwable thr) {
   1.113 +                    throw wrapper.couldNotSetArray( thr,
   1.114 +                        getPropertyName(), new Integer(ctr),
   1.115 +                        componentType, new Integer(size),
   1.116 +                        obj.toString() ) ;
   1.117 +                }
   1.118 +                ctr++ ;
   1.119 +            }
   1.120 +
   1.121 +            return result ;
   1.122 +        } else
   1.123 +            return null ;
   1.124 +    }
   1.125 +}

mercurial