01: /*
02: * Created on 6 Aug 2006
03: */
04: package uk.org.ponder.beanutil.support;
05:
06: import uk.org.ponder.beanutil.WriteableBeanLocator;
07: import uk.org.ponder.stringutil.StringList;
08: import uk.org.ponder.stringutil.StringSet;
09: import uk.org.ponder.util.UniversalRuntimeException;
10:
11: public class SecuredBeanLocator implements WriteableBeanLocator {
12:
13: private StringSet permittedroots = new StringSet();
14: private WriteableBeanLocator wbl;
15:
16: public void setPermittedBeanRoots(StringList permittedroots) {
17: this .permittedroots.addAll(permittedroots);
18: //this.permittedroots = permittedroots.toStringArray();
19: }
20:
21: private boolean isPermitted(String rootpath) {
22: return permittedroots != null
23: && permittedroots.contains(rootpath);
24: }
25:
26: public void setBeanLocator(WriteableBeanLocator wbl) {
27: this .wbl = wbl;
28: }
29:
30: private static void reportImpermissiblePath(String rootpath) {
31: throw UniversalRuntimeException.accumulate(
32: new SecurityException(), "Impermissible bean path "
33: + rootpath);
34: }
35:
36: public Object locateBean(String path) {
37: if (isPermitted(path)) {
38: return wbl.locateBean(path);
39: } else {
40: reportImpermissiblePath(path);
41: return null;
42: }
43: }
44:
45: private void checkRootPath(String rootpath) {
46: if (!isPermitted(rootpath)) {
47: reportImpermissiblePath(rootpath);
48: }
49: }
50:
51: public boolean remove(String beanname) {
52: checkRootPath(beanname);
53: return wbl.remove(beanname);
54: }
55:
56: public void set(String beanname, Object toset) {
57: checkRootPath(beanname);
58: wbl.set(beanname, toset);
59: }
60:
61: }
|