001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import org.apache.avalon.framework.service.ServiceSelector;
021: import org.apache.lenya.ac.AccessController;
022: import org.apache.lenya.ac.AccessControllerResolver;
023: import org.apache.lenya.ac.AccreditableManager;
024: import org.apache.lenya.ac.GroupManager;
025: import org.apache.lenya.ac.IPRangeManager;
026: import org.apache.lenya.ac.RoleManager;
027: import org.apache.lenya.ac.UserManager;
028: import org.apache.lenya.cms.usecase.AbstractUsecase;
029:
030: /**
031: * Super class for access-control related usecases.
032: *
033: * @version $Id: AccessControlUsecase.java 407305 2006-05-17 16:21:49Z andreas $
034: */
035: public class AccessControlUsecase extends AbstractUsecase {
036:
037: /**
038: * Ctor.
039: */
040: public AccessControlUsecase() {
041: super ();
042: }
043:
044: private UserManager userManager;
045: private GroupManager groupManager;
046: private IPRangeManager ipRangeManager;
047: private RoleManager roleManager;
048: private AccessController accessController;
049:
050: /**
051: * Initializes the accreditable managers. FIXME: This method resolves the
052: * AccessController, it has to be released after it is used!
053: */
054: protected void initializeAccessController() {
055: super .doInitialize();
056:
057: if (getLogger().isDebugEnabled())
058: getLogger().debug("initializeAccessController() called");
059:
060: this .accessController = null;
061: ServiceSelector selector = null;
062: AccessControllerResolver resolver = null;
063:
064: try {
065: selector = (ServiceSelector) this .manager
066: .lookup(AccessControllerResolver.ROLE + "Selector");
067: resolver = (AccessControllerResolver) selector
068: .select(AccessControllerResolver.DEFAULT_RESOLVER);
069:
070: this .accessController = resolver
071: .resolveAccessController(getSourceURL());
072:
073: if (this .accessController == null) {
074: throw new RuntimeException(
075: "No access controller could be resolved for URL ["
076: + getSourceURL() + "].");
077: }
078:
079: AccreditableManager accreditableManager = this .accessController
080: .getAccreditableManager();
081:
082: this .userManager = accreditableManager.getUserManager();
083: this .groupManager = accreditableManager.getGroupManager();
084: this .roleManager = accreditableManager.getRoleManager();
085: this .ipRangeManager = accreditableManager
086: .getIPRangeManager();
087:
088: } catch (Exception e) {
089: throw new RuntimeException("Initialization failed: ", e);
090: } finally {
091: if (selector != null) {
092: if (resolver != null) {
093: selector.release(resolver);
094: }
095: this .manager.release(selector);
096: }
097: }
098:
099: }
100:
101: /**
102: * @return Returns the groupManager.
103: */
104: protected GroupManager getGroupManager() {
105: if (this .groupManager == null) {
106: initializeAccessController();
107: }
108: return this .groupManager;
109: }
110:
111: /**
112: * @return Returns the ipRangeManager.
113: */
114: protected IPRangeManager getIpRangeManager() {
115: if (this .ipRangeManager == null) {
116: initializeAccessController();
117: }
118: return this .ipRangeManager;
119: }
120:
121: /**
122: * @return Returns the roleManager.
123: */
124: protected RoleManager getRoleManager() {
125: if (this .roleManager == null) {
126: initializeAccessController();
127: }
128: return this .roleManager;
129: }
130:
131: /**
132: * @return Returns the userManager.
133: */
134: protected UserManager getUserManager() {
135: if (this .userManager == null) {
136: if (getLogger().isDebugEnabled())
137: getLogger()
138: .debug(
139: "getUserManager() accessed, is null, so calling initializeAccessController");
140: initializeAccessController();
141: }
142: return this .userManager;
143: }
144:
145: /**
146: * @return Returns the accessController.
147: */
148: protected AccessController getAccessController() {
149: if (this.accessController == null) {
150: initializeAccessController();
151: }
152: return this.accessController;
153: }
154: }
|