carfield.com.hk AnotherRealSubject.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.aspectj; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ /** * This class will serve as a Delegate for RealSubject. Note that no * interfaces need to be implemented. Note further that it is possible to * have the delegate's method be static (illustrated here). * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see RealSubject */ public class AnotherRealSubject{ /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;write(String)&lt;/code&gt;. * * @param s the string to print */ public static void print(String s) { System.out.println(&quot;[AnotherRealSubject.print:] &quot;+s); } /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;print(String)&lt;/code&gt; * * @param s the string to print */ public static void write(String s) { System.out.println(&quot;[AnotherRealSubject.write:] &quot;+s); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z DelegationProxy.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.aspectj; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import ca.ubc.cs.spl.pattern.library.ProxyProtocol; import org.aspectj.lang.JoinPoint; /** * Implements a concrete proxy pattern instance. Here, all method calls from * &lt;code&gt;Main&lt;/code&gt; to &lt;code&gt;RealSubject.print(String)&lt;/code&gt; are blocked.&lt;p&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public aspect DelegationProxy extends ProxyProtocol { /** * Assigns the &lt;i&gt;Subject&lt;/i&gt; role to &lt;code&gt;RealSubject&lt;/code&gt;. */ declare parents: RealSubject implements Subject; /** * Captures all accesses to the subject that should be protected by * this pattern instance. Here: All calls to &lt;code&gt;RealSubject.write(..) * &lt;/code&gt;. */ protected pointcut protectedAccess(): call(* RealSubject.write(..)); /** * Checks whether the access should be denied or not. Here: All accesses * matched by the &lt;code&gt;protectedAccesses()&lt;/code&gt; joinpoint. * * @param caller the object responsible for the protected access * @param subject the subject receiving the call * @param joinPoint the joinpoint associated with the protected access * * @returns true */ protected boolean rejection(Object caller, Subject subject, JoinPoint joinPoint) { System.out.println(&quot;[DelegationProxy] delegating a write() call to &quot; +&quot;AnotherRealSubject&quot;); return true; } /** * For delegation: Provides an alternative return value if access * is denied. Here, it also calls an appropriate method on a delegate * (to illustrate how delegation works). * * @param caller the object responsible for the protected access * @param subject the subject receiving the call * @param joinPoint the joinpoint associated with the protected access * * @returns null */ protected Object handleFailedAccess(Object caller, Subject subject, JoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); if (args != null) { AnotherRealSubject.write((String)args[0]); } else { AnotherRealSubject.write(&quot;&quot;); } return null; } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Main.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.aspectj; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ /** * Implements the driver for the proxy design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Provide a surrogate or placeholder for another object to control * access to it.&lt;/i&gt;&lt;p&gt; * * Participatng objects are &lt;code&gt;RealSubject&lt;/code&gt;s and &lt;code&gt;Proxy&lt;/code&gt;. * Both implement the &lt;code&gt;Subject&lt;/code&gt; interface. * * &lt;i&gt;Proxy&lt;/i&gt; forwards or blocks requests to &lt;i&gt;RealSubject&lt;/i&gt; * * &lt;p&gt;&lt;i&gt;This is the AspectJ version.&lt;/i&gt;&lt;p&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see RealSubject * @see AnotherRealSubject * @see ProtectionProxy * @see DelegationProxy */ public class Main { /** * Creates a new Main object and runs the test suite. */ public Main() { /** * The &lt;i&gt;RealSubject&lt;/i&gt; that the client sends all requests to */ RealSubject real = new RealSubject(); real.print(&quot;PRINT&quot;); real.write(&quot;WRITE&quot;); real.print(&quot;PRINT&quot;); real.write(&quot;WRITE&quot;); } /** * Implements the driver for the proxy design pattern example. * Experimental setup (AOP): RealSubject and Proxy are identical. * No need for common interface. Concrete Aspect intercepts * method calls of interest. No performance penalty for methods * that are not of interest. &lt;p&gt; * * Note that this library aspect is not suited for remote or * virtual proxies.&lt;p&gt; * * This example illustrates two applications of the proxy pattern. * One pattern instance protects the &lt;code&gt;print(String)&lt;/code&gt; on * &lt;code&gt;RealSubject&lt;/code&gt;s and denies access to callers of type * &lt;code&gt;Main&lt;/code&gt;. The other instance intercepts calls to * &lt;code&gt;print(String)&lt;/code&gt; on &lt;code&gt;RealSubject&lt;/code&gt;s and * delegates them to &lt;code&gt;AnotherRealSubject&lt;/code&gt;. */ public static void main (String[] args) { Main main = new Main(); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z ProtectionProxy.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.aspectj; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import ca.ubc.cs.spl.pattern.library.ProxyProtocol; import org.aspectj.lang.JoinPoint; /** * Implements a concrete proxy pattern instance. Here, all method calls from * &lt;code&gt;Main&lt;/code&gt; to &lt;code&gt;RealSubject.print(String)&lt;/code&gt; are blocked.&lt;p&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public aspect ProtectionProxy extends ProxyProtocol { /** * Assigns the &lt;i&gt;Subject&lt;/i&gt; role to &lt;code&gt;RealSubject&lt;/code&gt;. */ declare parents: RealSubject implements Subject; /** * Captures all accesses to the subject that should be protected by * this pattern instance. Here: All calls to &lt;code&gt;RealSubject.print(..) * &lt;/code&gt;. */ protected pointcut protectedAccess(): call(* RealSubject.print(..)); /** * Checks whether the access should be denied or not. Here: All accesses * that come from &lt;code&gt;Main&lt;/code&gt; objects are denied. * * @param caller the object responsible for the protected access * @param subject the subject receiving the call * @param joinPoint the joinpoint associated with the protected access * * @returns true if the access is from a Main object, false otherwise */ protected boolean rejection(Object caller, Subject subject, JoinPoint joinPoint) { if (joinPoint.getThis() instanceof Main) { System.out.println(&quot;[MyRejecting:] Intercepting calls to &quot; +&quot;RealSubject.print() from Main&quot;); return true; } else { return false; } } /** * For delegation: Provides an alternative return value if access * is denied. * * @param caller the object responsible for the protected access * @param subject the subject receiving the call * @param joinPoint the joinpoint associated with the protected access * * @returns null */ protected Object handleFailedAccess(Object caller, Subject subject, JoinPoint joinPoint) { return null; } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z RealSubject.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.aspectj; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ /** * Implements a &lt;i&gt;RealSubject&lt;/i&gt; according to GoF. Note that it does not * have to implement any interface * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see AnotherRealSubject */ public class RealSubject { /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;write(String)&lt;/code&gt; * * @param s the string to print */ public void print(String s) { System.out.println(&quot;[RealSubject.print:] &quot;+s); } /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;print(String)&lt;/code&gt; * * @param s the string to print */ public void write(String s) { System.out.println(&quot;[RealSubject.write:] &quot;+s); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z files.lst 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z