ch.sahits.util
Class FriendPattern

java.lang.Object
  extended by ch.sahits.util.FriendPattern

public class FriendPattern
extends java.lang.Object

This class helps to ensure, that a call of a method only stems from a specific class or even a specific method within that class.
This is a realization of the friend pattern

Version:
1.0
Author:
Andi Hotz (c) Sahits.ch 2007

Constructor Summary
FriendPattern(java.lang.Class c, java.lang.String mName)
          Constructor initializes the allowed class and method.
FriendPattern(java.lang.Class c, java.lang.String mName, boolean includClassHirarchy, java.lang.Class class2BChecked)
          Constructor initializes the allowed class and method.
 
Method Summary
 void checkAccess()
          Check if the method call stems from an allowed Class/Method.
protected  java.lang.ClassLoader getClassLoader()
          Retrieve the default class loader for this class
protected  void setClassLoader(java.lang.ClassLoader cl)
          Set the ClassLoader that is needed in checkAccess().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FriendPattern

public FriendPattern(java.lang.Class c,
                     java.lang.String mName)
Constructor initializes the allowed class and method. The method name may be null if access from all methods of the indicated class is granted.

Parameters:
c - Class from whence access is allowed
mName - method name from which access is allowed.

FriendPattern

public FriendPattern(java.lang.Class c,
                     java.lang.String mName,
                     boolean includClassHirarchy,
                     java.lang.Class class2BChecked)
Constructor initializes the allowed class and method. The method name may be null if access from all methods of the indicated class is granted.

Parameters:
c - Class from whence access is allowed
mName - method name from which access is allowed.
includClassHirarchy - flag indicating if the class hierarchy should be taken into account
class2BChecked - Class to be checkted may be null if includClassHirarchy is false
Method Detail

checkAccess

public void checkAccess()
Check if the method call stems from an allowed Class/Method. If the call comes from somewhere else an IllegalAccessException is thrown.
ATTENTION: When in the calling class more than one method with the allowed name exist but only form one of them the call is allowed, this case is not handled here.

Throws:
java.lang.RuntimeException - Method is not allowed to be accessed or the method was called from somewhere not allowed

setClassLoader

protected void setClassLoader(java.lang.ClassLoader cl)
Set the ClassLoader that is needed in checkAccess(). This implementation does nothing since the default class loader is always used.

Parameters:
cl - Instance of a class loader or null

getClassLoader

protected java.lang.ClassLoader getClassLoader()
Retrieve the default class loader for this class

Returns:
class loader for this class
See Also:
getClassLoader()