Skip to content

Instantly share code, notes, and snippets.

@sndyuk
Created November 5, 2012 18:59
Show Gist options
  • Save sndyuk/4019608 to your computer and use it in GitHub Desktop.
Save sndyuk/4019608 to your computer and use it in GitHub Desktop.
logic to determine Dao calls based on logged in user and servlet called
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Bean {
}
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class BeanFactory {
private static List<Class<?>> getAllClasses() {
try {
final List<Class<?>> classses = new ArrayList<>();
URL url = BeanFactory.class.getResource("BeanFactory.class");
String scheme = url.getProtocol();
if ("jar".equals(scheme)) {
// Need to walk all jars.
throw new UnsupportedOperationException("The sample code only allows file scheme.");
} else if ("file".equals(scheme)) {
// Need to walk all directories.
// But now, It walks only current directory.
Files.walkFileTree(new File(url.getFile()).toPath().getParent(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
String name = file.getFileName().toString();
if (!name.endsWith(".class") || name.endsWith("BeanFactory.class")) {
return FileVisitResult.CONTINUE;
}
try {
classses.add(BeanFactory.class.getClassLoader().loadClass(name.replace(".class", "")));
} catch (Exception e) {
throw new RuntimeException(e);
}
return FileVisitResult.CONTINUE;
}
});
} else {
throw new RuntimeException("Unknown scheme type: " + scheme);
}
return classses;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Map<String, Map<Roles, String>> targetClasses = new HashMap<>();
public static void init() {
List<Class<?>> classes = getAllClasses();
for (Class<?> clazz : classes) {
Role r = clazz.getAnnotation(Role.class);
if (r == null) {
continue;
}
Class<?> sc = getBeanClass(clazz);
Map<Roles, String> m = targetClasses.get(sc.getCanonicalName());
if (m == null) {
m = new HashMap<>();
targetClasses.put(sc.getCanonicalName(), m);
}
m.put(r.has(), clazz.getCanonicalName());
}
}
private static Class<?> getBeanClass(Class<?> clazz) {
Class<?> sc = null;
for (;;) {
sc = clazz.getSuperclass();
if (sc == null) {
break;
}
if (sc.isAnnotationPresent(Bean.class)) {
break;
}
clazz = sc;
}
return sc;
}
@SuppressWarnings("unchecked")
public static <T> T createBean(Class<T> clazz, Roles role) {
Map<Roles, String> m = targetClasses.get(clazz.getCanonicalName());
String className = m.get(role);
if (className == null) {
throw new RuntimeException("Need to create a concrete role class: " + role + " for the class: " + clazz.getCanonicalName());
}
try {
return (T) clazz.getClassLoader().loadClass(className).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
import java.util.List;
@Bean
public abstract class Comment {
public abstract String getName();
public abstract List<? extends Comment> getList(String request);
}
import java.util.List;
public class Main {
public static void main(String[] args) {
BeanFactory.init();
List<? extends Comment> managerComments = BeanFactory.createBean(Comment.class, Roles.managers).getList("request");
for (Comment comment : managerComments) {
System.out.println(comment.getName());
}
List<? extends Comment> presidentComments = BeanFactory.createBean(Comment.class, Roles.president).getList("request");
for (Comment comment : presidentComments) {
System.out.println(comment.getName());
}
List<? extends Comment> supervisorsComments = BeanFactory.createBean(Comment.class, Roles.supervisors).getList("request");
for (Comment comment : supervisorsComments) {
System.out.println(comment.getName());
}
}
}
import java.util.Arrays;
import java.util.List;
@Role(has=Roles.managers)
class ManagerComment extends Comment {
@Override
public String getName() {
return "managers";
}
@Override
public List<ManagerComment> getList(String request) {
// dao calls
return Arrays.asList(new ManagerComment());
}
}
import java.util.Arrays;
import java.util.List;
@Role(has=Roles.president)
class PresidentComment extends Comment {
@Override
public String getName() {
return "president";
}
@Override
public List<PresidentComment> getList(String request) {
// dao calls
return Arrays.asList(new PresidentComment());
}
}
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Role {
Roles has();
}
public enum Roles {
supervisors,
managers,
president
}
import java.util.Arrays;
import java.util.List;
@Role(has=Roles.supervisors)
class SupervisorComment extends Comment {
@Override
public String getName() {
return "supervisors";
}
@Override
public List<SupervisorComment> getList(String request) {
// dao calls
return Arrays.asList(new SupervisorComment());
}
}
@sndyuk
Copy link
Author

sndyuk commented Nov 5, 2012

Run Main.java:

managers
president
supervisors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment