jdk/src/share/classes/com/sun/jmx/namespace/serial/DefaultRewritingProcessor.java
changeset 1156 bbc2d15aaf7a
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
       
     1 /*
       
     2  * Copyright 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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.jmx.namespace.serial;
       
    27 
       
    28 
       
    29 import javax.management.ObjectInstance;
       
    30 import javax.management.ObjectName;
       
    31 
       
    32 /**
       
    33  * Class DefaultRewritingProcessor. Rewrite ObjectName in input & output
       
    34  * parameters.
       
    35  * <p><b>
       
    36  * This API is a Sun internal API and is subject to changes without notice.
       
    37  * </b></p>
       
    38  * @since 1.7
       
    39  */
       
    40 // We know that rewriting using serialization is costly.
       
    41 // This object tries to determine whether an object needs rewriting prior
       
    42 // to rewriting, and rewrites by creating a new object in those cases
       
    43 // where we know how to recreate a new object (e.g. a Notification).
       
    44 // Rewriting is however usually not used - so this object is just a
       
    45 // skeleton that eventually uses serialization...
       
    46 //
       
    47 class DefaultRewritingProcessor extends RewritingProcessor {
       
    48 
       
    49 
       
    50     private static enum RewriteMode {
       
    51         INPUT,  // Input from target to source  (parameters)
       
    52         OUTPUT  // Output from source to target (results)
       
    53     };
       
    54 
       
    55     private final boolean identity;
       
    56 
       
    57     public DefaultRewritingProcessor(String targetDirName) {
       
    58         this(targetDirName,null);
       
    59     }
       
    60 
       
    61     /** Creates a new instance of SerialParamProcessor */
       
    62     public DefaultRewritingProcessor(final String remove, final String add) {
       
    63         super(new SerialRewritingProcessor(remove, add));
       
    64         identity = remove.equals(add);
       
    65     }
       
    66 
       
    67     private ObjectName rewriteObjectName(RewriteMode mode,
       
    68             ObjectName name) {
       
    69         return changeContext(mode, name);
       
    70     }
       
    71 
       
    72     private ObjectInstance rewriteObjectInstance(RewriteMode mode,
       
    73             ObjectInstance moi) {
       
    74         final ObjectName srcName = moi.getObjectName();
       
    75         final ObjectName targetName = changeContext(mode,srcName);
       
    76         if (targetName == srcName) return moi;
       
    77         return new ObjectInstance(targetName,moi.getClassName());
       
    78     }
       
    79 
       
    80 
       
    81     private Object processObject(RewriteMode mode, Object obj) {
       
    82         if (obj == null) return null;
       
    83 
       
    84         // Some things which will always needs rewriting:
       
    85         // ObjectName, ObjectInstance, and Notifications.
       
    86         // Take care of those we can handle here...
       
    87         //
       
    88         if (obj instanceof ObjectName)
       
    89             return rewriteObjectName(mode,(ObjectName) obj);
       
    90         else if (obj instanceof ObjectInstance)
       
    91             return rewriteObjectInstance(mode,(ObjectInstance) obj);
       
    92 
       
    93         // TODO: add other standard JMX classes - like e.g. MBeanInfo...
       
    94         //
       
    95 
       
    96         // Well, the object may contain an ObjectName => pass it to
       
    97         // our serial rewriting delegate...
       
    98         //
       
    99         return processAnyObject(mode,obj);
       
   100     }
       
   101 
       
   102 
       
   103     private Object processAnyObject(RewriteMode mode, Object obj) {
       
   104         switch (mode) {
       
   105             case INPUT:
       
   106                 return super.rewriteInput(obj);
       
   107             case OUTPUT:
       
   108                 return super.rewriteOutput(obj);
       
   109             default: // can't happen.
       
   110                 throw new AssertionError();
       
   111         }
       
   112     }
       
   113 
       
   114     private ObjectName changeContext(RewriteMode mode, ObjectName name) {
       
   115         switch (mode) {
       
   116             case INPUT:
       
   117                 return toSourceContext(name);
       
   118             case OUTPUT:
       
   119                 return toTargetContext(name);
       
   120             default: // can't happen.
       
   121                 throw new AssertionError();
       
   122         }
       
   123     }
       
   124 
       
   125     @Override
       
   126     public ObjectName toTargetContext(ObjectName srcName) {
       
   127         if (identity) return srcName;
       
   128         return super.toTargetContext(srcName);
       
   129     }
       
   130 
       
   131     @Override
       
   132     public ObjectName toSourceContext(ObjectName targetName) {
       
   133         if (identity) return targetName;
       
   134         return super.toSourceContext(targetName);
       
   135     }
       
   136 
       
   137     @SuppressWarnings("unchecked")
       
   138     @Override
       
   139     public <T> T rewriteInput(T input) {
       
   140         if (identity) return input;
       
   141         return (T) processObject(RewriteMode.INPUT,input);
       
   142     }
       
   143 
       
   144     @SuppressWarnings("unchecked")
       
   145     @Override
       
   146     public <T> T rewriteOutput(T result) {
       
   147         if (identity) return result;
       
   148         return (T) processObject(RewriteMode.OUTPUT,result);
       
   149     }
       
   150 }