diff options
author | Manolo Carrasco <manolo@vaadin.com> | 2014-04-30 13:26:32 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-06-26 08:30:46 +0000 |
commit | 9a84fb14f2b7fc3c405f1446a007d5b9cd796c97 (patch) | |
tree | 40f1a0420f22fa70b91f7d59d064fe9975c2c33f /client-compiler/src/com/vaadin/tools/CvalAddonsChecker.java | |
parent | 0ff4e15b96b91becdd6c8e38ad20d8272c5455b7 (diff) | |
download | vaadin-framework-9a84fb14f2b7fc3c405f1446a007d5b9cd796c97.tar.gz vaadin-framework-9a84fb14f2b7fc3c405f1446a007d5b9cd796c97.zip |
License Checker for vaadin cval products (#13696 #13474)
- This patch includes four elements:
1.- A class able to validate a licensed product against Vaadin
license server. It can be used in any vaadin product (thought
for non addons like TB) just adding vaadin dependency, or
copying the class.
2.- A class able to inspect all addons in the classpath and figure
out, based on the MANIFEST.MF info, whether we have to check
developer license.
3.- A modification to Vaadin connector generator to use the classes
above and to stop compilation in case.
4.- A modification to ConnectorBundleLoader, so as when a new connector
is instatiated, we check whether it is using an evaluation
license and show a notice. We only show the notice once.
- In addition to validating developer licenses, the checker caches the
server response for using it when there are connection problems.
- This stuff is in Vaadin core, so as we dont maintain license code in
each addon. For checking an addon license we just add the license type
to the manifest when packaging the artefact.
- It checks expiration time, product name and major version.
Fixes: #13696
In some-way related with: #13474
Change-Id: Ib61b1c2e9c3cacd463a1ce5db02c2cfbc06851c2
Diffstat (limited to 'client-compiler/src/com/vaadin/tools/CvalAddonsChecker.java')
-rw-r--r-- | client-compiler/src/com/vaadin/tools/CvalAddonsChecker.java | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/client-compiler/src/com/vaadin/tools/CvalAddonsChecker.java b/client-compiler/src/com/vaadin/tools/CvalAddonsChecker.java new file mode 100644 index 0000000000..6780b1bc75 --- /dev/null +++ b/client-compiler/src/com/vaadin/tools/CvalAddonsChecker.java @@ -0,0 +1,191 @@ +/* + * 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. + */ +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"); + } +} |