1 // ========================================================================
2 // Copyright (c) 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
15 package org.eclipse.jetty.webapp;
16
17 import java.util.List;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.jetty.util.resource.Resource;
21
22 /**
23 * FragmentConfiguration
24 *
25 * This configuration supports some Servlet 3.0 features in jetty-7.
26 *
27 * Process web-fragments in jars
28 */
29 public class FragmentConfiguration implements Configuration
30 {
31 public final static String FRAGMENT_RESOURCES="org.eclipse.jetty.webFragments";
32
33 public void preConfigure(WebAppContext context) throws Exception
34 {
35 if (!context.isConfigurationDiscovered())
36 return;
37
38 WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR);
39 if (processor == null)
40 {
41 processor = new WebXmlProcessor (context);
42 context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
43 }
44
45
46 //parse web-fragment.xmls
47 parseWebFragments(context, processor);
48
49 //TODO for jetty-8/servletspec 3 we will need to merge the parsed web fragments into the
50 //effective pom in this preConfigure step
51 }
52
53 public void configure(WebAppContext context) throws Exception
54 {
55 if (!context.isConfigurationDiscovered())
56 return;
57
58 //TODO for jetty-8/servletspec3 the fragments will not be separately processed here, but
59 //will be done by webXmlConfiguration when it processes the effective merged web.xml
60 WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR);
61 if (processor == null)
62 {
63 processor = new WebXmlProcessor (context);
64 context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
65 }
66
67 processor.processFragments();
68 }
69
70 public void deconfigure(WebAppContext context) throws Exception
71 {
72
73 }
74
75 public void postConfigure(WebAppContext context) throws Exception
76 {
77 context.setAttribute(FRAGMENT_RESOURCES, null);
78 }
79
80 /* ------------------------------------------------------------------------------- */
81 /**
82 * Look for any web.xml fragments in META-INF of jars in WEB-INF/lib
83 *
84 * @throws Exception
85 */
86 public void parseWebFragments (final WebAppContext context, final WebXmlProcessor processor) throws Exception
87 {
88 List<Resource> frags = (List<Resource>)context.getAttribute(FRAGMENT_RESOURCES);
89 if (frags!=null)
90 {
91 for (Resource frag : frags)
92 {
93 processor.parseFragment(Resource.newResource("jar:"+frag.getURL()+"!/META-INF/web-fragment.xml"));
94 }
95 }
96 }
97
98 }