Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.oracle.webservices.internal.api.message; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.message.Packet; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.client.RequestContext; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.client.ResponseContext; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import javax.xml.ws.WebServiceContext; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import java.util.AbstractMap; |
aoqi@0 | 37 | import java.util.Map.Entry; |
aoqi@0 | 38 | import java.util.HashSet; |
aoqi@0 | 39 | import java.util.IdentityHashMap; |
aoqi@0 | 40 | import java.util.Map; |
aoqi@0 | 41 | import java.util.Set; |
aoqi@0 | 42 | |
aoqi@0 | 43 | /** |
aoqi@0 | 44 | * {@link PropertySet} that combines properties exposed from multiple |
aoqi@0 | 45 | * {@link PropertySet}s into one. |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * <p> |
aoqi@0 | 48 | * This implementation allows one {@link PropertySet} to assemble |
aoqi@0 | 49 | * all properties exposed from other "satellite" {@link PropertySet}s. |
aoqi@0 | 50 | * (A satellite may itself be a {@link DistributedPropertySet}, so |
aoqi@0 | 51 | * in general this can form a tree.) |
aoqi@0 | 52 | * |
aoqi@0 | 53 | * <p> |
aoqi@0 | 54 | * This is useful for JAX-WS because the properties we expose to the application |
aoqi@0 | 55 | * are contributed by different pieces, and therefore we'd like each of them |
aoqi@0 | 56 | * to have a separate {@link PropertySet} implementation that backs up |
aoqi@0 | 57 | * the properties. For example, this allows FastInfoset to expose its |
aoqi@0 | 58 | * set of properties to {@link RequestContext} by using a strongly-typed fields. |
aoqi@0 | 59 | * |
aoqi@0 | 60 | * <p> |
aoqi@0 | 61 | * This is also useful for a client-side transport to expose a bunch of properties |
aoqi@0 | 62 | * into {@link ResponseContext}. It simply needs to create a {@link PropertySet} |
aoqi@0 | 63 | * object with methods for each property it wants to expose, and then add that |
aoqi@0 | 64 | * {@link PropertySet} to {@link Packet}. This allows property values to be |
aoqi@0 | 65 | * lazily computed (when actually asked by users), thus improving the performance |
aoqi@0 | 66 | * of the typical case where property values are not asked. |
aoqi@0 | 67 | * |
aoqi@0 | 68 | * <p> |
aoqi@0 | 69 | * A similar benefit applies on the server-side, for a transport to expose |
aoqi@0 | 70 | * a bunch of properties to {@link WebServiceContext}. |
aoqi@0 | 71 | * |
aoqi@0 | 72 | * <p> |
aoqi@0 | 73 | * To achieve these benefits, access to {@link DistributedPropertySet} is slower |
aoqi@0 | 74 | * compared to {@link PropertySet} (such as get/set), while adding a satellite |
aoqi@0 | 75 | * object is relatively fast. |
aoqi@0 | 76 | * |
aoqi@0 | 77 | * @author Kohsuke Kawaguchi |
aoqi@0 | 78 | */ |
aoqi@0 | 79 | public abstract class BaseDistributedPropertySet extends BasePropertySet implements DistributedPropertySet { |
aoqi@0 | 80 | |
aoqi@0 | 81 | /** |
aoqi@0 | 82 | * All {@link PropertySet}s that are bundled into this {@link PropertySet}. |
aoqi@0 | 83 | */ |
aoqi@0 | 84 | private final Map<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet> satellites |
aoqi@0 | 85 | = new IdentityHashMap<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet>(); |
aoqi@0 | 86 | |
aoqi@0 | 87 | private final Map<String, Object> viewthis; |
aoqi@0 | 88 | |
aoqi@0 | 89 | public BaseDistributedPropertySet() { |
aoqi@0 | 90 | this.viewthis = super.createView(); |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | @Override |
aoqi@0 | 94 | public void addSatellite(@NotNull PropertySet satellite) { |
aoqi@0 | 95 | addSatellite(satellite.getClass(), satellite); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | @Override |
aoqi@0 | 99 | public void addSatellite(@NotNull Class<? extends com.oracle.webservices.internal.api.message.PropertySet> keyClass, @NotNull PropertySet satellite) { |
aoqi@0 | 100 | satellites.put(keyClass, satellite); |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | @Override |
aoqi@0 | 104 | public void removeSatellite(PropertySet satellite) { |
aoqi@0 | 105 | satellites.remove(satellite.getClass()); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | public void copySatelliteInto(@NotNull DistributedPropertySet r) { |
aoqi@0 | 109 | for (Map.Entry<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, PropertySet> entry : satellites.entrySet()) { |
aoqi@0 | 110 | r.addSatellite(entry.getKey(), entry.getValue()); |
aoqi@0 | 111 | } |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | @Override |
aoqi@0 | 115 | public void copySatelliteInto(MessageContext r) { |
aoqi@0 | 116 | copySatelliteInto((DistributedPropertySet)r); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | @Override |
aoqi@0 | 120 | public @Nullable <T extends com.oracle.webservices.internal.api.message.PropertySet> T getSatellite(Class<T> satelliteClass) { |
aoqi@0 | 121 | T satellite = (T) satellites.get(satelliteClass); |
aoqi@0 | 122 | if (satellite != null) { |
aoqi@0 | 123 | return satellite; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 127 | if (satelliteClass.isInstance(child)) { |
aoqi@0 | 128 | return satelliteClass.cast(child); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | |
aoqi@0 | 131 | if (DistributedPropertySet.class.isInstance(child)) { |
aoqi@0 | 132 | satellite = DistributedPropertySet.class.cast(child).getSatellite(satelliteClass); |
aoqi@0 | 133 | if (satellite != null) { |
aoqi@0 | 134 | return satellite; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | } |
aoqi@0 | 138 | return null; |
aoqi@0 | 139 | } |
aoqi@0 | 140 | |
aoqi@0 | 141 | @Override |
aoqi@0 | 142 | public Map<Class<? extends com.oracle.webservices.internal.api.message.PropertySet>, com.oracle.webservices.internal.api.message.PropertySet> getSatellites() { |
aoqi@0 | 143 | return satellites; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | @Override |
aoqi@0 | 147 | public Object get(Object key) { |
aoqi@0 | 148 | // check satellites |
aoqi@0 | 149 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 150 | if (child.supports(key)) { |
aoqi@0 | 151 | return child.get(key); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | // otherwise it must be the master |
aoqi@0 | 156 | return super.get(key); |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | @Override |
aoqi@0 | 160 | public Object put(String key, Object value) { |
aoqi@0 | 161 | // check satellites |
aoqi@0 | 162 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 163 | if(child.supports(key)) { |
aoqi@0 | 164 | return child.put(key,value); |
aoqi@0 | 165 | } |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | // otherwise it must be the master |
aoqi@0 | 169 | return super.put(key,value); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | @Override |
aoqi@0 | 173 | public boolean containsKey(Object key) { |
aoqi@0 | 174 | if (viewthis.containsKey(key)) |
aoqi@0 | 175 | return true; |
aoqi@0 | 176 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 177 | if (child.containsKey(key)) { |
aoqi@0 | 178 | return true; |
aoqi@0 | 179 | } |
aoqi@0 | 180 | } |
aoqi@0 | 181 | return false; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | @Override |
aoqi@0 | 185 | public boolean supports(Object key) { |
aoqi@0 | 186 | // check satellites |
aoqi@0 | 187 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 188 | if (child.supports(key)) { |
aoqi@0 | 189 | return true; |
aoqi@0 | 190 | } |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | return super.supports(key); |
aoqi@0 | 194 | } |
aoqi@0 | 195 | |
aoqi@0 | 196 | @Override |
aoqi@0 | 197 | public Object remove(Object key) { |
aoqi@0 | 198 | // check satellites |
aoqi@0 | 199 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 200 | if (child.supports(key)) { |
aoqi@0 | 201 | return child.remove(key); |
aoqi@0 | 202 | } |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | return super.remove(key); |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | @Override |
aoqi@0 | 209 | protected void createEntrySet(Set<Entry<String, Object>> core) { |
aoqi@0 | 210 | super.createEntrySet(core); |
aoqi@0 | 211 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 212 | ((BasePropertySet) child).createEntrySet(core); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | } |
aoqi@0 | 215 | |
aoqi@0 | 216 | protected Map<String, Object> asMapLocal() { |
aoqi@0 | 217 | return viewthis; |
aoqi@0 | 218 | } |
aoqi@0 | 219 | |
aoqi@0 | 220 | protected boolean supportsLocal(Object key) { |
aoqi@0 | 221 | return super.supports(key); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | class DistributedMapView extends AbstractMap<String, Object> { |
aoqi@0 | 225 | @Override |
aoqi@0 | 226 | public Object get(Object key) { |
aoqi@0 | 227 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 228 | if (child.supports(key)) { |
aoqi@0 | 229 | return child.get(key); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | return viewthis.get(key); |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | @Override |
aoqi@0 | 237 | public int size() { |
aoqi@0 | 238 | int size = viewthis.size(); |
aoqi@0 | 239 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 240 | size += child.asMap().size(); |
aoqi@0 | 241 | } |
aoqi@0 | 242 | return size; |
aoqi@0 | 243 | } |
aoqi@0 | 244 | |
aoqi@0 | 245 | @Override |
aoqi@0 | 246 | public boolean containsKey(Object key) { |
aoqi@0 | 247 | if (viewthis.containsKey(key)) |
aoqi@0 | 248 | return true; |
aoqi@0 | 249 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 250 | if (child.containsKey(key)) |
aoqi@0 | 251 | return true; |
aoqi@0 | 252 | } |
aoqi@0 | 253 | return false; |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | @Override |
aoqi@0 | 257 | public Set<Entry<String, Object>> entrySet() { |
aoqi@0 | 258 | Set<Entry<String, Object>> entries = new HashSet<Entry<String, Object>>(); |
aoqi@0 | 259 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 260 | for (Entry<String,Object> entry : child.asMap().entrySet()) { |
aoqi@0 | 261 | // the code below is here to avoid entries.addAll(child.asMap().entrySet()); which works differently on JDK6/7 |
aoqi@0 | 262 | // see DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS |
aoqi@0 | 263 | entries.add(new SimpleImmutableEntry<String, Object>(entry.getKey(), entry.getValue())); |
aoqi@0 | 264 | } |
aoqi@0 | 265 | } |
aoqi@0 | 266 | for (Entry<String,Object> entry : viewthis.entrySet()) { |
aoqi@0 | 267 | // the code below is here to avoid entries.addAll(child.asMap().entrySet()); which works differently on JDK6/7 |
aoqi@0 | 268 | // see DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS |
aoqi@0 | 269 | entries.add(new SimpleImmutableEntry<String, Object>(entry.getKey(), entry.getValue())); |
aoqi@0 | 270 | } |
aoqi@0 | 271 | |
aoqi@0 | 272 | return entries; |
aoqi@0 | 273 | } |
aoqi@0 | 274 | |
aoqi@0 | 275 | @Override |
aoqi@0 | 276 | public Object put(String key, Object value) { |
aoqi@0 | 277 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 278 | if (child.supports(key)) { |
aoqi@0 | 279 | return child.put(key, value); |
aoqi@0 | 280 | } |
aoqi@0 | 281 | } |
aoqi@0 | 282 | |
aoqi@0 | 283 | return viewthis.put(key, value); |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | @Override |
aoqi@0 | 287 | public void clear() { |
aoqi@0 | 288 | satellites.clear(); |
aoqi@0 | 289 | viewthis.clear(); |
aoqi@0 | 290 | } |
aoqi@0 | 291 | |
aoqi@0 | 292 | @Override |
aoqi@0 | 293 | public Object remove(Object key) { |
aoqi@0 | 294 | for (PropertySet child : satellites.values()) { |
aoqi@0 | 295 | if (child.supports(key)) { |
aoqi@0 | 296 | return child.remove(key); |
aoqi@0 | 297 | } |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | return viewthis.remove(key); |
aoqi@0 | 301 | } |
aoqi@0 | 302 | } |
aoqi@0 | 303 | |
aoqi@0 | 304 | @Override |
aoqi@0 | 305 | protected Map<String, Object> createView() { |
aoqi@0 | 306 | return new DistributedMapView(); |
aoqi@0 | 307 | } |
aoqi@0 | 308 | } |