View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.webapp;
15  
16  import java.util.Random;
17  
18  import javax.naming.Context;
19  import javax.naming.InitialContext;
20  import javax.naming.NameNotFoundException;
21  
22  import org.eclipse.jetty.plus.annotation.InjectionCollection;
23  import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
24  import org.eclipse.jetty.plus.annotation.RunAsCollection;
25  import org.eclipse.jetty.plus.jndi.Transaction;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.webapp.AbstractConfiguration;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  
30  
31  /**
32   * Configuration
33   *
34   *
35   */
36  public class PlusConfiguration extends AbstractConfiguration
37  {
38      private Integer _key;
39      
40      @Override
41      public void preConfigure (WebAppContext context)
42      throws Exception
43      {      
44          context.addDecorator(new PlusDecorator(context));  
45      }
46   
47      @Override
48      public void cloneConfigure(WebAppContext template, WebAppContext context) throws Exception
49      {
50          context.addDecorator(new PlusDecorator(context));  
51      }
52  
53      @Override
54      public void configure (WebAppContext context)
55      throws Exception
56      {
57          bindUserTransaction(context);
58          
59          context.getMetaData().addDescriptorProcessor(new PlusDescriptorProcessor());
60      }
61  
62      @Override
63      public void postConfigure(WebAppContext context) throws Exception
64      {
65          //lock this webapp's java:comp namespace as per J2EE spec
66          lockCompEnv(context);
67      }
68  
69      @Override
70      public void deconfigure (WebAppContext context)
71      throws Exception
72      {
73          unlockCompEnv(context);
74          _key = null;
75          context.setAttribute(InjectionCollection.INJECTION_COLLECTION,null);
76          context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION,null);
77      }
78      
79      public void bindUserTransaction (WebAppContext context)
80      throws Exception
81      {
82          try
83          {
84             Transaction.bindToENC();
85          }
86          catch (NameNotFoundException e)
87          {
88              Log.info("No Transaction manager found - if your webapp requires one, please configure one.");
89          }
90      }
91      
92   
93    
94      protected void lockCompEnv (WebAppContext wac)
95      throws Exception
96      {
97          ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
98          Thread.currentThread().setContextClassLoader(wac.getClassLoader());
99          try
100         {
101             Random random = new Random ();
102             _key = new Integer(random.nextInt());
103             Context context = new InitialContext();
104             Context compCtx = (Context)context.lookup("java:comp");
105             compCtx.addToEnvironment("org.eclipse.jndi.lock", _key);
106         }
107         finally
108         {
109             Thread.currentThread().setContextClassLoader(old_loader);
110         }
111     }
112     
113     protected void unlockCompEnv (WebAppContext wac)
114     throws Exception
115     {
116         if (_key!=null)
117         {
118             ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
119             Thread.currentThread().setContextClassLoader(wac.getClassLoader());
120 
121             try
122             {
123                 Context context = new InitialContext();
124                 Context compCtx = (Context)context.lookup("java:comp");
125                 compCtx.addToEnvironment("org.eclipse.jndi.unlock", _key); 
126             }
127             finally
128             {
129                 Thread.currentThread().setContextClassLoader(old_loader);
130             }
131         }
132     }
133 }