001package org.apache.fulcrum.security.spi; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021import org.apache.avalon.framework.activity.Disposable; 022import org.apache.avalon.framework.logger.AbstractLogEnabled; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.avalon.framework.thread.ThreadSafe; 027import org.apache.fulcrum.security.GroupManager; 028import org.apache.fulcrum.security.PermissionManager; 029import org.apache.fulcrum.security.RoleManager; 030import org.apache.fulcrum.security.UserManager; 031import org.apache.fulcrum.security.util.DataBackendException; 032 033/** 034 * 035 * This abstract implementation provides most of the functionality that a 036 * manager will need. 037 * 038 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> 039 * @version $Id$ 040 */ 041public abstract class AbstractManager extends AbstractLogEnabled implements Serviceable, Disposable, ThreadSafe 042{ 043 boolean composed = false; 044 045 protected ServiceManager manager = null; 046 private PermissionManager permissionManager; 047 private RoleManager roleManager; 048 private GroupManager groupManager; 049 private UserManager userManager; 050 051 /** 052 * @return the service manager 053 */ 054 protected ServiceManager getServiceManager() 055 { 056 return manager; 057 } 058 059 /** 060 * @return the user manager 061 * @throws DataBackendException if fail to connect to datasource 062 */ 063 protected UserManager getUserManager() throws DataBackendException 064 { 065 if (userManager == null) 066 { 067 try 068 { 069 userManager = (UserManager) manager.lookup(UserManager.ROLE); 070 071 } 072 catch (ServiceException ce) 073 { 074 throw new DataBackendException(ce.getMessage(), ce); 075 } 076 } 077 return userManager; 078 } 079 080 /** 081 * @return the permission manager 082 * @throws DataBackendException if fail to connect to datasource 083 */ 084 protected PermissionManager getPermissionManager() throws DataBackendException 085 { 086 if (permissionManager == null) 087 { 088 try 089 { 090 permissionManager = (PermissionManager) manager.lookup(PermissionManager.ROLE); 091 092 } 093 catch (ServiceException ce) 094 { 095 throw new DataBackendException(ce.getMessage(), ce); 096 } 097 } 098 return permissionManager; 099 } 100 101 /** 102 * @return the role manager 103 * @throws DataBackendException if fail to connect to datasource 104 */ 105 protected RoleManager getRoleManager() throws DataBackendException 106 { 107 if (roleManager == null) 108 { 109 try 110 { 111 roleManager = (RoleManager) manager.lookup(RoleManager.ROLE); 112 113 } 114 catch (ServiceException ce) 115 { 116 throw new DataBackendException(ce.getMessage(), ce); 117 } 118 } 119 return roleManager; 120 } 121 122 /** 123 * @return the group manager 124 * @throws DataBackendException if fail to connect to datasource 125 */ 126 protected GroupManager getGroupManager() throws DataBackendException 127 { 128 if (groupManager == null) 129 { 130 try 131 { 132 groupManager = (GroupManager) manager.lookup(GroupManager.ROLE); 133 134 } 135 catch (ServiceException ce) 136 { 137 throw new DataBackendException(ce.getMessage(), ce); 138 } 139 } 140 return groupManager; 141 } 142 143 /** 144 * Avalon Service lifecycle method 145 * @throws ServiceException if fail to connect 146 */ 147 @Override 148 public void service(ServiceManager manager) throws ServiceException 149 { 150 this.manager = manager; 151 152 } 153 154 @Override 155 public void dispose() 156 { 157 release(roleManager); 158 release(permissionManager); 159 release(groupManager); 160 release(userManager); 161 manager = null; 162 } 163 164 /** 165 * @param obj the object to release 166 */ 167 protected void release(Object obj) 168 { 169 if (obj != null) 170 { 171 manager.release(obj); 172 } 173 } 174 175 /** 176 * @param lookup the object to resolve 177 * @return the actual object 178 * @throws RuntimeException exception if fails to find the manager 179 */ 180 protected Object resolve(String lookup) throws RuntimeException 181 { 182 Object component = null; 183 { 184 try 185 { 186 component = manager.lookup(lookup); 187 } 188 catch (ServiceException ce) 189 { 190 throw new RuntimeException(ce.getMessage(), ce); 191 } 192 } 193 return component; 194 } 195 196}