RestrictorFactory { public static Restrictor createRestrictor(Configuration pConfig, LogHandler logHandler) { Restrictor customRestrictor = createCustomRestrictor(pConfig); if (customRestrictor != null) { logHandler.info("Using restrictor " + customRestrictor.getClass().getCanonicalName()); return customRestrictor; } String location = NetworkUtil.replaceExpression(pConfig.get(ConfigKey.POLICY_LOCATION)); try { Restrictor ret = RestrictorFactory.lookupPolicyRestrictor(location); if (ret != null) { logHandler.info("Using policy access restrictor " + location); return ret; } else { logHandler.info("No access restrictor found, access to any MBean is allowed"); return new AllowAllRestrictor(); } } catch (IOException e) { logHandler.error("Error while accessing access restrictor at " + location + ". Denying all access to MBeans for security reasons. Exception: " + e, e); return new DenyAllRestrictor(); } } private  RestrictorFactory(); static Restrictor createRestrictor(Configuration pConfig, LogHandler logHandler); static PolicyRestrictor lookupPolicyRestrictor(String pLocation);  }
@Test public void testAllowAllRestrictor() throws Exception { Configuration config = getConfig(); Restrictor restrictor = RestrictorFactory.createRestrictor(config, dummyLogHandler); assertTrue(restrictor.getClass().isAssignableFrom(AllowAllRestrictor.class)); }
@Test public void testDenyAllRestrictor() throws Exception { Configuration config = getConfig(ConfigKey.POLICY_LOCATION, "file: Restrictor restrictor = RestrictorFactory.createRestrictor(config, dummyLogHandler); assertTrue(restrictor.getClass().isAssignableFrom(DenyAllRestrictor.class)); }
@Test public void testPolicyRestrictor() throws Exception { Configuration config = getConfig(ConfigKey.POLICY_LOCATION, "classpath:/access-restrictor-factory-test.xml"); Restrictor restrictor = RestrictorFactory.createRestrictor(config, dummyLogHandler); assertTrue(restrictor.getClass().isAssignableFrom(PolicyRestrictor.class)); }
@Test public void testCustomRestrictor() throws Exception { Configuration config = getConfig(ConfigKey.RESTRICTOR_CLASS, "org.jolokia.restrictor.TestRestrictor"); Restrictor restrictor = RestrictorFactory.createRestrictor(config, dummyLogHandler); assertTrue(restrictor.getClass().isAssignableFrom(TestRestrictor.class)); }