|
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; |
|
27 |
|
28 import static javax.management.namespace.JMXNamespaces.NAMESPACE_SEPARATOR; |
|
29 |
|
30 import javax.management.MalformedObjectNameException; |
|
31 import javax.management.ObjectInstance; |
|
32 import javax.management.ObjectName; |
|
33 |
|
34 /** |
|
35 * The ObjectNameRouter is used to rewrite routing object names. |
|
36 * <p><b> |
|
37 * This API is a Sun internal API and is subject to changes without notice. |
|
38 * </b></p> |
|
39 * @since 1.7 |
|
40 */ |
|
41 public class ObjectNameRouter { |
|
42 |
|
43 private static final int NAMESPACE_SEPARATOR_LENGTH = |
|
44 NAMESPACE_SEPARATOR.length(); |
|
45 |
|
46 final String targetPrefix; |
|
47 final String sourcePrefix; |
|
48 final int slen; |
|
49 final int tlen; |
|
50 final boolean identity; |
|
51 |
|
52 |
|
53 public ObjectNameRouter(String targetDirName) { |
|
54 this(targetDirName,null); |
|
55 } |
|
56 |
|
57 /** Creates a new instance of ObjectNameRouter */ |
|
58 public ObjectNameRouter(final String remove, final String add) { |
|
59 this.targetPrefix = (remove==null?"":remove); |
|
60 this.sourcePrefix = (add==null?"":add); |
|
61 tlen = targetPrefix.length(); |
|
62 slen = sourcePrefix.length(); |
|
63 identity = targetPrefix.equals(sourcePrefix); |
|
64 } |
|
65 |
|
66 public final ObjectName toTargetContext(ObjectName sourceName, |
|
67 boolean removeLeadingSeparators) { |
|
68 if (sourceName == null) return null; |
|
69 if (identity) return sourceName; |
|
70 String srcDomain = sourceName.getDomain(); |
|
71 |
|
72 // if the ObjectName starts with // and removeLeadingSeparators is |
|
73 // true, then recursively strip leading //. |
|
74 // Otherwise, do not rewrite ObjectName. |
|
75 // |
|
76 if (srcDomain.startsWith(NAMESPACE_SEPARATOR)) { |
|
77 if (!removeLeadingSeparators) return sourceName; |
|
78 else srcDomain = normalizeDomain(srcDomain,true); |
|
79 } |
|
80 if (slen != 0) { |
|
81 if (!srcDomain.startsWith(sourcePrefix) || |
|
82 !srcDomain.startsWith(NAMESPACE_SEPARATOR,slen)) |
|
83 throw new IllegalArgumentException( |
|
84 "ObjectName does not start with expected prefix " |
|
85 + sourcePrefix + ": " + |
|
86 String.valueOf(sourceName)); |
|
87 srcDomain = srcDomain.substring(slen+NAMESPACE_SEPARATOR_LENGTH); |
|
88 } |
|
89 final String targetDomain = |
|
90 (tlen>0?targetPrefix+NAMESPACE_SEPARATOR+srcDomain:srcDomain); |
|
91 try { |
|
92 return sourceName.withDomain(targetDomain); |
|
93 } catch (MalformedObjectNameException x) { |
|
94 throw new IllegalArgumentException(String.valueOf(sourceName),x); |
|
95 } |
|
96 } |
|
97 |
|
98 public final ObjectName toSourceContext(ObjectName targetName, |
|
99 boolean removeLeadingSeparators) { |
|
100 if (targetName == null) return null; |
|
101 if (identity) return targetName; |
|
102 String targetDomain = targetName.getDomain(); |
|
103 if (targetDomain.startsWith(NAMESPACE_SEPARATOR)) { |
|
104 if (!removeLeadingSeparators) return targetName; |
|
105 else targetDomain = |
|
106 normalizeDomain(targetDomain,true); |
|
107 } |
|
108 if (tlen != 0) { |
|
109 if (!targetDomain.startsWith(targetPrefix) || |
|
110 !targetDomain.startsWith(NAMESPACE_SEPARATOR,tlen)) |
|
111 throw new IllegalArgumentException( |
|
112 "ObjectName does not start with expected prefix " |
|
113 + targetPrefix + ": " + |
|
114 String.valueOf(targetName)); |
|
115 targetDomain = targetDomain. |
|
116 substring(tlen+NAMESPACE_SEPARATOR_LENGTH); |
|
117 } |
|
118 final String sourceDomain = |
|
119 (slen>0?sourcePrefix+NAMESPACE_SEPARATOR+targetDomain: |
|
120 targetDomain); |
|
121 try { |
|
122 return targetName.withDomain(sourceDomain); |
|
123 } catch (MalformedObjectNameException x) { |
|
124 throw new IllegalArgumentException(String.valueOf(targetName),x); |
|
125 } |
|
126 } |
|
127 |
|
128 public final ObjectInstance toTargetContext(ObjectInstance sourceMoi, |
|
129 boolean removeLeadingSeparators) { |
|
130 if (sourceMoi == null) return null; |
|
131 if (identity) return sourceMoi; |
|
132 return new ObjectInstance( |
|
133 toTargetContext(sourceMoi.getObjectName(), |
|
134 removeLeadingSeparators), |
|
135 sourceMoi.getClassName()); |
|
136 } |
|
137 |
|
138 /** |
|
139 * Removes leading, trailing, or duplicate // in a name space path. |
|
140 **/ |
|
141 public static String normalizeDomain(String domain, |
|
142 boolean removeLeadingSep) { |
|
143 return normalizeNamespacePath(domain,removeLeadingSep,false,true); |
|
144 } |
|
145 |
|
146 /** |
|
147 * Removes leading, trailing, or duplicate // in a name space path. |
|
148 **/ |
|
149 public static String normalizeNamespacePath(String namespacePath, |
|
150 boolean removeLeadingSep, |
|
151 boolean removeTrailingSep, |
|
152 boolean endsWithDomain) { |
|
153 if (namespacePath.equals("")) |
|
154 return ""; |
|
155 final String[] components = namespacePath.split(NAMESPACE_SEPARATOR); |
|
156 final StringBuilder b = |
|
157 new StringBuilder(namespacePath.length()+NAMESPACE_SEPARATOR_LENGTH); |
|
158 String sep = null; |
|
159 if (!removeLeadingSep && namespacePath.startsWith(NAMESPACE_SEPARATOR)) |
|
160 b.append(NAMESPACE_SEPARATOR); |
|
161 int count = 0; |
|
162 for (int i=0; i<components.length; i++) { |
|
163 final String n=components[i]; |
|
164 if (n.equals("")) continue; |
|
165 if (n.startsWith("/")||n.endsWith("/")) { |
|
166 // throw exception unless we're looking at the last domain |
|
167 // part of the ObjectName |
|
168 if (! (endsWithDomain && i==(components.length-1))) { |
|
169 throw new IllegalArgumentException(n+ |
|
170 " is not a valid name space identifier"); |
|
171 } else { |
|
172 // There's a dirty little corner case when the domain |
|
173 // part (last item) is exactly '/' - in that case we must |
|
174 // not append '//' |
|
175 // |
|
176 removeTrailingSep = removeTrailingSep || n.equals("/"); |
|
177 } |
|
178 } |
|
179 if (sep != null) b.append(sep); |
|
180 b.append(n); |
|
181 sep = NAMESPACE_SEPARATOR; |
|
182 count++; |
|
183 } |
|
184 if (!removeTrailingSep && namespacePath.endsWith(NAMESPACE_SEPARATOR) |
|
185 && count > 0) |
|
186 b.append(NAMESPACE_SEPARATOR); |
|
187 return b.toString(); |
|
188 } |
|
189 |
|
190 |
|
191 } |