001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.session;
019    
020    import java.util.Arrays;
021    import java.util.HashSet;
022    import org.apache.log4j.Logger;
023    import org.objectweb.jac.core.*;
024    import org.objectweb.jac.util.*;
025    
026    /**
027     * This aspect component handle the session aspect within JAC
028     * applications.
029     *
030     * <p>The session aspects memorizes some contextual informations as
031     * users id or password and link them to the current session id (the
032     * "Session.sid" attribute in the context). If the client correctly
033     * sets this attribute for each interaction, then the session aspect
034     * restores the saved information so that the user will not have to
035     * input extra information (such as his password) for each
036     * interaction.
037    
038     * @see org.objectweb.jac.aspects.session.SessionWrapper
039     *
040     * @author Renaud Pawlak */
041    
042    public class SessionAC extends AspectComponent implements SessionConf {
043        static Logger logger = Logger.getLogger("session");
044    
045        public static final String SESSION_ID = "Session.sid";
046        public static final String INITIALIZED = "Session.initialized";
047    
048        static {
049            Collaboration.setGlobal(SESSION_ID);
050            Collaboration.setGlobal(INITIALIZED);
051        }
052    
053        public String[] getDefaultConfigs() {
054            return new String[] {"org/objectweb/jac/aspects/session/session.acc"};
055        }
056    
057        SessionWrapper wrapper;
058    
059        public void clearCurrentSessionAttribute(String name) {
060            wrapper.clearCurrentSessionAttribute(name);
061        }
062    
063        protected SessionWrapper getWrapper() {
064            // We must wait until the AC is registered, because the wrapper
065            // stores the AC's name (which will be NULL is the AC is not registered)
066            if (wrapper==null)
067                wrapper = new SessionWrapper(this);
068            return wrapper;
069        }
070    
071        public void defineSessionHandlers(String classExpr, 
072                                          String methodExpr, 
073                                          String objectExpr) {
074            pointcut(objectExpr, classExpr, methodExpr, 
075                     getWrapper(), null);
076        }
077    
078        public void definePerSessionObjects(String classExpr, 
079                                            String objectExpr) {
080            logger.debug("defining per-session objects: "+
081                         classExpr+","+objectExpr );
082            pointcut(objectExpr, classExpr, "ALL", 
083                     PerSessionObjectWrapper.class.getName(), 
084                     null, false);
085        }
086    
087        HashSet storedAttributes = new HashSet();
088    
089        public void declareStoredAttributes(String attributes[]) {
090            storedAttributes.addAll(Arrays.asList(attributes));
091        }
092    
093        /**
094         * Stored attributes accessor.
095         *
096         * @return the stored attributes
097         */
098        public String[] getStoredAttributes() {
099            return (String[])storedAttributes.toArray(new String[] {});
100        }
101    
102    }