|
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 com.sun.jmx.defaults.JmxProperties; |
|
29 import java.io.IOException; |
|
30 import java.io.ObjectInputStream; |
|
31 import java.io.ObjectOutputStream; |
|
32 import java.util.logging.Level; |
|
33 import java.util.logging.Logger; |
|
34 |
|
35 |
|
36 /** |
|
37 * The JMXNamespaceContext class is used to implement a thread local |
|
38 * serialization / deserialization context for namespaces. |
|
39 * <p> |
|
40 * This class is consulted by {@link javax.management.ObjectName} at |
|
41 * serialization / deserialization time. |
|
42 * The serialization or deserialization context is established by |
|
43 * by the {@link SerialRewritingProcessor} defined in this package. |
|
44 * <p> |
|
45 * These classes are Sun proprietary APIs, subject to change without |
|
46 * notice. Do not use these classes directly. |
|
47 * The public API to rewrite ObjectNames embedded in parameters is |
|
48 * defined in {@link javax.management.namespace.JMXNamespaces}. |
|
49 * |
|
50 * <p><b> |
|
51 * This API is a Sun internal API and is subject to changes without notice. |
|
52 * </b></p> |
|
53 * @since 1.7 |
|
54 */ |
|
55 public class JMXNamespaceContext { |
|
56 |
|
57 private static final Logger LOG = JmxProperties.NAMESPACE_LOGGER; |
|
58 |
|
59 public final String prefixToRemove; |
|
60 public final String prefixToAdd; |
|
61 |
|
62 private JMXNamespaceContext(String add, String remove) { |
|
63 prefixToRemove = (remove==null?"":remove); |
|
64 prefixToAdd = (add==null?"":add); |
|
65 } |
|
66 |
|
67 private final static class SerialContext { |
|
68 private JMXNamespaceContext serializationContext; |
|
69 private JMXNamespaceContext deserializationContext; |
|
70 public SerialContext(){ |
|
71 serializationContext = new JMXNamespaceContext("",""); |
|
72 deserializationContext = new JMXNamespaceContext("",""); |
|
73 } |
|
74 } |
|
75 |
|
76 private final static ThreadLocal<SerialContext> prefix = |
|
77 new ThreadLocal<SerialContext>() { |
|
78 @Override |
|
79 protected SerialContext initialValue() { |
|
80 return new SerialContext(); |
|
81 } |
|
82 }; |
|
83 |
|
84 public static JMXNamespaceContext getSerializationContext() { |
|
85 return prefix.get().serializationContext; |
|
86 } |
|
87 |
|
88 public static JMXNamespaceContext getDeserializationContext() { |
|
89 return prefix.get().deserializationContext; |
|
90 } |
|
91 |
|
92 private static String[] setSerializationContext(String oldPrefix, |
|
93 String newPrefix) { |
|
94 final SerialContext c = prefix.get(); |
|
95 JMXNamespaceContext dc = c.serializationContext; |
|
96 String[] old = {dc.prefixToRemove, dc.prefixToAdd}; |
|
97 c.serializationContext = new JMXNamespaceContext(newPrefix,oldPrefix); |
|
98 return old; |
|
99 } |
|
100 |
|
101 private static String[] setDeserializationContext(String oldPrefix, |
|
102 String newPrefix) { |
|
103 final SerialContext c = prefix.get(); |
|
104 JMXNamespaceContext dc = c.deserializationContext; |
|
105 String[] old = {dc.prefixToRemove, dc.prefixToAdd}; |
|
106 c.deserializationContext = new JMXNamespaceContext(newPrefix,oldPrefix); |
|
107 return old; |
|
108 } |
|
109 |
|
110 static void serialize(ObjectOutputStream stream, Object obj, |
|
111 String prefixToRemove, String prefixToAdd) |
|
112 throws IOException { |
|
113 final String[] old = |
|
114 setSerializationContext(prefixToRemove,prefixToAdd); |
|
115 try { |
|
116 stream.writeObject(obj); |
|
117 } finally { |
|
118 try { |
|
119 setSerializationContext(old[0],old[1]); |
|
120 } catch (Exception x) { |
|
121 LOG.log(Level.FINEST, |
|
122 "failed to restore serialization context",x); |
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 static Object deserialize(ObjectInputStream stream, |
|
128 String prefixToRemove, |
|
129 String prefixToAdd) |
|
130 throws IOException, ClassNotFoundException { |
|
131 final String[] old = |
|
132 setDeserializationContext(prefixToRemove,prefixToAdd); |
|
133 try { |
|
134 return stream.readObject(); |
|
135 } finally { |
|
136 try { |
|
137 setDeserializationContext(old[0],old[1]); |
|
138 } catch (Exception x) { |
|
139 LOG.log(Level.FINEST, |
|
140 "failed to restore serialization context",x); |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 } |