001/*
002 * (C) Copyright 2012-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     mguillaume
018 */
019package org.nuxeo.launcher.info;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.Set;
026import java.util.StringTokenizer;
027
028import javax.xml.bind.annotation.XmlAccessType;
029import javax.xml.bind.annotation.XmlAccessorType;
030import javax.xml.bind.annotation.XmlRootElement;
031import javax.xml.bind.annotation.XmlType;
032import javax.xml.parsers.DocumentBuilder;
033import javax.xml.parsers.DocumentBuilderFactory;
034import javax.xml.parsers.ParserConfigurationException;
035
036import org.apache.commons.logging.LogFactory;
037import org.nuxeo.connect.update.LocalPackage;
038import org.nuxeo.connect.update.NuxeoValidationState;
039import org.nuxeo.connect.update.Package;
040import org.nuxeo.connect.update.PackageDependency;
041import org.nuxeo.connect.update.PackageException;
042import org.nuxeo.connect.update.PackageState;
043import org.nuxeo.connect.update.PackageType;
044import org.nuxeo.connect.update.PackageVisibility;
045import org.nuxeo.connect.update.ProductionState;
046import org.w3c.dom.Document;
047import org.w3c.dom.Element;
048import org.w3c.dom.NodeList;
049import org.xml.sax.SAXException;
050
051@XmlAccessorType(XmlAccessType.FIELD)
052@XmlRootElement(name = "package")
053@XmlType(propOrder = { "id", "state", "version", "name", "type", "visibility", "targetPlatforms", "vendor",
054        "supportsHotReload", "supported", "productionState", "validationState", "provides", "dependencies", "conflicts",
055        "title", "description", "homePage", "licenseType", "licenseUrl", "templates" })
056public class PackageInfo {
057
058    public String name;
059
060    public String version;
061
062    public String id;
063
064    public PackageState state;
065
066    public String title;
067
068    public String description;
069
070    public String homePage;
071
072    public String licenseType;
073
074    public String licenseUrl;
075
076    public ProductionState productionState;
077
078    public NuxeoValidationState validationState;
079
080    public String[] targetPlatforms;
081
082    public PackageType type;
083
084    public String vendor;
085
086    public PackageVisibility visibility;
087
088    public PackageDependency[] provides;
089
090    public PackageDependency[] dependencies;
091
092    public PackageDependency[] conflicts;
093
094    public boolean supportsHotReload;
095
096    public boolean supported;
097
098    public Set<String> templates;
099
100    public PackageInfo() {
101    }
102
103    /**
104     * @since 5.7
105     */
106    public PackageInfo(Package pkg) {
107        name = pkg.getName();
108        version = pkg.getVersion().toString();
109        id = pkg.getId();
110        state = pkg.getPackageState();
111        title = pkg.getTitle();
112        description = pkg.getDescription();
113        homePage = pkg.getHomePage();
114        licenseType = pkg.getLicenseType();
115        licenseUrl = pkg.getLicenseUrl();
116        productionState = pkg.getProductionState();
117        validationState = pkg.getValidationState();
118        targetPlatforms = pkg.getTargetPlatforms();
119        type = pkg.getType();
120        vendor = pkg.getVendor();
121        visibility = pkg.getVisibility();
122        if (visibility == null) {
123            visibility = PackageVisibility.UNKNOWN;
124        }
125        provides = pkg.getProvides();
126        dependencies = pkg.getDependencies();
127        conflicts = pkg.getConflicts();
128        supportsHotReload = pkg.supportsHotReload();
129        supported = pkg.isSupported();
130        templates = templates(pkg);
131    }
132
133    /**
134     * @since 8.3
135     */
136    private Set<String> templates(Package pkg) {
137        if (!(pkg instanceof LocalPackage)) {
138            return Collections.emptySet();
139        }
140        Set<String> templatesFound = new HashSet<>();
141        try {
142            File installFile = ((LocalPackage) pkg).getInstallFile();
143            if (!installFile.exists()) {
144                return Collections.emptySet();
145            }
146            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
147            DocumentBuilder db = dbf.newDocumentBuilder();
148            Document dom = db.parse(installFile);
149            NodeList nodes = dom.getDocumentElement().getElementsByTagName("config");
150            for (int i = 0; i < nodes.getLength(); i++) {
151                Element node = (Element) nodes.item(i);
152                if (!node.hasAttribute("addtemplate")) {
153                    continue;
154                }
155                StringTokenizer tokenizer = new StringTokenizer(node.getAttribute("addtemplate"), ",");
156                while (tokenizer.hasMoreTokens()) {
157                    templatesFound.add(tokenizer.nextToken());
158                }
159
160            }
161        } catch (PackageException | ParserConfigurationException | SAXException | IOException e) {
162            LogFactory.getLog(PackageInfo.class).warn("Could not parse install file for " + pkg.getName(), e);
163        }
164        return templatesFound;
165    }
166}