001: package de.webman.acl;
002:
003: import com.teamkonzept.lib.ConfigurationManager;
004: import com.teamkonzept.lib.TKException;
005: import com.teamkonzept.lib.TKVector;
006: import de.webman.acl.db.*;
007: import de.webman.acl.resolver.ResolverFactory;
008: import com.teamkonzept.webman.mainint.WebmanExceptionHandler;
009:
010: /**
011: * Factory for policy objects.
012: *
013: * @version 1.0
014: * @since 1.0
015: * @author © 2001 Webman AG
016: */
017: public class PolicyFactory extends ObjectFactoryBase implements
018: ObjectFactory {
019:
020: // $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/PolicyFactory.java,v 1.1 2001/08/20 08:25:07 mischa Exp $
021:
022: // Constants
023:
024: /**
025: * Singleton instance.
026: */
027: private static PolicyFactory SINGLETON = null;
028:
029: // Constructors
030:
031: /**
032: * Inhibits instantiation from outside.
033: */
034: private PolicyFactory() {
035: super ();
036: }
037:
038: // Instance
039:
040: /**
041: * Returns the singleton instance of the factory.
042: *
043: * @return the singleton instance of the factory.
044: * @exception com.teamkonzept.lib.TKException if an error occured during initialization.
045: */
046: public static synchronized final PolicyFactory getInstance()
047: throws TKException {
048: if (SINGLETON == null) {
049: SINGLETON = new PolicyFactory();
050: SINGLETON.configurationChanged();
051: ConfigurationManager.getInstance()
052: .registerConfigurationListener(SINGLETON,
053: PROPERTY_GROUP_NAME);
054: }
055:
056: return SINGLETON;
057: }
058:
059: // Method implementations
060:
061: /**
062: * Returns the policy database interface.
063: *
064: * @return the policy database interface.
065: */
066: public final ObjectDBInterface getDBInterface() {
067: return PolicyDBInterface.getInstance();
068: }
069:
070: /**
071: * Returns a policy data wrapper.
072: *
073: * @param id the ID of the policy.
074: * @return a policy data wrapper.
075: */
076: public final ObjectDBData getDBData(Integer id) {
077: return new PolicyDBData(id, null, null, null, null, null, null);
078: }
079:
080: /**
081: * Returns a policy data wrapper.
082: *
083: * @param object the policy.
084: * @return a policy data wrapper.
085: */
086: public final ObjectDBData getDBData(WMObject object) {
087: return new PolicyDBData((Policy) object);
088: }
089:
090: /**
091: * Builds a concrete policy object.
092: *
093: * @param data the initial policy data.
094: * @return a concrete policy object.
095: */
096: public final WMObject buildObject(ObjectDBData data) {
097: return new Policy((PolicyDBData) data);
098: }
099:
100: // Convenience methods
101:
102: /**
103: * Retrieves the specified policy.
104: *
105: * @param id the ID of the policy.
106: * @return the specified policy.
107: * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
108: */
109: public final Policy getPolicy(Integer id) throws TKException {
110: return (Policy) getObject(id);
111: }
112:
113: /**
114: * Retrieves all known policies.
115: *
116: * @return all known policies.
117: * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
118: */
119: public final TKVector getPolicies() throws TKException {
120: return getObjects();
121: }
122:
123: /**
124: * Retrieves all policy IDs referenced by the specified login.
125: *
126: * @param login the ID of the login (<I>required</I>).
127: * @param context the ID of the context (<I>optional</I>).
128: * @param type the object type (<I>optional</I>).
129: * @param reference the object reference (<I>optional</I>).
130: * @return all policy IDs referenced by the specified login.
131: * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
132: */
133: public final TKVector getPolicyProxies(Integer login,
134: Integer context, Integer type, Integer reference)
135: throws TKException {
136: TKVector proxies = null;
137:
138: try {
139: // Create appropriate data.
140: PolicyDBData data = new PolicyDBData(null, login, null,
141: context, type, reference, null);
142: // Choose appropriate query.
143: Class choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER;
144:
145: if (context != null) {
146: choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER_CONTEXT;
147:
148: if (type != null) {
149: if (Policy.CONTENT_TREE_ID.equals(type)) {
150: choice = PolicyDBInterface.WM_POLICY_SELECT_BY_CONTENT_TREE;
151: } else if (Policy.SITE_TREE_ID.equals(type)) {
152: choice = PolicyDBInterface.WM_POLICY_SELECT_BY_SITE_TREE;
153: } else {
154: choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER_CONTEXT_TYPE;
155:
156: if (reference != null) {
157: choice = PolicyDBInterface.WM_POLICY_SELECT_BY_USER_CONTEXT_TYPE_REFERENCE;
158: }
159: }
160: }
161: }
162:
163: data.setQuery(choice);
164: data.setPrototype(new ObjectCollectionDBData(null, null,
165: PolicyDBInterface.PRIMARY_KEY_NAME, null));
166:
167: // Database lookup.
168: proxies = getObjectIDs(data);
169: } catch (Exception x) {
170: throw WebmanExceptionHandler.getException(x);
171: }
172:
173: return proxies;
174: }
175:
176: /**
177: * Retrieves all policy IDs referenced by the specified event.
178: *
179: * @param event the ID of the event (<I>required</I>).
180: * @param login the ID of the login (<I>required</I>).
181: * @param context the ID of the context (<I>required</I>).
182: * @param type the object type (<I>optional</I>).
183: * @param reference the object reference (<I>optional</I>).
184: * @param access the access mode of the policy.
185: * @return all policy IDs referenced by the specified event.
186: * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
187: */
188: public final TKVector getPolicyProxies(Integer event,
189: Integer login, Integer context, Integer type,
190: Integer reference, boolean access) throws TKException {
191: TKVector proxies = null;
192:
193: try {
194: // Create appropriate data.
195: PolicyDBData data = new PolicyDBData(null, login, null,
196: context, type, reference,
197: access ? PolicyDBInterface.MODE_ALLOW
198: : PolicyDBInterface.MODE_DENY);
199: // Choose appropriate query.
200: Class choice = PolicyDBInterface.WM_POLICY_SELECT_BY_ATOMIC_EVENT;
201:
202: if (Policy.CONTENT_TREE_ID.equals(type)) {
203: choice = PolicyDBInterface.WM_POLICY_SELECT_BY_CONTENT_TREE_EVENT;
204: } else if (Policy.SITE_TREE_ID.equals(type)) {
205: choice = PolicyDBInterface.WM_POLICY_SELECT_BY_SITE_TREE_EVENT;
206: }
207:
208: data.setQuery(choice);
209: data.setPrototype(new ObjectCollectionDBData(
210: EventDBInterface.PRIMARY_KEY_NAME, event,
211: PolicyDBInterface.PRIMARY_KEY_NAME, null));
212:
213: // Database lookup.
214: proxies = getObjectIDs(data);
215: } catch (Exception x) {
216: throw WebmanExceptionHandler.getException(x);
217: }
218:
219: return proxies;
220: }
221:
222: /**
223: * Creates the specified policy.
224: *
225: * @param login the login of the policy.
226: * @param role the role of the policy.
227: * @param context the context of the policy.
228: * @param type the object type of the policy.
229: * @param reference the object reference of the policy.
230: * @param access the access mode of the policy.
231: * @return the specified policy.
232: * @exception com.teamkonzept.lib.TKException if a policy with the specified
233: * attributes already exists or an error occured during policy creation.
234: */
235: public final Policy createPolicy(Login login, Role role,
236: Context context, Integer type, Integer reference,
237: boolean access) throws TKException {
238: return (Policy) createObject(new PolicyDBData(null, login
239: .getID(), role.getID(), context.getID(), type,
240: reference, access ? PolicyDBInterface.MODE_ALLOW
241: : PolicyDBInterface.MODE_DENY));
242: }
243:
244: /**
245: * Modifies the given policy.
246: *
247: * @param policy the policy to be modified.
248: * @exception com.teamkonzept.lib.TKException if an error occured during policy modification.
249: */
250: public final void modifyPolicy(Policy policy) throws TKException {
251: if (policy.isModifiedAssociations()) {
252: ResolverFactory.getInstance().removeResolver(
253: policy.getLogin());
254: }
255:
256: modifyObject(policy);
257: }
258:
259: /**
260: * Deletes the given policy.
261: *
262: * @param policy the policy to be deleted.
263: * @exception com.teamkonzept.lib.TKException if an error occured during policy deletion.
264: */
265: public final void deletePolicy(Policy policy) throws TKException {
266: ResolverFactory.getInstance().removeResolver(policy.getLogin());
267:
268: deleteObject(policy);
269: }
270:
271: }
|