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

changeset 368
0989ad8c0860
child 374
72e03566f0a6
     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	Tue Apr 09 14:51:13 2013 +0100
     1.3 @@ -0,0 +1,323 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * The contents of this file are subject to the terms of either the GNU
    1.10 + * General Public License Version 2 only ("GPL") or the Common Development
    1.11 + * and Distribution License("CDDL") (collectively, the "License").  You
    1.12 + * may not use this file except in compliance with the License.  You can
    1.13 + * obtain a copy of the License at
    1.14 + * http://glassfish.java.net/public/CDDL+GPL_1_1.html
    1.15 + * or packager/legal/LICENSE.txt.  See the License for the specific
    1.16 + * language governing permissions and limitations under the License.
    1.17 + *
    1.18 + * When distributing the software, include this License Header Notice in each
    1.19 + * file and include the License file at packager/legal/LICENSE.txt.
    1.20 + *
    1.21 + * GPL Classpath Exception:
    1.22 + * Oracle designates this particular file as subject to the "Classpath"
    1.23 + * exception as provided by Oracle in the GPL Version 2 section of the License
    1.24 + * file that accompanied this code.
    1.25 + *
    1.26 + * Modifications:
    1.27 + * If applicable, add the following below the License Header, with the fields
    1.28 + * enclosed by brackets [] replaced by your own identifying information:
    1.29 + * "Portions Copyright [year] [name of copyright owner]"
    1.30 + *
    1.31 + * Contributor(s):
    1.32 + * If you wish your version of this file to be governed by only the CDDL or
    1.33 + * only the GPL Version 2, indicate your decision by adding "[Contributor]
    1.34 + * elects to include this software in this distribution under the [CDDL or GPL
    1.35 + * Version 2] license."  If you don't indicate a single choice of license, a
    1.36 + * recipient has the option to distribute your version of this file under
    1.37 + * either the CDDL, the GPL Version 2 or to extend the choice of license to
    1.38 + * its licensees as provided above.  However, if you add GPL Version 2 code
    1.39 + * and therefore, elected the GPL Version 2 license, then the option applies
    1.40 + * only if the new code is made subject to such option by the copyright
    1.41 + * holder.
    1.42 + */
    1.43 +
    1.44 +package com.oracle.webservices.internal.api.message;
    1.45 +
    1.46 +import com.sun.istack.internal.NotNull;
    1.47 +import com.sun.istack.internal.Nullable;
    1.48 +import com.sun.xml.internal.ws.api.message.Packet;
    1.49 +import com.sun.xml.internal.ws.client.RequestContext;
    1.50 +import com.sun.xml.internal.ws.client.ResponseContext;
    1.51 +
    1.52 +import javax.xml.ws.WebServiceContext;
    1.53 +
    1.54 +import java.util.AbstractMap;
    1.55 +import java.util.Map.Entry;
    1.56 +import java.util.HashSet;
    1.57 +import java.util.IdentityHashMap;
    1.58 +import java.util.Map;
    1.59 +import java.util.Set;
    1.60 +
    1.61 +/**
    1.62 + * {@link PropertySet} that combines properties exposed from multiple
    1.63 + * {@link PropertySet}s into one.
    1.64 + *
    1.65 + * <p>
    1.66 + * This implementation allows one {@link PropertySet} to assemble
    1.67 + * all properties exposed from other "satellite" {@link PropertySet}s.
    1.68 + * (A satellite may itself be a {@link DistributedPropertySet}, so
    1.69 + * in general this can form a tree.)
    1.70 + *
    1.71 + * <p>
    1.72 + * This is useful for JAX-WS because the properties we expose to the application
    1.73 + * are contributed by different pieces, and therefore we'd like each of them
    1.74 + * to have a separate {@link PropertySet} implementation that backs up
    1.75 + * the properties. For example, this allows FastInfoset to expose its
    1.76 + * set of properties to {@link RequestContext} by using a strongly-typed fields.
    1.77 + *
    1.78 + * <p>
    1.79 + * This is also useful for a client-side transport to expose a bunch of properties
    1.80 + * into {@link ResponseContext}. It simply needs to create a {@link PropertySet}
    1.81 + * object with methods for each property it wants to expose, and then add that
    1.82 + * {@link PropertySet} to {@link Packet}. This allows property values to be
    1.83 + * lazily computed (when actually asked by users), thus improving the performance
    1.84 + * of the typical case where property values are not asked.
    1.85 + *
    1.86 + * <p>
    1.87 + * A similar benefit applies on the server-side, for a transport to expose
    1.88 + * a bunch of properties to {@link WebServiceContext}.
    1.89 + *
    1.90 + * <p>
    1.91 + * To achieve these benefits, access to {@link DistributedPropertySet} is slower
    1.92 + * compared to {@link PropertySet} (such as get/set), while adding a satellite
    1.93 + * object is relatively fast.
    1.94 + *
    1.95 + * @author Kohsuke Kawaguchi
    1.96 + */
    1.97 +public abstract class BaseDistributedPropertySet extends BasePropertySet implements DistributedPropertySet {
    1.98 +
    1.99 +    /**
   1.100 +     * All {@link PropertySet}s that are bundled into this {@link PropertySet}.
   1.101 +     */
   1.102 +    private final Map<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet> satellites
   1.103 +        = new IdentityHashMap<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet>();
   1.104 +
   1.105 +    private final Map<String, Object> viewthis;
   1.106 +
   1.107 +    public BaseDistributedPropertySet() {
   1.108 +        this.viewthis = super.createView();
   1.109 +    }
   1.110 +
   1.111 +    @Override
   1.112 +    public void addSatellite(@NotNull PropertySet satellite) {
   1.113 +        addSatellite(satellite.getClass(), satellite);
   1.114 +    }
   1.115 +
   1.116 +    @Override
   1.117 +    public void addSatellite(@NotNull Class<? extends com.oracle.webservices.internal.api.message.PropertySet> keyClass, @NotNull PropertySet satellite) {
   1.118 +        satellites.put(keyClass, satellite);
   1.119 +    }
   1.120 +
   1.121 +    @Override
   1.122 +    public void removeSatellite(PropertySet satellite) {
   1.123 +        satellites.remove(satellite.getClass());
   1.124 +    }
   1.125 +
   1.126 +    public void copySatelliteInto(@NotNull DistributedPropertySet r) {
   1.127 +        for (Map.Entry<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet> entry : satellites.entrySet()) {
   1.128 +            r.addSatellite(entry.getKey(), entry.getValue());
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    @Override
   1.133 +    public void copySatelliteInto(MessageContext r) {
   1.134 +        copySatelliteInto((DistributedPropertySet)r);
   1.135 +    }
   1.136 +
   1.137 +    @Override
   1.138 +    public @Nullable <T extends com.oracle.webservices.internal.api.message.PropertySet> T getSatellite(Class<T> satelliteClass) {
   1.139 +        T satellite = (T) satellites.get(satelliteClass);
   1.140 +        if (satellite != null) {
   1.141 +            return satellite;
   1.142 +        }
   1.143 +
   1.144 +        for (PropertySet child : satellites.values()) {
   1.145 +            if (satelliteClass.isInstance(child)) {
   1.146 +                return satelliteClass.cast(child);
   1.147 +            }
   1.148 +
   1.149 +            if (DistributedPropertySet.class.isInstance(child)) {
   1.150 +                satellite = DistributedPropertySet.class.cast(child).getSatellite(satelliteClass);
   1.151 +                if (satellite != null) {
   1.152 +                    return satellite;
   1.153 +                }
   1.154 +            }
   1.155 +        }
   1.156 +        return null;
   1.157 +    }
   1.158 +
   1.159 +    @Override
   1.160 +    public Map<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, com.oracle.webservices.internal.api.message.PropertySet> getSatellites() {
   1.161 +        return satellites;
   1.162 +    }
   1.163 +
   1.164 +    @Override
   1.165 +    public Object get(Object key) {
   1.166 +        // check satellites
   1.167 +        for (PropertySet child : satellites.values()) {
   1.168 +            if (child.supports(key)) {
   1.169 +                return child.get(key);
   1.170 +            }
   1.171 +        }
   1.172 +
   1.173 +        // otherwise it must be the master
   1.174 +        return super.get(key);
   1.175 +    }
   1.176 +
   1.177 +    @Override
   1.178 +    public Object put(String key, Object value) {
   1.179 +        // check satellites
   1.180 +        for (PropertySet child : satellites.values()) {
   1.181 +            if(child.supports(key)) {
   1.182 +                return child.put(key,value);
   1.183 +            }
   1.184 +        }
   1.185 +
   1.186 +        // otherwise it must be the master
   1.187 +        return super.put(key,value);
   1.188 +    }
   1.189 +
   1.190 +    @Override
   1.191 +    public boolean containsKey(Object key) {
   1.192 +        if (viewthis.containsKey(key))
   1.193 +            return true;
   1.194 +        for (PropertySet child : satellites.values()) {
   1.195 +            if (child.containsKey(key)) {
   1.196 +                return true;
   1.197 +            }
   1.198 +        }
   1.199 +        return false;
   1.200 +    }
   1.201 +
   1.202 +    @Override
   1.203 +    public boolean supports(Object key) {
   1.204 +        // check satellites
   1.205 +        for (PropertySet child : satellites.values()) {
   1.206 +            if (child.supports(key)) {
   1.207 +                return true;
   1.208 +            }
   1.209 +        }
   1.210 +
   1.211 +        return super.supports(key);
   1.212 +    }
   1.213 +
   1.214 +    @Override
   1.215 +    public Object remove(Object key) {
   1.216 +        // check satellites
   1.217 +        for (PropertySet child : satellites.values()) {
   1.218 +            if (child.supports(key)) {
   1.219 +                return child.remove(key);
   1.220 +            }
   1.221 +        }
   1.222 +
   1.223 +        return super.remove(key);
   1.224 +    }
   1.225 +
   1.226 +    @Override
   1.227 +    protected void createEntrySet(Set<Entry<String, Object>> core) {
   1.228 +        super.createEntrySet(core);
   1.229 +        for (PropertySet child : satellites.values()) {
   1.230 +            ((BasePropertySet) child).createEntrySet(core);
   1.231 +        }
   1.232 +    }
   1.233 +
   1.234 +    protected Map<String, Object> asMapLocal() {
   1.235 +        return viewthis;
   1.236 +    }
   1.237 +
   1.238 +    protected boolean supportsLocal(Object key) {
   1.239 +        return super.supports(key);
   1.240 +    }
   1.241 +
   1.242 +    class DistributedMapView extends AbstractMap<String, Object> {
   1.243 +        @Override
   1.244 +        public Object get(Object key) {
   1.245 +            for (PropertySet child : satellites.values()) {
   1.246 +                if (child.supports(key)) {
   1.247 +                    return child.get(key);
   1.248 +                }
   1.249 +            }
   1.250 +
   1.251 +            return viewthis.get(key);
   1.252 +        }
   1.253 +
   1.254 +        @Override
   1.255 +        public int size() {
   1.256 +            int size = viewthis.size();
   1.257 +            for (PropertySet child : satellites.values()) {
   1.258 +                size += child.asMap().size();
   1.259 +            }
   1.260 +            return size;
   1.261 +        }
   1.262 +
   1.263 +        @Override
   1.264 +        public boolean containsKey(Object key) {
   1.265 +            if (viewthis.containsKey(key))
   1.266 +                return true;
   1.267 +            for (PropertySet child : satellites.values()) {
   1.268 +                if (child.containsKey(key))
   1.269 +                    return true;
   1.270 +            }
   1.271 +            return false;
   1.272 +        }
   1.273 +
   1.274 +        @Override
   1.275 +        public Set<Entry<String, Object>> entrySet() {
   1.276 +            Set<Entry<String, Object>> entries = new HashSet<Entry<String, Object>>();
   1.277 +            for (PropertySet child : satellites.values()) {
   1.278 +                for (Entry<String,Object> entry : child.asMap().entrySet()) {
   1.279 +                    // the code below is here to avoid entries.addAll(child.asMap().entrySet()); which works differently on JDK6/7
   1.280 +                    // see DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS
   1.281 +                    entries.add(new SimpleImmutableEntry<String, Object>(entry.getKey(), entry.getValue()));
   1.282 +                }
   1.283 +            }
   1.284 +            for (Entry<String,Object> entry : viewthis.entrySet()) {
   1.285 +                // the code below is here to avoid entries.addAll(child.asMap().entrySet()); which works differently on JDK6/7
   1.286 +                // see DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS
   1.287 +                entries.add(new SimpleImmutableEntry<String, Object>(entry.getKey(), entry.getValue()));
   1.288 +            }
   1.289 +
   1.290 +            return entries;
   1.291 +        }
   1.292 +
   1.293 +        @Override
   1.294 +        public Object put(String key, Object value) {
   1.295 +            for (PropertySet child : satellites.values()) {
   1.296 +                if (child.supports(key)) {
   1.297 +                    return child.put(key, value);
   1.298 +                }
   1.299 +            }
   1.300 +
   1.301 +            return viewthis.put(key, value);
   1.302 +        }
   1.303 +
   1.304 +        @Override
   1.305 +        public void clear() {
   1.306 +            satellites.clear();
   1.307 +            viewthis.clear();
   1.308 +        }
   1.309 +
   1.310 +        @Override
   1.311 +        public Object remove(Object key) {
   1.312 +            for (PropertySet child : satellites.values()) {
   1.313 +                if (child.supports(key)) {
   1.314 +                    return child.remove(key);
   1.315 +                }
   1.316 +            }
   1.317 +
   1.318 +            return viewthis.remove(key);
   1.319 +        }
   1.320 +    }
   1.321 +
   1.322 +    @Override
   1.323 +    protected Map<String, Object> createView() {
   1.324 +        return new DistributedMapView();
   1.325 +    }
   1.326 +}

mercurial