
Aspect-oriented programming with AspectJ Aspect-Oriented Programming with AspectJ™ the AspectJ.org team Xerox PARC Bill Griswold, Erik Hilsdale, Jim Hugunin, Vladimir Ivanovic, Mik Kersten, Gregor Kiczales, Jeffrey Palm partially funded by DARPA under contract F30602-97-C0246 this tutorial is about... • using AOP and AspectJ to: – improve the modularity of crosscutting concerns • design modularity • source code modularity • development process • aspects are two things: – concerns that crosscut [design level] – a programming construct [implementation level] • enables crosscutting concerns to be captured in modular units • AspectJ is: – is an aspect-oriented extension to Java™ that supports general-purpose aspect-oriented programming 2 AspectJ Tutorial (c) Copyright Xerox Corporation, 1998- 2001. All rights reserved. 1 Aspect-oriented programming with AspectJ problems like… logging is not modularized • where is logging in org.apache.tomcat – red shows lines of code that handle logging – not in just one place – not even in a small number of places 3 AspectJ Tutorial problems like… session expiration is not modularized ApplicationSession StandardSession /* public void invalidate() { package org.apache.tomcat.session; /** * ==================================================================== serverSession.removeApplicationSession (context); /** * Bind an object to this session, using the specified name.If an object * * Return the <code>HttpSession</code> for which this * of the same name is already bound to this session, the jectob is * The Apache Software License, Version 1.1 // remove everything in the session import java.io.IOException; object * replaced. * Copyright (c) 1999 The Apache Software Foundation. All rights Enumeration enum = values.keys(); import java.io.ObjectInputStreamObjectOutputStream; **/ is the facade. * <p>After this method executes, and if the object implements * reserved. while ( enum.hasMoreElements()) { import java.io.Serializable ; public HttpSession getSession() { * <code> HttpSessionBindingListener </code>, the container calls * String name = (String) enum.nextElement(); import java.util.Enumeration; * <code> valueBound()</code> on the object. * Redistribution and use in source and binary forms, with or thoutwi removeValue(name); import java.util. Hashtable; return (( HttpSession) this); * * modification, are permitted provided that the following conditions } import java.util.Vector; * @ param name Name to which the object is bound, cannot be null * are met: valid = false; importjavax.servlet .ServletException.http.HttpSession ; } * @ param value Object to be bound, cannot be null * 1. Redistributions of source code must retain the above copyright } importjavax.servlet .http.HttpSessionBindingEvent ; * @exception IllegalStateException if this method is called on an * notice, this list of conditions and the following disclaimer. importjavax.servlet .http.HttpSessionBindingListener; // ------------------------------------------------- * invalidated session * public boolean isNew() { importjavax.servlet .http.HttpSessionContext ; Session Public Methods * * 2. Redistributions in binary form must reproduce the above pyrightco if (! valid) { import org.apache.tomcat. catalina.*; * @deprecated As of Version 2.2, this method is replaced by * notice,the documentation this list ofand/or conditions other materialsand the following provided disclaimwith theer in Stringmsg = sm.getString (" applicationSession.session.ise"); import org.apache.tomcat.util.StringManager; /** **/ <code>setAttribute()</code> * distribution. throw newIllegalStateException(msg); * Update the accessed time information for this session. public void putValue(String name, Object value) { * } /** This method * 3. The end -user documentation included with the redistribution, if * Standard implementation of the <b>Session</b> * should be called by the context when a request comes in setAttribute (name, value); * any, must include the followingacknowlegement : if (thisAccessTime== creationTime) { interface. This object is for a particular * "This Apache product Software includes Foundation software (http://www.apache.org/)." developed by the } returnelse { true; persistent*serializable storage, so or that transferred it can be stored in **/ session, even if the application does not reference it. } * Alternately, thisacknowlegementmay appear in the software return false; * to a different JVM for distributable session public void access() { itself, } support. /** * if and wherever such third -partyacknowlegementsnormally appear. } * <p> this. lastAccessedTime = this. thisAccessedTime ; * Remove the object bound with the specified name from thissession. If * * <b>IMPLEMENTATION NOTE</b>: An instance of this this. thisAccessedTime = System. currentTimeMillis (); * the session does not have an object bound with this name,this method * 4. Foundation"The names "Themust Jakartanot be usedProject", to endorse "Tomcat", or promoteand "Apache product Softwares /** class* internal represents (Session) both the and application level } this. isNew=false; * <p> does nothing. derived * @deprecated (HttpSession) view of the session. * After this method executes, and if the object implements * from this software without prior written permission. Forritten w */ * However, because the class itself is not declared * <code> HttpSessionBindingListener </code>, the container calls * permission, please contact [email protected]. public, Java logic outside /** * <code> valueUnbound ()</code> on the object. * public void putValue(String name, Object value) { * of the <code>org.apache.tomcat.session</code> * Perform the internal processing required to invalidate * * 5. norProducts may "Apache" derived appearfrom this in theirsoftware names may without not be calledprior writt "Apache"en } setAttribute (name, value); package*HttpSession cannot castview an of this instance back to a this session,* without triggering an exception if the session has * @ param name Name of the object to remove from this session. * permission of the Apache Group. Session view. already expired. * @exception IllegalStateException if this method is called on an * public void setAttribute(String name, Object value) { * */ * invalidated session * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED if (! valid) { * @author Craig R. McClanahan public void expire() { */ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES Stringmsg = sm.getString (" applicationSession.session.ise"); * @version $Revision: 1.2 $ $Date: 2000/05/15 public void removeAttribute(String name) { * DISCLAIMED.OF MERCHANTABILITY IN NO ANDEVENT FITNESS SHALL FORTHE AAPACHE PARTICULAR SOFTWARE PURPOSE FOUNDATION ARE OR throw newIllegalStateException(msg); 17:54:10*/ $ sessions // Remove this session from our manager's active synchronized (attributes) { * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, } if ((manager != null) && (manager instanceof Object object = attributes.get(name); * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUTNOT final classStandardSession ManagerBase)) if (object == null) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSSOF if (name == null) { implementsHttpSession, Session { ((ManagerBase ) manager).remove(this); return; * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND Stringmsg = sm.getString (" applicationSession.value.iae"); attributes.remove(name); * ORON TORTANY THEORY(INCLUDING OF LIABILITY, NEGLIGENCE WHETHER OR OTHERWISE) IN CONTRACT, ARISING STRICT IN ANYLIABI LITY,YWA OUT throw newIllegalArgumentException (msg); // ---------------------------------------------- //Vector Unbind results any objects= new associatedVector(); with this session //if (object System.out.instanceofprintln HttpSessionBindingListener( "Removing attribute) { " + name ); * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF } ------------- Constructors Enumeration attrs =getAttributeNames(); ((HttpSessionBindingListener ) object).valueUnbound * SUCH DAMAGE. while ( attrs .hasMoreElements()) { (new HttpSessionBindingEvent((HttpSession ) this, name)); * ==================================================================== removeValue(name); // remove any existing binding Stringattr= (String) attrs.nextElement(); } * /** results. addElement(attr); } * individualsThis software on consistsbehalf ofof thevoluntary Apache Softwarecontributions Foundation. made by Formanymore if HttpSessionBindingEvent(value != null && value instanceofe = HttpSessionBindingListener) { specified* Construct Manager. a new Session associated with the }Enumeration names = results.elements(); } * information on the Apache Software Foundation, please see new HttpSessionBindingEvent(this, name); * while (names.hasMoreElements()) { * <http://www.apache.org/>. * @ param manager The manager with which this String name = (String) names. nextElement(); * ((HttpSessionBindingListener)value).valueBound (e); Session is associated removeAttribute (name); /** * [Additional notices, if required by prior licensing conditions] } */ } * Remove the object bound with the specified name from thissession. If **/ values.put(name, value); public StandardSession(Manager manager) { // Mark this session as invalid * doesthe nothing. session does not have an object bound with this name,this method } super(); setValid(false); * <p> this.manager = manager; * After this method executes, and if the object implements /** } * <code> HttpSessionBindingListener </code>, the container
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages78 Page
-
File Size-