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