001/* 002 * (C) Copyright 2006-2010 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 * Bogdan Stefanescu 018 * Thierry Delprat 019 */ 020package org.nuxeo.apidoc.introspection; 021 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.IOException; 025import java.net.URL; 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031import java.util.zip.ZipEntry; 032import java.util.zip.ZipFile; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036import org.nuxeo.apidoc.api.BaseNuxeoArtifact; 037import org.nuxeo.apidoc.api.ComponentInfo; 038import org.nuxeo.apidoc.api.ExtensionInfo; 039import org.nuxeo.apidoc.api.ExtensionPointInfo; 040import org.nuxeo.apidoc.api.ServiceInfo; 041import org.nuxeo.apidoc.documentation.DocumentationHelper; 042import org.nuxeo.common.utils.FileUtils; 043import org.nuxeo.common.utils.Path; 044 045public class ComponentInfoImpl extends BaseNuxeoArtifact implements ComponentInfo { 046 047 protected final BundleInfoImpl bundle; 048 049 protected final String name; 050 051 protected final Map<String, ExtensionPointInfo> extensionPoints; 052 053 protected final Collection<ExtensionInfo> extensions; 054 055 protected final List<String> serviceNames = new ArrayList<String>(); 056 057 protected final List<ServiceInfo> services = new ArrayList<ServiceInfo>(); 058 059 protected URL xmlFileUrl; 060 061 protected String componentClass; 062 063 protected String documentation; // TODO 064 065 protected static final Log log = LogFactory.getLog(ComponentInfoImpl.class); 066 067 public ComponentInfoImpl(BundleInfoImpl bundleInfo, String name) { 068 this.bundle = bundleInfo; 069 this.name = name; 070 extensionPoints = new HashMap<String, ExtensionPointInfo>(); 071 extensions = new ArrayList<ExtensionInfo>(); 072 } 073 074 @Override 075 public String getName() { 076 return name; 077 } 078 079 @Override 080 public BundleInfoImpl getBundle() { 081 return bundle; 082 } 083 084 @Override 085 public Collection<ExtensionPointInfo> getExtensionPoints() { 086 return extensionPoints.values(); 087 } 088 089 @Override 090 public Collection<ExtensionInfo> getExtensions() { 091 return extensions; 092 } 093 094 public void addExtensionPoint(ExtensionPointInfoImpl xp) { 095 extensionPoints.put(xp.getId(), xp); 096 } 097 098 @Override 099 public ExtensionPointInfo getExtensionPoint(String name) { 100 return extensionPoints.get(name); 101 } 102 103 public void addExtension(ExtensionInfoImpl xt) { 104 extensions.add(xt); 105 } 106 107 @Override 108 public String getDocumentation() { 109 return documentation; 110 } 111 112 @Override 113 public String getDocumentationHtml() { 114 return DocumentationHelper.getHtml(getDocumentation()); 115 } 116 117 public void setDocumentation(String documentation) { 118 this.documentation = documentation; 119 } 120 121 public void addService(String serviceName) { 122 serviceNames.add(serviceName); 123 ServiceInfo si = new ServiceInfoImpl(serviceName, this); 124 services.add(si); 125 } 126 127 @Override 128 public List<String> getServiceNames() { 129 return serviceNames; 130 } 131 132 @Override 133 public String getComponentClass() { 134 return componentClass; 135 } 136 137 public void setComponentClass(String componentClass) { 138 this.componentClass = componentClass; 139 } 140 141 @Override 142 public boolean isXmlPureComponent() { 143 return componentClass == null; 144 } 145 146 @Override 147 public URL getXmlFileUrl() { 148 return xmlFileUrl; 149 } 150 151 public void setXmlFileUrl(URL xmlFileUrl) { 152 this.xmlFileUrl = xmlFileUrl; 153 } 154 155 @Override 156 public String getXmlFileName() { 157 if (xmlFileUrl == null) { 158 return ""; 159 } 160 String path = xmlFileUrl.getPath(); 161 String[] parts = path.split("!"); 162 if (parts.length == 2) { 163 return parts[1]; 164 } else { 165 return path; 166 } 167 } 168 169 @Override 170 public String getXmlFileContent() { 171 if (xmlFileUrl == null) { 172 return ""; 173 } 174 String path = xmlFileUrl.getPath(); 175 String[] parts = path.split("!"); 176 177 File jar = new File(parts[0].replace("file:", "")); 178 if (!jar.exists()) { 179 return "Unable to locate Bundle :" + parts[0]; 180 } 181 182 try { 183 String xml; 184 if (jar.getAbsolutePath().endsWith(".xml")) { 185 xml = FileUtils.read(new FileInputStream(jar)); 186 } else if (jar.isDirectory()) { 187 File file = new File(new Path(jar.getAbsolutePath()).append(parts[1]).toString()); 188 if (!file.exists()) { 189 return "Unable to locate file :" + file.getAbsolutePath(); 190 } 191 xml = FileUtils.readFile(file); 192 } else { 193 ZipFile jarArchive = new ZipFile(jar); 194 ZipEntry entry = jarArchive.getEntry(parts[1].substring(1)); 195 xml = FileUtils.read(jarArchive.getInputStream(entry)); 196 } 197 return DocumentationHelper.secureXML(xml); 198 } catch (IOException e) { 199 log.error("Error while getting XML file", e); 200 return ""; 201 } 202 } 203 204 @Override 205 public String getId() { 206 return name; 207 } 208 209 @Override 210 public String getVersion() { 211 return bundle.getVersion(); 212 } 213 214 @Override 215 public String getArtifactType() { 216 return TYPE_NAME; 217 } 218 219 @Override 220 public List<ServiceInfo> getServices() { 221 return services; 222 } 223 224 @Override 225 public String getHierarchyPath() { 226 return getBundle().getHierarchyPath() + "/" + getId(); 227 } 228 229}