1   // ========================================================================
2   // $Id: Configuration.java 2952 2008-06-10 04:00:02Z gregw $
3   // Copyright 2006 Mort Bay Consulting Pty. Ltd.
4   // ------------------------------------------------------------------------
5   // Licensed under the Apache License, Version 2.0 (the "License");
6   // you may not use this file except in compliance with the License.
7   // You may obtain a copy of the License at 
8   // http://www.apache.org/licenses/LICENSE-2.0
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // ========================================================================
15  
16  package org.mortbay.jetty.plus.webapp;
17  
18  import java.util.Random;
19  
20  import javax.naming.Context;
21  import javax.naming.InitialContext;
22  import javax.naming.NameNotFoundException;
23  
24  import org.mortbay.jetty.plus.naming.EnvEntry;
25  import org.mortbay.jetty.plus.naming.NamingEntry;
26  import org.mortbay.jetty.plus.naming.NamingEntryUtil;
27  import org.mortbay.jetty.plus.naming.Transaction;
28  import org.mortbay.log.Log;
29  
30  
31  /**
32   * Configuration
33   *
34   *
35   */
36  public class Configuration extends AbstractConfiguration
37  {
38  
39      private Integer _key;
40      
41      
42      public Configuration () throws ClassNotFoundException
43      {
44          super();
45      }
46      
47      /** 
48       * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#bindEnvEntry(java.lang.String, java.lang.String)
49       * @param name
50       * @param value
51       * @throws Exception
52       */
53      public void bindEnvEntry(String name, Object value) throws Exception
54      {    
55          EnvEntry.bindToENC(name, value);
56      }
57  
58      /** 
59       * Bind a resource reference.
60       * 
61       * If a resource reference with the same name is in a jetty-env.xml
62       * file, it will already have been bound.
63       * 
64       * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#bindResourceRef(java.lang.String)
65       * @param name
66       * @throws Exception
67       */
68      public void bindResourceRef(String name, Class typeClass)
69      throws Exception
70      {
71          try
72          {
73              String mappedName = NamingEntryUtil.getMappedName (name);
74              NamingEntryUtil.bindToENC(name, mappedName);
75          }
76          catch (NameNotFoundException e)
77          {
78              //There is no matching resource bound into the container's environment, try a default name.
79              //The default name syntax is: the [res-type]/default
80              //eg       javax.sql.DataSource/default
81              NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default");
82              if (defaultNE!=null)
83                  defaultNE.bindToENC(name);
84          }
85      }
86  
87      /** 
88       * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#bindResourceEnvRef(java.lang.String)
89       * @param name
90       * @throws Exception
91       */
92      public void bindResourceEnvRef(String name, Class typeClass)
93      throws Exception
94      {
95          try
96          {
97              String mappedName = NamingEntryUtil.getMappedName (name);
98              NamingEntryUtil.bindToENC(name, mappedName);
99          }
100         catch (NameNotFoundException e)
101         {
102             //There is no matching resource bound into the container's environment, try a default            
103             //The default name syntax is: the [res-type]/default
104             //eg       javax.sql.DataSource/default
105             NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default");
106             if (defaultNE!=null)
107                 defaultNE.bindToENC(name);
108         }
109     }
110     
111     
112     public void bindMessageDestinationRef(String name, Class typeClass)
113     throws Exception
114     {
115         try
116         {            
117             String mappedName = NamingEntryUtil.getMappedName (name);
118             NamingEntryUtil.bindToENC(name, mappedName);
119         }        
120         catch (NameNotFoundException e)
121         {
122             //There is no matching resource bound into the container's environment, try a default            
123             //The default name syntax is: the [res-type]/default
124             //eg       javax.sql.DataSource/default
125             NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default");
126             if (defaultNE!=null)
127                 defaultNE.bindToENC(name);
128         }
129     }
130     
131     public void bindUserTransaction ()
132     throws Exception
133     {
134         try
135         {
136            Transaction.bindToENC();
137         }
138         catch (NameNotFoundException e)
139         {
140             Log.info("No Transaction manager found - if your webapp requires one, please configure one.");
141         }
142     }
143     
144     public void configureClassLoader ()
145     throws Exception
146     {      
147         super.configureClassLoader();
148     }
149 
150     
151     public void configureDefaults ()
152     throws Exception
153     {
154         super.configureDefaults();
155     }
156 
157 
158     public void configureWebApp ()
159     throws Exception
160     {
161         super.configureWebApp();
162         //lock this webapp's java:comp namespace as per J2EE spec
163         ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
164         Thread.currentThread().setContextClassLoader(getWebAppContext().getClassLoader());
165         lockCompEnv();
166         Thread.currentThread().setContextClassLoader(oldLoader);
167     }
168     
169     public void deconfigureWebApp() throws Exception
170     {
171         ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
172         Thread.currentThread().setContextClassLoader(getWebAppContext().getClassLoader());
173         unlockCompEnv();
174         Thread.currentThread().setContextClassLoader(oldLoader);
175         super.deconfigureWebApp();
176     }
177     
178     protected void lockCompEnv ()
179     throws Exception
180     {
181         Random random = new Random ();
182         _key = new Integer(random.nextInt());
183         Context context = new InitialContext();
184         Context compCtx = (Context)context.lookup("java:comp");
185         compCtx.addToEnvironment("org.mortbay.jndi.lock", _key);
186     }
187     
188     protected void unlockCompEnv ()
189     throws Exception
190     {
191         if (_key!=null)
192         {
193             Context context = new InitialContext();
194             Context compCtx = (Context)context.lookup("java:comp");
195             compCtx.addToEnvironment("org.mortbay.jndi.unlock", _key); 
196         }
197     }
198 
199     /** 
200      * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#parseAnnotations()
201      */
202     public void parseAnnotations() throws Exception
203     {
204         Log.info(getClass().getName()+" does not support annotations on source. Use org.mortbay.jetty.annotations.Configuration instead");
205     }
206     
207 }