Author: cziegeler Date: Wed Sep 29 23:48:32 2004 New Revision: 47558 Added: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/AuthenticationFWUserInfoProvider.java cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/UserInfoProvider.java Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java Log: Decouple profile manager from authentication block
Added: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/AuthenticationFWUserInfoProvider.java ============================================================================== --- (empty file) +++ cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/AuthenticationFWUserInfoProvider.java Wed Sep 29 23:48:32 2004 @@ -0,0 +1,93 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.portal.profile.impl; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceManager; +import org.apache.avalon.framework.service.Serviceable; +import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.webapps.authentication.AuthenticationManager; +import org.apache.cocoon.webapps.authentication.configuration.ApplicationConfiguration; +import org.apache.cocoon.webapps.authentication.user.RequestState; +import org.apache.cocoon.webapps.authentication.user.UserHandler; + +/** + * Get the information about the current user. + * This implementation uses the authentication-fw block + * + * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> + * @version CVS $Id: MapProfileLS.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public class AuthenticationFWUserInfoProvider +implements UserInfoProvider, Serviceable { + + protected ServiceManager manager; + + + /* (non-Javadoc) + * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager) + */ + public void service(ServiceManager manager) throws ServiceException { + this.manager = manager; + } + + /* (non-Javadoc) + * @see org.apache.cocoon.portal.profile.impl.UserInfoProvider#getUserInfo(java.lang.String, java.lang.String) + */ + public UserInfo getUserInfo(String portalName, String layoutKey) + throws Exception { + AuthenticationManager authManager = null; + try { + authManager = (AuthenticationManager)this.manager.lookup(AuthenticationManager.ROLE); + final UserInfo info = new UserInfo(portalName, layoutKey); + + final RequestState state = authManager.getState(); + final UserHandler handler = state.getHandler(); + + info.setUserName(handler.getUserId()); + try { + info.setGroup((String)handler.getContext().getContextInfo().get("group")); + } catch (ProcessingException pe) { + // ignore this + } + + final ApplicationConfiguration ac = state.getApplicationConfiguration(); + if ( ac == null ) { + throw new ProcessingException("Configuration for portal not found in application configuration."); + } + final Configuration appConf = ac.getConfiguration("portal"); + if ( appConf == null ) { + throw new ProcessingException("Configuration for portal not found in application configuration."); + } + final Configuration config = appConf.getChild("profiles"); + final Configuration[] children = config.getChildren(); + final Map configs = new HashMap(); + if ( children != null ) { + for(int i=0; i < children.length; i++) { + configs.put(children[i].getName(), children[i].getAttribute("uri")); + } + } + info.setConfigurations(configs); + return info; + } finally { + this.manager.release( authManager ); + } + } +} Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java (original) +++ cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java Wed Sep 29 23:48:32 2004 @@ -23,7 +23,15 @@ import java.util.Map; import org.apache.avalon.framework.CascadingRuntimeException; -import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.activity.Disposable; +import org.apache.avalon.framework.activity.Initializable; +import org.apache.avalon.framework.container.ContainerUtil; +import org.apache.avalon.framework.context.Context; +import org.apache.avalon.framework.context.ContextException; +import org.apache.avalon.framework.context.Contextualizable; +import org.apache.avalon.framework.parameters.ParameterException; +import org.apache.avalon.framework.parameters.Parameterizable; +import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.service.ServiceException; import org.apache.avalon.framework.service.ServiceSelector; import org.apache.cocoon.ProcessingException; @@ -34,10 +42,7 @@ import org.apache.cocoon.portal.coplet.adapter.CopletAdapter; import org.apache.cocoon.portal.layout.Layout; import org.apache.cocoon.portal.profile.ProfileLS; -import org.apache.cocoon.webapps.authentication.AuthenticationManager; -import org.apache.cocoon.webapps.authentication.configuration.ApplicationConfiguration; -import org.apache.cocoon.webapps.authentication.user.RequestState; -import org.apache.cocoon.webapps.authentication.user.UserHandler; +import org.apache.cocoon.util.ClassUtils; import org.apache.commons.collections.map.LinkedMap; import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.excalibur.source.SourceNotFoundException; @@ -65,7 +70,8 @@ * @version CVS $Id: AbstractUserProfileManager.java 37123 2004-08-27 12:11:53Z cziegeler $ */ public class GroupBasedProfileManager - extends AbstractProfileManager { + extends AbstractProfileManager + implements Parameterizable, Contextualizable, Initializable, Disposable { public static final String CATEGORY_GLOBAL = "global"; public static final String CATEGORY_GROUP = "group"; @@ -76,6 +82,49 @@ protected Map copletBaseDatas; protected Map copletDatas; + /** The userinfo provider - the connection to the authentication mechanism */ + protected UserInfoProvider provider; + + /** The class name of the userinfo provider */ + protected String userInfoProviderClassName; + + /** The component context */ + protected Context context; + + /* (non-Javadoc) + * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context) + */ + public void contextualize(Context context) throws ContextException { + this.context = context; + } + + /* (non-Javadoc) + * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters) + */ + public void parameterize(Parameters params) throws ParameterException { + this.userInfoProviderClassName = params.getParameter("userinfo-provider"); + } + + /* (non-Javadoc) + * @see org.apache.avalon.framework.activity.Initializable#initialize() + */ + public void initialize() throws Exception { + this.provider = (UserInfoProvider)ClassUtils.newInstance(this.userInfoProviderClassName); + ContainerUtil.enableLogging(this.provider, this.getLogger()); + ContainerUtil.contextualize(this.provider, this.context); + ContainerUtil.service(this.provider, this.manager); + ContainerUtil.initialize(this.provider); + } + + /* (non-Javadoc) + * @see org.apache.avalon.framework.activity.Disposable#dispose() + */ + public void dispose() { + ContainerUtil.dispose(this.provider); + this.provider = null; + this.manager = null; + } + protected UserProfile getUserProfile(String layoutKey) { PortalService service = null; try { @@ -333,64 +382,15 @@ } /** - * Return the user info about the current user. - * This implementation uses the authentication framework - if you - * want to use a different authentication method just overwrite this - * method. - */ - protected UserInfo getUserInfo(String portalName, String layoutKey) - throws Exception { - AuthenticationManager authManager = null; - try { - authManager = (AuthenticationManager)this.manager.lookup(AuthenticationManager.ROLE); - final UserInfo info = new UserInfo(portalName, layoutKey); - - final RequestState state = authManager.getState(); - final UserHandler handler = state.getHandler(); - - info.setUserName(handler.getUserId()); - try { - info.setGroup((String)handler.getContext().getContextInfo().get("group")); - } catch (ProcessingException pe) { - // ignore this - } - - final ApplicationConfiguration ac = state.getApplicationConfiguration(); - if ( ac == null ) { - throw new ProcessingException("Configuration for portal not found in application configuration."); - } - final Configuration appConf = ac.getConfiguration("portal"); - if ( appConf == null ) { - throw new ProcessingException("Configuration for portal not found in application configuration."); - } - final Configuration config = appConf.getChild("profiles"); - final Configuration[] children = config.getChildren(); - final Map configs = new HashMap(); - if ( children != null ) { - for(int i=0; i < children.length; i++) { - configs.put(children[i].getName(), children[i].getAttribute("uri")); - } - } - info.setConfigurations(configs); - return info; - } catch (ServiceException ce) { - // ignore this here - return null; - } finally { - this.manager.release( authManager ); - } - } - - /** * Load the profile */ protected UserProfile loadProfile(final String layoutKey, final PortalService service) throws Exception { - final UserInfo info = this.getUserInfo(service.getPortalName(), layoutKey); + final UserInfo info = this.provider.getUserInfo(service.getPortalName(), layoutKey); ProfileLS loader = null; try { loader = (ProfileLS)this.manager.lookup( ProfileLS.ROLE ); - final UserProfile profile = new UserProfile(); + final UserProfile profile = new UserProfile(); this.storeUserProfile(layoutKey, service, profile); // first "load" the global data @@ -414,7 +414,7 @@ } } - return profile; + return profile; } catch (ServiceException se) { throw new CascadingRuntimeException("Unable to get component profilels.", se); } finally { @@ -465,8 +465,8 @@ this.copletDatas = ((CopletDataManager)loader.loadProfile(key, parameters)).getCopletData(); this.prepareObject(this.copletDatas, service); } - } -} + } + } return this.copletDatas; } Added: cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/UserInfoProvider.java ============================================================================== --- (empty file) +++ cocoon/branches/BRANCH_2_1_X/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/UserInfoProvider.java Wed Sep 29 23:48:32 2004 @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.portal.profile.impl; + +/** + * Get the information about the current user. + * This data object is used for loading the profile. It decouples the + * portal from the used authentication method. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> + * @version CVS $Id: MapProfileLS.java 30941 2004-07-29 19:56:58Z vgritsenko $ + */ +public interface UserInfoProvider { + + /** + * Return the user info about the current user. + */ + UserInfo getUserInfo(String portalName, String layoutKey) + throws Exception; +}