ObjectName.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /* ObjectName.java -- Represent the name of a bean, or a pattern for a name.
  2. Copyright (C) 2006, 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.management;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.Serializable;
  34. import java.util.Hashtable;
  35. import java.util.Iterator;
  36. import java.util.Map;
  37. import java.util.TreeMap;
  38. import java.io.IOException;
  39. import java.io.InvalidObjectException;
  40. import java.io.ObjectInputStream;
  41. import java.io.ObjectOutputStream;
  42. /**
  43. * <p>
  44. * An {@link ObjectName} instance represents the name of a management
  45. * bean, or a pattern which may match the name of one or more
  46. * management beans. Patterns are distinguished from names by the
  47. * presence of the '?' and '*' characters (which match a single
  48. * character and a series of zero or more characters, respectively).
  49. * </p>
  50. * <p>
  51. * Each name begins with a domain element, which is terminated by
  52. * a ':' character. The domain may be empty. If so, it will be
  53. * replaced by the default domain of the bean server in certain
  54. * contexts. The domain is a pattern, if it contains either '?'
  55. * or '*'. To avoid collisions, it is usual to use reverse
  56. * DNS names for the domain, as in Java package and property names.
  57. * </p>
  58. * <p>
  59. * Following the ':' character is a series of properties. The list
  60. * is separated by commas, and largely consists of unordered key-value
  61. * pairs, separated by an equals sign ('='). At most one element may
  62. * be an asterisk ('*'), which turns the {@link ObjectName} instance
  63. * into a <emph>property list pattern</emph>. In this situation, the pattern
  64. * matches a name if the name contains at least those key-value pairs
  65. * given and has the same domain.
  66. * </p>
  67. * <p>
  68. * A <emph>key</emph> is a string of characters which doesn't include
  69. * any of those used as delimiters or in patterns (':', '=', ',', '?'
  70. * and '*'). Keys must be unique.
  71. * </p>
  72. * <p>
  73. * A value may be <emph>quoted</emph> or <emph>unquoted</emph>. Unquoted
  74. * values obey the same rules as given for keys above. Quoted values are
  75. * surrounded by quotation marks ("), and use a backslash ('\') character
  76. * to include quotes ('\"'), backslashes ('\\'), newlines ('\n'), and
  77. * the pattern characters ('\?' and '\*'). The quotes and backslashes
  78. * (after expansion) are considered part of the value.
  79. * </p>
  80. * <p>
  81. * Both quoted and unquoted values may contain the wildcard characters
  82. * '?' and '*'. A name with at least one value containing a wildcard
  83. * character is known as a <emph>property value pattern</emph>. A
  84. * name is generally a <emph>property pattern</emph> if it is either
  85. * a <emph>property list pattern</emph> or <emph>property value pattern</emph>.
  86. * </p>
  87. * <p>
  88. * Spaces are maintained within the different parts of the name. Thus,
  89. * '<code>domain: key1 = value1 </code>' has a key ' key1 ' with value
  90. * ' value1 '. Newlines are disallowed, except where escaped in quoted
  91. * values.
  92. * </p>
  93. *
  94. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  95. * @since 1.5
  96. */
  97. public class ObjectName
  98. implements Serializable, QueryExp
  99. {
  100. private static final long serialVersionUID = 1081892073854801359L;
  101. /**
  102. * The wildcard {@link ObjectName} {@code "*:*"}
  103. *
  104. * @since 1.6
  105. */
  106. public static final ObjectName WILDCARD;
  107. /**
  108. * The domain of the name.
  109. */
  110. private transient String domain;
  111. /**
  112. * The properties, as key-value pairs.
  113. */
  114. private transient TreeMap<String,String> properties;
  115. /**
  116. * The properties as a string (stored for ordering).
  117. */
  118. private transient String propertyListString;
  119. /**
  120. * True if this object name is a property list pattern.
  121. */
  122. private transient boolean propertyListPattern;
  123. /**
  124. * True if this object name is a property value pattern.
  125. */
  126. private transient boolean propertyValuePattern;
  127. /**
  128. * The management server associated with this object name.
  129. */
  130. private transient MBeanServer server;
  131. /**
  132. * Static initializer to set up the wildcard.
  133. */
  134. static
  135. {
  136. try
  137. {
  138. WILDCARD = new ObjectName("");
  139. }
  140. catch (MalformedObjectNameException e)
  141. {
  142. throw (InternalError) (new InternalError("A problem occurred " +
  143. "initializing the ObjectName " +
  144. "wildcard.").initCause(e));
  145. }
  146. }
  147. /**
  148. * Constructs an {@link ObjectName} instance from the given string,
  149. * which should be of the form
  150. * &lt;domain&gt;:&lt;properties&gt;&lt;wild&gt;. &lt;domain&gt;
  151. * represents the domain section of the name. &lt;properties&gt;
  152. * represents the key-value pairs, as returned by {@link
  153. * #getKeyPropertyListString()}. &lt;wild&gt; is the optional
  154. * asterisk present in the property list. If the string doesn't
  155. * represent a property pattern, it will be empty. If it does,
  156. * it will be either ',*' or '*', depending on whether other
  157. * properties are present or not, respectively.
  158. *
  159. * @param name the string to use to construct this instance.
  160. * @throws MalformedObjectNameException if the string is of the
  161. * wrong format.
  162. * @throws NullPointerException if <code>name</code> is
  163. * <code>null</code>.
  164. */
  165. public ObjectName(String name)
  166. throws MalformedObjectNameException
  167. {
  168. if (name.length() == 0)
  169. name = "*:*";
  170. parse(name);
  171. }
  172. /**
  173. * Parse the name in the same form as the constructor. Used by
  174. * readObject().
  175. */
  176. private void parse(String name)
  177. throws MalformedObjectNameException
  178. {
  179. int domainSep = name.indexOf(':');
  180. if (domainSep == -1)
  181. throw new MalformedObjectNameException("No domain separator was found.");
  182. domain = name.substring(0, domainSep);
  183. String rest = name.substring(domainSep + 1);
  184. properties = new TreeMap<String,String>();
  185. String[] pairs = rest.split(",");
  186. if (pairs.length == 0 && !isPattern())
  187. throw new MalformedObjectNameException("A name that is not a " +
  188. "pattern must contain at " +
  189. "least one key-value pair.");
  190. propertyListString = "";
  191. for (int a = 0; a < pairs.length; ++a)
  192. {
  193. if (pairs[a].equals("*"))
  194. {
  195. if (propertyListPattern)
  196. throw new MalformedObjectNameException("Multiple wildcards " +
  197. "in properties.");
  198. propertyListPattern = true;
  199. continue;
  200. }
  201. int sep = pairs[a].indexOf('=');
  202. if (sep == -1)
  203. throw new MalformedObjectNameException("A key must be " +
  204. "followed by a value.");
  205. String key = pairs[a].substring(0, sep);
  206. if (properties.containsKey(key))
  207. throw new MalformedObjectNameException("The same key occurs " +
  208. "more than once.");
  209. String value = pairs[a].substring(sep+1);
  210. properties.put(key, value);
  211. propertyListString += key + "=" + value + ",";
  212. }
  213. if (propertyListString.length() > 0)
  214. propertyListString =
  215. propertyListString.substring(0, propertyListString.length() - 1);
  216. checkComponents();
  217. }
  218. /**
  219. * Constructs an {@link ObjectName} instance using the given
  220. * domain and the one specified property.
  221. *
  222. * @param domain the domain part of the object name.
  223. * @param key the key of the property.
  224. * @param value the value of the property.
  225. * @throws MalformedObjectNameException the domain, key or value
  226. * contains an illegal
  227. * character or the value
  228. * does not follow the quoting
  229. * specifications.
  230. * @throws NullPointerException if one of the parameters is
  231. * <code>null</code>.
  232. */
  233. public ObjectName(String domain, String key, String value)
  234. throws MalformedObjectNameException
  235. {
  236. this.domain = domain;
  237. properties = new TreeMap<String,String>();
  238. properties.put(key, value);
  239. checkComponents();
  240. }
  241. /**
  242. * Constructs an {@link ObjectName} instance using the given
  243. * domain and properties.
  244. *
  245. * @param domain the domain part of the object name.
  246. * @param properties the key-value property pairs.
  247. * @throws MalformedObjectNameException the domain, a key or a value
  248. * contains an illegal
  249. * character or a value
  250. * does not follow the quoting
  251. * specifications.
  252. * @throws NullPointerException if one of the parameters is
  253. * <code>null</code>.
  254. */
  255. public ObjectName(String domain, Hashtable<String,String> properties)
  256. throws MalformedObjectNameException
  257. {
  258. this.domain = domain;
  259. this.properties = new TreeMap<String,String>();
  260. this.properties.putAll(properties);
  261. checkComponents();
  262. }
  263. /**
  264. * Checks the legality of the domain and the properties.
  265. *
  266. * @throws MalformedObjectNameException the domain, a key or a value
  267. * contains an illegal
  268. * character or a value
  269. * does not follow the quoting
  270. * specifications.
  271. */
  272. private void checkComponents()
  273. throws MalformedObjectNameException
  274. {
  275. if (domain.indexOf(':') != -1)
  276. throw new MalformedObjectNameException("The domain includes a ':' " +
  277. "character.");
  278. if (domain.indexOf('\n') != -1)
  279. throw new MalformedObjectNameException("The domain includes a newline " +
  280. "character.");
  281. char[] keychars = new char[] { '\n', ':', ',', '*', '?', '=' };
  282. char[] valchars = new char[] { '\n', ':', ',', '=' };
  283. for (Map.Entry<String,String> entry : properties.entrySet())
  284. {
  285. for (int a = 0; a < keychars.length; ++a)
  286. if (entry.getKey().indexOf(keychars[a]) != -1)
  287. throw new MalformedObjectNameException("A key contains a '" +
  288. keychars[a] + "' " +
  289. "character.");
  290. String value = entry.getValue();
  291. int quote = value.indexOf('"');
  292. if (quote == 0)
  293. {
  294. try
  295. {
  296. unquote(value);
  297. }
  298. catch (IllegalArgumentException e)
  299. {
  300. throw (MalformedObjectNameException)
  301. new MalformedObjectNameException("The quoted value is " +
  302. "invalid.").initCause(e);
  303. }
  304. }
  305. else if (quote != -1)
  306. throw new MalformedObjectNameException("A value contains " +
  307. "a '\"' character.");
  308. else
  309. {
  310. for (int a = 0; a < valchars.length; ++a)
  311. if (value.indexOf(valchars[a]) != -1)
  312. throw new MalformedObjectNameException("A value contains " +
  313. "a '" + valchars[a] + "' " +
  314. "character.");
  315. }
  316. if (value.indexOf('*') != -1 || value.indexOf('?') != -1)
  317. propertyValuePattern = true;
  318. }
  319. }
  320. /**
  321. * <p>
  322. * Attempts to find a match between this name and the one supplied.
  323. * The following criteria are used:
  324. * </p>
  325. * <ul>
  326. * <li>If the supplied name is a pattern, <code>false</code> is
  327. * returned.</li>
  328. * <li>If this name is a pattern, this method returns <code>true</code>
  329. * if the supplied name matches the pattern.</li>
  330. * <li>If this name is not a pattern, the result of
  331. * <code>equals(name)</code> is returned.
  332. * </ul>
  333. *
  334. * @param name the name to find a match with.
  335. * @return true if the name either matches this pattern or is
  336. * equivalent to this name under the criteria of
  337. * {@link #equals(java.lang.Object)}
  338. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  339. */
  340. public boolean apply(ObjectName name)
  341. {
  342. if (name.isPattern())
  343. return false;
  344. if (!isPattern())
  345. return equals(name);
  346. if (isDomainPattern())
  347. {
  348. if (!domainMatches(domain, 0, name.getDomain(), 0))
  349. return false;
  350. }
  351. else
  352. {
  353. if (!domain.equals(name.getDomain()))
  354. return false;
  355. }
  356. if (isPropertyPattern())
  357. {
  358. Hashtable<String,String> oProps = name.getKeyPropertyList();
  359. for (Map.Entry<String,String> entry : properties.entrySet())
  360. {
  361. String key = entry.getKey();
  362. if (!(oProps.containsKey(key)))
  363. return false;
  364. String val = entry.getValue();
  365. if (!(val.equals(oProps.get(key))))
  366. return false;
  367. }
  368. }
  369. else
  370. {
  371. if (!getCanonicalKeyPropertyListString().equals
  372. (name.getCanonicalKeyPropertyListString()))
  373. return false;
  374. }
  375. return true;
  376. }
  377. /**
  378. * Returns true if the domain matches the pattern.
  379. *
  380. * @param pattern the pattern to match against.
  381. * @param patternindex the index into the pattern to start matching.
  382. * @param domain the domain to match.
  383. * @param domainindex the index into the domain to start matching.
  384. * @return true if the domain matches the pattern.
  385. */
  386. private static boolean domainMatches(String pattern, int patternindex,
  387. String domain, int domainindex)
  388. {
  389. while (patternindex < pattern.length())
  390. {
  391. char c = pattern.charAt(patternindex++);
  392. if (c == '*')
  393. {
  394. for (int i = domain.length(); i >= domainindex; i--)
  395. {
  396. if (domainMatches(pattern, patternindex, domain, i))
  397. return true;
  398. }
  399. return false;
  400. }
  401. if (domainindex >= domain.length())
  402. return false;
  403. if (c != '?' && c != domain.charAt(domainindex))
  404. return false;
  405. domainindex++;
  406. }
  407. return true;
  408. }
  409. /**
  410. * Compares the specified object with this one. The two
  411. * are judged to be equivalent if the given object is an
  412. * instance of {@link ObjectName} and has an equal canonical
  413. * form (as returned by {@link #getCanonicalName()}).
  414. *
  415. * @param obj the object to compare with this.
  416. * @return true if the object is also an {@link ObjectName}
  417. * with an equivalent canonical form.
  418. */
  419. public boolean equals(Object obj)
  420. {
  421. if (obj instanceof ObjectName)
  422. {
  423. ObjectName o = (ObjectName) obj;
  424. return getCanonicalName().equals(o.getCanonicalName());
  425. }
  426. return false;
  427. }
  428. /**
  429. * Returns the property list in canonical form. The keys
  430. * are ordered using the lexicographic ordering used by
  431. * {@link java.lang.String#compareTo(java.lang.Object)}.
  432. *
  433. * @return the property list, with the keys in lexicographic
  434. * order.
  435. */
  436. public String getCanonicalKeyPropertyListString()
  437. {
  438. CPStringBuilder builder = new CPStringBuilder();
  439. Iterator<Map.Entry<String,String>> i = properties.entrySet().iterator();
  440. while (i.hasNext())
  441. {
  442. Map.Entry<String,String> entry = i.next();
  443. builder.append(entry.getKey() + "=" + entry.getValue());
  444. if (i.hasNext())
  445. builder.append(",");
  446. }
  447. return builder.toString();
  448. }
  449. /**
  450. * <p>
  451. * Returns the name as a string in canonical form. More precisely,
  452. * this returns a string of the format
  453. * &lt;domain&gt;:&lt;properties&gt;&lt;wild&gt;. &lt;properties&gt;
  454. * is the same value as returned by
  455. * {@link #getCanonicalKeyPropertyListString()}. &lt;wild&gt;
  456. * is:
  457. * </p>
  458. * <ul>
  459. * <li>an empty string, if the object name is not a property pattern.</li>
  460. * <li>'*' if &lt;properties&gt; is empty.</li>
  461. * <li>',*' if there is at least one key-value pair.</li>
  462. * </ul>
  463. *
  464. * @return the canonical string form of the object name, as specified
  465. * above.
  466. */
  467. public String getCanonicalName()
  468. {
  469. return domain + ":" +
  470. getCanonicalKeyPropertyListString() +
  471. (isPropertyPattern() ? (properties.isEmpty() ? "*" : ",*") : "");
  472. }
  473. /**
  474. * Returns the domain part of the object name.
  475. *
  476. * @return the domain.
  477. */
  478. public String getDomain()
  479. {
  480. return domain;
  481. }
  482. /**
  483. * Returns an {@link ObjectName} instance that is substitutable for the
  484. * one given. The instance returned may be a subclass of {@link ObjectName},
  485. * but is not guaranteed to be of the same type as the given name, if that
  486. * should also turn out to be a subclass. The returned instance may or may
  487. * not be equivalent to the one given. The purpose of this method is to provide
  488. * an instance of {@link ObjectName} with a well-defined semantics, such as may
  489. * be used in cases where the given name is not trustworthy.
  490. *
  491. * @param name the {@link ObjectName} to provide a substitute for.
  492. * @return a substitute for the given name, which may or may not be a subclass
  493. * of {@link ObjectName}. In either case, the returned object is
  494. * guaranteed to have the semantics defined here.
  495. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  496. */
  497. public static ObjectName getInstance(ObjectName name)
  498. {
  499. try
  500. {
  501. return new ObjectName(name.getCanonicalName());
  502. }
  503. catch (MalformedObjectNameException e)
  504. {
  505. throw (InternalError)
  506. (new InternalError("The canonical name of " +
  507. "the given name is invalid.").initCause(e));
  508. }
  509. }
  510. /**
  511. * Returns an {@link ObjectName} instance for the specified name, represented
  512. * as a {@link java.lang.String}. The instance returned may be a subclass of
  513. * {@link ObjectName} and may or may not be equivalent to earlier instances
  514. * returned by this method for the same string.
  515. *
  516. * @param name the {@link ObjectName} to provide an instance of.
  517. * @return a instance for the given name, which may or may not be a subclass
  518. * of {@link ObjectName}.
  519. * @throws MalformedObjectNameException the domain, a key or a value
  520. * contains an illegal
  521. * character or a value
  522. * does not follow the quoting
  523. * specifications.
  524. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  525. */
  526. public static ObjectName getInstance(String name)
  527. throws MalformedObjectNameException
  528. {
  529. return new ObjectName(name);
  530. }
  531. /**
  532. * Returns an {@link ObjectName} instance for the specified name, represented
  533. * as a series of {@link java.lang.String} objects for the domain and a single
  534. * property, as a key-value pair. The instance returned may be a subclass of
  535. * {@link ObjectName} and may or may not be equivalent to earlier instances
  536. * returned by this method for the same parameters.
  537. *
  538. * @param domain the domain part of the object name.
  539. * @param key the key of the property.
  540. * @param value the value of the property.
  541. * @return a instance for the given name, which may or may not be a subclass
  542. * of {@link ObjectName}.
  543. * @throws MalformedObjectNameException the domain, a key or a value
  544. * contains an illegal
  545. * character or a value
  546. * does not follow the quoting
  547. * specifications.
  548. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  549. */
  550. public static ObjectName getInstance(String domain, String key, String value)
  551. throws MalformedObjectNameException
  552. {
  553. return new ObjectName(domain, key, value);
  554. }
  555. /**
  556. * Returns an {@link ObjectName} instance for the specified name, represented
  557. * as a domain {@link java.lang.String} and a table of properties. The
  558. * instance returned may be a subclass of {@link ObjectName} and may or may
  559. * not be equivalent to earlier instances returned by this method for the
  560. * same string.
  561. *
  562. * @param domain the domain part of the object name.
  563. * @param properties the key-value property pairs.
  564. * @return a instance for the given name, which may or may not be a subclass
  565. * of {@link ObjectName}.
  566. * @throws MalformedObjectNameException the domain, a key or a value
  567. * contains an illegal
  568. * character or a value
  569. * does not follow the quoting
  570. * specifications.
  571. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  572. */
  573. public static ObjectName getInstance(String domain,
  574. Hashtable<String,String> properties)
  575. throws MalformedObjectNameException
  576. {
  577. return new ObjectName(domain, properties);
  578. }
  579. /**
  580. * Returns the property value corresponding to the given key.
  581. *
  582. * @param key the key of the property to be obtained.
  583. * @return the value of the specified property.
  584. * @throws NullPointerException if <code>key</code> is <code>null</code>.
  585. */
  586. public String getKeyProperty(String key)
  587. {
  588. if (key == null)
  589. throw new NullPointerException("Null key given in request for a value.");
  590. return (String) properties.get(key);
  591. }
  592. /**
  593. * Returns the properties in a {@link java.util.Hashtable}. The table
  594. * contains each of the properties as keys mapped to their value. The
  595. * returned table is not unmodifiable, but changes made to it will not
  596. * be reflected in the object name.
  597. *
  598. * @return a {@link java.util.Hashtable}, containing each of the object
  599. * name's properties.
  600. */
  601. public Hashtable<String,String> getKeyPropertyList()
  602. {
  603. return new Hashtable<String,String>(properties);
  604. }
  605. /**
  606. * Returns a {@link java.lang.String} representation of the property
  607. * list. If the object name was created using {@link
  608. * ObjectName(String)}, then this string will contain the properties
  609. * in the same order they were given in at creation.
  610. *
  611. * @return the property list.
  612. */
  613. public String getKeyPropertyListString()
  614. {
  615. if (propertyListString != null)
  616. return propertyListString;
  617. return getCanonicalKeyPropertyListString();
  618. }
  619. /**
  620. * Returns a hash code for this object name. This is calculated as the
  621. * summation of the hash codes of the domain and the properties.
  622. *
  623. * @return a hash code for this object name.
  624. */
  625. public int hashCode()
  626. {
  627. return domain.hashCode() + properties.hashCode();
  628. }
  629. /**
  630. * Returns true if the domain of this object name is a pattern.
  631. * This is the case if it contains one or more wildcard characters
  632. * ('*' or '?').
  633. *
  634. * @return true if the domain is a pattern.
  635. */
  636. public boolean isDomainPattern()
  637. {
  638. return domain.contains("?") || domain.contains("*");
  639. }
  640. /**
  641. * Returns true if this is an object name pattern. An object
  642. * name pattern has a domain containing a wildcard character
  643. * ('*' or '?') and/or a '*' in the list of properties.
  644. * This method will return true if either {@link #isDomainPattern()}
  645. * or {@link #isPropertyPattern()} does.
  646. *
  647. * @return true if this is an object name pattern.
  648. */
  649. public boolean isPattern()
  650. {
  651. return isDomainPattern() || isPropertyPattern();
  652. }
  653. /**
  654. * Returns true if this object name is a property list
  655. * pattern, a property value pattern or both.
  656. *
  657. * @return true if the properties of this name contain a pattern.
  658. * @see #isPropertyListPattern
  659. * @see #isPropertyValuePattern
  660. */
  661. public boolean isPropertyPattern()
  662. {
  663. return propertyListPattern || propertyValuePattern;
  664. }
  665. /**
  666. * Returns true if this object name is a property list pattern. This is
  667. * the case if the list of properties contains an '*'.
  668. *
  669. * @return true if this is a property list pattern.
  670. * @since 1.6
  671. */
  672. public boolean isPropertyListPattern()
  673. {
  674. return propertyListPattern;
  675. }
  676. /**
  677. * Returns true if this object name is a property value pattern. This is
  678. * the case if one of the values contains a wildcard character,
  679. * '?' or '*'.
  680. *
  681. * @return true if this is a property value pattern.
  682. * @since 1.6
  683. */
  684. public boolean isPropertyValuePattern()
  685. {
  686. return propertyValuePattern;
  687. }
  688. /**
  689. * Returns true if the value of the given key is a pattern. This is
  690. * the case if the value contains a wildcard character, '?' or '*'.
  691. *
  692. * @param key the key whose value should be checked.
  693. * @return true if the value of the given key is a pattern.
  694. * @since 1.6
  695. * @throws NullPointerException if {@code key} is {@code null}.
  696. * @throws IllegalArgumentException if {@code key} is not a valid
  697. * property.
  698. */
  699. public boolean isPropertyValuePattern(String key)
  700. {
  701. String value = getKeyProperty(key);
  702. if (value == null)
  703. throw new IllegalArgumentException(key + " is not a valid property.");
  704. return value.indexOf('?') != -1 || value.indexOf('*') != -1;
  705. }
  706. /**
  707. * <p>
  708. * Returns a quoted version of the supplied string. The string may
  709. * contain any character. The resulting quoted version is guaranteed
  710. * to be usable as the value of a property, so this method provides
  711. * a good way of ensuring that a value is legal.
  712. * </p>
  713. * <p>
  714. * The string is transformed as follows:
  715. * </p>
  716. * <ul>
  717. * <li>The string is prefixed with an opening quote character, '"'.
  718. * <li>For each character, s:
  719. * <ul>
  720. * <li>If s is a quote ('"'), it is replaced by a backslash
  721. * followed by a quote.</li>
  722. * <li>If s is a star ('*'), it is replaced by a backslash followed
  723. * by a star.</li>
  724. * <li>If s is a question mark ('?'), it is replaced by a backslash
  725. * followed by a question mark.</li>
  726. * <li>If s is a backslash ('\'), it is replaced by two backslashes.</li>
  727. * <li>If s is a newline character, it is replaced by a backslash followed by
  728. * a '\n'.</li>
  729. * <li>Otherwise, s is used verbatim.
  730. * </ul></li>
  731. * <li>The string is terminated with a closing quote character, '"'.</li>
  732. * </ul>
  733. *
  734. * @param string the string to quote.
  735. * @return a quoted version of the supplied string.
  736. * @throws NullPointerException if <code>string</code> is <code>null</code>.
  737. */
  738. public static String quote(String string)
  739. {
  740. CPStringBuilder builder = new CPStringBuilder();
  741. builder.append('"');
  742. for (int a = 0; a < string.length(); ++a)
  743. {
  744. char s = string.charAt(a);
  745. switch (s)
  746. {
  747. case '"':
  748. builder.append("\\\"");
  749. break;
  750. case '*':
  751. builder.append("\\*");
  752. break;
  753. case '?':
  754. builder.append("\\?");
  755. break;
  756. case '\\':
  757. builder.append("\\\\");
  758. break;
  759. case '\n':
  760. builder.append("\\\n");
  761. break;
  762. default:
  763. builder.append(s);
  764. }
  765. }
  766. builder.append('"');
  767. return builder.toString();
  768. }
  769. /**
  770. * Changes the {@link MBeanServer} on which this query is performed.
  771. *
  772. * @param server the new server to use.
  773. */
  774. public void setMBeanServer(MBeanServer server)
  775. {
  776. this.server = server;
  777. }
  778. /**
  779. * Returns a textual representation of the object name.
  780. *
  781. * <p>The format is unspecified beyond that equivalent object
  782. * names will return the same string from this method, but note
  783. * that Tomcat depends on the string returned by this method
  784. * being a valid textual representation of the object name and
  785. * will fail to start if it is not.
  786. *
  787. * @return a textual representation of the object name.
  788. */
  789. public String toString()
  790. {
  791. return getCanonicalName();
  792. }
  793. /**
  794. * Serialize this {@link ObjectName}. The serialized
  795. * form is the same as the string parsed by the constructor.
  796. *
  797. * @param out the output stream to write to.
  798. * @throws IOException if an I/O error occurs.
  799. */
  800. private void writeObject(ObjectOutputStream out)
  801. throws IOException
  802. {
  803. out.defaultWriteObject();
  804. CPStringBuilder buffer = new CPStringBuilder(getDomain());
  805. buffer.append(':');
  806. String properties = getKeyPropertyListString();
  807. buffer.append(properties);
  808. if (isPropertyPattern())
  809. {
  810. if (properties.length() == 0)
  811. buffer.append("*");
  812. else
  813. buffer.append(",*");
  814. }
  815. out.writeObject(buffer.toString());
  816. }
  817. /**
  818. * Reads the serialized form, which is that used
  819. * by the constructor.
  820. *
  821. * @param in the input stream to read from.
  822. * @throws IOException if an I/O error occurs.
  823. */
  824. private void readObject(ObjectInputStream in)
  825. throws IOException, ClassNotFoundException
  826. {
  827. in.defaultReadObject();
  828. String objectName = (String)in.readObject();
  829. try
  830. {
  831. parse(objectName);
  832. }
  833. catch (MalformedObjectNameException x)
  834. {
  835. throw new InvalidObjectException(x.toString());
  836. }
  837. }
  838. /**
  839. * Unquotes the supplied string. The quotation marks are removed as
  840. * are the backslashes preceding the escaped characters ('"', '?',
  841. * '*', '\n', '\\'). A one-to-one mapping exists between quoted and
  842. * unquoted values. As a result, a string <code>s</code> should be
  843. * equal to <code>unquote(quote(s))</code>.
  844. *
  845. * @param q the quoted string to unquote.
  846. * @return the unquoted string.
  847. * @throws NullPointerException if <code>q</code> is <code>null</code>.
  848. * @throws IllegalArgumentException if the string is not a valid
  849. * quoted string i.e. it is not
  850. * surrounded by quotation marks
  851. * and/or characters are not properly
  852. * escaped.
  853. */
  854. public static String unquote(String q)
  855. {
  856. if (q.charAt(0) != '"')
  857. throw new IllegalArgumentException("The string does " +
  858. "not start with a quote.");
  859. if (q.charAt(q.length() - 1) != '"')
  860. throw new IllegalArgumentException("The string does " +
  861. "not end with a quote.");
  862. CPStringBuilder builder = new CPStringBuilder();
  863. for (int a = 1; a < (q.length() - 1); ++a)
  864. {
  865. char n = q.charAt(a);
  866. if (n == '\\')
  867. {
  868. n = q.charAt(++a);
  869. if (n != '"' && n != '?' && n != '*' &&
  870. n != 'n' && n != '\\')
  871. throw new IllegalArgumentException("Illegal escaped character: "
  872. + n);
  873. }
  874. else if (n == '"' || n == '\n')
  875. throw new IllegalArgumentException("Illegal character: " + n);
  876. builder.append(n);
  877. }
  878. return builder.toString();
  879. }
  880. }