001/* 002 * (C) Copyright 2012 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 */ 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.NuxeoValidationState; 040import org.nuxeo.connect.update.Package; 041import org.nuxeo.connect.update.PackageDependency; 042import org.nuxeo.connect.update.PackageException; 043import org.nuxeo.connect.update.PackageState; 044import org.nuxeo.connect.update.PackageType; 045import org.nuxeo.connect.update.PackageVisibility; 046import org.nuxeo.connect.update.ProductionState; 047import org.w3c.dom.Document; 048import org.w3c.dom.Element; 049import org.w3c.dom.NodeList; 050import org.xml.sax.SAXException; 051 052@XmlAccessorType(XmlAccessType.FIELD) 053@XmlRootElement(name = "package") 054@XmlType(propOrder = { "id", "state", "version", "name", "type", "visibility", "targetPlatforms", "vendor", 055 "supportsHotReload", "supported", "productionState", "validationState", "provides", "dependencies", 056 "conflicts", "title", "description", "homePage", "licenseType", "licenseUrl", "templates" }) 057public class PackageInfo { 058 059 public String name; 060 061 public String version; 062 063 public String id; 064 065 public PackageState state; 066 067 public String title; 068 069 public String description; 070 071 public String homePage; 072 073 public String licenseType; 074 075 public String licenseUrl; 076 077 public ProductionState productionState; 078 079 public NuxeoValidationState validationState; 080 081 public String[] targetPlatforms; 082 083 public PackageType type; 084 085 public String vendor; 086 087 public PackageVisibility visibility; 088 089 public PackageDependency[] provides; 090 091 public PackageDependency[] dependencies; 092 093 public PackageDependency[] conflicts; 094 095 public boolean supportsHotReload; 096 097 public boolean supported; 098 099 public Set<String> templates; 100 101 public PackageInfo() { 102 } 103 104 /** 105 * @deprecated since 5.7 106 */ 107 @Deprecated 108 public PackageInfo(String name, String version, String id, int state) { 109 this.name = name; 110 this.version = version; 111 this.id = id; 112 this.state = PackageState.getByValue(state); 113 } 114 115 /** 116 * @since 5.7 117 */ 118 public PackageInfo(Package pkg) { 119 name = pkg.getName(); 120 version = pkg.getVersion().toString(); 121 id = pkg.getId(); 122 state = pkg.getPackageState(); 123 title = pkg.getTitle(); 124 description = pkg.getDescription(); 125 homePage = pkg.getHomePage(); 126 licenseType = pkg.getLicenseType(); 127 licenseUrl = pkg.getLicenseUrl(); 128 productionState = pkg.getProductionState(); 129 validationState = pkg.getValidationState(); 130 targetPlatforms = pkg.getTargetPlatforms(); 131 type = pkg.getType(); 132 vendor = pkg.getVendor(); 133 visibility = pkg.getVisibility(); 134 if (visibility == null) { 135 visibility = PackageVisibility.UNKNOWN; 136 } 137 provides = pkg.getProvides(); 138 dependencies = pkg.getDependencies(); 139 conflicts = pkg.getConflicts(); 140 supportsHotReload = pkg.supportsHotReload(); 141 supported = pkg.isSupported(); 142 templates = templates(pkg); 143 } 144 145 /** 146 * @since 8.3 147 */ 148 private Set<String> templates(Package pkg) { 149 if (!(pkg instanceof LocalPackage)) { 150 return Collections.emptySet(); 151 } 152 Set<String> templatesFound = new HashSet<>(); 153 try { 154 File installFile = ((LocalPackage) pkg).getInstallFile(); 155 if (!installFile.exists()) { 156 return Collections.emptySet(); 157 } 158 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 159 DocumentBuilder db = dbf.newDocumentBuilder(); 160 Document dom = db.parse(installFile); 161 NodeList nodes = dom.getDocumentElement().getElementsByTagName("config"); 162 for (int i = 0; i < nodes.getLength(); i++) { 163 Element node = (Element) nodes.item(i); 164 if (!node.hasAttribute("addtemplate")) { 165 continue; 166 } 167 StringTokenizer tokenizer = new StringTokenizer(node.getAttribute("addtemplate"), ","); 168 while (tokenizer.hasMoreTokens()) { 169 templatesFound.add(tokenizer.nextToken()); 170 } 171 172 } 173 } catch (PackageException | ParserConfigurationException | SAXException | IOException e) { 174 LogFactory.getLog(PackageInfo.class).warn("Could not parse install file for " + pkg.getName(), e); 175 } 176 return templatesFound; 177 } 178}