jdk/src/share/classes/com/sun/jmx/namespace/serial/SerialRewritingProcessor.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 import java.io.ByteArrayInputStream;
       
    29 import java.io.ByteArrayOutputStream;
       
    30 import java.io.IOException;
       
    31 import java.io.InputStream;
       
    32 import java.io.InvalidClassException;
       
    33 import java.io.ObjectInputStream;
       
    34 import java.io.ObjectOutputStream;
       
    35 import java.io.ObjectStreamClass;
       
    36 import java.io.OutputStream;
       
    37 import java.util.LinkedList;
       
    38 import java.util.Queue;
       
    39 
       
    40 import javax.management.ObjectName;
       
    41 
       
    42 /**
       
    43  * Class SerialRewritingProcessor. A RewritingProcessor that uses
       
    44  * Java Serialization to rewrite ObjectNames contained in
       
    45  * input & results...
       
    46  * <p><b>
       
    47  * This API is a Sun internal API and is subject to changes without notice.
       
    48  * </b></p>
       
    49  * @since 1.7
       
    50  */
       
    51 class SerialRewritingProcessor extends RewritingProcessor {
       
    52 
       
    53 
       
    54     private static class CloneOutput extends ObjectOutputStream {
       
    55         Queue<Class<?>> classQueue = new LinkedList<Class<?>>();
       
    56 
       
    57         CloneOutput(OutputStream out) throws IOException {
       
    58             super(out);
       
    59         }
       
    60 
       
    61         @Override
       
    62         protected void annotateClass(Class<?> c) {
       
    63             classQueue.add(c);
       
    64         }
       
    65 
       
    66         @Override
       
    67         protected void annotateProxyClass(Class<?> c) {
       
    68             classQueue.add(c);
       
    69         }
       
    70     }
       
    71 
       
    72     private static class CloneInput extends ObjectInputStream {
       
    73         private final CloneOutput output;
       
    74 
       
    75         CloneInput(InputStream in, CloneOutput output) throws IOException {
       
    76             super(in);
       
    77             this.output = output;
       
    78         }
       
    79 
       
    80         @Override
       
    81         protected Class<?> resolveClass(ObjectStreamClass osc)
       
    82         throws IOException, ClassNotFoundException {
       
    83             Class<?> c = output.classQueue.poll();
       
    84             String expected = osc.getName();
       
    85             String found = (c == null) ? null : c.getName();
       
    86             if (!expected.equals(found)) {
       
    87                 throw new InvalidClassException("Classes desynchronized: " +
       
    88                         "found " + found + " when expecting " + expected);
       
    89             }
       
    90             return c;
       
    91         }
       
    92 
       
    93         @Override
       
    94         protected Class<?> resolveProxyClass(String[] interfaceNames)
       
    95         throws IOException, ClassNotFoundException {
       
    96             return output.classQueue.poll();
       
    97         }
       
    98     }
       
    99 
       
   100 
       
   101     final String targetPrefix;
       
   102     final String sourcePrefix;
       
   103     final boolean identity;
       
   104 
       
   105 
       
   106     public SerialRewritingProcessor(String targetDirName) {
       
   107         this(targetDirName,null);
       
   108     }
       
   109 
       
   110     /** Creates a new instance of SerialRewritingProcessor */
       
   111     public SerialRewritingProcessor(final String remove, final String add) {
       
   112         super(new RoutingOnlyProcessor(remove,add));
       
   113         this.targetPrefix = remove;
       
   114         this.sourcePrefix = add;
       
   115         identity = targetPrefix.equals(sourcePrefix);
       
   116     }
       
   117 
       
   118     private <T> T  switchContext(T result, String from,String to)
       
   119             throws IOException, ClassNotFoundException {
       
   120         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
   121         final CloneOutput ostream = new CloneOutput(baos);
       
   122 
       
   123         JMXNamespaceContext.serialize(ostream,result,from,null);
       
   124         ostream.flush();
       
   125 
       
   126         final byte[] bytes = baos.toByteArray();
       
   127         final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
       
   128         final CloneInput istream = new CloneInput(bais, ostream);
       
   129         @SuppressWarnings("unchecked")
       
   130         final T clone = (T) JMXNamespaceContext.deserialize(istream,null,to);
       
   131         return clone;
       
   132     }
       
   133 
       
   134     @Override
       
   135     @SuppressWarnings("unchecked")
       
   136     public <T> T rewriteOutput(T result) {
       
   137         if (identity) return result;
       
   138         return (T) processOutput(result);
       
   139     }
       
   140 
       
   141     private Object processOutput(Object result) {
       
   142         try {
       
   143             if (result instanceof ObjectName)
       
   144                 return toTargetContext((ObjectName) result);
       
   145             return switchContext(result,sourcePrefix,targetPrefix);
       
   146         } catch (ClassNotFoundException x) {
       
   147             throw new IllegalArgumentException("Can't process result: "+x,x);
       
   148         } catch (IOException x) {
       
   149             throw new IllegalArgumentException("Can't process result: "+x,x);
       
   150         }
       
   151     }
       
   152 
       
   153     @Override
       
   154     @SuppressWarnings("unchecked")
       
   155     public <T> T rewriteInput(T input) {
       
   156         if (identity) return input;
       
   157         return (T) processInput(input);
       
   158     }
       
   159 
       
   160     private Object processInput(Object input) {
       
   161         try {
       
   162             if (input instanceof ObjectName)
       
   163                 return toSourceContext((ObjectName) input);
       
   164             return switchContext(input,targetPrefix,sourcePrefix);
       
   165         } catch (ClassNotFoundException x) {
       
   166             throw new IllegalArgumentException("Can't process input: "+x,x);
       
   167         } catch (IOException x) {
       
   168             throw new IllegalArgumentException("Can't process input: "+x,x);
       
   169         }
       
   170     }
       
   171 
       
   172 }