summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/server/ServletPortletHelper.java
blob: 9b1e60e6214d08fd825d6759b2f11bac20469978 (plain)
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
package com.vaadin.terminal.gwt.server;

import java.io.Serializable;

import com.vaadin.Application;
import com.vaadin.ui.Root;

/*
 @VaadinApache2LicenseForJavaFiles@
 */

class ServletPortletHelper implements Serializable {
    public static class ApplicationClassException extends Exception {

        public ApplicationClassException(String message, Throwable cause) {
            super(message, cause);
        }

        public ApplicationClassException(String message) {
            super(message);
        }
    }

    static Class<? extends Application> getApplicationClass(
            String applicationParameter, String rootParameter,
            ClassLoader classLoader) throws ApplicationClassException {
        if (applicationParameter == null) {

            // Validate the parameter value
            verifyRootClass(rootParameter, classLoader);

            // Application can be used if a valid rootLayout is defined
            return Application.class;
        }

        try {
            return (Class<? extends Application>) classLoader
                    .loadClass(applicationParameter);
        } catch (final ClassNotFoundException e) {
            throw new ApplicationClassException(
                    "Failed to load application class: " + applicationParameter,
                    e);
        }
    }

    private static void verifyRootClass(String className,
            ClassLoader classLoader) throws ApplicationClassException {
        if (className == null) {
            throw new ApplicationClassException(Application.ROOT_PARAMETER
                    + " init parameter not defined");
        }

        // Check that the root layout class can be found
        try {
            Class<?> rootClass = classLoader.loadClass(className);
            if (!Root.class.isAssignableFrom(rootClass)) {
                throw new ApplicationClassException(className
                        + " does not implement Root");
            }
            // Try finding a default constructor, else throw exception
            rootClass.getConstructor();
        } catch (ClassNotFoundException e) {
            throw new ApplicationClassException(className
                    + " could not be loaded", e);
        } catch (SecurityException e) {
            throw new ApplicationClassException("Could not access " + className
                    + " class", e);
        } catch (NoSuchMethodException e) {
            throw new ApplicationClassException(className
                    + " doesn't have a public no-args constructor");
        }
    }
}