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;
20
21 import javax.naming.Reference;
22
23 import org.eclipse.jetty.deploy.App;
24 import org.eclipse.jetty.deploy.AppLifeCycle;
25 import org.eclipse.jetty.deploy.graph.Node;
26 import org.eclipse.jetty.plus.jndi.Resource;
27 import org.eclipse.jetty.server.handler.ContextHandler;
28 import org.eclipse.jetty.webapp.WebAppContext;
29
30 /**
31 * Perform some basic weld configuration of WebAppContext
32 */
33 public class WeldDeploymentBinding implements AppLifeCycle.Binding
34 {
35 public String[] getBindingTargets()
36 {
37 return new String[]
38 { "deploying" };
39 }
40
41 public void processBinding(Node node, App app) throws Exception
42 {
43 ContextHandler handler = app.getContextHandler();
44 if (handler == null)
45 {
46 throw new NullPointerException("No Handler created for App: " + app);
47 }
48
49 if (handler instanceof WebAppContext)
50 {
51 WebAppContext webapp = (WebAppContext)handler;
52
53 // Add context specific weld container reference.
54 // See https://issues.jboss.org/browse/WELD-1710
55 // 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
56 webapp.setInitParameter("org.jboss.weld.environment.container.class",
57 "org.jboss.weld.environment.jetty.JettyContainer");
58
59 // Setup Weld BeanManager reference
60 Reference ref = new Reference("javax.enterprise.inject.spi.BeanManager",
61 "org.jboss.weld.resources.ManagerObjectFactory", null);
62 new Resource(webapp,"BeanManager",ref);
63
64 // webapp cannot change / replace weld classes
65 webapp.addSystemClass("org.jboss.weld.");
66 webapp.addSystemClass("org.jboss.classfilewriter.");
67 webapp.addSystemClass("org.jboss.logging.");
68 webapp.addSystemClass("com.google.common.");
69
70 // don't hide weld classes from webapps (allow webapp to use ones from system classloader)
71 webapp.addServerClass("-org.jboss.weld.");
72 webapp.addServerClass("-org.jboss.classfilewriter.");
73 webapp.addServerClass("-org.jboss.logging.");
74 webapp.addServerClass("-com.google.common.");
75 }
76 }
77 }