0N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.management.openmbean;
0N/A
0N/Aimport com.sun.jmx.mbeanserver.MXBeanLookup;
1790N/Aimport com.sun.jmx.mbeanserver.MXBeanMapping;
1790N/Aimport com.sun.jmx.mbeanserver.MXBeanMappingFactory;
353N/Aimport com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory;
0N/Aimport java.lang.reflect.InvocationHandler;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.lang.reflect.Proxy;
0N/A
0N/A/**
0N/A <p>An {@link InvocationHandler} that forwards getter methods to a
0N/A {@link CompositeData}. If you have an interface that contains
0N/A only getter methods (such as {@code String getName()} or
0N/A {@code boolean isActive()}) then you can use this class in
0N/A conjunction with the {@link Proxy} class to produce an implementation
0N/A of the interface where each getter returns the value of the
0N/A corresponding item in a {@code CompositeData}.</p>
0N/A
0N/A <p>For example, suppose you have an interface like this:
0N/A
0N/A <blockquote>
0N/A <pre>
0N/A public interface NamedNumber {
0N/A public int getNumber();
0N/A public String getName();
0N/A }
0N/A </pre>
0N/A </blockquote>
0N/A
0N/A and a {@code CompositeData} constructed like this:
0N/A
0N/A <blockquote>
0N/A <pre>
0N/A CompositeData cd =
0N/A new {@link CompositeDataSupport}(
0N/A someCompositeType,
0N/A new String[] {"number", "name"},
0N/A new Object[] {<b>5</b>, "five"}
0N/A );
0N/A </pre>
0N/A </blockquote>
0N/A
0N/A then you can construct an object implementing {@code NamedNumber}
0N/A and backed by the object {@code cd} like this:
0N/A
0N/A <blockquote>
0N/A <pre>
0N/A InvocationHandler handler =
0N/A new CompositeDataInvocationHandler(cd);
0N/A NamedNumber nn = (NamedNumber)
0N/A Proxy.newProxyInstance(NamedNumber.class.getClassLoader(),
0N/A new Class[] {NamedNumber.class},
0N/A handler);
0N/A </pre>
0N/A </blockquote>
0N/A
0N/A A call to {@code nn.getNumber()} will then return <b>5</b>.</p>
0N/A
0N/A <p>If the first letter of the property defined by a getter is a
0N/A capital, then this handler will look first for an item in the
0N/A {@code CompositeData} beginning with a capital, then, if that is
0N/A not found, for an item beginning with the corresponding lowercase
0N/A letter or code point. For a getter called {@code getNumber()}, the
0N/A handler will first look for an item called {@code Number}, then for
0N/A {@code number}. If the getter is called {@code getnumber()}, then
0N/A the item must be called {@code number}.</p>
0N/A
0N/A <p>If the method given to {@link #invoke invoke} is the method
0N/A {@code boolean equals(Object)} inherited from {@code Object}, then
0N/A it will return true if and only if the argument is a {@code Proxy}
0N/A whose {@code InvocationHandler} is also a {@code
0N/A CompositeDataInvocationHandler} and whose backing {@code
0N/A CompositeData} is equal (not necessarily identical) to this
0N/A object's. If the method given to {@code invoke} is the method
0N/A {@code int hashCode()} inherited from {@code Object}, then it will
0N/A return a value that is consistent with this definition of {@code
0N/A equals}: if two objects are equal according to {@code equals}, then
0N/A they will have the same {@code hashCode}.</p>
0N/A
0N/A @since 1.6
0N/A*/
0N/Apublic class CompositeDataInvocationHandler implements InvocationHandler {
0N/A /**
0N/A <p>Construct a handler backed by the given {@code
0N/A CompositeData}.</p>
0N/A
0N/A @param compositeData the {@code CompositeData} that will supply
0N/A information to getters.
0N/A
0N/A @throws IllegalArgumentException if {@code compositeData}
0N/A is null.
0N/A */
0N/A public CompositeDataInvocationHandler(CompositeData compositeData) {
1790N/A this(compositeData, null);
0N/A }
0N/A
0N/A /**
0N/A <p>Construct a handler backed by the given {@code
0N/A CompositeData}.</p>
0N/A
0N/A @param mbsc the {@code MBeanServerConnection} related to this
0N/A {@code CompositeData}. This is only relevant if a method in
0N/A the interface for which this is an invocation handler returns
0N/A a type that is an MXBean interface. Otherwise, it can be null.
0N/A
0N/A @param compositeData the {@code CompositeData} that will supply
0N/A information to getters.
0N/A
0N/A @throws IllegalArgumentException if {@code compositeData}
0N/A is null.
0N/A */
0N/A CompositeDataInvocationHandler(CompositeData compositeData,
0N/A MXBeanLookup lookup) {
0N/A if (compositeData == null)
0N/A throw new IllegalArgumentException("compositeData");
0N/A this.compositeData = compositeData;
0N/A this.lookup = lookup;
0N/A }
0N/A
0N/A /**
0N/A Return the {@code CompositeData} that was supplied to the
0N/A constructor.
0N/A @return the {@code CompositeData} that this handler is backed
0N/A by. This is never null.
0N/A */
0N/A public CompositeData getCompositeData() {
0N/A assert compositeData != null;
0N/A return compositeData;
0N/A }
0N/A
0N/A public Object invoke(Object proxy, Method method, Object[] args)
0N/A throws Throwable {
0N/A final String methodName = method.getName();
0N/A
0N/A // Handle the methods from java.lang.Object
0N/A if (method.getDeclaringClass() == Object.class) {
0N/A if (methodName.equals("toString") && args == null)
0N/A return "Proxy[" + compositeData + "]";
0N/A else if (methodName.equals("hashCode") && args == null)
0N/A return compositeData.hashCode() + 0x43444948;
0N/A else if (methodName.equals("equals") && args.length == 1
0N/A && method.getParameterTypes()[0] == Object.class)
0N/A return equals(proxy, args[0]);
0N/A else {
0N/A /* Either someone is calling invoke by hand, or
0N/A it is a non-final method from Object overriden
0N/A by the generated Proxy. At the time of writing,
0N/A the only non-final methods in Object that are not
0N/A handled above are finalize and clone, and these
0N/A are not overridden in generated proxies. */
6287N/A // this plain Method.invoke is called only if the declaring class
6287N/A // is Object and so it's safe.
0N/A return method.invoke(this, args);
0N/A }
0N/A }
0N/A
353N/A String propertyName = DefaultMXBeanMappingFactory.propertyName(method);
0N/A if (propertyName == null) {
0N/A throw new IllegalArgumentException("Method is not getter: " +
0N/A method.getName());
0N/A }
0N/A Object openValue;
0N/A if (compositeData.containsKey(propertyName))
0N/A openValue = compositeData.get(propertyName);
0N/A else {
353N/A String decap = DefaultMXBeanMappingFactory.decapitalize(propertyName);
0N/A if (compositeData.containsKey(decap))
0N/A openValue = compositeData.get(decap);
0N/A else {
0N/A final String msg =
0N/A "No CompositeData item " + propertyName +
0N/A (decap.equals(propertyName) ? "" : " or " + decap) +
0N/A " to match " + methodName;
0N/A throw new IllegalArgumentException(msg);
0N/A }
0N/A }
353N/A MXBeanMapping mapping =
1790N/A MXBeanMappingFactory.DEFAULT.mappingForType(method.getGenericReturnType(),
353N/A MXBeanMappingFactory.DEFAULT);
353N/A return mapping.fromOpenValue(openValue);
0N/A }
0N/A
0N/A /* This method is called when equals(Object) is
0N/A * called on our proxy and hence forwarded to us. For example, if we
0N/A * are a proxy for an interface like this:
0N/A * public interface GetString {
0N/A * public String string();
0N/A * }
0N/A * then we must compare equal to another CompositeDataInvocationHandler
0N/A * proxy for the same interface and where string() returns the same value.
0N/A *
0N/A * You might think that we should also compare equal to another
0N/A * object that implements GetString directly rather than using
0N/A * Proxy, provided that its string() returns the same result as
0N/A * ours, and in fact an earlier version of this class did that (by
0N/A * converting the other object into a CompositeData and comparing
0N/A * that with ours). But in fact that doesn't make a great deal of
0N/A * sense because there's absolutely no guarantee that the
0N/A * resulting equals would be reflexive (otherObject.equals(this)
0N/A * might be false even if this.equals(otherObject) is true), and,
0N/A * especially, there's no way we could generate a hashCode() that
0N/A * would be equal to otherObject.hashCode() when
0N/A * this.equals(otherObject), because we don't know how
0N/A * otherObject.hashCode() is computed.
0N/A */
0N/A private boolean equals(Object proxy, Object other) {
0N/A if (other == null)
0N/A return false;
0N/A
686N/A final Class<?> proxyClass = proxy.getClass();
686N/A final Class<?> otherClass = other.getClass();
0N/A if (proxyClass != otherClass)
0N/A return false;
0N/A InvocationHandler otherih = Proxy.getInvocationHandler(other);
0N/A if (!(otherih instanceof CompositeDataInvocationHandler))
0N/A return false;
0N/A CompositeDataInvocationHandler othercdih =
0N/A (CompositeDataInvocationHandler) otherih;
0N/A return compositeData.equals(othercdih.compositeData);
0N/A }
0N/A
0N/A private final CompositeData compositeData;
0N/A private final MXBeanLookup lookup;
0N/A}