001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management;
009:
010: import java.security.BasicPermission;
011:
012: /**
013: * A Permission to perform actions related to MBeanServers.
014: * The <em>name</em> of the permission specifies the operation requested
015: * or granted by the permission. For a granted permission, it can be
016: * <code>*</code> to allow all of the MBeanServer operations specified below.
017: * Otherwise, for a granted or requested permission, it must be one of the
018: * following:
019: * <dl>
020: * <dt>createMBeanServer</dt>
021: * <dd>Create a new MBeanServer object using the method
022: * {@link javax.management.MBeanServerFactory#createMBeanServer()} or
023: * {@link javax.management.MBeanServerFactory#createMBeanServer(java.lang.String)}.
024: * <dt>findMBeanServer</dt>
025: * <dd>Find an MBeanServer with a given name, or all MBeanServers in this
026: * JVM, using the method {@link javax.management.MBeanServerFactory#findMBeanServer}.
027: * <dt>newMBeanServer</dt>
028: * <dd>Create a new MBeanServer object without keeping a reference to it,
029: * using the method {@link javax.management.MBeanServerFactory#newMBeanServer()} or
030: * {@link javax.management.MBeanServerFactory#newMBeanServer(java.lang.String)}.
031: * <dt>releaseMBeanServer</dt>
032: * <dd>Remove the MBeanServerFactory's reference to an MBeanServer,
033: * using the method {@link javax.management.MBeanServerFactory#releaseMBeanServer}.
034: * </dl>
035: * The <em>name</em> of the permission can also denote a list of one or more
036: * comma-separated operations. Spaces are allowed at the beginning and
037: * end of the <em>name</em> and before and after commas.
038: * <p>
039: * <code>MBeanServerPermission("createMBeanServer")</code> implies
040: * <code>MBeanServerPermission("newMBeanServer")</code>.
041: *
042: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
043: */
044:
045: public class MBeanServerPermission extends BasicPermission {
046:
047: public static final String CREATE_MBEAN_SERVER = "createMBeanServer";
048: public static final String FIND_MBEAN_SERVER = "findMBeanServer";
049: public static final String NEW_MBEAN_SERVER = "newMBeanServer";
050: public static final String RELEASE_MBEAN_SERVER = "releaseMBeanServer";
051: private static final String knownNames[] = { CREATE_MBEAN_SERVER,
052: FIND_MBEAN_SERVER, NEW_MBEAN_SERVER, RELEASE_MBEAN_SERVER };
053:
054: private final static MBeanServerPermission CREATE_MBEAN_SERVER_PERMISSION = new MBeanServerPermission(
055: CREATE_MBEAN_SERVER);
056: private final static MBeanServerPermission FIND_MBEAN_SERVER_PERMISSION = new MBeanServerPermission(
057: FIND_MBEAN_SERVER);
058: private final static MBeanServerPermission NEW_MBEAN_SERVER_PERMISSION = new MBeanServerPermission(
059: NEW_MBEAN_SERVER);
060: private final static MBeanServerPermission RELEASE_MBEAN_SERVER_PERMISSION = new MBeanServerPermission(
061: RELEASE_MBEAN_SERVER);
062:
063: /**
064: * Create a new MBeanServerPermission with the given name.
065: * This constructor is equivalent to MBeanServerPermission(name,null).
066: *
067: * @param name the name of the granted permission.
068: * It must respect the constraints spelt out in the description of the MBeanServerPermission class.
069: */
070: public MBeanServerPermission(String name) {
071: this (name, null);
072: }
073:
074: /**
075: * Create a new MBeanServerPermission with the given name.
076: *
077: * @param name the name of the granted permission.
078: * It must respect the constraints spelt out in the description of the MBeanServerPermission class.
079: * @param action the associated actions.
080: * This parameter is not currently used and must be null or the empty string.
081: */
082: public MBeanServerPermission(String name, String action) {
083: super (name);
084: if (action != null && action.length() > 0)
085: throw new IllegalArgumentException(
086: "MBeanServerPermission actions must be null: "
087: + action);
088: if (name.equals("*"))
089: return;
090: for (int i = 0; i < knownNames.length; i++) {
091: if (name.equals(knownNames[i]))
092: return;
093: }
094: throw new IllegalArgumentException(
095: "unknown MBeanServerPermission: " + name);
096: }
097:
098: static MBeanServerPermission getInstance(String name)
099: throws SecurityException {
100: if (name.equalsIgnoreCase(CREATE_MBEAN_SERVER)) {
101: return CREATE_MBEAN_SERVER_PERMISSION;
102: } else if (name.equalsIgnoreCase(FIND_MBEAN_SERVER)) {
103: return FIND_MBEAN_SERVER_PERMISSION;
104: } else if (name.equalsIgnoreCase(NEW_MBEAN_SERVER)) {
105: return NEW_MBEAN_SERVER_PERMISSION;
106: } else if (name.equalsIgnoreCase(RELEASE_MBEAN_SERVER)) {
107: return RELEASE_MBEAN_SERVER_PERMISSION;
108: } else {
109: throw new SecurityException(
110: "No such MBeanServerPermission: " + name);
111: }
112: }
113: }
|