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