Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.resteasy.cdi.injection;
  
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
 
 import javax.ejb.EJB;
 import  javax.enterprise.context.ApplicationScoped;
 import  javax.enterprise.context.Dependent;
 import  javax.enterprise.context.RequestScoped;
 import  javax.enterprise.context.SessionScoped;
 import  javax.enterprise.inject.Instance;
 import  javax.enterprise.inject.New;
 import  javax.enterprise.inject.spi.Bean;
 import  javax.enterprise.inject.spi.BeanManager;
 import  javax.jms.DeliveryMode;
 import  javax.jms.JMSException;
 import  javax.jms.MessageConsumer;
 import  javax.jms.MessageProducer;
 import  javax.jms.Queue;
 import  javax.jms.Session;
 import  javax.jms.TextMessage;
 import  javax.persistence.EntityManager;
 import  javax.persistence.PersistenceContext;
 

Author(s):
Ron Sigal
Version:
$Revision: 1.1 $ Copyright May 7, 2012
 
 @Path("/")
 @RequestScoped
 public class BookResource
 {  
    public static final String BOOK_READER = "BookReader";
    public static final String BOOK_WRITER = "BookWriter";
    public static final String BOOK_RESOURCE = "BookResource";
    public static final String BOOK_READER_DEPENDENT = "BookReaderDependent";
    public static final String BOOK_WRITER_DEPENDENT = "BookWriterDependent";
    public static final String BOOK_RESOURCE_DEPENDENT = "BookResourceDependent";
    public static final String BOOK_READER_STATEFUL = "BookReaderStateless";
    public static final String BOOK_WRITER_STATEFUL = "BookWriterStateless";
    public static final String BOOK_RESOURCE_STATEFUL = "BookResourceStateless";
    public static final String BOOK_RESOURCE_STATEFUL2 = "BookResourceStateless2";
    public static final String COUNTER = "counter";
    public static final String COLLECTION = "collection";
    public static final String BOOK_BAG = "bookBag";
    public static final String NEW_BEAN_APPLICATION_SCOPED = "newBeanApplicationScoped";
    public static final String NEW_BEAN_DEPENDENT_SCOPED = "newBeanDependentScoped";
    public static final String STEREOTYPED_APPLICATION_SCOPED = "stereotypedApplicationScoped";
    public static final String STEREOTYPED_DEPENDENT_SCOPED = "stereotypedDependentScoped";
    
    private static HashMap<String,Objectstore;
    
    public static HashMap<StringObjectgetStore()
    {
       return ;
    }
 
    public static void setStore(HashMap<StringObjectstore)
    {
       . = store;
    }
    
    private HashSet<Bookset = new HashSet<Book>();
    
    public HashSet<BookgetSet()
    {
       return ;
    }
   public void setSet(HashSet<Bookset)
   {
      this. = set;
   }
   
   private static AtomicInteger constructCounter = new AtomicInteger();
   private static AtomicInteger destroyCounter = new AtomicInteger();
   
   public void preDestroy()
   {
      .info("preDestroy(): destroyCounter: " + .get());
   }
   
   public void postConstruct()
   {
      .info("postConstruct(): constructCounter: " + .get());
   }
   
   private static CountDownLatch latch = new CountDownLatch(2);
   
   {
      .info("latch.getCount(): " + .getCount());
      return ;
   }
   
   @Inject private BeanManager beanManager;
   @Inject private int secret;                 // used to determine identity
   @Inject private DependentScoped dependent;  // dependent scoped managed bean
   
   @Inject @CounterBinding private Counter counter;  // application scoped singleton: injected as Weld proxy
   @EJB private BookCollection collection;           // application scoped singleton: injected as EJB proxy
   
   // @Note: stateful and stateful2 are two very different objects.
   // stateful is an EJB proxy and stateful2 is a Weld proxy.
   @EJB    private StatefulEJB stateful;       // dependent scoped SLSB
   @Inject private StatefulEJB stateful2;      // dependent scoped SLSB
   
   @Inject private BookBagLocal bookBag;       // session scoped SFSB
   
   @Inject
   @PersistenceContext(unitName="test")
   EntityManager em;
   
   @Inject 
   private Session session;   
   @Inject 
   private Queue bookQueue;
   
   @Inject
   private NewBean newBean1;
   
   @Inject
   @New
   private NewBean newBean2;
   
   @Inject
   
   @Inject
   
//   @Inject
//   @ResourceBinding
//   private ConnectionFactory connectionFactory;
   
//   private Context applicationContext;
//   private Context requestContext;
//   private Context sessionContext;
   
   private Logger log;
   
   @Inject
   public void init(Instance<LoggerlogInstance)
   {
      this. = logInstance.get();
   }
   
   @GET
   @Path("verifyScopes")
   public Response verifyScopes()
   {
      .info("entering verifyScopes()");
      ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
      BookReader reader = BookReader.class.cast(factory.getMessageBodyReader(Book.classnullnull.));
      BookWriter writer = BookWriter.class.cast(factory.getMessageBodyWriter(Book.classnullnull.));
      
      if ( == null)
      {
         .info("Counter scope:          " + getScope(Counter.class));
         .info("BookCollection scope:   " + getScope(BookCollection.class));
         .info("BookResource scope:     " + getScope(BookResource.class));
         .info("BookReader scope:       " + getScope(BookReader.class));
         .info("BookWriter scope:       " + getScope(BookWriter.class));
         .info("UnscopedResource scope: " + getScope(UnscopedResource.class)); 
         .info("DependentScoped scope:  " + getScope(DependentScoped.class));
         .info("StatelessEJB scope:     " + getScope(StatefulEJB.class));
         .info("BookBagLocal scope:     " + getScope(BookBagLocal.class));
//         log.info("BookMDB scope:          " + getScope(BookMDB.class));
         .info("NewBean scope:          " + getScope(NewBean.class));
         .info("stereotypeApplicationScoped: " + getScope(StereotypedApplicationScope.class));
         .info("stereotypeRequestScoped:     " + getScope(StereotypedDependentScope.class));
         
          = new HashMap<String,Object>();
         .put(reader);
         .put(writer);
         .put(this);
         .put(reader.getDependent());
         .put(writer.getDependent());
         .put(reader.getStateful());
         .put(writer.getStateful());
         .put();
         .put();
         .put();
         return Response.ok().build();
      }
      
      if (isApplicationScoped(Counter.class)                     &&
          isApplicationScoped(BookCollection.class)              &&
          isApplicationScoped(BookReader.class)                  &&
          isApplicationScoped(BookWriter.class)                  &&
          isRequestScoped(BookResource.class)                    &&
          isDependentScoped(DependentScoped.class)               &&
          isDependentScoped(StatefulEJB.class)                   &&
          isRequestScoped(UnscopedResource.class)                &&
          isApplicationScoped(JaxRsActivator.class)              &&
          isSessionScoped(BookBagLocal.class)                    &&
          isApplicationScoped(NewBean.class)                     &&
          isDependentScoped(StereotypedDependentScope.class)     &&
//          getScope(BookMDB.class) == null                        &&
          .get() == reader                       &&
          .get() == writer                       &&
          .get() != this                       &&
          .get() == reader.getDependent()    &&
          .get() == writer.getDependent()    &&
          .get() !=               &&
          .get().equals(reader.getStateful()) &&
          .get().equals(writer.getStateful()) &&
         !.get().equals()           &&
         !.get().equals()         &&
          .get().equals()                           &&
          .get().equals()                     &&
          .get().equals()                          &&
          .get().equals()      &&
         !.get().equals()        &&
         !.equals()                                    &&
          )
      {
         return Response.ok().build();
      }
      else
      {
         return Response.serverError().build();
      }
   }
   
   @POST
   @Path("empty")
   public void empty()
   {
      .empty();   
   }
   
   @POST
   @Path("create")
   @Consumes("application/test+xml")
   public Response createBook(Book book)
   {
      .info("entering createBook()");
      int id = .getNext();
      book.setId(id);
      .addBook(book);  
      .info("stored: " + id + "->" + book);
      return Response.ok(id).build();
   }
   
   @GET
   @Path("books")
   {
      .info("entering listAllMembers()");
      .info("this.theSecret(): " + this.theSecret());
      Collection<Bookbooks = .getBooks();
      .info("listAllMembers(): " + books);
      return books;
   }
   @GET
   @Path("book/{id:[0-9][0-9]*}")
   @Produces("application/test+xml")
   public Book lookupBookById(@PathParam("id"int id)
   {
      .info("entering lookupBookById(" + id + ")");
      .info("books: " + .getBooks());
      Book book = .getBook(id);
      if (book == null)
      {
         throw new WebApplicationException(..);
      }
      return book;
   }
   
   @POST
   @Path("entityManager")
   {
      .info("entering testEntityManager()");
      Book book2 = .find(Book.class.);
      return book1.equals(book2) ? Response.ok().build() : Response.serverError().build();
   }
   
   @POST
   @Path("session/add")
   public Response sessionAdd(@Context HttpServletRequest requestBook book)
   {
      .info("entering sessionAdd()");
      .info("new session: " + request.getSession().isNew());
      .addBook(book);
      return Response.ok().build();
   }
   @GET
   @Path("session/get")
   {
      .info("entering sessionGetBag()");
      .info("new session: " + request.getSession().isNew());
      Collection<Bookbooks = .getContents();
      .info("sessionGetBag(): " + books);
      request.getSession().invalidate();
      return books;
   }
   
   @POST
   @Path("session/test")
   {
      .info("entering sessionTest()");
      .info("new session: " + request.getSession().isNew());
      Collection<Bookcontents = .getContents();
      .info("bookBag: " + contents);
      if (request.getSession().isNew() && contents.isEmpty())
      {
         return Response.ok().build();
      }
      else
      {
         return Response.serverError().build();
      }
   }
   
   @GET
   @Path("mdb/get")
   {
      .info("entering mdbGetBag()");
      Collection<Bookbooks = .getContents();
      .info("sessionGetBag(): " + books);
      return new GenericEntity<Collection<Book>>(books){};
   }
   
   @GET
   @Path("getCounters")
   public String getCounters()
   {
      return Integer.toString(.get()) + ":" + Integer.toString(.get()) + ":";
   }
   
   @GET
   @Path("disposer")
   public Response testDisposer()
   {
      .info("entering testDisposer()");
      return ResourceProducer.isDisposed() ? Response.ok().build() : Response.serverError().build();
   }
   
   @POST
   @Path("produceMessage")
   @Consumes("application/test+xml")
   public Response produceBookMessage(Book book)
   {
      .info("entering produceBookMessage()");
      try
      {
         .info("queue: " + );
         .info("ResourceProducer scope: " + getScope(ResourceProducer.class));
         .info("queue scope: " + getScope(Queue.class));
         MessageProducer producer = .createProducer();
         TextMessage message = .createTextMessage(book.getName());
         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
         producer.send(message);
         .info("sent: " + message.getText());
         return Response.ok().build();
      }
      catch (JMSException e)
      {
         e.printStackTrace();
         return Response.serverError().entity("JMS error: " + e.getMessage()).build();
      }
   }
   
   @GET
   @Path("queue/consumeMessage")
   {
      .info("entering consumeBookMessageFromQueue() xxx");
      .info("getting consumer");
      try
      {
         MessageConsumer consumer = .createConsumer();
         .info("got consumer");
         TextMessage message = (TextMessage) consumer.receiveNoWait();
         .info("message: " + message);
         if (message == null)
         {
            return Response.serverError().build();
         }
         .info("message text: " + message.getText());
         return Response.ok(message.getText()).build();
      }
      catch (JMSException e)
      {
         return Response.serverError().entity("JMS error: " + e.getMessage()).build();
      }
   }
   
   @GET
   @Path("mdb/consumeMessage")
   {
      .info("entering consumeBookMessageFromMDB()");
      getCountDownLatch().await(5000, .); // Wait until BookMDB has stored book in BookCollection.
      BookCollection collection = getBookCollection();
      .info("consumeBookMessageFromMDB(): collection.size(): " + collection.getBooks().size());
      if (collection.getBooks().size() == 1)
      {
         String name = collection.getBooks().iterator().next().getName();
         .info("got book name: " + name);
         return Response.ok(name).build();
      }
      else
      {
         return Response.serverError().entity("Collection size: " + collection.getBooks().size()).build();
      }
   }
   
   @GET
   @Path("mdb/books")
   {
      .info("entering getBooksMDB()");
      .info("getBooksMDB(): waiting on latch");
      .await();
      .info("this.theSecret(): " + this.theSecret());
      Collection<Bookbooks = .getBooks();
      .info("getBooksMDB(): " + books);
      return books;
   }
   
   {
      .info("entering getBookCollection()");
      return ;
   }
   
   {
      .info("returning: " + );
      return ;
   }
   
   boolean isApplicationScoped(Class<?> c)
   {
      return testScope(c, ApplicationScoped.class);
   }
   boolean isDependentScoped(Class<?> c)
   {
      return testScope(c, Dependent.class);
   }
   
   boolean isRequestScoped(Class<?> c)
   {
      return testScope(c, RequestScoped.class);
   }
   
   boolean isSessionScoped(Class<?> c)
   {
      return testScope(c, SessionScoped.class);
   }
   
   boolean testScope(Class<?> cClass<?> scopeClass)
   {
      Class<? extends Annotationannotation = getScope(c);
      if (annotation == null)
      {
         return false;
      }
      return annotation.isAssignableFrom(scopeClass);
   }
   
   Class<? extends AnnotationgetScope(Class<?> c)
   {
      Set<Bean<?>> beans = .getBeans(c);
      Iterator<Bean<?>> it = beans.iterator();
      if (it.hasNext())
      {
         Bean<?> bean = beans.iterator().next();
         return bean.getScope();
      }
//      Annotation[] annotations = c.getAnnotations();
//      for (int i = 0; i < annotations.length; i++)
//      {
//         log.info(annotations[i].annotationType().toString());
//         if (beanManager.isScope(annotations[i].annotationType()))
//         {
//            return annotations[i].getClass();
//         }
//         if (beanManager.isStereotype(annotations[i].annotationType()))
//         {
//            return getScope(annotations[i].annotationType());
//         }
//      }
      return null;
   }
   
   public boolean theSame(BookResource that)
   {
      return this. == that.secret;
   }
   
   public int theSecret()
   {
      return ;
   }
New to GrepCode? Check out our FAQ X