Skip to content

Instantly share code, notes, and snippets.

@afterhill
Created April 24, 2013 14:44
Show Gist options
  • Save afterhill/5452674 to your computer and use it in GitHub Desktop.
Save afterhill/5452674 to your computer and use it in GitHub Desktop.

Revisions

  1. afterhill created this gist Apr 24, 2013.
    5 changes: 5 additions & 0 deletions BaseDao.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    package daoannotation;

    public interface BaseDao {

    }
    70 changes: 70 additions & 0 deletions DaoProxyFactory.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    package daoannotation;

    import java.lang.annotation.AnnotationFormatError;
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;

    public class DaoProxyFactory {

    @SuppressWarnings("unchecked")
    public static <T> T newRoleDaoProxy(Class<T> dao) {
    Implement implAnnotation = dao.getAnnotation(Implement.class);

    if (implAnnotation == null) {
    throw new AnnotationFormatError("this is no annotation for this interface: " +
    dao.getSimpleName().toString());
    }

    BaseDao implClass = null;

    try {
    implClass = implAnnotation.value().newInstance();
    } catch (Exception e) {
    throw new RuntimeException("this interface cannot be instanized!", e);
    }

    return (T) Proxy.newProxyInstance(
    DaoProxyFactory.class.getClassLoader(),
    new Class<?>[] { dao },
    new RoleInovationHandler(implClass));

    }

    private static final class RoleInovationHandler implements InvocationHandler {
    private BaseDao target;

    public RoleInovationHandler(BaseDao target) {
    this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {
    Permission permitAnnotation = method.getAnnotation(Permission.class);

    outter:
    if (permitAnnotation != null) {
    Role currentRole = RoleContext.INSTANCE.getCurrentRole();

    for (Role role : permitAnnotation.value()) {
    if (role.equals(currentRole)) {
    break outter;
    }
    }

    throw new SecurityException("user is not allowed to this operation!");

    }
    return method.invoke(target, args);
    }

    }

    public static void main(String[] args) {
    UserDao userDao = DaoProxyFactory.newRoleDaoProxy(UserDao.class);
    userDao.save();
    userDao.delete();
    //userDao.query();
    }
    }
    12 changes: 12 additions & 0 deletions Implement.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    package daoannotation;

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Implement {
    Class<? extends UserDao> value();
    }
    12 changes: 12 additions & 0 deletions Permission.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    package daoannotation;

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Permission {
    Role[] value();
    }
    15 changes: 15 additions & 0 deletions Role.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    package daoannotation;

    public enum Role {

    ADMINISTRATOR,
    SYSTEM;


    public Role get() {
    // TODO Auto-generated method stub
    return null;
    }


    }
    19 changes: 19 additions & 0 deletions RoleContext.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    package daoannotation;

    public enum RoleContext {
    INSTANCE;

    private ThreadLocal<Role> role = new ThreadLocal<>();

    public Role getCurrentRole() {
    return Role.SYSTEM;
    }

    public void setCurrentRole(Role role) {
    this.role.set(role);
    }

    public static void main(String[] args) {
    System.out.println(RoleContext.INSTANCE.getCurrentRole());
    }
    }
    13 changes: 13 additions & 0 deletions UserDao.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    package daoannotation;

    @Implement(UserDaoImpl.class)
    public interface UserDao extends BaseDao {
    @Permission({Role.ADMINISTRATOR, Role.SYSTEM})
    void save();

    @Permission(Role.SYSTEM)
    void delete();

    @Permission(Role.ADMINISTRATOR)
    void query();
    }
    22 changes: 22 additions & 0 deletions UserDaoImpl.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    package daoannotation;

    public class UserDaoImpl implements UserDao {

    @Override
    public void save() {
    System.out.println("UserDaoImpl.save()!");
    }

    @Override
    public void delete() {
    System.out.println("UserDaoImpl.delete()!");

    }

    @Override
    public void query() {
    System.out.println("UserDaoImpl.query()!");

    }

    }