summaryrefslogtreecommitdiffstats
path: root/client-compiler/src/com/vaadin/tools/CvalAddonsChecker.java
blob: aab723125801a4a42c2efb15e42fffd8dd05d256 (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
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
/*
 * 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.tools;

import static com.vaadin.tools.CvalChecker.LINE;
import static com.vaadin.tools.CvalChecker.computeMajorVersion;
import static com.vaadin.tools.CvalChecker.getErrorMessage;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import com.vaadin.client.metadata.ConnectorBundleLoader.CValUiInfo;
import com.vaadin.tools.CvalChecker.CvalInfo;
import com.vaadin.tools.CvalChecker.CvalServer;
import com.vaadin.tools.CvalChecker.InvalidCvalException;
import com.vaadin.tools.CvalChecker.UnreachableCvalServerException;

/**
 * This class is able to visit all MANIFEST.MF files present in the classpath,
 * filter by name, and check if the user has a valid license.
 *
 * Manifest files should have a few attributes indicating the license type of
 * the addon:
 * <ul>
 * <li>Implementation-Version: 4.x.x
 * <li>AdVaaName: addon_name
 * <li>AdVaaLicen: cval, agpl, empty
 * <li>AdVaaPkg: package of the widgets in this addon
 * </ul>
 *
 * The class also have a method to check just one product.
 *
 * @since 7.3
 */
public final class CvalAddonsChecker {

    // Manifest attributes
    public static final String VAADIN_ADDON_LICENSE = "AdVaaLicen";
    public static final String VAADIN_ADDON_NAME = "AdVaaName";
    public static final String VAADIN_ADDON_WIDGETSET = "Vaadin-Widgetsets";
    public static final String VAADIN_ADDON_VERSION = "Implementation-Version";
    public static final String VAADIN_ADDON_TITLE = "Implementation-Title";

    // License types
    public static final String VAADIN_AGPL = "agpl";
    public static final String VAADIN_CVAL = "cval";

    private CvalChecker cvalChecker = new CvalChecker();
    private String filterPattern;

    /**
     * The constructor.
     */
    public CvalAddonsChecker() {
        setLicenseProvider(new CvalServer());
        setFilter(".*vaadin.*");
    }

    /**
     * Visit all MANIFEST.MF files in the classpath validating licenses.
     * 
     * Return a list of Cval licensed products in order to have enough info to
     * generate nag messages in the UI.
     */
    public List<CValUiInfo> run() throws InvalidCvalException {
        List<CValUiInfo> ret = new ArrayList<CValUiInfo>();
        try {
            // Visit all MANIFEST in our classpath
            Enumeration<URL> manifests = Thread.currentThread()
                    .getContextClassLoader()
                    .getResources(JarFile.MANIFEST_NAME);
            while (manifests.hasMoreElements()) {
                try {
                    URL url = manifests.nextElement();
                    // Discard manifests whose name does not match the filter
                    // pattern
                    if (!url.getPath().matches(filterPattern)) {
                        continue;
                    }
                    InputStream is = url.openStream();
                    // Should never happen, but we don't want a NPE here
                    if (is == null) {
                        continue;
                    }
                    // Read manifest attributes
                    Manifest manifest = new Manifest(is);
                    Attributes attribs = manifest.getMainAttributes();
                    String license = attribs.getValue(VAADIN_ADDON_LICENSE);
                    String name = attribs.getValue(VAADIN_ADDON_NAME);
                    String vers = attribs.getValue(VAADIN_ADDON_VERSION) == null ? ""
                            : attribs.getValue(VAADIN_ADDON_VERSION);
                    String title = attribs.getValue(VAADIN_ADDON_TITLE) == null ? name
                            : attribs.getValue(VAADIN_ADDON_TITLE);

                    String widgetsets = attribs
                            .getValue(VAADIN_ADDON_WIDGETSET) == null ? name
                            : attribs.getValue(VAADIN_ADDON_WIDGETSET);

                    if (name == null || license == null) {
                        continue;
                    }
                    if (VAADIN_AGPL.equals(license)) {
                        // For agpl version we print an info message
                        printAgplLicense(title, vers);
                    } else if (VAADIN_CVAL.equals(license)) {
                        // We only check cval licensed products
                        CvalInfo info;
                        try {
                            info = cvalChecker.validateProduct(name, vers,
                                    title);
                            printValidLicense(info, title, vers);
                        } catch (UnreachableCvalServerException e) {
                            info = CvalChecker.parseJson("{'product':{'name':'"
                                    + name + "'}}");
                            printServerUnreachable(title, vers);
                        }
                        for (String w : widgetsets.split("[, ]+")) {
                            ret.add(new CValUiInfo(title, String
                                    .valueOf(computeMajorVersion(vers)), w,
                                    info.getType()));
                        }
                    }
                } catch (IOException ignored) {
                }
            }
        } catch (IOException ignored) {
        }
        return ret;
    }

    /**
     * Set the filter regexp of .jar names which we have to consider.
     * 
     * default is '.*touchkit.*'
     */
    public CvalAddonsChecker setFilter(String regexp) {
        filterPattern = regexp;
        return this;
    }

    /*
     * Change the license provider, only used in tests.
     */
    protected CvalAddonsChecker setLicenseProvider(CvalServer p) {
        cvalChecker.setLicenseProvider(p);
        return this;
    }

    private void printAgplLicense(String name, String version) {
        System.out.println(LINE + "\n"
                + getErrorMessage("agpl", name, computeMajorVersion(version))
                + "\n" + LINE);
    }

    private void printServerUnreachable(String name, String version) {
        System.out.println(LINE
                + "\n"
                + getErrorMessage("unreachable", name,
                        computeMajorVersion(version)) + "\n" + LINE);
    }

    private void printValidLicense(CvalInfo info, String title, String version) {
        String msg = info.getMessage();
        if (msg == null) {
            String key = "evaluation".equals(info.getType()) ? "evaluation"
                    : "valid";
            msg = getErrorMessage(key, title, computeMajorVersion(version),
                    info.getLicensee());
        }
        System.out.println("\n" + LINE + "\n" + msg + "\n" + LINE + "\n");
    }
}