packageutil;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.io.PrintStream;importjava.util.logging.Logger;importjavax.naming.InitialContext;importjavax.naming.NamingException;importorg.jboss.ejb3.embedded.EJB3StandaloneBootstrap;/**
* This class was originally necessary when using the ALPHA 5 version of the
* embeddable container. With the alpha 9 release, initialization is quite
* simple, you need just 2 lines to initialize your JBoss Embeddable EJB3
* Container Environment. Unfortunately, the one that is available for download
* uses System.out.println() in a few places, so this simple utility hides that
* output and also provides a simple lookup mechanism.
*/publicclassJBossUtil{privatestaticPrintStreamoriginalOut;privatestaticPrintStreamoriginalErr;privatestaticOutputStreamtestOutputStream;privatestaticPrintStreamtestOuputPrintStream;staticbooleaninitialized;staticInitialContextctx;privateJBossUtil(){// I'm a utility class, do not instantiate me}/**
* JBoss EJB3 Embeddable Container uses System.out. Redirect that output to
* keep the console output clean.
*/privatestaticvoidredirectStreams(){// configure the console to get rid of hard-coded System.out's in// the JBoss librariestestOutputStream=newByteArrayOutputStream(2048);testOuputPrintStream=newPrintStream(testOutputStream);originalOut=System.out;originalErr=System.err;System.setOut(testOuputPrintStream);System.setErr(testOuputPrintStream);}/**
* Restore the System.out and System.err streams to their original state.
* Close the temporary stream created for the purpose of redirecting I/O
* while the initializing is going on.
*/privatestaticvoidrestoreStreams(){System.setOut(originalOut);System.setErr(originalErr);testOuputPrintStream.close();try{testOutputStream.close();}catch(IOExceptione){Logger.getLogger(JBossUtil.class.getName()).info("Unable to close testoutstream");}}/**
* This method starts and initializes the embeddable container. We do not
* offer a method to properly clean up the container since this is really
* meant for testing only.
*
* This method may freely be called as often as you'd like since it lazily
* initializes the container only once.
*/publicstaticvoidstartDeployer(){if(!initialized){redirectStreams();EJB3StandaloneBootstrap.boot(null);EJB3StandaloneBootstrap.scanClasspath();initialized=true;restoreStreams();}}/**
* This is for symmetry. Given how we are using this class, there's little
* need to actually shutdown the container since we run a quick application
* and then stop the JVM.
*/publicstaticvoidshutdownDeployer(){EJB3StandaloneBootstrap.shutdown();}privatestaticInitialContextgetContext(){/**
* We only keep one context around, so lazily initialize it
*/if(ctx==null){try{ctx=newInitialContext();}catch(NamingExceptione){thrownewRuntimeException("Unable to get initial context",e);}}returnctx;}/**
* The lookup method on InitialContext returns Object. This simple wrapper
* asks first for the expected type and the for the name to find. It gets
* the name out of JNDI and performs a simple type-check. It then casts to
* the type provided as the first parameter.
*
* This isn't strictly correct since the cast uses the expression (T), where
* T is the generic parameter and the type is erased at run-time. However,
* since we first perform a type check, we know this cast is safe. The -at-
* SuppressWarnings lets the Java Compiler know that we think we know what
* we are doing.
*
* @param <T>
* Type type provided as the first parameter
* @param clazz
* The type to cast to upon return
* @param name
* The name to find in Jndi, e.g. XxxDao/local or, XxxDao/Remote
* @return Something out of Jndi cast to the type provided as the first
* parameter.
*/@SuppressWarnings("unchecked")publicstatic<T>Tlookup(Class<T>clazz,Stringname){finalInitialContextctx=getContext();/**
* Perform the lookup, verify that it is type-compatible with clazz and
* cast the return type (using the erased type because that's all we
* have) so the client does not need to perform the cast.
*/try{finalObjectobject=ctx.lookup(name);if(clazz.isAssignableFrom(object.getClass())){return(T)object;}else{thrownewRuntimeException(String.format("Class found: %s cannot be assigned to type: %s",object.getClass(),clazz));}}catch(NamingExceptione){thrownewRuntimeException(String.format("Unable to find ejb for %s",clazz.getName()),e);}}}
EqualsUtil.java
packageutil;/**
* We typically need to compare two object and also perform null checking. This
* class provides a simple wrapper to accomplish doing so.
*/publicclassEqualsUtil{privateEqualsUtil(){// I'm a utility class, do not instantiate me}publicstaticbooleanequals(finalObjectlhs,finalObjectrhs){returnlhs==null&&rhs==null||(lhs!=null&&rhs!=null&&lhs.equals(rhs));}}
Comments