Showing posts with label Google Juice. Show all posts
Showing posts with label Google Juice. Show all posts

Friday, June 20, 2014

Aop with juice on controller




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

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
public @interface Aop {

}





import java.math.BigDecimal;
import java.sql.Time;
import java.util.Date;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.JsonNode;

import Model.Modal1;
import Model.Pojo1;
import Model.Pojo2;
import Model.Pojo3;

@Path("/test")
public class GuiceResource {

   
@Aop
    @GET
    @Produces("text/plain")
    public String getIt() {
System.out.println("I am called after interception");
        return "Hello From Guice: ";
    }
@Aop
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Modal1 getTrackInJSON() {
System.out.println("i am json called for get ");

Modal1 model = new Modal1();
model.setVariable1(new BigDecimal(1));
model.setVariable2("string");
model.setVariable3(new Date());
model.setVariable4(new Time(1));
model.setPojo1(new Pojo1());
model.setPojo2(new Pojo2());
model.setPojo3(new Pojo3());

// List <BasicPatientInformationServiceDto> test= getAllBasicPatientInformation();
return model;

}
@Aop
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(JsonNode jsonNode) {
System.out.println("i am json called for post ");

String result = "Track saved : " + jsonNode;
return Response.status(201).entity(result).build();

}
}


import java.util.HashMap;
import java.util.Map;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.matcher.Matchers;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

public class GuiceServletConfig extends GuiceServletContextListener {

@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
bind(GuiceResource.class);
Map<String, String> initParams = new HashMap<String, String>();
                 initParams.put("com.sun.jersey.api.json.POJOMappingFeature",
                         "true");
// serve("/*").with(GuiceContainer.class);
bindInterceptor(Matchers.any(),Matchers.annotatedWith(Aop.class),new SecurityGuard());
serve("/*").with(
                         GuiceContainer.class,
                         initParams);
           
filter("/*").through(LogFilter.class);
}
});
}
}



;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class SecurityGuard implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("I am intercepted ");
// if (false) {
// return null;
// }
return invocation.proceed();

}
}

Annotation binding in google juice

import com.google.inject.*;
import com.google.inject.name.*;

public class ExampleModule extends AbstractModule
{
    @Override
    protected void configure()
    {
        bindConstant().annotatedWith(Names.named("message")).to("Hello, World!");
    }
}


public class GuiceConfig extends GuiceServletContextListener
{
    @Override
    protected Injector getInjector()
    {
        final Map<String, String> params = new HashMap<String, String>();
        params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "com.package");
        return Guice.createInjector(new ExampleModule(), new ServletModule()
        {
            @Override
            protected void configureServlets()
            {
                serve("/*").with(GuiceContainer.class, params);
            }
        });
    }
}


@Path("java")
public class JavaResource
{
    private String message;

    @Inject
    public JavaResource(@Named("message") String message)
    {
        this.message = message;
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String get()
    {
        return "From Java: " + message;
    }
}

Aop with Juice

package aop1;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AopInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Interceptor: before");
Object o = invocation.proceed();
System.out.println("Interceptor: Done !");
return o;
}
}


package aop1;

import com.google.inject.AbstractModule;
import com.google.inject.matcher.Matchers;

public class AopModule extends AbstractModule {

 @Override
 protected void configure() {
 bind(BasicDummyService.class);
 bind(Service1.class);
 bind(Service2.class);
 bind(Service3.class);
 bindInterceptor(Matchers.any(),Matchers.annotatedWith(InjectedTransaction.class),new AopInterceptor());
 }
}

package aop1;

public class BasicDummyService {

 @InjectedTransaction
 public void testService() {
 System.out.println("doing testService");
 }
 @InjectedTransaction
 public void testService1() throws Exception{
throw new Exception();
// System.out.println("doing testService");
 }
}

package aop1;

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

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
public @interface InjectedTransaction {

}

package aop1;

import org.junit.Test;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {
@Test
void Mehtod(){
Injector injector = Guice.createInjector(new AopModule());
BasicDummyService test = injector.getInstance(BasicDummyService.class);
Service1 test1 = injector.getInstance(Service1Impl.class);
Service2 test2 = injector.getInstance(Service2Impl.class);
Service3 test3 = injector.getInstance(Service3Impl.class);

try {
test.testService();
} catch (Exception e) {


}
test1.Method1(new Object());
try {
test2.Method1(new Object());
} catch (Exception e) {
e.printStackTrace();
}
test3.Method1(new Object());
}
}

package aop1;

public interface Service1 {
Object Method1(Object test);

}


package aop1;

public class Service1Impl  implements Service1 {
@InjectedTransaction
@Override
public Object Method1(Object test) {
System.out.println("I am service1");
return test;
}

}


package aop1;

public interface Service2 {
Object Method1(Object test) throws Exception;

}

package aop1;

public class Service2Impl implements Service2{
@InjectedTransaction
@Override
public Object Method1(Object test) throws Exception {
System.out.println("I am service2");
return test;
}

}

package aop1;

public interface Service3 {
Object Method1(Object test) throws RuntimeException;
}

package aop1;

public class Service3Impl implements Service3 {
@InjectedTransaction
@Override
public Object Method1(Object test) throws RuntimeException {
System.out.println("I am service3");
return null;
}

}

Thursday, June 19, 2014

Dependency Injection with google Juice

package CommonDtoServiceImpl;

import com.google.inject.Binder;

public interface Module {
public void configure(Binder binder);

}


package CommonDtoServiceImpl;



import CommonDaoImpl.CommonDao;
import CommonDaoImpl.CommonDaoImpl;

import com.google.inject.Binder;
import com.google.inject.Module;

public class AddModule1 implements Module{

public void configure(Binder paramBinder) {
paramBinder.bind(CommonDao.class).to(CommonDaoImpl.class);
}

}


package CommonDtoServiceImpl;


import prototype.project.business6.Buiness6Service;
import prototype.project.business6.Buiness6ServiceImpl;

import com.google.inject.Binder;
import com.google.inject.Module;

public class AddModule implements Module{

    public void configure(Binder binder) {
        binder.bind(Buiness6Service.class).to(Buiness6ServiceImpl.class);
    
    }

}


Controller


@Path("/hello")
public class ServiceMappingBusinessLogic1 {


@GET
public Response getMsg( String msg) {

 Injector injector = Guice.createInjector(new AddModule());
 Buiness6Service business6service = injector.getInstance(Buiness6Service.class);
String output =business6service.FindClient();



return Response.status(200).entity(output).build();

}