Hussachai has posted 2 posts at DZone. View Full User Profile

Stars the new Stripes framework extension for full stack development

04.27.2011
| 2682 views |
  • submit to reddit
Location: 
http://code.google.com/p/stars/
I'm happy to announce the new Stripes framework extension, Stars!
Stars and Stripes is the perfect couple to get the job done.

The benefit of using Stars

  • Be able to change the service provider easily (Just a few line of configurations)
  • Be able to reuse existing knowledge because you can use target framework's native annotations which you are already familiar with such as @EJB, @Autowired, etc.
  • Small footprint and lightweight.

Features

  • Stars enhances existing ActionBean classes by providing dependency injection capability.
  • JSR 250's annotations @PostConstruct, @PreDestroy and @Resource
  • Spring integration - @Autowired annotation support and Spring callback interfaces such as InitializingBean, DisposableBean, ApplicationContextAware, BeanFactoryAware.
  • EJB integration - @EJB annotation support.
  • For Java Persistence API - @PersistenceUnit, @PersistenceContext with multiple persistence units support. Transaction type support depends on configuration and service provider.
  • Bootstrap class with dependency injection support
  • Built-in service container called Stars local service which supports annotation configuration as well. For example @Service, @ServiceBean It's easy to configure and customize.
  • Useful taglibs
  • The below snippet show how to implement bootstrap and use annotation in Stars.
    Stars does not manage transaction for Bootstrap class even you're using Stars local service,
    so you have to manage your own transaction. I use @SkipInjectionError to mark the bootstrap class
    in order to avoid injection error blocks the bootstrap job.
    Injection error?
    Yes! because Stars supports multiple service providers and therefore fail to inject the missing resource.
    In this example, if you run the web application in Java EE container managed environment, the UserTransaction
    resource will be avaiable; otherwise the resource will be missing. That's why @SkipInjectionError comes into play.
    Why Stars does not set the null value to attribute for unavailable resource instead of throwing error?
    The answer is Fail-fast

    BugzookyBootstrap.java
    @SkipInjectionError
    public class BugzookyBootstrap implements StarsBootstrap {

    @PersistenceContext
    private EntityManager em;

    @Resource
    private UserTransaction userTx;

    @Override
    public void execute(ServletContext servletContext) throws Exception {
    if(ServiceProvider.isEjb(servletContext)){
    userTx.begin();
    }else{
    if(!em.getTransaction().isActive())
    em.getTransaction().begin();
    }
    ...
    Another example is using DI in Stripes ActionBean class
    The below snippet show how to inject service instance to ActionBean's attribute using
    annotation configuration. There are 3 annotations for 3 types of service provider- Stars local service,
    Spring, and EJB respectively. The amazing feature of Stars, you can change service provider easily
    by changing a few line of configuration (No code change!!!). It means that the below ActionBean can
    use 3 difference types of service by just changing a few configurations without code changing.
    That's cool, isn't it.
    MultiBugActionBean.java
    @UrlBinding("/bugzooky/multiBug.action")
    public class MultiBugActionBean extends BugzookyActionBean {

    @Service(impl=BugManagerImpl.class)
    @Autowired
    @EJB
    private BugManager bugManager;
    ...

    You can download it here http://code.google.com/p/stars/
0
Published at DZone with permission of its author, Hussachai Puripunpinyo.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)