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 quickStartWebXml = getQuickStartWebXml(context);
77 LOG.debug("quickStartWebXml={}",quickStartWebXml);
78
79 context.getMetaData().setWebXml(quickStartWebXml);
80 }
81
82
83
84
85
86
87
88
89
90 public Resource getQuickStartWebXml (WebAppContext context) throws Exception
91 {
92 Resource webInf = context.getWebInf();
93 if (webInf == null || !webInf.exists())
94 throw new IllegalStateException("No WEB-INF");
95 LOG.debug("webinf={}",webInf);
96
97 Resource quickStartWebXml = webInf.addPath("quickstart-web.xml");
98 if (!quickStartWebXml.exists())
99 throw new IllegalStateException ("No WEB-INF/quickstart-web.xml");
100 return quickStartWebXml;
101 }
102
103
104
105
106
107
108 @Override
109 public void configure(WebAppContext context) throws Exception
110 {
111 LOG.debug("configure {}",this);
112 if (context.isStarted())
113 {
114 LOG.warn("Cannot configure webapp after it is started");
115 return;
116 }
117
118
119 Resource webInf = context.getWebInf();
120
121 if (webInf != null && webInf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)
122 {
123
124 Resource classes= webInf.addPath("classes/");
125 if (classes.exists())
126 ((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);
127
128
129 Resource lib= webInf.addPath("lib/");
130 if (lib.exists() || lib.isDirectory())
131 ((WebAppClassLoader)context.getClassLoader()).addJars(lib);
132 }
133
134
135 context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
136
137
138 context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());
139
140
141 context.addDecorator(new AnnotationDecorator(context));
142
143
144 ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
145 if (starter != null)
146 throw new IllegalStateException("ServletContainerInitializersStarter already exists");
147 starter = new ServletContainerInitializersStarter(context);
148 context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
149 context.addBean(starter, true);
150
151 LOG.debug("configured {}",this);
152 }
153
154 }