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 public class QuickStartConfiguration extends WebInfConfiguration
39 {
40 private static final Logger LOG = Log.getLogger(QuickStartConfiguration.class);
41
42
43
44
45 @Override
46 public void preConfigure(WebAppContext context) throws Exception
47 {
48
49 String war = context.getWar();
50 if (war == null || war.length()<=0)
51 throw new IllegalStateException ("No location for webapp");
52
53
54 resolveTempDirectory(context);
55
56 Resource webApp = context.newResource(war);
57
58
59 if (webApp.isAlias())
60 {
61 LOG.debug(webApp + " anti-aliased to " + webApp.getAlias());
62 webApp = context.newResource(webApp.getAlias());
63 }
64
65
66 if (!webApp.exists() || !webApp.isDirectory() || webApp.toString().startsWith("jar:"))
67 throw new IllegalStateException("Webapp does not exist or is not unpacked");
68
69 context.setBaseResource(webApp);
70
71 LOG.debug("webapp={}",webApp);
72
73
74
75 Resource quickStartWebXml = getQuickStartWebXml(context);
76 LOG.debug("quickStartWebXml={}",quickStartWebXml);
77
78 context.getMetaData().setWebXml(quickStartWebXml);
79 }
80
81
82
83
84
85
86
87
88
89 public Resource getQuickStartWebXml (WebAppContext context) throws Exception
90 {
91 Resource webInf = context.getWebInf();
92 if (webInf == null || !webInf.exists())
93 throw new IllegalStateException("No WEB-INF");
94 LOG.debug("webinf={}",webInf);
95
96 Resource quickStartWebXml = webInf.addPath("quickstart-web.xml");
97 if (!quickStartWebXml.exists())
98 throw new IllegalStateException ("No WEB-INF/quickstart-web.xml");
99 return quickStartWebXml;
100 }
101
102
103
104
105
106
107 @Override
108 public void configure(WebAppContext context) throws Exception
109 {
110 LOG.debug("configure {}",this);
111 if (context.isStarted())
112 {
113 LOG.warn("Cannot configure webapp after it is started");
114 return;
115 }
116
117
118 Resource webInf = context.getWebInf();
119
120 if (webInf != null && webInf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)
121 {
122
123 Resource classes= webInf.addPath("classes/");
124 if (classes.exists())
125 ((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);
126
127
128 Resource lib= webInf.addPath("lib/");
129 if (lib.exists() || lib.isDirectory())
130 ((WebAppClassLoader)context.getClassLoader()).addJars(lib);
131 }
132
133
134 context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
135
136
137 context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());
138
139
140 context.getObjectFactory().addDecorator(new AnnotationDecorator(context));
141
142
143 ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
144 if (starter != null)
145 throw new IllegalStateException("ServletContainerInitializersStarter already exists");
146 starter = new ServletContainerInitializersStarter(context);
147 context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
148 context.addBean(starter, true);
149
150 LOG.debug("configured {}",this);
151 }
152
153 }