Skip to content

Instantly share code, notes, and snippets.

@mlesikov
Created April 20, 2012 08:38
Show Gist options
  • Select an option

  • Save mlesikov/2427153 to your computer and use it in GitHub Desktop.

Select an option

Save mlesikov/2427153 to your computer and use it in GitHub Desktop.

Revisions

  1. mlesikov revised this gist Apr 20, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    public class ChangeLoggers {

    customer, @Named("CUSTOMER_NAME") String customerName);

    public static <T> T create(Class<? extends ChangeLogger> clazz, final CurrentUser currentUser, final ChangeLogBase base) {

    return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, new InvocationHandler() {
  2. mlesikov created this gist Apr 20, 2012.
    105 changes: 105 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    public class ChangeLoggers {

    customer, @Named("CUSTOMER_NAME") String customerName);

    public static <T> T create(Class<? extends ChangeLogger> clazz, final CurrentUser currentUser, final ChangeLogBase base) {

    return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, new InvocationHandler() {

    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

    Annotation[][] annotations = method.getParameterAnnotations();

    ...

    ChangeLog changeLog = ChangeLog.aNewChangeLog().type(type).createdFrom(currentUser.email()).createdOn(currentUser.time()).build();


    Object parent = null;

    int index = 0;

    for (Annotation[] annotation : annotations) {

    if (annotation.length > 0 && annotation[0] instanceof Named) {

    Named named = (Named) annotation[0];

    //check the value of the tag
    if (objects[index] != null && !Strings.empty(objects[index].toString())) {
    changeLog.tag(named.value(), objects[index].toString());
    }

    } else if (annotation.length > 0 && annotation[0] instanceof LogParent) {

    parent = objects[index];

    }else if (annotation.length > 0 && annotation[0] instanceof ChangeLogModule.ChangeLogAction){

    ChangeLogModule.ChangeAction actualAction = (ChangeLogModule.ChangeAction) objects[index];
    changeLog.setMessageKey(actualAction.getMessageKey());
    }

    index++;

    }

    if (parent != null) {
    base.save(changeLog, parent);
    }

    return changeLog;
    }
    });
    }
    }


    //the change log action interface that will represent the actions that are log
    public interface ChangeAction {

    String getMessageKey();
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE, ElementType.PARAMETER})
    @BindingAnnotation
    //annotation that indicate the
    public @interface ChangeLogAction {

    }

    injector = Guice.createInjector(new AbstractModule() {
    ...
    @Provides
    public MyChangeLogger getMyChangeLogger(CurrentUser currentUser, ChangeLogBase base){
    return ChangeLoggers.create(MyChangeLogger.class,currentUser,base);
    }
    ...
    });


    public interface MyChangeLogger extends ChangeLogger {

    ChangeLog log(@ChangeLogAction ChangeLogModule.ChangeAction action, @Named("DEVICE") Long id, @Named("DEVICE_IDENTIFICATION") String identificationNumber, @Named("CONTRACT") String lastReferentContractCode, @LogParent Device device);

    }



    //implementation that shows the concrete action
    public class DeviceInstallationAction implements ChangeLogModule.ChangeAction {

    public String getMessageKey() {
    return "installDevice";
    }
    }

    @Test
    public void testName() throws Exception {
    MyChangeLogger logger = injector.getInstance(MyChangeLogger.class);

    ChangeLog changeLog = logger.log(new DeviceInstallationAction(), 1l, "serial number", "1234567", device);

    assertEquals("installDevice", changeLog.getMessageKey());
    }