You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2004/09/01 10:25:02 UTC

svn commit: rev 37329 - cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl

Author: cziegeler
Date: Wed Sep  1 01:25:01 2004
New Revision: 37329

Modified:
   cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java
   cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/UserProfile.java
Log:
Improving group based profile manager

Modified: cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java
==============================================================================
--- cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java	(original)
+++ cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/GroupBasedProfileManager.java	Wed Sep  1 01:25:01 2004
@@ -23,17 +23,24 @@
 import java.util.Map;
 
 import org.apache.avalon.framework.CascadingRuntimeException;
+import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.portal.PortalService;
 import org.apache.cocoon.portal.coplet.CopletData;
+import org.apache.cocoon.portal.coplet.CopletFactory;
 import org.apache.cocoon.portal.coplet.CopletInstanceData;
 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.commons.collections.map.LinkedMap;
+import org.apache.commons.lang.exception.ExceptionUtils;
+import org.apache.excalibur.source.SourceNotFoundException;
 
 /**
  * The profile manager using the authentication framework.
@@ -102,6 +109,29 @@
         }
     }
 
+    /**
+     * Prepares the object by using the specified factory.
+     */
+    protected void prepareObject(Object object, PortalService service)
+    throws ProcessingException {
+        if ( object != null ) {
+            if (object instanceof Layout) {
+                service.getComponentManager().getLayoutFactory().prepareLayout((Layout)object);
+            } else if (object instanceof Collection) {
+                final CopletFactory copletFactory = service.getComponentManager().getCopletFactory();
+                final Iterator iterator = ((Collection)object).iterator();
+                while (iterator.hasNext()) {
+                    final Object o = iterator.next();
+                    if ( o instanceof CopletData ) {
+                        copletFactory.prepare((CopletData)o);
+                    } else if ( o instanceof CopletInstanceData) {
+                        copletFactory.prepare((CopletInstanceData)o);
+                    }
+                }
+            }
+        }
+    }
+
     /* (non-Javadoc)
      * @see org.apache.cocoon.portal.profile.ProfileManager#login()
      */
@@ -283,17 +313,40 @@
      * want to use a different authentication method just overwrite this
      * method.
      */
-    protected UserInfo getUserInfo(String portalName, String layoutKey) {
+    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);
-            info.setUserName(authManager.getState().getHandler().getUserId());
+
+            final RequestState state = authManager.getState();
+            final UserHandler handler = state.getHandler();
+
+            info.setUserName(handler.getUserId());
             try {
-                info.setGroup((String)authManager.getState().getHandler().getContext().getContextInfo().get("group"));
+                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].getValue());
+                }
+            }
+            info.setConfigurations(configs);
             return info;    
         } catch (ServiceException ce) {
             // ignore this here
@@ -315,10 +368,26 @@
             final UserProfile profile = new UserProfile();
         
             // first "load" the global data
-            profile.setCopletBaseDatas( this.getGlobalBaseDatas(loader, info) );
-            profile.setCopletDatas( this.getGlobalDatas(loader, info) );
+            profile.setCopletBaseDatas( this.getGlobalBaseDatas(loader, info, service) );
+            profile.setCopletDatas( this.getGlobalDatas(loader, info, service) );
             
             // now load the user/group specific data
+            if ( !this.getCopletInstanceDatas(loader, profile, info, service, CATEGORY_USER) ) {
+                if ( !this.getCopletInstanceDatas(loader, profile, info, service, CATEGORY_GROUP)) {
+                    if ( !this.getCopletInstanceDatas(loader, profile, info, service, CATEGORY_GLOBAL) ) {
+                        throw new ProcessingException("No profile for copletinstancedatas found.");
+                    }
+                }
+            }
+
+            if ( !this.getLayout(loader, profile, info, service, CATEGORY_USER) ) {
+                if ( !this.getLayout(loader, profile, info, service, CATEGORY_GROUP)) {
+                    if ( !this.getLayout(loader, profile, info, service, CATEGORY_GLOBAL) ) {
+                        throw new ProcessingException("No profile for layout found.");
+                    }
+                }
+            }
+
             return profile;
         } catch (ServiceException se) {
             throw new CascadingRuntimeException("Unable to get component profilels.", se);
@@ -328,7 +397,8 @@
     }
     
     protected Map getGlobalBaseDatas(final ProfileLS     loader,
-                                     final UserInfo      info) 
+                                     final UserInfo      info,
+                                     final PortalService service) 
     throws Exception {
         if ( this.copletBaseDatas == null ) {
             synchronized ( this ) {
@@ -341,7 +411,8 @@
                     parameters.put(ProfileLS.PARAMETER_PROFILETYPE, 
                                    ProfileLS.PROFILETYPE_COPLETBASEDATA);        
 
-                    this.copletBaseDatas = (Map)loader.loadProfile(key, parameters);                    
+                    this.copletBaseDatas = ((CopletBaseDataManager)(Map)loader.loadProfile(key, parameters)).getCopletBaseData();
+                    this.prepareObject(this.copletBaseDatas, service);
                 }
             }
         }
@@ -349,7 +420,8 @@
     }
     
     protected Map getGlobalDatas(final ProfileLS     loader,
-                                 final UserInfo      info) 
+                                 final UserInfo      info,
+                                 final PortalService service) 
     throws Exception {
         if ( this.copletDatas == null ) {
             synchronized ( this ) {
@@ -364,15 +436,83 @@
                     parameters.put(ProfileLS.PARAMETER_OBJECTMAP, 
                                    this.copletBaseDatas);
                     
-                    this.copletDatas = (Map)loader.loadProfile(key, parameters);                    
+                    this.copletDatas = ((CopletDataManager)(Map)loader.loadProfile(key, parameters)).getCopletData();                    
+                    this.prepareObject(this.copletDatas, service);
                 }
             }
         }
         return this.copletDatas;
     }
 
-    // Type: global, group or user
-    // ProfileType: layout, copletbasedata, copletdata or copletinstance-data
+    private boolean isSourceNotFoundException(Throwable t) {
+        while (t != null) {
+            if (t instanceof SourceNotFoundException) {
+                return true;
+            }
+            t = ExceptionUtils.getCause(t);
+        }
+        return false;
+    }
+
+    protected boolean getCopletInstanceDatas(final ProfileLS     loader,
+                                             final UserProfile   profile,
+                                             final UserInfo      info,
+                                             final PortalService service,
+                                             final String        category) 
+    throws Exception {
+        Map key = this.buildKey(category, 
+                                ProfileLS.PROFILETYPE_COPLETINSTANCEDATA, 
+                                info, 
+                                true);
+        Map parameters = new HashMap();
+        parameters.put(ProfileLS.PARAMETER_PROFILETYPE, 
+                       ProfileLS.PROFILETYPE_COPLETINSTANCEDATA);        
+        parameters.put(ProfileLS.PARAMETER_OBJECTMAP, 
+                       profile.getCopletDatas());
+
+        try {
+            CopletInstanceDataManager cidm = (CopletInstanceDataManager)loader.loadProfile(key, parameters);
+            profile.setCopletInstanceDatas(cidm.getCopletInstanceData());
+            this.prepareObject(profile.getCopletInstanceDatas(), service);
+            
+            return true;
+        } catch (Exception e) {
+            if (!isSourceNotFoundException(e)) {
+                throw e;
+            }
+            return false;
+        }
+    }
+
+    protected boolean getLayout(final ProfileLS     loader,
+                                final UserProfile   profile,
+                                final UserInfo      info,
+                                final PortalService service,
+                                final String        category) 
+    throws Exception {
+        final Map key = this.buildKey(category, 
+                                      ProfileLS.PROFILETYPE_LAYOUT,  
+                                      info, 
+                                      true);
+        final Map parameters = new HashMap();
+        parameters.put(ProfileLS.PARAMETER_PROFILETYPE, 
+                       ProfileLS.PROFILETYPE_LAYOUT);        
+        parameters.put(ProfileLS.PARAMETER_OBJECTMAP, 
+                       profile.getCopletInstanceDatas());
+        try {
+            Layout l = (Layout)loader.loadProfile(key, parameters);
+            this.prepareObject(l, service);
+            profile.setRootLayout(l);
+
+            return true;
+        } catch (Exception e) {
+            if (!isSourceNotFoundException(e)) {
+                throw e;
+            }
+            return false;
+        }
+    }
+
     protected Map buildKey(String        category,
                            String        profileType,
                            UserInfo      info,

Modified: cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/UserProfile.java
==============================================================================
--- cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/UserProfile.java	(original)
+++ cocoon/trunk/src/blocks/portal/java/org/apache/cocoon/portal/profile/impl/UserProfile.java	Wed Sep  1 01:25:01 2004
@@ -15,8 +15,12 @@
  */
 package org.apache.cocoon.portal.profile.impl;
 
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.cocoon.portal.layout.CompositeLayout;
+import org.apache.cocoon.portal.layout.Item;
 import org.apache.cocoon.portal.layout.Layout;
 
 /**
@@ -90,13 +94,6 @@
     }
     
     /**
-     * @param layouts The layouts to set.
-     */
-    public void setLayouts(Map layouts) {
-        this.layouts = layouts;
-    }
-    
-    /**
      * @return Returns the rootLayout.
      */
     public Layout getRootLayout() {
@@ -108,5 +105,24 @@
      */
     public void setRootLayout(Layout rootLayout) {
         this.rootLayout = rootLayout;
+        this.layouts = new HashMap();
+        this.cacheLayouts(this.layouts, rootLayout);
     }
+    
+    protected void cacheLayouts(Map layoutMap, Layout layout) {
+        if ( layout != null ) {
+            if ( layout.getId() != null ) {
+                layoutMap.put( layout.getId(), layout );
+            }
+            if ( layout instanceof CompositeLayout ) {
+                final CompositeLayout cl = (CompositeLayout)layout;
+                final Iterator i = cl.getItems().iterator();
+                while ( i.hasNext() ) {
+                    final Item current = (Item)i.next();
+                    this.cacheLayouts( layoutMap, current.getLayout() );
+                }
+            }
+        }        
+    }
+    
 }