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      }
76      
77      public void bindUserTransaction (WebAppContext context)
78      throws Exception
79      {
80          try
81          {
82             Transaction.bindToENC();
83          }
84          catch (NameNotFoundException e)
85          {
86              Log.info("No Transaction manager found - if your webapp requires one, please configure one.");
87          }
88      }
89      
90   
91    
92      protected void lockCompEnv (WebAppContext wac)
93      throws Exception
94      {
95          ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
96          Thread.currentThread().setContextClassLoader(wac.getClassLoader());
97          try
98          {
99              Random random = new Random ();
100             _key = new Integer(random.nextInt());
101             Context context = new InitialContext();
102             Context compCtx = (Context)context.lookup("java:comp");
103             compCtx.addToEnvironment("org.eclipse.jndi.lock", _key);
104         }
105         finally
106         {
107             Thread.currentThread().setContextClassLoader(old_loader);
108         }
109     }
110     
111     protected void unlockCompEnv (WebAppContext wac)
112     throws Exception
113     {
114         if (_key!=null)
115         {
116             ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
117             Thread.currentThread().setContextClassLoader(wac.getClassLoader());
118 
119             try
120             {
121                 Context context = new InitialContext();
122                 Context compCtx = (Context)context.lookup("java:comp");
123                 compCtx.addToEnvironment("org.eclipse.jndi.unlock", _key); 
124             }
125             finally
126             {
127                 Thread.currentThread().setContextClassLoader(old_loader);
128             }
129         }
130     }
131 }