1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.launcher;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.vaadin.launcher.CustomDeploymentConfiguration.Conf;
import com.vaadin.server.DefaultDeploymentConfiguration;
import com.vaadin.server.DeploymentConfiguration;
import com.vaadin.server.LegacyApplication;
import com.vaadin.server.LegacyVaadinServlet;
import com.vaadin.server.ServiceException;
import com.vaadin.server.SessionInitEvent;
import com.vaadin.server.SessionInitListener;
import com.vaadin.server.UIClassSelectionEvent;
import com.vaadin.server.UIProvider;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServletRequest;
import com.vaadin.server.VaadinSession;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.UI;
import com.vaadin.util.CurrentInstance;
@SuppressWarnings("serial")
public class ApplicationRunnerServlet extends LegacyVaadinServlet {
/**
* The name of the application class currently used. Only valid within one
* request.
*/
private LinkedHashSet<String> defaultPackages = new LinkedHashSet<String>();
private transient final ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
String initParameter = servletConfig
.getInitParameter("defaultPackages");
if (initParameter != null) {
Collections.addAll(defaultPackages, initParameter.split(","));
}
String str = TestBase.class.getName().replace('.', '/') + ".class";
URL url = getService().getClassLoader().getResource(str);
if ("file".equals(url.getProtocol())) {
String path = url.getPath();
try {
path = new URI(path).getPath();
} catch (URISyntaxException e) {
getLogger().log(Level.FINE, "Failed to decode url", e);
}
File comVaadinTests = new File(path).getParentFile()
.getParentFile();
addDirectories(comVaadinTests, defaultPackages, "com.vaadin.tests");
}
}
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
getService().addSessionInitListener(new SessionInitListener() {
@Override
public void sessionInit(SessionInitEvent event)
throws ServiceException {
onVaadinSessionStarted(event.getRequest(), event.getSession());
}
});
}
private void addDirectories(File parent, LinkedHashSet<String> packages,
String parentPackage) {
packages.add(parentPackage);
for (File f : parent.listFiles()) {
if (f.isDirectory()) {
String newPackage = parentPackage + "." + f.getName();
addDirectories(f, packages, newPackage);
}
}
}
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.request.set(request);
try {
super.service(request, response);
} finally {
this.request.set(null);
}
}
@Override
protected URL getApplicationUrl(HttpServletRequest request)
throws MalformedURLException {
URL url = super.getApplicationUrl(request);
String path = url.toString();
path += getApplicationRunnerApplicationClassName(request);
path += "/";
return new URL(path);
}
@Override
protected Class<? extends LegacyApplication> getApplicationClass()
throws ClassNotFoundException {
return getClassToRun().asSubclass(LegacyApplication.class);
}
@Override
protected boolean shouldCreateApplication(HttpServletRequest request)
throws ServletException {
try {
return LegacyApplication.class.isAssignableFrom(getClassToRun());
} catch (ClassNotFoundException e) {
throw new ServletException(e);
}
}
protected void onVaadinSessionStarted(VaadinRequest request,
VaadinSession session) throws ServiceException {
try {
final Class<?> classToRun = getClassToRun();
if (UI.class.isAssignableFrom(classToRun)) {
session.addUIProvider(new ApplicationRunnerUIProvider(
classToRun));
} else if (LegacyApplication.class.isAssignableFrom(classToRun)) {
// Avoid using own UIProvider for legacy Application
} else if (UIProvider.class.isAssignableFrom(classToRun)) {
session.addUIProvider((UIProvider) classToRun.newInstance());
} else {
throw new ServiceException(classToRun.getCanonicalName()
+ " is neither an Application nor a UI");
}
} catch (final IllegalAccessException e) {
throw new ServiceException(e);
} catch (final InstantiationException e) {
throw new ServiceException(e);
} catch (final ClassNotFoundException e) {
throw new ServiceException(
new InstantiationException(
"Failed to load application class: "
+ getApplicationRunnerApplicationClassName((VaadinServletRequest) request)));
}
}
private String getApplicationRunnerApplicationClassName(
HttpServletRequest request) {
return getApplicationRunnerURIs(request).applicationClassname;
}
private static final class ApplicationRunnerUIProvider extends UIProvider {
private final Class<?> classToRun;
private ApplicationRunnerUIProvider(Class<?> classToRun) {
this.classToRun = classToRun;
}
@Override
public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
return (Class<? extends UI>) classToRun;
}
}
// TODO Don't need to use a data object now that there's only one field
private static class URIS {
// String staticFilesPath;
// String applicationURI;
// String context;
// String runner;
String applicationClassname;
}
/**
* Parses application runner URIs.
*
* If request URL is e.g.
* http://localhost:8080/vaadin/run/com.vaadin.demo.Calc then
* <ul>
* <li>context=vaadin</li>
* <li>Runner servlet=run</li>
* <li>Vaadin application=com.vaadin.demo.Calc</li>
* </ul>
*
* @param request
* @return string array containing widgetset URI, application URI and
* context, runner, application classname
*/
private static URIS getApplicationRunnerURIs(HttpServletRequest request) {
final String[] urlParts = request.getRequestURI().toString()
.split("\\/");
// String runner = null;
URIS uris = new URIS();
String applicationClassname = null;
String contextPath = request.getContextPath();
if (urlParts[1].equals(contextPath.replaceAll("\\/", ""))) {
// class name comes after web context and runner application
// runner = urlParts[2];
if (urlParts.length == 3) {
throw new IllegalArgumentException("No application specified");
}
applicationClassname = urlParts[3];
// uris.applicationURI = "/" + context + "/" + runner + "/"
// + applicationClassname;
// uris.context = context;
// uris.runner = runner;
uris.applicationClassname = applicationClassname;
} else {
// no context
// runner = urlParts[1];
if (urlParts.length == 2) {
throw new IllegalArgumentException("No application specified");
}
applicationClassname = urlParts[2];
// uris.applicationURI = "/" + runner + "/" + applicationClassname;
// uris.context = context;
// uris.runner = runner;
uris.applicationClassname = applicationClassname;
}
return uris;
}
private Class<?> getClassToRun() throws ClassNotFoundException {
// TODO use getClassLoader() ?
Class<?> appClass = null;
String baseName = getApplicationRunnerApplicationClassName(request
.get());
try {
appClass = getClass().getClassLoader().loadClass(baseName);
return appClass;
} catch (Exception e) {
//
for (String pkg : defaultPackages) {
try {
appClass = getClass().getClassLoader().loadClass(
pkg + "." + baseName);
} catch (ClassNotFoundException ee) {
// Ignore as this is expected for many packages
} catch (Exception e2) {
// TODO: handle exception
getLogger().log(
Level.FINE,
"Failed to find application class " + pkg + "."
+ baseName, e2);
}
if (appClass != null) {
return appClass;
}
}
}
throw new ClassNotFoundException(baseName);
}
private Logger getLogger() {
return Logger.getLogger(ApplicationRunnerServlet.class.getName());
}
@Override
protected DeploymentConfiguration createDeploymentConfiguration(
Properties initParameters) {
// Get the original configuration from the super class
final DeploymentConfiguration originalConfiguration = super
.createDeploymentConfiguration(initParameters);
// And then create a proxy instance that delegates to the original
// configuration or a customized version
return (DeploymentConfiguration) Proxy.newProxyInstance(
DeploymentConfiguration.class.getClassLoader(),
new Class[] { DeploymentConfiguration.class },
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
if (method.getDeclaringClass() == DeploymentConfiguration.class) {
// Find the configuration instance to delegate to
DeploymentConfiguration configuration = findDeploymentConfiguration(originalConfiguration);
return method.invoke(configuration, args);
} else {
return method.invoke(proxy, args);
}
}
});
}
private DeploymentConfiguration findDeploymentConfiguration(
DeploymentConfiguration originalConfiguration) throws Exception {
// First level of cache
DeploymentConfiguration configuration = CurrentInstance
.get(DeploymentConfiguration.class);
if (configuration == null) {
// Not in cache, try to find a VaadinSession to get it from
VaadinSession session = VaadinSession.getCurrent();
if (session == null) {
/*
* There's no current session, request or response when serving
* static resources, but there's still the current request
* maintained by AppliationRunnerServlet, and there's most
* likely also a HttpSession containing a VaadinSession for that
* request.
*/
HttpServletRequest currentRequest = request.get();
if (currentRequest != null) {
HttpSession httpSession = currentRequest.getSession(false);
if (httpSession != null) {
Map<Class<?>, CurrentInstance> oldCurrent = CurrentInstance
.setCurrent((VaadinSession) null);
try {
session = getService().findVaadinSession(
new VaadinServletRequest(currentRequest,
getService()));
} finally {
/*
* Clear some state set by findVaadinSession to
* avoid accidentally depending on it when coding on
* e.g. static request handling.
*/
CurrentInstance.restoreInstances(oldCurrent);
currentRequest.removeAttribute(VaadinSession.class
.getName());
}
}
}
}
if (session != null) {
String name = ApplicationRunnerServlet.class.getName()
+ ".deploymentConfiguration";
try {
session.lock();
configuration = (DeploymentConfiguration) session
.getAttribute(name);
if (configuration == null) {
Class<?> classToRun;
try {
classToRun = getClassToRun();
} catch (ClassNotFoundException e) {
/*
* This happens e.g. if the UI class defined in the
* URL is not found or if this servlet just serves
* static resources while there's some other servlet
* that serves the UI (e.g. when using /run-push/).
*/
return originalConfiguration;
}
CustomDeploymentConfiguration customDeploymentConfiguration = classToRun
.getAnnotation(CustomDeploymentConfiguration.class);
if (customDeploymentConfiguration != null) {
Properties initParameters = new Properties(
originalConfiguration.getInitParameters());
for (Conf entry : customDeploymentConfiguration
.value()) {
initParameters.put(entry.name(), entry.value());
}
configuration = new DefaultDeploymentConfiguration(
getClass(), initParameters);
} else {
configuration = originalConfiguration;
}
session.setAttribute(name, configuration);
}
} finally {
session.unlock();
}
CurrentInstance.set(DeploymentConfiguration.class,
configuration);
} else {
configuration = originalConfiguration;
}
}
return configuration;
}
}
|