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.
annotation depency injection
package daoannotation;
public interface BaseDao {
}
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();
}
}
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();
}
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();
}
package daoannotation;
public enum Role {
ADMINISTRATOR,
SYSTEM;
public Role get() {
// TODO Auto-generated method stub
return null;
}
}
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());
}
}
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();
}
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()!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment