src/share/jaxws_classes/com/oracle/webservices/internal/api/message/BaseDistributedPropertySet.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/oracle/webservices/internal/api/message/BaseDistributedPropertySet.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,308 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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.oracle.webservices.internal.api.message;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.istack.internal.Nullable;
    1.33 +import com.sun.xml.internal.ws.api.message.Packet;
    1.34 +import com.sun.xml.internal.ws.client.RequestContext;
    1.35 +import com.sun.xml.internal.ws.client.ResponseContext;
    1.36 +
    1.37 +import javax.xml.ws.WebServiceContext;
    1.38 +
    1.39 +import java.util.AbstractMap;
    1.40 +import java.util.Map.Entry;
    1.41 +import java.util.HashSet;
    1.42 +import java.util.IdentityHashMap;
    1.43 +import java.util.Map;
    1.44 +import java.util.Set;
    1.45 +
    1.46 +/**
    1.47 + * {@link PropertySet} that combines properties exposed from multiple
    1.48 + * {@link PropertySet}s into one.
    1.49 + *
    1.50 + * <p>
    1.51 + * This implementation allows one {@link PropertySet} to assemble
    1.52 + * all properties exposed from other "satellite" {@link PropertySet}s.
    1.53 + * (A satellite may itself be a {@link DistributedPropertySet}, so
    1.54 + * in general this can form a tree.)
    1.55 + *
    1.56 + * <p>
    1.57 + * This is useful for JAX-WS because the properties we expose to the application
    1.58 + * are contributed by different pieces, and therefore we'd like each of them
    1.59 + * to have a separate {@link PropertySet} implementation that backs up
    1.60 + * the properties. For example, this allows FastInfoset to expose its
    1.61 + * set of properties to {@link RequestContext} by using a strongly-typed fields.
    1.62 + *
    1.63 + * <p>
    1.64 + * This is also useful for a client-side transport to expose a bunch of properties
    1.65 + * into {@link ResponseContext}. It simply needs to create a {@link PropertySet}
    1.66 + * object with methods for each property it wants to expose, and then add that
    1.67 + * {@link PropertySet} to {@link Packet}. This allows property values to be
    1.68 + * lazily computed (when actually asked by users), thus improving the performance
    1.69 + * of the typical case where property values are not asked.
    1.70 + *
    1.71 + * <p>
    1.72 + * A similar benefit applies on the server-side, for a transport to expose
    1.73 + * a bunch of properties to {@link WebServiceContext}.
    1.74 + *
    1.75 + * <p>
    1.76 + * To achieve these benefits, access to {@link DistributedPropertySet} is slower
    1.77 + * compared to {@link PropertySet} (such as get/set), while adding a satellite
    1.78 + * object is relatively fast.
    1.79 + *
    1.80 + * @author Kohsuke Kawaguchi
    1.81 + */
    1.82 +public abstract class BaseDistributedPropertySet extends BasePropertySet implements DistributedPropertySet {
    1.83 +
    1.84 +    /**
    1.85 +     * All {@link PropertySet}s that are bundled into this {@link PropertySet}.
    1.86 +     */
    1.87 +    private final Map<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet> satellites
    1.88 +        = new IdentityHashMap<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet>();
    1.89 +
    1.90 +    private final Map<String, Object> viewthis;
    1.91 +
    1.92 +    public BaseDistributedPropertySet() {
    1.93 +        this.viewthis = super.createView();
    1.94 +    }
    1.95 +
    1.96 +    @Override
    1.97 +    public void addSatellite(@NotNull PropertySet satellite) {
    1.98 +        addSatellite(satellite.getClass(), satellite);
    1.99 +    }
   1.100 +
   1.101 +    @Override
   1.102 +    public void addSatellite(@NotNull Class<? extends com.oracle.webservices.internal.api.message.PropertySet> keyClass, @NotNull PropertySet satellite) {
   1.103 +        satellites.put(keyClass, satellite);
   1.104 +    }
   1.105 +
   1.106 +    @Override
   1.107 +    public void removeSatellite(PropertySet satellite) {
   1.108 +        satellites.remove(satellite.getClass());
   1.109 +    }
   1.110 +
   1.111 +    public void copySatelliteInto(@NotNull DistributedPropertySet r) {
   1.112 +        for (Map.Entry<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet> entry : satellites.entrySet()) {
   1.113 +            r.addSatellite(entry.getKey(), entry.getValue());
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    @Override
   1.118 +    public void copySatelliteInto(MessageContext r) {
   1.119 +        copySatelliteInto((DistributedPropertySet)r);
   1.120 +    }
   1.121 +
   1.122 +    @Override
   1.123 +    public @Nullable <T extends com.oracle.webservices.internal.api.message.PropertySet> T getSatellite(Class<T> satelliteClass) {
   1.124 +        T satellite = (T) satellites.get(satelliteClass);
   1.125 +        if (satellite != null) {
   1.126 +            return satellite;
   1.127 +        }
   1.128 +
   1.129 +        for (PropertySet child : satellites.values()) {
   1.130 +            if (satelliteClass.isInstance(child)) {
   1.131 +                return satelliteClass.cast(child);
   1.132 +            }
   1.133 +
   1.134 +            if (DistributedPropertySet.class.isInstance(child)) {
   1.135 +                satellite = DistributedPropertySet.class.cast(child).getSatellite(satelliteClass);
   1.136 +                if (satellite != null) {
   1.137 +                    return satellite;
   1.138 +                }
   1.139 +            }
   1.140 +        }
   1.141 +        return null;
   1.142 +    }
   1.143 +
   1.144 +    @Override
   1.145 +    public Map<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, com.oracle.webservices.internal.api.message.PropertySet> getSatellites() {
   1.146 +        return satellites;
   1.147 +    }
   1.148 +
   1.149 +    @Override
   1.150 +    public Object get(Object key) {
   1.151 +        // check satellites
   1.152 +        for (PropertySet child : satellites.values()) {
   1.153 +            if (child.supports(key)) {
   1.154 +                return child.get(key);
   1.155 +            }
   1.156 +        }
   1.157 +
   1.158 +        // otherwise it must be the master
   1.159 +        return super.get(key);
   1.160 +    }
   1.161 +
   1.162 +    @Override
   1.163 +    public Object put(String key, Object value) {
   1.164 +        // check satellites
   1.165 +        for (PropertySet child : satellites.values()) {
   1.166 +            if(child.supports(key)) {
   1.167 +                return child.put(key,value);
   1.168 +            }
   1.169 +        }
   1.170 +
   1.171 +        // otherwise it must be the master
   1.172 +        return super.put(key,value);
   1.173 +    }
   1.174 +
   1.175 +    @Override
   1.176 +    public boolean containsKey(Object key) {
   1.177 +        if (viewthis.containsKey(key))
   1.178 +            return true;
   1.179 +        for (PropertySet child : satellites.values()) {
   1.180 +            if (child.containsKey(key)) {
   1.181 +                return true;
   1.182 +            }
   1.183 +        }
   1.184 +        return false;
   1.185 +    }
   1.186 +
   1.187 +    @Override
   1.188 +    public boolean supports(Object key) {
   1.189 +        // check satellites
   1.190 +        for (PropertySet child : satellites.values()) {
   1.191 +            if (child.supports(key)) {
   1.192 +                return true;
   1.193 +            }
   1.194 +        }
   1.195 +
   1.196 +        return super.supports(key);
   1.197 +    }
   1.198 +
   1.199 +    @Override
   1.200 +    public Object remove(Object key) {
   1.201 +        // check satellites
   1.202 +        for (PropertySet child : satellites.values()) {
   1.203 +            if (child.supports(key)) {
   1.204 +                return child.remove(key);
   1.205 +            }
   1.206 +        }
   1.207 +
   1.208 +        return super.remove(key);
   1.209 +    }
   1.210 +
   1.211 +    @Override
   1.212 +    protected void createEntrySet(Set<Entry<String, Object>> core) {
   1.213 +        super.createEntrySet(core);
   1.214 +        for (PropertySet child : satellites.values()) {
   1.215 +            ((BasePropertySet) child).createEntrySet(core);
   1.216 +        }
   1.217 +    }
   1.218 +
   1.219 +    protected Map<String, Object> asMapLocal() {
   1.220 +        return viewthis;
   1.221 +    }
   1.222 +
   1.223 +    protected boolean supportsLocal(Object key) {
   1.224 +        return super.supports(key);
   1.225 +    }
   1.226 +
   1.227 +    class DistributedMapView extends AbstractMap<String, Object> {
   1.228 +        @Override
   1.229 +        public Object get(Object key) {
   1.230 +            for (PropertySet child : satellites.values()) {
   1.231 +                if (child.supports(key)) {
   1.232 +                    return child.get(key);
   1.233 +                }
   1.234 +            }
   1.235 +
   1.236 +            return viewthis.get(key);
   1.237 +        }
   1.238 +
   1.239 +        @Override
   1.240 +        public int size() {
   1.241 +            int size = viewthis.size();
   1.242 +            for (PropertySet child : satellites.values()) {
   1.243 +                size += child.asMap().size();
   1.244 +            }
   1.245 +            return size;
   1.246 +        }
   1.247 +
   1.248 +        @Override
   1.249 +        public boolean containsKey(Object key) {
   1.250 +            if (viewthis.containsKey(key))
   1.251 +                return true;
   1.252 +            for (PropertySet child : satellites.values()) {
   1.253 +                if (child.containsKey(key))
   1.254 +                    return true;
   1.255 +            }
   1.256 +            return false;
   1.257 +        }
   1.258 +
   1.259 +        @Override
   1.260 +        public Set<Entry<String, Object>> entrySet() {
   1.261 +            Set<Entry<String, Object>> entries = new HashSet<Entry<String, Object>>();
   1.262 +            for (PropertySet child : satellites.values()) {
   1.263 +                for (Entry<String,Object> entry : child.asMap().entrySet()) {
   1.264 +                    // the code below is here to avoid entries.addAll(child.asMap().entrySet()); which works differently on JDK6/7
   1.265 +                    // see DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS
   1.266 +                    entries.add(new SimpleImmutableEntry<String, Object>(entry.getKey(), entry.getValue()));
   1.267 +                }
   1.268 +            }
   1.269 +            for (Entry<String,Object> entry : viewthis.entrySet()) {
   1.270 +                // the code below is here to avoid entries.addAll(child.asMap().entrySet()); which works differently on JDK6/7
   1.271 +                // see DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS
   1.272 +                entries.add(new SimpleImmutableEntry<String, Object>(entry.getKey(), entry.getValue()));
   1.273 +            }
   1.274 +
   1.275 +            return entries;
   1.276 +        }
   1.277 +
   1.278 +        @Override
   1.279 +        public Object put(String key, Object value) {
   1.280 +            for (PropertySet child : satellites.values()) {
   1.281 +                if (child.supports(key)) {
   1.282 +                    return child.put(key, value);
   1.283 +                }
   1.284 +            }
   1.285 +
   1.286 +            return viewthis.put(key, value);
   1.287 +        }
   1.288 +
   1.289 +        @Override
   1.290 +        public void clear() {
   1.291 +            satellites.clear();
   1.292 +            viewthis.clear();
   1.293 +        }
   1.294 +
   1.295 +        @Override
   1.296 +        public Object remove(Object key) {
   1.297 +            for (PropertySet child : satellites.values()) {
   1.298 +                if (child.supports(key)) {
   1.299 +                    return child.remove(key);
   1.300 +                }
   1.301 +            }
   1.302 +
   1.303 +            return viewthis.remove(key);
   1.304 +        }
   1.305 +    }
   1.306 +
   1.307 +    @Override
   1.308 +    protected Map<String, Object> createView() {
   1.309 +        return new DistributedMapView();
   1.310 +    }
   1.311 +}

mercurial