View Javadoc

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