001/* 002 * (C) Copyright 2012-2019 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 * Yannis JULIENNE 019 */ 020package org.nuxeo.launcher.info; 021 022import java.io.File; 023import java.io.IOException; 024import java.util.Collections; 025import java.util.HashSet; 026import java.util.Set; 027import java.util.StringTokenizer; 028 029import javax.xml.bind.annotation.XmlAccessType; 030import javax.xml.bind.annotation.XmlAccessorType; 031import javax.xml.bind.annotation.XmlRootElement; 032import javax.xml.bind.annotation.XmlType; 033import javax.xml.parsers.DocumentBuilder; 034import javax.xml.parsers.DocumentBuilderFactory; 035import javax.xml.parsers.ParserConfigurationException; 036 037import org.apache.commons.logging.LogFactory; 038import org.nuxeo.connect.update.LocalPackage; 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.w3c.dom.Document; 045import org.w3c.dom.Element; 046import org.w3c.dom.NodeList; 047import org.xml.sax.SAXException; 048 049@XmlAccessorType(XmlAccessType.FIELD) 050@XmlRootElement(name = "package") 051@XmlType(propOrder = { "id", "state", "version", "name", "type", "targetPlatforms", "vendor", "supportsHotReload", 052 "provides", "dependencies", "conflicts", "title", "description", "licenseType", "licenseUrl", "templates" }) 053public class PackageInfo { 054 055 public String name; 056 057 public String version; 058 059 public String id; 060 061 public PackageState state; 062 063 public String title; 064 065 public String description; 066 067 public String licenseType; 068 069 public String licenseUrl; 070 071 public String[] targetPlatforms; 072 073 public PackageType type; 074 075 public String vendor; 076 077 public PackageDependency[] provides; 078 079 public PackageDependency[] dependencies; 080 081 public PackageDependency[] conflicts; 082 083 public boolean supportsHotReload; 084 085 public Set<String> templates; 086 087 public PackageInfo() { 088 } 089 090 /** 091 * @since 5.7 092 */ 093 public PackageInfo(Package pkg) { 094 name = pkg.getName(); 095 version = pkg.getVersion().toString(); 096 id = pkg.getId(); 097 state = pkg.getPackageState(); 098 title = pkg.getTitle(); 099 description = pkg.getDescription(); 100 licenseType = pkg.getLicenseType(); 101 licenseUrl = pkg.getLicenseUrl(); 102 targetPlatforms = pkg.getTargetPlatforms(); 103 type = pkg.getType(); 104 provides = pkg.getProvides(); 105 dependencies = pkg.getDependencies(); 106 conflicts = pkg.getConflicts(); 107 supportsHotReload = pkg.supportsHotReload(); 108 templates = templates(pkg); 109 } 110 111 /** 112 * @since 8.3 113 */ 114 private Set<String> templates(Package pkg) { 115 if (!(pkg instanceof LocalPackage)) { 116 return Collections.emptySet(); 117 } 118 Set<String> templatesFound = new HashSet<>(); 119 try { 120 File installFile = ((LocalPackage) pkg).getInstallFile(); 121 if (!installFile.exists()) { 122 return Collections.emptySet(); 123 } 124 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 125 DocumentBuilder db = dbf.newDocumentBuilder(); 126 Document dom = db.parse(installFile); 127 NodeList nodes = dom.getDocumentElement().getElementsByTagName("config"); 128 for (int i = 0; i < nodes.getLength(); i++) { 129 Element node = (Element) nodes.item(i); 130 if (!node.hasAttribute("addtemplate")) { 131 continue; 132 } 133 StringTokenizer tokenizer = new StringTokenizer(node.getAttribute("addtemplate"), ","); 134 while (tokenizer.hasMoreTokens()) { 135 templatesFound.add(tokenizer.nextToken()); 136 } 137 138 } 139 } catch (PackageException | ParserConfigurationException | SAXException | IOException e) { 140 LogFactory.getLog(PackageInfo.class).warn("Could not parse install file for " + pkg.getName(), e); 141 } 142 return templatesFound; 143 } 144}