001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.satsa.acl;
028:
029: import com.sun.satsa.acl.AccessControlManager;
030: import com.sun.satsa.util.Utils;
031:
032: import java.util.Vector;
033:
034: /**
035: * This class represents JCRMI permission.
036: */
037: public class JCRMIPermission {
038:
039: /**
040: * Hash modifier value.
041: */
042: private String hashModifier;
043: /**
044: * The list of class names.
045: */
046: private String[] classes;
047: /**
048: * The list of method identifiers.
049: */
050: private int[] methods;
051:
052: /**
053: * Constructor.
054: * @param hashModifier hash modifier string.
055: * @param classList the list of classes.
056: * @param methodList the list of method names and signatures.
057: */
058: public JCRMIPermission(String hashModifier, Vector classList,
059: Vector methodList) {
060:
061: this .hashModifier = hashModifier;
062:
063: classes = new String[classList.size()];
064: classList.copyInto(classes);
065:
066: if (!methodList.isEmpty()) {
067: methods = new int[methodList.size()];
068: for (int i = 0; i < methodList.size(); i++) {
069: if (methodList.elementAt(i) instanceof String) {
070: methods[i] = getMethodId(hashModifier,
071: (String) methodList.elementAt(i));
072: } else {
073: methods[i] = Utils.getInt((byte[]) methodList
074: .elementAt(i), 0);
075: }
076: }
077: }
078: }
079:
080: /**
081: * Verify if this permission allows to invoke this method.
082: * @param className the name of class.
083: * @param method method name and signature.
084: * @return true if this permission allows to invoke this method.
085: */
086: public boolean checkAccess(String className, String method) {
087:
088: boolean found = false;
089:
090: for (int i = 0; i < classes.length; i++) {
091:
092: if (classes[i].equals(className)) {
093: found = true;
094: break;
095: }
096: }
097:
098: if (!found) {
099: return false;
100: }
101:
102: if (methods == null) {
103: return true;
104: }
105:
106: int id = getMethodId(hashModifier, method);
107:
108: for (int i = 0; i < methods.length; i++) {
109: if (id == methods[i])
110: return true;
111: }
112: return false;
113: }
114:
115: /**
116: * Calculates method ID for given hash modifier and method name.
117: * @param hashModifier hash modifier value.
118: * @param method method name and signature.
119: * @return the identifier.
120: */
121: private static int getMethodId(String hashModifier, String method) {
122:
123: if (hashModifier != null) {
124: method = hashModifier + method;
125: }
126: byte data[] = Utils.stringToBytes(method);
127: data = AccessControlManager.getHash(data, 0, data.length);
128: return Utils.getInt(data, 0);
129: }
130: }
|