jdk/test/javax/management/mxbean/customtypes/IntegerIsLongFactory.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     1 /*
       
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 // IntegerIsLongFactory.java - see CustomTypeTest
       
    25 
       
    26 package customtypes;
       
    27 
       
    28 import java.io.InvalidObjectException;
       
    29 import java.lang.reflect.Type;
       
    30 import javax.management.openmbean.MXBeanMapping;
       
    31 import javax.management.openmbean.MXBeanMappingFactory;
       
    32 import javax.management.openmbean.OpenDataException;
       
    33 import javax.management.openmbean.SimpleType;
       
    34 
       
    35 public class IntegerIsLongFactory implements MXBeanMappingFactory {
       
    36     public MXBeanMapping forType(Type t, MXBeanMappingFactory f)
       
    37     throws OpenDataException {
       
    38         if (t == Integer.class)
       
    39             return IntegerIsLongMapping;
       
    40         else
       
    41             return MXBeanMappingFactory.DEFAULT.forType(t, f);
       
    42     }
       
    43 
       
    44     private static final MXBeanMapping IntegerIsLongMapping =
       
    45             new IntegerIsLongMapping();
       
    46 
       
    47     private static class IntegerIsLongMapping extends MXBeanMapping {
       
    48         IntegerIsLongMapping() {
       
    49             super(Integer.class, SimpleType.STRING);
       
    50         }
       
    51 
       
    52         public Object fromOpenValue(Object openValue)
       
    53         throws InvalidObjectException {
       
    54             try {
       
    55                 return (Long) openValue;
       
    56             } catch (Exception e) {
       
    57                 InvalidObjectException ioe = new InvalidObjectException("oops");
       
    58                 ioe.initCause(e);
       
    59                 throw ioe;
       
    60             }
       
    61         }
       
    62 
       
    63         public Object toOpenValue(Object javaValue) throws OpenDataException {
       
    64             try {
       
    65                 Integer i = (Integer) javaValue;
       
    66                 return new Long((int) i);
       
    67             } catch (Exception e) {
       
    68                 OpenDataException ode = new OpenDataException("oops");
       
    69                 ode.initCause(e);
       
    70                 throw ode;
       
    71             }
       
    72         }
       
    73     }
       
    74 }