001    /* CompositeDataInvocationHandler.java - Pseudo-accessors for CompositeData.
002       Copyright (C) 2007 Free Software Foundation
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.management.openmbean;
039    
040    import java.lang.reflect.InvocationHandler;
041    import java.lang.reflect.Method;
042    import java.lang.reflect.Proxy;
043    
044    /**
045     * <p>
046     * Provides an {@link java.lang.reflect.InvocationHandler} which
047     * implements a series of accessor methods (those beginning with
048     * {@code "get"} or {@code "is"}) using the content of a
049     * {@link CompositeData} object.  An instance of {@link CompositeData}
050     * consists of a series of key-value mappings.  This handler assumes
051     * these keys to be the names of attributes, and thus provides
052     * accessor methods by returning the associated value.  
053     * </p>
054     * <p>
055     * As an example, consider the following interface:
056     * </p>
057     * <pre>
058     * public interface Person
059     * {
060     *   public String getName();
061     *   public Date getBirthday();
062     * }
063     * </pre>
064     * <p>
065     * This specifies two accessor methods for retrieving the attributes,
066     * {@code name} and {@code birthday}.  An implementation of this interface
067     * can be provided by creating an instance of this class, using a
068     * {@link CompositeData} object with appropriate key-value mappings
069     * (e.g. "name" => "Fred", "birthday" => 30/06/1974), and then passing
070     * that to {@link java.lang.reflect.Proxy#newProxyInstance} along with
071     * the interface itself.  The invocation handler implements the methods
072     * by calling {@link CompositeData#get(String)} with the appropriate key.
073     * </p>
074     * <p>
075     * The attribute name is derived by taking the remainder of the method
076     * name following {@code "get"}.  If the first letter of this substring
077     * is uppercase, then two attempts are made to retrieve the attribute
078     * from the {@link CompositeData} instance: one using the original substring,
079     * and one with the first letter changed to its lower-case equivalent.
080     * If the first letter is lowercase, only the exact substring is used.
081     * </p>
082     * <p>
083     * An {@link Object#equals(Object)} implementation is provided.  This returns
084     * true if the argument is a proxy with a {@link CompositeDataInvocationHandler}
085     * using an equivalent {@link CompositeData} instance.  {@link Object#hashCode()}
086     * is also defined so as to match this implementation and give equivalent instances
087     * the same hashcode.
088     * </p>
089     *
090     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
091     * @since 1.6
092     */
093    public class CompositeDataInvocationHandler
094      implements InvocationHandler
095    {
096    
097      /**
098       * The {@link CompositeData} providing the key-value mappings.
099       */
100      private CompositeData data;
101    
102      /**
103       * Constructs a new {@link CompositeDataInvocationHandler}
104       * with the specified {@link CompositeData} instance.
105       *
106       * @param data the {@link CompositeData} instance to use.
107       * @throws IllegalArgumentException if {@code data} is {@code null}.
108       */
109      public CompositeDataInvocationHandler(CompositeData data)
110      {
111        if (data == null)
112          throw new IllegalArgumentException("The CompositeData instance " +
113                                             "must be non-null.");
114        this.data = data;
115      }
116    
117      /**
118       * Returns the {@link CompositeData} instance which provides
119       * the key-value mappings for this instance.  This is never
120       * {@code null}.
121       *
122       * @return the {@link CompositeData} instance.
123       */
124      public CompositeData getCompositeData()
125      {
126        return data;
127      }
128    
129      /**
130       * Called by the proxy class whenever a method is called.  The
131       * handler only deals with accessor methods (beginning with
132       * {@code "get"} or {@code "is"}), {@code equals}, and
133       * {@code "hashCode"}.  Accessor methods are implemented by
134       * returning the appropriate value from the {@link CompositeData}
135       * instance, while {@code equals} and {@code hashCode} allow
136       * two proxies with a {@link CompositeDataInvocationHandler} using
137       * the same {@link CompositeData} instance to be classified
138       * as equivalent.
139       *
140       * @param proxy the proxy on which the method was called.
141       * @param method the method which was called.
142       * @param args the arguments supplied to the method.
143       * @return the return value from the method.
144       * @throws Throwable if an exception is thrown in the process.
145       */
146      public Object invoke(Object proxy, Method method, Object[] args)
147        throws Throwable
148      {
149        String mName = method.getName();
150        if (mName.equals("equals"))
151          {
152            if (args[0] instanceof Proxy)
153              {
154                InvocationHandler h = Proxy.getInvocationHandler(args[0]);
155                if (h instanceof CompositeDataInvocationHandler)
156                  return data.equals(((CompositeDataInvocationHandler)
157                                      h).getCompositeData());
158              }
159            return false;
160          }
161        if (mName.equals("hashCode"))
162          {
163            return data.hashCode();
164          }
165        String attrib = null;
166        if (mName.startsWith("get"))
167          attrib = mName.substring(3);
168        else if (mName.startsWith("is"))
169          attrib = mName.substring(2);
170        if (attrib == null)
171          throw new NoSuchMethodException(mName + " is not an accessor.");
172        if (!data.containsKey(attrib))
173          {
174            if (Character.isLowerCase(attrib.charAt(0)))
175              throw new NoSuchMethodException("The attribute " +
176                                              attrib + " is not available " +
177                                              "in the given CompositeData " +
178                                              "object");
179            attrib = Character.toLowerCase(attrib.charAt(0))
180              + attrib.substring(1);
181            if (!data.containsKey(attrib))
182              throw new NoSuchMethodException("The attribute " +
183                                              attrib + " is not available " +
184                                              "in the given CompositeData " +
185                                              "object");
186          }
187        return data.get(attrib);
188      }
189    
190    }