001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.security;
030:
031: import com.caucho.loader.EnvironmentClassLoader;
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034:
035: import java.security.*;
036: import java.util.ArrayList;
037: import java.util.logging.Logger;
038:
039: /**
040: * Defines the policy for the current security context.
041: */
042: public class PolicyImpl extends Policy {
043: private static Logger _log;
044: private static L10N _L;
045:
046: private static final PolicyImpl _policy = new PolicyImpl();
047:
048: private ClassLoader _systemClassLoader;
049: private Policy _parent;
050:
051: private PolicyImpl() {
052: _parent = Policy.getPolicy();
053: _systemClassLoader = ClassLoader.getSystemClassLoader();
054: }
055:
056: public static PolicyImpl getPolicy() {
057: return _policy;
058: }
059:
060: public static void init() {
061: Policy.setPolicy(_policy);
062: }
063:
064: public PermissionCollection getPermissions(CodeSource codesource) {
065: PermissionCollection perms = new Permissions();
066: perms.add(new AllPermission());
067:
068: return perms;
069: }
070:
071: public PermissionCollection getPermissions(ProtectionDomain domain) {
072: PermissionCollection perms = new Permissions();
073: perms.add(new AllPermission());
074:
075: return perms;
076: }
077:
078: public boolean implies(ProtectionDomain domain,
079: Permission permission) {
080: /*
081: if (domain == null)
082: return true; // handle null value passed from RMI
083: */
084:
085: ClassLoader loader = domain.getClassLoader();
086:
087: if (loader == _systemClassLoader)
088: return true;
089:
090: // XXX: temporary to restore default security-manager
091: if (true)
092: return true;
093: if (true && _parent != null)
094: return _parent.implies(domain, permission);
095: else if (true)
096: return true;
097:
098: for (; loader != null; loader = loader.getParent()) {
099: if (loader instanceof EnvironmentClassLoader) {
100: EnvironmentClassLoader envLoader;
101: envLoader = (EnvironmentClassLoader) loader;
102:
103: ArrayList<Permission> perms = envLoader
104: .getPermissions();
105:
106: if (perms == null)
107: return _parent.implies(domain, permission);
108:
109: for (int i = perms.size() - 1; i >= 0; i--) {
110: Permission perm = perms.get(i);
111:
112: if (permission.implies(perm))
113: return true;
114: }
115:
116: return _parent.implies(domain, permission);
117: }
118: }
119:
120: if (loader == null)
121: return true;
122:
123: return true;
124: }
125:
126: public void refresh() {
127: }
128:
129: private Logger log() {
130: if (_log == null)
131: _log = Log.open(PolicyImpl.class);
132:
133: return _log;
134: }
135:
136: private L10N L() {
137: if (_L == null)
138: _L = new L10N(PolicyImpl.class);
139:
140: return _L;
141: }
142:
143: public String toString() {
144: return "PolicyImpl[]";
145: }
146: }
|