View Javadoc

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.plus.webapp;
20  
21  import java.util.Random;
22  
23  import javax.naming.Context;
24  import javax.naming.InitialContext;
25  import javax.naming.NameNotFoundException;
26  
27  import org.eclipse.jetty.plus.annotation.InjectionCollection;
28  import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
29  import org.eclipse.jetty.plus.jndi.Transaction;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  import org.eclipse.jetty.webapp.AbstractConfiguration;
33  import org.eclipse.jetty.webapp.WebAppContext;
34  
35  
36  /**
37   * Configuration
38   *
39   *
40   */
41  public class PlusConfiguration extends AbstractConfiguration
42  {
43      private static final Logger LOG = Log.getLogger(PlusConfiguration.class);
44  
45      private Integer _key;
46  
47      @Override
48      public void preConfigure (WebAppContext context)
49      throws Exception
50      {
51          context.getObjectFactory().addDecorator(new PlusDecorator(context));
52      }
53  
54      @Override
55      public void cloneConfigure(WebAppContext template, WebAppContext context) throws Exception
56      {
57          context.getObjectFactory().addDecorator(new PlusDecorator(context));
58      }
59  
60      @Override
61      public void configure (WebAppContext context)
62      throws Exception
63      {
64          bindUserTransaction(context);
65  
66          context.getMetaData().addDescriptorProcessor(new PlusDescriptorProcessor());
67      }
68  
69      @Override
70      public void postConfigure(WebAppContext context) throws Exception
71      {
72          //lock this webapp's java:comp namespace as per J2EE spec
73          lockCompEnv(context);
74      }
75  
76      @Override
77      public void deconfigure (WebAppContext context)
78      throws Exception
79      {
80          unlockCompEnv(context);
81          _key = null;
82          context.setAttribute(InjectionCollection.INJECTION_COLLECTION,null);
83          context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION,null);
84      }
85  
86      public void bindUserTransaction (WebAppContext context)
87      throws Exception
88      {
89          try
90          {
91             Transaction.bindToENC();
92          }
93          catch (NameNotFoundException e)
94          {
95              LOG.debug("No Transaction manager found - if your webapp requires one, please configure one.");
96          }
97      }
98  
99  
100 
101     protected void lockCompEnv (WebAppContext wac)
102     throws Exception
103     {
104         ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
105         Thread.currentThread().setContextClassLoader(wac.getClassLoader());
106         try
107         {
108             Random random = new Random ();
109             _key = new Integer(random.nextInt());
110             Context context = new InitialContext();
111             Context compCtx = (Context)context.lookup("java:comp");
112             compCtx.addToEnvironment("org.eclipse.jetty.jndi.lock", _key);
113         }
114         finally
115         {
116             Thread.currentThread().setContextClassLoader(old_loader);
117         }
118     }
119 
120     protected void unlockCompEnv (WebAppContext wac)
121     throws Exception
122     {
123         if (_key!=null)
124         {
125             ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
126             Thread.currentThread().setContextClassLoader(wac.getClassLoader());
127 
128             try
129             {
130                 Context context = new InitialContext();
131                 Context compCtx = (Context)context.lookup("java:comp");
132                 compCtx.addToEnvironment("org.eclipse.jetty.jndi.unlock", _key);
133             }
134             finally
135             {
136                 Thread.currentThread().setContextClassLoader(old_loader);
137             }
138         }
139     }
140 }