277294.ijbsn.asiaAnotherRealSubject.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>System.out</code>. This is
* equivalent to <code>write(String)</code>.
*
* @param s the string to print
*/
public static void print(String s) {
System.out.println("[AnotherRealSubject.print:] "+s);
}
/**
* Prints the argument string to <code>System.out</code>. This is
* equivalent to <code>print(String)</code>
*
* @param s the string to print
*/
public static void write(String s) {
System.out.println("[AnotherRealSubject.write:] "+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:00ZDelegationProxy.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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
* <code>Main</code> to <code>RealSubject.print(String)</code> are blocked.<p>
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
*/
public aspect DelegationProxy extends ProxyProtocol {
/**
* Assigns the <i>Subject</i> role to <code>RealSubject</code>.
*/
declare parents: RealSubject implements Subject;
/**
* Captures all accesses to the subject that should be protected by
* this pattern instance. Here: All calls to <code>RealSubject.write(..)
* </code>.
*/
protected pointcut protectedAccess(): call(* RealSubject.write(..));
/**
* Checks whether the access should be denied or not. Here: All accesses
* matched by the <code>protectedAccesses()</code> 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("[DelegationProxy] delegating a write() call to "
+"AnotherRealSubject");
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("");
}
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:00ZMain.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.java;
/* -*- 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 "License"); 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 "AS IS" 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.<p>
*
* Intent: <i>Provide a surrogate or placeholder for another object to control
* access to it.</i><p>
*
* Participatng objects are <code>RealSubject</code>s and <code>Proxy</code>.
* Both implement the <code>Subject</code> interface.
*
* <i>Proxy</i> forwards or blocks requests to <i>RealSubject</i>
*
* <p><i>This is the Java version.</i><p>
*
* <code>Proxy</code> needs to implement all methods of <i>Subject</i>, even
* those it is not interested in. <i>Proxy</i> needs to be aware of its role
* in the pattern.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Subject
* @see RealSubject
* @see Proxy
*/
public class Main {
/**
* Implements the driver for the proxy design pattern. Experimental setup:
* The proxy is to count the number of calls to RealSubject.print(String).
* It is not interested in calls to Subject.write(String).
* Proxy has to implement all method of subject, even those that are of
* no interest.
*/
public static void main (String[] args) {
Subject real = new RealSubject();
Subject proxy = new Proxy(real);
proxy.print("PRINT");
proxy.write("WRITE");
proxy.print("PRINT");
proxy.write("WRITE");
}
}
</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:00ZProtectionProxy.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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
* <code>Main</code> to <code>RealSubject.print(String)</code> are blocked.<p>
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
*/
public aspect ProtectionProxy extends ProxyProtocol {
/**
* Assigns the <i>Subject</i> role to <code>RealSubject</code>.
*/
declare parents: RealSubject implements Subject;
/**
* Captures all accesses to the subject that should be protected by
* this pattern instance. Here: All calls to <code>RealSubject.print(..)
* </code>.
*/
protected pointcut protectedAccess(): call(* RealSubject.print(..));
/**
* Checks whether the access should be denied or not. Here: All accesses
* that come from <code>Main</code> 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("[MyRejecting:] Intercepting calls to "
+"RealSubject.print() from Main");
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:00ZProxy.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.java;
/* -*- 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 "License"); 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 "AS IS" 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 <i>Proxy</i> according to GoF. It implements the
* <i>Subject</i> interface as <i>RealSubject</i> does.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Subject
* @see Proxy
*/
public class Proxy implements Subject {
/**
* a reference to the RealSubject for method forwarding
*/
private Subject realSubject;
/**
* An internal counter for the number of calls to <code>
* print(String)</code>.
*/
private int printCalls = 0;
/**
* Creates a new <i>Proxy</i> with the given <i>RealSubject</i>.
*
* @param subject The <i>RealSubject</i> to forward method calls to
*/
public Proxy(Subject subject) {
this.realSubject = subject;
}
/**
* Prints the argument string to <code>System.out</code>. This is
* equivalent to <code>write(String)</code>
*
* @param s the string to print
*/
public void print(String s) {
printCalls++;
realSubject.print(s);
System.out.println("[Proxy:] That was call "+printCalls+" to Subject.print(String)");
}
/**
* Prints the argument string to <code>System.out</code>. This is
* equivalent to <code>print(String)</code>
*
* @param s the string to print
*/
public void write(String s) {
realSubject.write(s);
System.out.println("[Proxy:] Not interested in write calls, but must implement anyway");
}
}</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:00ZRealSubject.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.java;
/* -*- 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 "License"); 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 "AS IS" 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 <i>RealSubject</i> according to GoF. It implements the
* <i>Subject</i> interface as <i>Proxy</i> does.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Subject
* @see Proxy
*/
public class RealSubject implements Subject {
/**
* Prints the argument string to <code>System.out</code>. This is
* equivalent to <code>write(String)</code>
*
* @param s the string to print
*/
public void print(String s) {
System.out.println("[RealSubject.print:] "+s);
}
/**
* Prints the argument string to <code>System.out</code>. This is
* equivalent to <code>print(String)</code>
*
* @param s the string to print
*/
public void write(String s) {
System.out.println("[RealSubject.write:] "+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:00ZSubject.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.java;
/* -*- 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 "License"); 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 "AS IS" 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):
*/
/**
* Defines the <i>Subject</i> interface that is implemented by both <i>Proxy
* </i> and <i>RealSubject</i>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see RealSubject
* @see Proxy
*/
public interface Subject {
/**
* Prints the argument string to <code>System.out</code>. This is
* equivalent to <code>write(String)</code>
*
* @param s the string to print
*/
public void print(String s);
/**
* Prints the argument string to <code>System.out</code>. This is
* equivalent to <code>print(String)</code>
*
* @param s the string to print
*/
public void write(String 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:00Zfiles.lst2004-03-24T16:00:00Z2004-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