bugzilla 293506 - resolve double locking mechanism and address violation in getPermissions that jmx tickles.

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1017 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Jesse McConnell 2009-10-29 18:16:10 +00:00
parent 3562c5e457
commit bbbf5d7503
1 changed files with 27 additions and 63 deletions

View File

@ -94,29 +94,19 @@ public class JettyPolicy extends Policy
public PermissionCollection getPermissions(ProtectionDomain domain) public PermissionCollection getPermissions(ProtectionDomain domain)
{ {
if (!_initialized) synchronized (_initialized)
{ {
synchronized (_initialized) if (!_initialized)
{ {
// make sure we haven't been initialized since obtaining lock refresh();
if (!_initialized)
{
refresh();
}
} }
} }
if (_cache.containsKey(domain))
{
return _cache.get(domain);
}
synchronized (_cache) synchronized (_cache)
{ {
// check if it was added in since we obtained lock
if (_cache.containsKey(domain)) if (_cache.containsKey(domain))
{ {
return _cache.get(domain); return copyOf(_cache.get(domain));
} }
PermissionCollection perms = new Permissions(); PermissionCollection perms = new Permissions();
@ -158,36 +148,26 @@ public class JettyPolicy extends Policy
_cache.put(domain,perms); _cache.put(domain,perms);
return perms; return copyOf(perms);
} }
} }
@Override @Override
public PermissionCollection getPermissions(CodeSource codesource) public PermissionCollection getPermissions(CodeSource codesource)
{ {
if (!_initialized) synchronized (_initialized)
{ {
synchronized (_initialized) if (!_initialized)
{ {
// make sure we haven't been initialized since obtaining lock refresh();
if (!_initialized)
{
refresh();
}
} }
} }
if (_cache.containsKey(codesource))
{
return _cache.get(codesource);
}
synchronized (_cache) synchronized (_cache)
{ {
// check if it was added in since we obtained lock
if (_cache.containsKey(codesource)) if (_cache.containsKey(codesource))
{ {
return _cache.get(codesource); return copyOf(_cache.get(codesource));
} }
PermissionCollection perms = new Permissions(); PermissionCollection perms = new Permissions();
@ -225,45 +205,16 @@ public class JettyPolicy extends Policy
_cache.put(codesource,perms); _cache.put(codesource,perms);
return perms; return copyOf(perms);
} }
} }
@Override @Override
public boolean implies(ProtectionDomain domain, Permission permission) { public boolean implies(ProtectionDomain domain, Permission permission)
PermissionCollection pc; {
PermissionCollection pc = getPermissions(domain);
if (!_initialized)
{
synchronized (_initialized)
{
// make sure we haven't been initialized since obtaining lock
if (!_initialized)
{
refresh();
}
}
}
synchronized (_cache) {
pc = _cache.get(domain);
}
if (pc != null) {
return pc.implies(permission);
}
pc = getPermissions(domain); return (pc == null ? false : pc.implies(permission));
if (pc == null) {
return false;
}
synchronized (_cache) {
// cache it
_cache.put(domain, pc);
}
return pc.implies(permission);
} }
@ -398,4 +349,17 @@ public class JettyPolicy extends Policy
} }
write.flush(); write.flush();
} }
public PermissionCollection copyOf(final PermissionCollection in)
{
PermissionCollection out = new Permissions();
synchronized (in)
{
for (Enumeration el = in.elements() ; el.hasMoreElements() ;)
{
out.add((Permission)el.nextElement());
}
}
return out;
}
} }