src/share/jaxws_classes/com/sun/xml/internal/ws/api/PropertySet.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.api;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.istack.internal.Nullable;
ohair@286 30 import com.sun.xml.internal.ws.util.ReadOnlyPropertyException;
ohair@286 31
ohair@286 32 import java.lang.reflect.Field;
ohair@286 33 import java.lang.reflect.InvocationTargetException;
ohair@286 34 import java.lang.reflect.Method;
ohair@286 35 import java.security.AccessController;
ohair@286 36 import java.security.PrivilegedAction;
ohair@286 37 import java.util.AbstractMap;
ohair@286 38 import java.util.HashMap;
ohair@286 39 import java.util.HashSet;
ohair@286 40 import java.util.Map;
ohair@286 41 import java.util.Map.Entry;
ohair@286 42 import java.util.Set;
ohair@286 43
ohair@286 44 /**
ohair@286 45 * A set of "properties" that can be accessed via strongly-typed fields
ohair@286 46 * as well as reflexibly through the property name.
ohair@286 47 *
ohair@286 48 * @author Kohsuke Kawaguchi
ohair@286 49 */
ohair@286 50 @SuppressWarnings("SuspiciousMethodCalls")
ohair@286 51 public abstract class PropertySet implements com.sun.xml.internal.org.jvnet.ws.message.PropertySet {
ohair@286 52
ohair@286 53 /**
ohair@286 54 * Creates a new instance of TypedMap.
ohair@286 55 */
ohair@286 56 protected PropertySet() {}
ohair@286 57
ohair@286 58 /**
ohair@286 59 * Represents the list of strongly-typed known propertyies
ohair@286 60 * (keyed by property names.)
ohair@286 61 *
ohair@286 62 * <p>
ohair@286 63 * Just giving it an alias to make the use of this class more fool-proof.
ohair@286 64 */
ohair@286 65 protected static final class PropertyMap extends HashMap<String,Accessor> {}
ohair@286 66
ohair@286 67 /**
ohair@286 68 * Map representing the Fields and Methods annotated with {@link Property}.
ohair@286 69 * Model of {@link PropertySet} class.
ohair@286 70 *
ohair@286 71 * <p>
ohair@286 72 * At the end of the derivation chain this method just needs to be implemented
ohair@286 73 * as:
ohair@286 74 *
ohair@286 75 * <pre>
ohair@286 76 * private static final PropertyMap model;
ohair@286 77 * static {
ohair@286 78 * model = parse(MyDerivedClass.class);
ohair@286 79 * }
ohair@286 80 * protected PropertyMap getPropertyMap() {
ohair@286 81 * return model;
ohair@286 82 * }
ohair@286 83 * </pre>
ohair@286 84 */
ohair@286 85 protected abstract PropertyMap getPropertyMap();
ohair@286 86
ohair@286 87 // maybe we can use this some time
ohair@286 88 ///**
ohair@286 89 // * If all the properties defined on this {@link PropertySet} has a certain prefix
ohair@286 90 // * (such as, say, "javax.xml.ws.http."), then return it.
ohair@286 91 // *
ohair@286 92 // * <p>
ohair@286 93 // * Returning a non-null name from this method allows methods like
ohair@286 94 // * {@link #get(Object)} and {@link #put(String, Object)} to work faster.
ohair@286 95 // * This is especially so with {@link DistributedPropertySet}, so implementations
ohair@286 96 // * are encouraged to set a common prefix, as much as possible.
ohair@286 97 // *
ohair@286 98 // * <p>
ohair@286 99 // * Currently, this is used only by {@link DistributedPropertySet}.
ohair@286 100 // *
ohair@286 101 // * @return
ohair@286 102 // * Null if no such common prefix exists. Otherwise string like
ohair@286 103 // * "javax.xml.ws.http." (the dot at the last is usually preferrable,
ohair@286 104 // * so that properties like "javax.xml.ws.https.something" won't match.
ohair@286 105 // */
ohair@286 106 //protected abstract String getPropertyPrefix();
ohair@286 107
ohair@286 108 /**
ohair@286 109 * This method parses a class for fields and methods with {@link Property}.
ohair@286 110 */
ohair@286 111 protected static PropertyMap parse(final Class clazz) {
ohair@286 112 // make all relevant fields and methods accessible.
ohair@286 113 // this allows runtime to skip the security check, so they runs faster.
ohair@286 114 return AccessController.doPrivileged(new PrivilegedAction<PropertyMap>() {
ohair@286 115 public PropertyMap run() {
ohair@286 116 PropertyMap props = new PropertyMap();
ohair@286 117 for( Class c=clazz; c!=null; c=c.getSuperclass()) {
ohair@286 118 for (Field f : c.getDeclaredFields()) {
ohair@286 119 Property cp = f.getAnnotation(Property.class);
ohair@286 120 if(cp!=null) {
ohair@286 121 for(String value : cp.value()) {
ohair@286 122 props.put(value, new FieldAccessor(f, value));
ohair@286 123 }
ohair@286 124 }
ohair@286 125 }
ohair@286 126 for (Method m : c.getDeclaredMethods()) {
ohair@286 127 Property cp = m.getAnnotation(Property.class);
ohair@286 128 if(cp!=null) {
ohair@286 129 String name = m.getName();
ohair@286 130 assert name.startsWith("get") || name.startsWith("is");
ohair@286 131
ohair@286 132 String setName = name.startsWith("is") ? "set"+name.substring(3) : // isFoo -> setFoo
ohair@286 133 's'+name.substring(1); // getFoo -> setFoo
ohair@286 134 Method setter;
ohair@286 135 try {
ohair@286 136 setter = clazz.getMethod(setName,m.getReturnType());
ohair@286 137 } catch (NoSuchMethodException e) {
ohair@286 138 setter = null; // no setter
ohair@286 139 }
ohair@286 140 for(String value : cp.value()) {
ohair@286 141 props.put(value, new MethodAccessor(m, setter, value));
ohair@286 142 }
ohair@286 143 }
ohair@286 144 }
ohair@286 145 }
ohair@286 146
ohair@286 147 return props;
ohair@286 148 }
ohair@286 149 });
ohair@286 150 }
ohair@286 151
ohair@286 152 /**
ohair@286 153 * Represents a typed property defined on a {@link PropertySet}.
ohair@286 154 */
ohair@286 155 protected interface Accessor {
ohair@286 156 String getName();
ohair@286 157 boolean hasValue(PropertySet props);
ohair@286 158 Object get(PropertySet props);
ohair@286 159 void set(PropertySet props, Object value);
ohair@286 160 }
ohair@286 161
ohair@286 162 static final class FieldAccessor implements Accessor {
ohair@286 163 /**
ohair@286 164 * Field with the annotation.
ohair@286 165 */
ohair@286 166 private final Field f;
ohair@286 167
ohair@286 168 /**
ohair@286 169 * One of the values in {@link Property} annotation on {@link #f}.
ohair@286 170 */
ohair@286 171 private final String name;
ohair@286 172
ohair@286 173 protected FieldAccessor(Field f, String name) {
ohair@286 174 this.f = f;
ohair@286 175 f.setAccessible(true);
ohair@286 176 this.name = name;
ohair@286 177 }
ohair@286 178
ohair@286 179 public String getName() {
ohair@286 180 return name;
ohair@286 181 }
ohair@286 182
ohair@286 183 public boolean hasValue(PropertySet props) {
ohair@286 184 return get(props)!=null;
ohair@286 185 }
ohair@286 186
ohair@286 187 public Object get(PropertySet props) {
ohair@286 188 try {
ohair@286 189 return f.get(props);
ohair@286 190 } catch (IllegalAccessException e) {
ohair@286 191 throw new AssertionError();
ohair@286 192 }
ohair@286 193 }
ohair@286 194
ohair@286 195 public void set(PropertySet props, Object value) {
ohair@286 196 try {
ohair@286 197 f.set(props,value);
ohair@286 198 } catch (IllegalAccessException e) {
ohair@286 199 throw new AssertionError();
ohair@286 200 }
ohair@286 201 }
ohair@286 202 }
ohair@286 203
ohair@286 204 static final class MethodAccessor implements Accessor {
ohair@286 205 /**
ohair@286 206 * Getter method.
ohair@286 207 */
ohair@286 208 private final @NotNull Method getter;
ohair@286 209 /**
ohair@286 210 * Setter method.
ohair@286 211 * Some property is read-only.
ohair@286 212 */
ohair@286 213 private final @Nullable Method setter;
ohair@286 214
ohair@286 215 /**
ohair@286 216 * One of the values in {@link Property} annotation on {@link #getter}.
ohair@286 217 */
ohair@286 218 private final String name;
ohair@286 219
ohair@286 220 protected MethodAccessor(Method getter, Method setter, String value) {
ohair@286 221 this.getter = getter;
ohair@286 222 this.setter = setter;
ohair@286 223 this.name = value;
ohair@286 224 getter.setAccessible(true);
ohair@286 225 if(setter!=null)
ohair@286 226 setter.setAccessible(true);
ohair@286 227 }
ohair@286 228
ohair@286 229 public String getName() {
ohair@286 230 return name;
ohair@286 231 }
ohair@286 232
ohair@286 233 public boolean hasValue(PropertySet props) {
ohair@286 234 return get(props)!=null;
ohair@286 235 }
ohair@286 236
ohair@286 237 public Object get(PropertySet props) {
ohair@286 238 try {
ohair@286 239 return getter.invoke(props);
ohair@286 240 } catch (IllegalAccessException e) {
ohair@286 241 throw new AssertionError();
ohair@286 242 } catch (InvocationTargetException e) {
ohair@286 243 handle(e);
ohair@286 244 return 0; // never reach here
ohair@286 245 }
ohair@286 246 }
ohair@286 247
ohair@286 248 public void set(PropertySet props, Object value) {
ohair@286 249 if(setter==null)
ohair@286 250 throw new ReadOnlyPropertyException(getName());
ohair@286 251 try {
ohair@286 252 setter.invoke(props,value);
ohair@286 253 } catch (IllegalAccessException e) {
ohair@286 254 throw new AssertionError();
ohair@286 255 } catch (InvocationTargetException e) {
ohair@286 256 handle(e);
ohair@286 257 }
ohair@286 258 }
ohair@286 259
ohair@286 260 /**
ohair@286 261 * Since we don't expect the getter/setter to throw a checked exception,
ohair@286 262 * it should be possible to make the exception propagation transparent.
ohair@286 263 * That's what we are trying to do here.
ohair@286 264 */
ohair@286 265 private Exception handle(InvocationTargetException e) {
ohair@286 266 Throwable t = e.getTargetException();
ohair@286 267 if(t instanceof Error)
ohair@286 268 throw (Error)t;
ohair@286 269 if(t instanceof RuntimeException)
ohair@286 270 throw (RuntimeException)t;
ohair@286 271 throw new Error(e);
ohair@286 272 }
ohair@286 273 }
ohair@286 274
ohair@286 275
ohair@286 276 public final boolean containsKey(Object key) {
ohair@286 277 return get(key)!=null;
ohair@286 278 }
ohair@286 279
ohair@286 280 /**
ohair@286 281 * Gets the name of the property.
ohair@286 282 *
ohair@286 283 * @param key
ohair@286 284 * This field is typed as {@link Object} to follow the {@link Map#get(Object)}
ohair@286 285 * convention, but if anything but {@link String} is passed, this method
ohair@286 286 * just returns null.
ohair@286 287 */
ohair@286 288 public Object get(Object key) {
ohair@286 289 Accessor sp = getPropertyMap().get(key);
ohair@286 290 if(sp!=null)
ohair@286 291 return sp.get(this);
ohair@286 292 throw new IllegalArgumentException("Undefined property "+key);
ohair@286 293 }
ohair@286 294
ohair@286 295 /**
ohair@286 296 * Sets a property.
ohair@286 297 *
ohair@286 298 * <h3>Implementation Note</h3>
ohair@286 299 * This method is slow. Code inside JAX-WS should define strongly-typed
ohair@286 300 * fields in this class and access them directly, instead of using this.
ohair@286 301 *
ohair@286 302 * @throws ReadOnlyPropertyException
ohair@286 303 * if the given key is an alias of a strongly-typed field,
ohair@286 304 * and if the name object given is not assignable to the field.
ohair@286 305 *
ohair@286 306 * @see Property
ohair@286 307 */
ohair@286 308 public Object put(String key, Object value) {
ohair@286 309 Accessor sp = getPropertyMap().get(key);
ohair@286 310 if(sp!=null) {
ohair@286 311 Object old = sp.get(this);
ohair@286 312 sp.set(this,value);
ohair@286 313 return old;
ohair@286 314 } else {
ohair@286 315 throw new IllegalArgumentException("Undefined property "+key);
ohair@286 316 }
ohair@286 317 }
ohair@286 318
ohair@286 319 /**
ohair@286 320 * Checks if this {@link PropertySet} supports a property of the given name.
ohair@286 321 */
ohair@286 322 public boolean supports(Object key) {
ohair@286 323 return getPropertyMap().containsKey(key);
ohair@286 324 }
ohair@286 325
ohair@286 326 public Object remove(Object key) {
ohair@286 327 Accessor sp = getPropertyMap().get(key);
ohair@286 328 if(sp!=null) {
ohair@286 329 Object old = sp.get(this);
ohair@286 330 sp.set(this,null);
ohair@286 331 return old;
ohair@286 332 } else {
ohair@286 333 throw new IllegalArgumentException("Undefined property "+key);
ohair@286 334 }
ohair@286 335 }
ohair@286 336
ohair@286 337
ohair@286 338 /**
ohair@286 339 * Lazily created view of {@link Property}s that
ohair@286 340 * forms the core of {@link #createMapView()}.
ohair@286 341 */
ohair@286 342 /*package*/ Set<Entry<String,Object>> mapViewCore;
ohair@286 343
ohair@286 344 /**
ohair@286 345 * Creates a {@link Map} view of this {@link PropertySet}.
ohair@286 346 *
ohair@286 347 * <p>
ohair@286 348 * This map is partially live, in the sense that values you set to it
ohair@286 349 * will be reflected to {@link PropertySet}.
ohair@286 350 *
ohair@286 351 * <p>
ohair@286 352 * However, this map may not pick up changes made
ohair@286 353 * to {@link PropertySet} after the view is created.
ohair@286 354 *
ohair@286 355 * @return
ohair@286 356 * always non-null valid instance.
ohair@286 357 */
ohair@286 358 public final Map<String,Object> createMapView() {
ohair@286 359 final Set<Entry<String,Object>> core = new HashSet<Entry<String,Object>>();
ohair@286 360 createEntrySet(core);
ohair@286 361
ohair@286 362 return new AbstractMap<String, Object>() {
ohair@286 363 public Set<Entry<String,Object>> entrySet() {
ohair@286 364 return core;
ohair@286 365 }
ohair@286 366 };
ohair@286 367 }
ohair@286 368
ohair@286 369 /*package*/ void createEntrySet(Set<Entry<String,Object>> core) {
ohair@286 370 for (final Entry<String, Accessor> e : getPropertyMap().entrySet()) {
ohair@286 371 core.add(new Entry<String, Object>() {
ohair@286 372 public String getKey() {
ohair@286 373 return e.getKey();
ohair@286 374 }
ohair@286 375
ohair@286 376 public Object getValue() {
ohair@286 377 return e.getValue().get(PropertySet.this);
ohair@286 378 }
ohair@286 379
ohair@286 380 public Object setValue(Object value) {
ohair@286 381 Accessor acc = e.getValue();
ohair@286 382 Object old = acc.get(PropertySet.this);
ohair@286 383 acc.set(PropertySet.this,value);
ohair@286 384 return old;
ohair@286 385 }
ohair@286 386 });
ohair@286 387 }
ohair@286 388 }
ohair@286 389 }

mercurial