1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.quickstart;
20
21 import org.eclipse.jetty.annotations.AnnotationConfiguration;
22 import org.eclipse.jetty.annotations.AnnotationDecorator;
23 import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
24 import org.eclipse.jetty.util.log.Log;
25 import org.eclipse.jetty.util.log.Logger;
26 import org.eclipse.jetty.util.resource.Resource;
27 import org.eclipse.jetty.webapp.StandardDescriptorProcessor;
28 import org.eclipse.jetty.webapp.WebAppClassLoader;
29 import org.eclipse.jetty.webapp.WebAppContext;
30 import org.eclipse.jetty.webapp.WebInfConfiguration;
31
32
33
34
35
36
37
38
39 public class QuickStartConfiguration extends WebInfConfiguration
40 {
41 private static final Logger LOG = Log.getLogger(QuickStartConfiguration.class);
42
43
44
45
46 @Override
47 public void preConfigure(WebAppContext context) throws Exception
48 {
49
50 String war = context.getWar();
51 if (war == null || war.length()<=0)
52 throw new IllegalStateException ("No location for webapp");
53
54
55 resolveTempDirectory(context);
56
57 Resource webApp = context.newResource(war);
58
59
60 if (webApp.getAlias() != null)
61 {
62 LOG.debug(webApp + " anti-aliased to " + webApp.getAlias());
63 webApp = context.newResource(webApp.getAlias());
64 }
65
66
67 if (!webApp.exists() || !webApp.isDirectory() || webApp.toString().startsWith("jar:"))
68 throw new IllegalStateException("Webapp does not exist or is not unpacked");
69
70 context.setBaseResource(webApp);
71
72 LOG.debug("webapp={}",webApp);
73
74
75
76 Resource webInf = context.getWebInf();
77 if (webInf == null || !webInf.exists())
78 throw new IllegalStateException("No WEB-INF");
79 LOG.debug("webinf={}",webInf);
80
81 Resource quickStartWebXml = webInf.addPath("quickstart-web.xml");
82 if (!quickStartWebXml.exists())
83 throw new IllegalStateException ("No WEB-INF/quickstart-web.xml");
84 LOG.debug("quickStartWebXml={}",quickStartWebXml);
85
86 context.getMetaData().setWebXml(quickStartWebXml);
87 }
88
89
90
91
92
93 @Override
94 public void configure(WebAppContext context) throws Exception
95 {
96 LOG.debug("configure {}",this);
97 if (context.isStarted())
98 {
99 LOG.warn("Cannot configure webapp after it is started");
100 return;
101 }
102
103
104 Resource webInf = context.getWebInf();
105
106 if (webInf != null && webInf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)
107 {
108
109 Resource classes= webInf.addPath("classes/");
110 if (classes.exists())
111 ((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);
112
113
114 Resource lib= webInf.addPath("lib/");
115 if (lib.exists() || lib.isDirectory())
116 ((WebAppClassLoader)context.getClassLoader()).addJars(lib);
117 }
118
119
120 context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
121
122
123 context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());
124
125
126 context.addDecorator(new AnnotationDecorator(context));
127
128
129 ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
130 if (starter != null)
131 throw new IllegalStateException("ServletContainerInitializersStarter already exists");
132 starter = new ServletContainerInitializersStarter(context);
133 context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
134 context.addBean(starter, true);
135
136 LOG.debug("configured {}",this);
137 }
138
139 }