From 1dd4d93527472840b14b3f335437fdb8ea13efc4 Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Thu, 10 Sep 2009 19:52:16 +0000 Subject: [PATCH] 289145 reload functionality to jetty policy an remove refresh() calls from constructor git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@860 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../org/eclipse/jetty/policy/JettyPolicy.java | 105 +++++++++--------- .../jetty/policy/JettyPolicyConfigurator.java | 1 + .../java/org/eclipse/jetty/start/Config.java | 16 +-- 3 files changed, 64 insertions(+), 58 deletions(-) diff --git a/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicy.java b/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicy.java index 04b105d82be..65e89a7c793 100644 --- a/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicy.java +++ b/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicy.java @@ -57,6 +57,7 @@ import org.eclipse.jetty.util.Scanner; public class JettyPolicy extends Policy { private static boolean __DEBUG = false; + private static boolean __RELOAD = false; // Policy files that are actively managed by the aggregate policy mechanism private final Set _policies; @@ -67,36 +68,42 @@ public class JettyPolicy extends Policy private final PolicyContext _context = new PolicyContext(); - private final Scanner scanner = new Scanner(); + private Boolean _initialized = false; - private boolean initialized = false; + private Scanner _scanner; public JettyPolicy(Set policies, Map properties) { - System.out.println("Activating the JettyPolicy"); - try { + + __RELOAD = Boolean.getBoolean("org.eclipse.jetty.policy.RELOAD"); __DEBUG = Boolean.getBoolean("org.eclipse.jetty.policy.DEBUG"); } catch (AccessControlException ace) { + __RELOAD = false; __DEBUG = false; } _policies = policies; _context.setProperties(properties); - - refresh(); } @Override public PermissionCollection getPermissions(ProtectionDomain domain) { - if (!initialized) - { - refresh(); + if (!_initialized) + { + synchronized (_initialized) + { + // make sure we haven't been initialized since obtaining lock + if (!_initialized) + { + refresh(); + } + } } if (_cache.containsKey(domain)) @@ -158,11 +165,15 @@ public class JettyPolicy extends Policy @Override public PermissionCollection getPermissions(CodeSource codesource) { - if (!initialized) + if (!_initialized) { - synchronized (this) + synchronized (_initialized) { - refresh(); + // make sure we haven't been initialized since obtaining lock + if (!_initialized) + { + refresh(); + } } } @@ -222,10 +233,15 @@ public class JettyPolicy extends Policy public boolean implies(ProtectionDomain domain, Permission permission) { PermissionCollection pc; - if (_cache == null) { - synchronized (_cache) + if (!_initialized) + { + synchronized (_initialized) { - refresh(); + // make sure we haven't been initialized since obtaining lock + if (!_initialized) + { + refresh(); + } } } @@ -285,22 +301,21 @@ public class JettyPolicy extends Policy try { - if (!initialized) + // initialize the reloading mechanism if enabled + if (__RELOAD && _scanner == null) { - initialize(); + initializeReloading(); } - synchronized (_cache) - { - for (Iterator i = _cache.keySet().iterator(); i.hasNext();) - { - System.out.println(i.next().toString()); - } - } - if (__DEBUG) { - System.out.println("refreshing policy files"); + synchronized (_cache) + { + for (Iterator i = _cache.keySet().iterator(); i.hasNext();) + { + System.out.println(i.next().toString()); + } + } } Set clean = new HashSet(); @@ -318,12 +333,7 @@ public class JettyPolicy extends Policy _grants.addAll(clean); _cache.clear(); } - - if (__DEBUG) - { - System.out.println("finished reloading policies"); - } - + _initialized = true; } catch (Exception e) { @@ -331,17 +341,9 @@ public class JettyPolicy extends Policy } } - /** - * TODO make this optional MUST be only called from refresh or race condition - */ - private synchronized void initialize() throws Exception + private void initializeReloading() throws Exception { - - // if we have been initialized since we got lock, return - if (initialized) - { - return; - } + _scanner = new Scanner(); List scanDirs = new ArrayList(); @@ -351,33 +353,34 @@ public class JettyPolicy extends Policy scanDirs.add(policyFile.getParentFile()); } - scanner.addListener(new Scanner.DiscreteListener() + _scanner.addListener(new Scanner.DiscreteListener() { public void fileRemoved(String filename) throws Exception - { // TODO Auto-generated method stub + { } + /* will trigger when files are changed, not on load time, just when changed */ public void fileChanged(String filename) throws Exception { - if (filename.endsWith("policy")) + if (filename.endsWith("policy")) // TODO match up to existing policies to avoid unnecessary reloads { + System.out.println("JettyPolicy: refreshing policy files"); refresh(); + System.out.println("JettyPolicy: finished refreshing policies"); } } public void fileAdded(String filename) throws Exception - { // TODO Auto-generated method stub + { } }); - scanner.setScanDirs(scanDirs); - scanner.start(); - scanner.setScanInterval(10); - - initialized = true; + _scanner.setScanDirs(scanDirs); + _scanner.start(); + _scanner.setScanInterval(10); } public void dump(PrintStream out) diff --git a/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicyConfigurator.java b/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicyConfigurator.java index d397a1f5dda..58214c2f99b 100644 --- a/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicyConfigurator.java +++ b/jetty-policy/src/main/java/org/eclipse/jetty/policy/JettyPolicyConfigurator.java @@ -51,6 +51,7 @@ public class JettyPolicyConfigurator System.out.println("Initializing Jetty Policy"); JettyPolicy jpolicy = new JettyPolicy( _policies, _properties ); + jpolicy.refresh(); Policy.setPolicy(jpolicy); System.setSecurityManager(new SecurityManager()); } diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/Config.java b/jetty-start/src/main/java/org/eclipse/jetty/start/Config.java index cdc5b7be2b3..06c3f3d49f6 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/Config.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/Config.java @@ -153,9 +153,9 @@ public class Config /** * Natural language sorting for key names. */ - private Comparator keySorter = new Comparator() + private final Comparator keySorter = new Comparator() { - private Collator collator = Collator.getInstance(); + private final Collator collator = Collator.getInstance(); public int compare(String o1, String o2) { @@ -167,11 +167,11 @@ public class Config private static final String _version; private static boolean DEBUG = false; - private Map _classpaths = new HashMap(); - private List _xml = new ArrayList(); - private Set _policies = new HashSet(); + private final Map _classpaths = new HashMap(); + private final List _xml = new ArrayList(); + private final Set _policies = new HashSet(); private String _classname = null; - private Set _activeOptions = new TreeSet(new Comparator() + private final Set _activeOptions = new TreeSet(new Comparator() { // Make sure "*" is always at the end of the list public int compare(String o1, String o2) @@ -187,7 +187,7 @@ public class Config return o1.compareTo(o2); } }); - private Map _properties = new HashMap(); + private final Map _properties = new HashMap(); private int argCount = 0; private boolean addClasspathComponent(List sections, String component) @@ -959,6 +959,8 @@ public class Config if (policyClass instanceof Policy) { + Policy p = (Policy)policyClass; + p.refresh(); return (Policy)policyClass; }