View Javadoc

1   // ========================================================================
2   // Copyright (c) 2010-2011 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.overlays;
15  
16  import java.io.IOException;
17  import java.security.PermissionCollection;
18  import java.util.Map;
19  
20  import org.eclipse.jetty.http.MimeTypes;
21  import org.eclipse.jetty.server.ResourceCache;
22  import org.eclipse.jetty.server.Server;
23  import org.eclipse.jetty.util.component.AggregateLifeCycle;
24  import org.eclipse.jetty.util.component.Destroyable;
25  import org.eclipse.jetty.util.resource.Resource;
26  import org.eclipse.jetty.webapp.ClasspathPattern;
27  import org.eclipse.jetty.webapp.WebAppClassLoader;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  import org.eclipse.jetty.xml.XmlConfiguration;
30  
31  /**
32   * A Cloudtide template context.
33   * <p>
34   * This class is configured by the template.xml files and is used to control the
35   * shared resource cache and classloader.
36   * <p>
37   * This class is an AggregateLifeCycle, so dependent beans may be added to the template and will be started, stopped and destroyed with the template.
38   * The template is started after the template.xml file have been applied. It is stopped and destroyed after the last instance using the template is undeployed.
39   */
40  public class TemplateContext extends AggregateLifeCycle implements WebAppClassLoader.Context, Destroyable
41  {
42      private final ClassLoader _libLoader;
43      
44      private final Resource _baseResource;
45      private final ResourceCache _resourceCache;
46      private final Server _server;
47      private final MimeTypes _mimeTypes;
48      private final WebAppClassLoader _webappLoader;
49      
50      private ClasspathPattern _systemClasses;
51      private ClasspathPattern _serverClasses;
52      private PermissionCollection _permissions;
53  
54      private boolean _parentLoaderPriority;
55  
56      private String _extraClasspath;
57  
58      private Map<String, Object> _idMap;
59  
60      
61      public ClassLoader getLibLoader()
62      {
63          return _libLoader;
64      }
65  
66      public TemplateContext()
67      {
68          _server=null;
69          _baseResource=null;
70          _mimeTypes=new MimeTypes();
71          _resourceCache=null;
72          _webappLoader=null;
73          _libLoader=null;
74      }
75      
76      public TemplateContext(String key, Server server,Resource baseResource, ClassLoader libLoader) throws IOException
77      {
78          _server=server;
79          _baseResource=baseResource;
80          _mimeTypes=new MimeTypes();
81          _resourceCache=new ResourceCache(null,baseResource,_mimeTypes);
82          
83          String[] patterns = (String[])_server.getAttribute(WebAppContext.SERVER_SRV_CLASSES);
84          _serverClasses=new ClasspathPattern(patterns==null?WebAppContext.__dftServerClasses:patterns);
85          patterns = (String[])_server.getAttribute(WebAppContext.SERVER_SYS_CLASSES);
86          _systemClasses=new ClasspathPattern(patterns==null?WebAppContext.__dftSystemClasses:patterns);
87          _libLoader=libLoader;
88          
89  
90          // Is this a webapp or a normal context
91          Resource classes=getBaseResource().addPath("WEB-INF/classes/");
92          Resource lib=getBaseResource().addPath("WEB-INF/lib/");
93          if (classes.exists() && classes.isDirectory() || lib.exists() && lib.isDirectory())
94          {
95              _webappLoader=new WebAppClassLoader(_libLoader,this);
96              _webappLoader.setName(key);
97              if (classes.exists())
98                  _webappLoader.addClassPath(classes);
99              if (lib.exists())
100                 _webappLoader.addJars(lib);            
101         }
102         else 
103             _webappLoader=null;
104         
105     }
106 
107     /* ------------------------------------------------------------ */
108     public Resource getBaseResource()
109     {
110         return _baseResource;
111     }
112     
113     /* ------------------------------------------------------------ */
114     /**
115      * @return Comma or semicolon separated path of filenames or URLs
116      * pointing to directories or jar files. Directories should end
117      * with '/'.
118      */
119     public String getExtraClasspath()
120     {
121         return _extraClasspath;
122     }
123 
124     /* ------------------------------------------------------------ */
125     public MimeTypes getMimeTypes()
126     {
127         return _mimeTypes;
128     }
129 
130     
131     /* ------------------------------------------------------------ */
132     public PermissionCollection getPermissions()
133     {
134         return _permissions;
135     }
136 
137     /* ------------------------------------------------------------ */
138     public ResourceCache getResourceCache()
139     {
140         return _resourceCache;
141     }
142 
143     /* ------------------------------------------------------------ */
144     public Server getServer()
145     {
146         return _server;
147     }
148 
149     /* ------------------------------------------------------------ */
150     WebAppClassLoader getWebappLoader()
151     {
152         return _webappLoader;
153     }
154 
155     /* ------------------------------------------------------------ */
156     public boolean isParentLoaderPriority()
157     {
158         return _parentLoaderPriority;
159     }
160 
161     /* ------------------------------------------------------------ */
162     public boolean isServerClass(String clazz)
163     {
164         return _serverClasses.match(clazz);
165     }
166 
167     /* ------------------------------------------------------------ */
168     public boolean isSystemClass(String clazz)
169     {
170         return _systemClasses.match(clazz);
171     }
172 
173     /* ------------------------------------------------------------ */
174     public Resource newResource(String urlOrPath) throws IOException
175     {
176         return Resource.newResource(urlOrPath);
177     }
178 
179     /* ------------------------------------------------------------ */
180     /**
181      * @param extraClasspath Comma or semicolon separated path of filenames or URLs
182      * pointing to directories or jar files. Directories should end
183      * with '/'.
184      */
185     public void setExtraClasspath(String extraClasspath)
186     {
187         _extraClasspath=extraClasspath;
188     }
189 
190     /* ------------------------------------------------------------ */
191     /**
192      * @param java2compliant The java2compliant to set.
193      */         
194     public void setParentLoaderPriority(boolean java2compliant)
195     {
196         _parentLoaderPriority = java2compliant;
197     }
198 
199     /* ------------------------------------------------------------ */
200     /**
201      * @param permissions The permissions to set.
202      */
203     public void setPermissions(PermissionCollection permissions)
204     {
205         _permissions = permissions;
206     }
207 
208     /* ------------------------------------------------------------ */
209     /**
210      * Set the server classes patterns.
211      * <p>
212      * Server classes/packages are classes used to implement the server and are hidden
213      * from the context.  If the context needs to load these classes, it must have its
214      * own copy of them in WEB-INF/lib or WEB-INF/classes.
215      * A class pattern is a string of one of the forms:<dl>
216      * <dt>org.package.Classname</dt><dd>Match a specific class</dd>
217      * <dt>org.package.</dt><dd>Match a specific package hierarchy</dd>
218      * <dt>-org.package.Classname</dt><dd>Exclude a specific class</dd>
219      * <dt>-org.package.</dt><dd>Exclude a specific package hierarchy</dd>
220      * </dl>
221      * @param serverClasses The serverClasses to set.
222      */
223     public void setServerClasses(String[] serverClasses)
224     {
225         _serverClasses = new ClasspathPattern(serverClasses);
226     }
227 
228     /* ------------------------------------------------------------ */
229     /**
230      * Set the system classes patterns.
231      * <p>
232      * System classes/packages are classes provided by the JVM and that
233      * cannot be replaced by classes of the same name from WEB-INF,
234      * regardless of the value of {@link #setParentLoaderPriority(boolean)}.
235      * A class pattern is a string of one of the forms:<dl>
236      * <dt>org.package.Classname</dt><dd>Match a specific class</dd>
237      * <dt>org.package.</dt><dd>Match a specific package hierarchy</dd>
238      * <dt>-org.package.Classname</dt><dd>Exclude a specific class</dd>
239      * <dt>-org.package.</dt><dd>Exclude a specific package hierarchy</dd>
240      * </dl>
241      * @param systemClasses The systemClasses to set.
242      */
243     public void setSystemClasses(String[] systemClasses)
244     {
245         _systemClasses = new ClasspathPattern(systemClasses);
246     }
247 
248     /* ------------------------------------------------------------ */
249     public void addSystemClass(String classname)
250     {
251         _systemClasses.addPattern(classname);
252     }
253 
254     /* ------------------------------------------------------------ */
255     public void addServerClass(String classname)
256     {
257         _serverClasses.addPattern(classname);
258     }
259     
260     /* ------------------------------------------------------------ */
261     public void destroy()
262     {
263         if (_baseResource!=null)
264             _baseResource.release();
265         if (_resourceCache!=null)
266             _resourceCache.flushCache();
267         if(_idMap!=null)
268             _idMap.clear();
269     }
270 
271     /* ------------------------------------------------------------ */
272     public void setIdMap(Map<String, Object> idMap)
273     {
274         _idMap=idMap;
275     }
276 
277     /* ------------------------------------------------------------ */
278     public Map<String, Object> getIdMap()
279     {
280         return _idMap;
281     }
282     
283     
284     
285 }