01: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.layout.restrictions;
07:
08: import org.jasig.portal.PortalException;
09: import org.jasig.portal.utils.ResourceLoader;
10:
11: import java.util.Properties;
12: import java.util.Enumeration;
13:
14: /**
15: * UserLayoutRestrictionFactory class.
16: * The factory for layout restrictions
17: *
18: * @author <a href="mailto:mvi@immagic.com">Michael Ivanov</a>
19: * @version $Revision: 35123 $
20: */
21:
22: public class UserLayoutRestrictionFactory {
23:
24: private static final String RESTRICTIONS_PATH = "/properties/al/restrictions.properties";
25:
26: private static Properties activeRestrictions;
27:
28: static {
29: try {
30: activeRestrictions = ResourceLoader
31: .getResourceAsProperties(
32: UserLayoutRestrictionFactory.class,
33: RESTRICTIONS_PATH);
34: } catch (Exception e) {
35: throw new RuntimeException("init: " + e.toString());
36: }
37: }
38:
39: private static IUserLayoutRestriction getRestriction(
40: String className) throws PortalException {
41: if (!activeRestrictions.contains(className))
42: throw new PortalException(
43: "The allowed set of restrictions does not contain the restriction '"
44: + className + "'");
45: try {
46: IUserLayoutRestriction restriction = (IUserLayoutRestriction) Class
47: .forName(className).newInstance();
48: for (Enumeration names = activeRestrictions.keys(); names
49: .hasMoreElements();) {
50: String name = (String) names.nextElement();
51: if (className.equals(activeRestrictions
52: .getProperty(name))) {
53: restriction.setName(name);
54: break;
55: }
56: }
57: return restriction;
58: } catch (Exception e) {
59: throw new PortalException("createRestriction: "
60: + e.toString());
61: }
62: }
63:
64: public static Properties getAvailableRestrictions() {
65: return activeRestrictions;
66: }
67:
68: public static IUserLayoutRestriction createRestriction(
69: String restrictionName) throws PortalException {
70: if (!activeRestrictions.containsKey(restrictionName))
71: throw new PortalException(
72: "The allowed set of restrictions does not contain the restriction '"
73: + restrictionName + "'");
74: return getRestriction(activeRestrictions
75: .getProperty(restrictionName));
76: }
77:
78: public static IUserLayoutRestriction createRestriction(
79: String restrictionName, String restrictionValue,
80: String restrictionPath) throws PortalException {
81: IUserLayoutRestriction restriction = createRestriction(restrictionName);
82: restriction.setRestrictionExpression(restrictionValue);
83: restriction.setRestrictionPath(restrictionPath);
84: return restriction;
85: }
86:
87: public static IUserLayoutRestriction createRestriction(
88: String restrictionName, String restrictionValue)
89: throws PortalException {
90: return createRestriction(restrictionName, restrictionValue,
91: IUserLayoutRestriction.LOCAL_RESTRICTION_PATH);
92: }
93:
94: }
|