MBeanServerPermission.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* MBeanServerPermission.java -- Permissions controlling server creation.
  2. Copyright (C) 2006 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 java.security.BasicPermission;
  33. import java.security.Permission;
  34. import java.security.PermissionCollection;
  35. import java.util.Enumeration;
  36. import java.util.NoSuchElementException;
  37. /**
  38. * <p>
  39. * Represents the permissions required to perform
  40. * operations provided by the {@link MBeanServerFactory}.
  41. * As with all {@link java.security.Permission} objects, an
  42. * instance of this class either represents a permission
  43. * already held or one that is required to access a
  44. * particular service. In the case of {@link MBeanServerPermission}s,
  45. * implication checks are made using an instance of this class
  46. * when a user requests an operation from the factory, and a
  47. * {@link SecurityManager} is in place.
  48. * </p>
  49. * <p>
  50. * The permission is defined by its name, which may be
  51. * either a <code>'*'</code> (to allow all) or one or
  52. * more of the following, separated by a <code>','</code>:
  53. * </p>
  54. * <ul>
  55. * <li><code>createMBeanServer</code> -- allows a registered
  56. * instance of a server to be obtained from the factory.</li>
  57. * <li><code>findMBeanServer</code> -- allows all or one
  58. * particular server instance to be retrieved from the factory.</li>
  59. * <li><code>newMBeanServer</code> -- allows an unregistered
  60. * instance of a server to be obtained from the factory.</li>
  61. * <li><code>releaseMBeanServer</code> -- allows a reference to
  62. * a server instance to be removed from the factory.</li>
  63. * </ul>
  64. * <p>
  65. * The names may be surrounded by arbitrary amounts of whitespace.
  66. * <code>createMBeanServer</code> implies <code>newMBeanServer</code>.
  67. * </p>
  68. *
  69. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  70. * @since 1.5
  71. */
  72. public class MBeanServerPermission
  73. extends BasicPermission
  74. {
  75. /**
  76. * Compatible with JDK 1.5
  77. */
  78. private static final long serialVersionUID = -5661980843569388590L;
  79. /**
  80. * <p>
  81. * Constructs a new {@link MBeanServerPermission} with
  82. * the given name. The name must not be <code>null</code>
  83. * and must be equal to either <code>"*"</code> or a
  84. * comma-separated list of valid permissions. The four
  85. * valid constraints are:
  86. * </p>
  87. * <ol>
  88. * <li><code>createMBeanServer</code></li>
  89. * <li><code>findMBeanServer</code></li>
  90. * <li><code>newMBeanServer</code></li>
  91. * <li><code>releaseMBeanServer</code></li>
  92. * </ol>
  93. * <p>
  94. * Calling this constructor is equivalent to calling
  95. * <code>MBeanPermission(name, null)</code>.
  96. * </p>
  97. *
  98. * @param name the name of this permission.
  99. * @throws NullPointerException if <code>name</code>
  100. * is <code>null</code>.
  101. * @throws IllegalArgumentException if <code>name</code>
  102. * is not either equal to
  103. * <code>"*"</code> or forms
  104. * a comma-separated list of
  105. * valid constraints.
  106. * @see #MBeanServerPermission(String,String)
  107. */
  108. public MBeanServerPermission(String name)
  109. {
  110. this(name, null);
  111. }
  112. /**
  113. * <p>
  114. * Constructs a new {@link MBeanServerPermission} with
  115. * the given name and actions. The actions are unused,
  116. * and must be either <code>null</code> or the empty
  117. * string. The name must not be <code>null</code>
  118. * and must be equal to either <code>"*"</code> or a
  119. * comma-separated list of valid permissions. The four
  120. * valid constraints are:
  121. * </p>
  122. * <ol>
  123. * <li><code>createMBeanServer</code></li>
  124. * <li><code>findMBeanServer</code></li>
  125. * <li><code>newMBeanServer</code></li>
  126. * <li><code>releaseMBeanServer</code></li>
  127. * </ol>
  128. * <p>
  129. * Calling this constructor is equivalent to calling
  130. * <code>MBeanPermission(name, null)</code>.
  131. * </p>
  132. *
  133. * @param name the name of this permission.
  134. * @throws NullPointerException if <code>name</code>
  135. * is <code>null</code>.
  136. * @throws IllegalArgumentException if <code>name</code>
  137. * is not either equal to
  138. * <code>"*"</code> or forms
  139. * a comma-separated list of
  140. * valid constraints, or if
  141. * <code>actions</code> is not
  142. * <code>null</code> or the
  143. * empty string.
  144. * @see #MBeanServerPermission(String,String)
  145. */
  146. public MBeanServerPermission(String name, String actions)
  147. {
  148. super(checkName(name), actions);
  149. if (actions != null && actions.length() > 0)
  150. throw new IllegalArgumentException("The supplied action list " +
  151. "was not equal to null or the " +
  152. "empty string.");
  153. }
  154. /**
  155. * Returns true if the given object is also an {@link MBeanServerPermission}
  156. * with the same name.
  157. *
  158. * @param obj the object to compare with this one.
  159. * @return true if the object is an {@link MBeanPermission}
  160. * with the same name.
  161. */
  162. public boolean equals(Object obj)
  163. {
  164. if (obj instanceof MBeanServerPermission)
  165. {
  166. MBeanServerPermission o = (MBeanServerPermission) obj;
  167. return o.getName().equals(getName());
  168. }
  169. return false;
  170. }
  171. /**
  172. * Returns a unique hash code for this permission.
  173. * This is simply the hashcode of {@link BasicPermission#getName()}.
  174. *
  175. * @return the hashcode of this permission.
  176. */
  177. public int hashCode()
  178. {
  179. return getName().hashCode();
  180. }
  181. /**
  182. * Returns true if this {@link MBeanServerPermission} implies
  183. * the given permission. This occurs if the given permission
  184. * is also an {@link MBeanServerPermission} and its target names
  185. * are a subset of the target names of this permission. Note that
  186. * the name <code>createMBeanServer</code> implies
  187. * <code>newMBeanServer</code>.
  188. *
  189. * @param p the permission to check for implication.
  190. * @return true if this permission implies <code>p</code>.
  191. */
  192. public boolean implies(Permission p)
  193. {
  194. if (p instanceof MBeanServerPermission)
  195. {
  196. if (getName().equals("*"))
  197. return true;
  198. MBeanServerPermission msp = (MBeanServerPermission) p;
  199. String[] thisCaps = getName().split(",");
  200. String[] mspCaps = msp.getName().split(",");
  201. for (int a = 0; a < mspCaps.length; ++a)
  202. {
  203. boolean found = false;
  204. String mc = mspCaps[a].trim();
  205. for (int b = 0; b < thisCaps.length; ++b)
  206. {
  207. String tc = thisCaps[b].trim();
  208. if (tc.equals(mc))
  209. found = true;
  210. if (tc.equals("createMBeanServer") &&
  211. mc.equals("newMBeanServer"))
  212. found = true;
  213. }
  214. if (!found)
  215. return false;
  216. }
  217. return true;
  218. }
  219. return false;
  220. }
  221. /**
  222. * Returns a {@link PermissionCollection} which stores
  223. * a series of {@link MBeanServerPermission}s as the union
  224. * of their capabilities.
  225. *
  226. * @return a collection for {@link MBeanServerPermission}s.
  227. */
  228. public PermissionCollection newPermissionCollection()
  229. {
  230. return new MBeanServerPermissionCollection();
  231. }
  232. /**
  233. * A collection of {@link MBeanServerPermission}s, stored
  234. * as a single permission with the union of the capabilities
  235. * as its capabilities.
  236. *
  237. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  238. * @since 1.5
  239. */
  240. private class MBeanServerPermissionCollection
  241. extends PermissionCollection
  242. {
  243. /**
  244. * Compatible with JDK 1.5
  245. */
  246. private static final long serialVersionUID = -5661980843569388590L;
  247. /**
  248. * The collected permission. This is <code>null</code> or
  249. * the union of the permissions held by all the collected
  250. * permissions.
  251. */
  252. private MBeanServerPermission collectionPermission;
  253. /**
  254. * Adds a new permission by unifying it with the existing
  255. * collection permission.
  256. *
  257. * @param p the permission to add.
  258. * @throws SecurityException if the collection is read only.
  259. * @see #isReadOnly()
  260. * @see #setReadOnly(boolean)
  261. */
  262. @Override
  263. public void add(Permission p)
  264. {
  265. if (isReadOnly())
  266. throw new SecurityException("This collection is read only.");
  267. if (p instanceof MBeanServerPermission)
  268. {
  269. MBeanServerPermission msp = (MBeanServerPermission) p;
  270. if (collectionPermission == null)
  271. collectionPermission = msp;
  272. else
  273. {
  274. String finalString = collectionPermission.getName();
  275. String[] cp = finalString.split(",");
  276. String[] np = msp.getName().split(",");
  277. int createms = finalString.indexOf("createMBeanServer");
  278. int newms = finalString.indexOf("newMBeanServer");
  279. for (int a = 0; a < np.length; ++a)
  280. {
  281. boolean found = false;
  282. String nps = np[a].trim();
  283. for (int b = 0; b < cp.length; ++b)
  284. {
  285. String cps = cp[b].trim();
  286. if (cps.equals(nps))
  287. found = true;
  288. if (nps.equals("newMBeanServer")
  289. && createms != -1)
  290. found = true;
  291. if (nps.equals("createMBeanServer")
  292. && newms != -1)
  293. finalString =
  294. finalString.replace("newMBeanServer",
  295. "createMBeanServer");
  296. }
  297. if (!found)
  298. finalString += "," + nps;
  299. }
  300. collectionPermission =
  301. new MBeanServerPermission(finalString);
  302. }
  303. }
  304. }
  305. /**
  306. * Returns an enumeration over the single permission.
  307. *
  308. * @return an enumeration over the collection permission.
  309. */
  310. @Override
  311. public Enumeration<Permission> elements()
  312. {
  313. return new
  314. MBeanServerPermissionEnumeration(collectionPermission);
  315. }
  316. /**
  317. * Provides an enumeration over a comma-separated list
  318. * of capabilities.
  319. *
  320. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  321. * @since 1.5
  322. */
  323. private class MBeanServerPermissionEnumeration
  324. implements Enumeration<Permission>
  325. {
  326. /**
  327. * The collected permission.
  328. */
  329. private MBeanServerPermission p;
  330. /**
  331. * True if we have returned the permission.
  332. */
  333. private boolean done;
  334. /**
  335. * Constructs a new {@link MBeanServerPermissionEnumeration}
  336. * using the given collected permission.
  337. *
  338. * @param p the collected permission.
  339. */
  340. public MBeanServerPermissionEnumeration(MBeanServerPermission p)
  341. {
  342. this.p = p;
  343. done = false;
  344. }
  345. /**
  346. * Returns true if there are more capabilities to return.
  347. *
  348. * @return true if there are more capabilities available.
  349. */
  350. public boolean hasMoreElements()
  351. {
  352. return !done;
  353. }
  354. /**
  355. * Returns the next capability.
  356. *
  357. * @return the next capability.
  358. */
  359. public Permission nextElement()
  360. {
  361. if (hasMoreElements())
  362. {
  363. done = true;
  364. return p;
  365. }
  366. else
  367. throw new NoSuchElementException("No more elements are available.");
  368. }
  369. }
  370. /**
  371. * Returns true if the collected {@link MBeanServerPermission}
  372. * implies the given permission. This occurs if the given permission
  373. * is also an {@link MBeanServerPermission} and its target names
  374. * are a subset of the target names of this permission. Note that
  375. * the name <code>createMBeanServer</code> implies
  376. * <code>newMBeanServer</code>.
  377. *
  378. * @param p the permission to check for implication.
  379. * @return true if this permission implies <code>p</code>.
  380. */
  381. @Override
  382. public boolean implies(Permission p)
  383. {
  384. return collectionPermission.implies(p);
  385. }
  386. }
  387. /**
  388. * Checks the name is valid, including removing
  389. * the <code>newMBeanServer</code> permission when
  390. * <code>createMBeanServer</code> is present.
  391. *
  392. * @param name the name to check.
  393. * @throws NullPointerException if <code>name</code>
  394. * is <code>null</code>.
  395. * @throws IllegalArgumentException if <code>name</code>
  396. * is not either equal to
  397. * <code>"*"</code> or forms
  398. * a comma-separated list of
  399. * valid constraints.
  400. */
  401. private static String checkName(String name)
  402. {
  403. if (!(name.equals("*")))
  404. {
  405. String[] constraints = name.split(",");
  406. name = "";
  407. boolean seenCreate = false;
  408. boolean seenNew = false;
  409. boolean start = true;
  410. for (int a = 0; a < constraints.length; ++a)
  411. {
  412. String next = constraints[a].trim();
  413. if (!(next.equals("createMBeanServer") ||
  414. next.equals("findMBeanServer") ||
  415. next.equals("newMBeanServer") ||
  416. next.equals("releaseMBeanServer")))
  417. throw new IllegalArgumentException("An invalid constraint, " +
  418. next + ", was specified.");
  419. if (next.equals("newMBeanServer"))
  420. seenNew = true;
  421. else if (next.equals("createMBeanServer"))
  422. seenCreate = true;
  423. else
  424. {
  425. if (!start)
  426. name += ",";
  427. name += next;
  428. start = false;
  429. }
  430. }
  431. if (seenNew && !seenCreate)
  432. name += (start ? "" : ",") + "newMBeanServer";
  433. else if (seenCreate)
  434. name += (start ? "" : ",") + "createMBeanServer";
  435. }
  436. return name;
  437. }
  438. }