Created
July 8, 2012 12:17
-
-
Save justinwyer/3070683 to your computer and use it in GitHub Desktop.
Java EE 6 Web Profile without the app server. Part 1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App | |
{ | |
private static String[] jettyConfigurationClasses = | |
{ | |
"org.eclipse.jetty.webapp.WebInfConfiguration", | |
"org.eclipse.jetty.webapp.WebXmlConfiguration", | |
"org.eclipse.jetty.webapp.MetaInfConfiguration", | |
"org.eclipse.jetty.webapp.FragmentConfiguration", | |
"org.eclipse.jetty.plus.webapp.EnvConfiguration", | |
"org.eclipse.jetty.webapp.JettyWebXmlConfiguration" | |
}; | |
private static Server server; | |
private static void initServer() | |
{ | |
try | |
{ | |
server = new Server(8080); | |
HandlerList handlerList = new HandlerList(); | |
WebAppContext webapp = new WebAppContext(); | |
webapp.setConfigurationClasses(jettyConfigurationClasses); | |
String webappPath = App.class.getClassLoader().getResource("webapp").toExternalForm(); | |
webapp.setDescriptor(webappPath + "/WEB-INF/web.xml"); | |
webapp.setContextPath("/"); | |
webapp.setResourceBase(webappPath); | |
webapp.setClassLoader(Thread.currentThread().getContextClassLoader()); | |
ServletContextHandler servletContextHandler; | |
servletContextHandler = new ServletContextHandler(getServer(), "/", ServletContextHandler.SESSIONS); | |
ResourceHandler resourceHandler = new ResourceHandler(); | |
resourceHandler.setResourceBase(webappPath); | |
handlerList.addHandler(servletContextHandler); | |
handlerList.addHandler(resourceHandler); | |
handlerList.addHandler(webapp); | |
getServer().setHandler(handlerList); | |
getServer().start(); | |
} | |
catch (Exception ex) | |
{ | |
ex.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); | |
root.setLevel(Level.INFO); | |
initServer(); | |
} | |
/** | |
* @return the server | |
*/ | |
public static Server getServer() | |
{ | |
return server; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppServlet implements Servlet | |
{ | |
Logger log = LoggerFactory.getLogger(AppServlet.class); | |
@Inject | |
private GoodStuff goodStuff; | |
@Override | |
public void init(ServletConfig sc) throws ServletException | |
{ | |
try | |
{ | |
App.getServer().join(); | |
} | |
catch (InterruptedException ex) | |
{ | |
log.error(ex.getMessage(), ex); | |
} | |
// Lets do something good! | |
goodStuff.doSomethingGood(); | |
} | |
@Override | |
public ServletConfig getServletConfig() | |
{ | |
throw new UnsupportedOperationException("Not supported yet."); | |
} | |
@Override | |
public void service(ServletRequest sr, ServletResponse sr1) throws ServletException, IOException | |
{ | |
} | |
@Override | |
public String getServletInfo() | |
{ | |
throw new UnsupportedOperationException("Not supported yet."); | |
} | |
@Override | |
public void destroy() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(Arquillian.class) | |
public class AppTest | |
{ | |
@Deployment | |
public static JavaArchive createDeployment() | |
{ | |
return ShrinkWrap.create(JavaArchive.class, "test.jar").addClasses( | |
GoodStuff.class, Good.class). | |
addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | |
} | |
@Inject | |
private GoodStuff goodStuff; | |
@Test | |
public void testDoSomethingGood() throws Exception | |
{ | |
goodStuff.doSomethingGood(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@InterceptorBinding | |
@Target({ElementType.TYPE}) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Good | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Good | |
@Interceptor | |
public class GoodInterceptor | |
{ | |
@AroundInvoke | |
public Object doOnlyGood(InvocationContext ctx) throws Exception | |
{ | |
System.out.println("Do only good is better than do no evil right?"); | |
return ctx.proceed(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Good | |
public class GoodStuff | |
{ | |
@PostConstruct | |
private void init() | |
{ | |
System.out.println("A GoodStuff bean has been constructed."); | |
} | |
public void doSomethingGood() | |
{ | |
System.out.println("Something good just happened!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<web-app version="3.0"> | |
<display-name>goodstuff-example</display-name> | |
<listener> | |
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> | |
</listener> | |
<servlet> | |
<servlet-name>app-serlvet</servlet-name> | |
<servlet-class> | |
com.lifeasageek.goodstuffexample.AppServlet | |
</servlet-class> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment