1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.cdi.servlet;
20
21 import javax.naming.NamingException;
22 import javax.naming.Reference;
23
24 import org.eclipse.jetty.plus.jndi.Resource;
25 import org.eclipse.jetty.server.handler.ContextHandler;
26 import org.eclipse.jetty.webapp.WebAppContext;
27
28 /**
29 * Utility class suitable for initializing CDI/Weld on Embedded Jetty
30 */
31 public class JettyWeldInitializer
32 {
33 /**
34 * Initialize WebAppContext to support CDI/Weld.
35 * <p>
36 * Initializes Context, then sets up WebAppContext system and server classes to allow Weld to operate from Server
37 * level.
38 * <p>
39 * Includes {@link #initContext(ContextHandler)} behavior as well.
40 * @param webapp the webapp
41 * @throws NamingException if unable to bind BeanManager context
42 */
43 public static void initWebApp(WebAppContext webapp) throws NamingException
44 {
45 initContext(webapp);
46
47 // webapp cannot change / replace weld classes
48 webapp.addSystemClass("org.jboss.weld.");
49 webapp.addSystemClass("org.jboss.classfilewriter.");
50 webapp.addSystemClass("org.jboss.logging.");
51 webapp.addSystemClass("com.google.common.");
52 webapp.addSystemClass("org.eclipse.jetty.cdi.websocket.annotation.");
53
54
55 // don't hide weld classes from webapps (allow webapp to use ones from system classloader)
56 webapp.prependServerClass("-org.eclipse.jetty.cdi.websocket.annotation.");
57 webapp.prependServerClass("-org.eclipse.jetty.cdi.core.");
58 webapp.prependServerClass("-org.eclipse.jetty.cdi.servlet.");
59 webapp.addServerClass("-org.jboss.weld.");
60 webapp.addServerClass("-org.jboss.classfilewriter.");
61 webapp.addServerClass("-org.jboss.logging.");
62 webapp.addServerClass("-com.google.common.");
63
64 }
65
66 public static void initContext(ContextHandler handler) throws NamingException
67 {
68 // Add context specific weld container reference.
69 // See https://issues.jboss.org/browse/WELD-1710
70 // and https://github.com/weld/core/blob/2.2.5.Final/environments/servlet/core/src/main/java/org/jboss/weld/environment/servlet/WeldServletLifecycle.java#L244-L253
71 handler.setInitParameter("org.jboss.weld.environment.container.class","org.jboss.weld.environment.jetty.JettyContainer");
72
73 // Setup Weld BeanManager reference
74 Reference ref = new Reference("javax.enterprise.inject.spi.BeanManager","org.jboss.weld.resources.ManagerObjectFactory",null);
75 new Resource(handler,"BeanManager",ref);
76 }
77 }