| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | *******************************************************************************/ |
| 9 | |
| 10 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module; |
| 11 | |
| 12 | import java.io.File; |
| 13 | import java.io.FileInputStream; |
| 14 | import java.io.FileNotFoundException; |
| 15 | import java.io.FileOutputStream; |
| 16 | import java.io.IOException; |
| 17 | import java.net.URL; |
| 18 | import java.nio.channels.FileChannel; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import javax.xml.parsers.ParserConfigurationException; |
| 24 | |
| 25 | import org.eclipse.core.runtime.FileLocator; |
| 26 | import org.eclipse.core.runtime.IConfigurationElement; |
| 27 | import org.eclipse.core.runtime.IPath; |
| 28 | import org.eclipse.core.runtime.ISafeRunnable; |
| 29 | import org.eclipse.core.runtime.Platform; |
| 30 | import org.eclipse.core.runtime.SafeRunner; |
| 31 | import org.eclipse.jdt.annotation.NonNull; |
| 32 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator; |
| 33 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType; |
| 34 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
| 35 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper; |
| 36 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleSource; |
| 37 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager; |
| 38 | import org.osgi.framework.Bundle; |
| 39 | import org.w3c.dom.Document; |
| 40 | import org.w3c.dom.Element; |
| 41 | import org.w3c.dom.NodeList; |
| 42 | import org.xml.sax.SAXException; |
| 43 | |
| 44 | /** |
| 45 | * Analysis module source who creates helpers for the analysis modules described |
| 46 | * in the imported XML files |
| 47 | * |
| 48 | * @author Geneviève Bastien |
| 49 | * @since 2.0 |
| 50 | */ |
| 51 | public class XmlAnalysisModuleSource implements IAnalysisModuleSource { |
| 52 | |
| 53 | /** Extension point ID */ |
| 54 | private static final String TMF_XML_BUILTIN_ID = "org.eclipse.linuxtools.tmf.analysis.xml.core.files"; //$NON-NLS-1$ |
| 55 | private static final String XML_FILE_ELEMENT = "xmlfile"; //$NON-NLS-1$ |
| 56 | |
| 57 | private static final String XML_FILE_ATTRIB = "file"; //$NON-NLS-1$ |
| 58 | |
| 59 | /* |
| 60 | * Legacy (Linux Tools) XML directory. |
| 61 | * TODO Remove once we feel the transition phase is over. |
| 62 | */ |
| 63 | private static final IPath XML_DIRECTORY_LEGACY = |
| 64 | Activator.getDefault().getStateLocation().removeLastSegments(1) |
| 65 | .append("org.eclipse.linuxtools.tmf.analysis.xml.core") //$NON-NLS-1$ |
| 66 | .append("xml_files"); //$NON-NLS-1$ |
| 67 | |
| 68 | private static List<@NonNull IAnalysisModuleHelper> fModules = null; |
| 69 | |
| 70 | /** |
| 71 | * Constructor. It adds the new module listener to the analysis manager. |
| 72 | */ |
| 73 | public XmlAnalysisModuleSource() { |
| 74 | |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() { |
| 79 | List<@NonNull IAnalysisModuleHelper> modules = fModules; |
| 80 | if (modules == null) { |
| 81 | modules = new ArrayList<>(); |
| 82 | fModules = modules; |
| 83 | populateBuiltinModules(); |
| 84 | populateAnalysisModules(); |
| 85 | } |
| 86 | return modules; |
| 87 | } |
| 88 | |
| 89 | private static void processFile(File xmlFile) { |
| 90 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | try { |
| 95 | Document doc = XmlUtils.getDocumentFromFile(xmlFile); |
| 96 | |
| 97 | /* get State Providers modules */ |
| 98 | NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER); |
| 99 | for (int i = 0; i < stateproviderNodes.getLength(); i++) { |
| 100 | Element node = (Element) stateproviderNodes.item(i); |
| 101 | |
| 102 | IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM); |
| 103 | fModules.add(helper); |
| 104 | } |
| 105 | |
| 106 | /* get pattern modules */ |
| 107 | NodeList patternNodes = doc.getElementsByTagName(TmfXmlStrings.PATTERN); |
| 108 | for (int i = 0; i < patternNodes.getLength(); i++) { |
| 109 | Element node = (Element) patternNodes.item(i); |
| 110 | |
| 111 | IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.PATTERN); |
| 112 | fModules.add(helper); |
| 113 | } |
| 114 | } catch (ParserConfigurationException | SAXException | IOException e) { |
| 115 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private static void populateBuiltinModules() { |
| 120 | /* Get the XML files advertised through the extension point */ |
| 121 | IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_XML_BUILTIN_ID); |
| 122 | for (IConfigurationElement element : elements) { |
| 123 | if (element.getName().equals(XML_FILE_ELEMENT)) { |
| 124 | final String filename = element.getAttribute(XML_FILE_ATTRIB); |
| 125 | final String name = element.getContributor().getName(); |
| 126 | // Run this in a safe runner in case there is an exception |
| 127 | // (IOException, FileNotFoundException, NPE, etc). |
| 128 | // This makes sure other extensions are not prevented from |
| 129 | // working if one is faulty. |
| 130 | SafeRunner.run(new ISafeRunnable() { |
| 131 | |
| 132 | @Override |
| 133 | public void run() throws IOException { |
| 134 | if (name != null) { |
| 135 | Bundle bundle = Platform.getBundle(name); |
| 136 | if (bundle != null) { |
| 137 | URL xmlUrl = bundle.getResource(filename); |
| 138 | if (xmlUrl == null) { |
| 139 | throw new FileNotFoundException(filename); |
| 140 | } |
| 141 | URL locatedURL = FileLocator.toFileURL(xmlUrl); |
| 142 | processFile(new File(locatedURL.getFile())); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void handleException(Throwable exception) { |
| 149 | // Handled sufficiently in SafeRunner |
| 150 | } |
| 151 | }); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private static void populateAnalysisModules() { |
| 157 | IPath pathToFiles = XmlUtils.getXmlFilesPath(); |
| 158 | File folder = pathToFiles.toFile(); |
| 159 | if (!(folder.isDirectory() && folder.exists())) { |
| 160 | return; |
| 161 | } |
| 162 | /* |
| 163 | * Transfer files from Linux Tools directory. |
| 164 | */ |
| 165 | File oldFolder = XML_DIRECTORY_LEGACY.toFile(); |
| 166 | final File[] oldAnalysisFiles = oldFolder.listFiles(); |
| 167 | if (oldAnalysisFiles != null) { |
| 168 | for (File fromFile : oldAnalysisFiles) { |
| 169 | File toFile = pathToFiles.append(fromFile.getName()).toFile(); |
| 170 | if (!toFile.exists() && !fromFile.isDirectory()) { |
| 171 | try (FileInputStream fis = new FileInputStream(fromFile); |
| 172 | FileOutputStream fos = new FileOutputStream(toFile); |
| 173 | FileChannel source = fis.getChannel(); |
| 174 | FileChannel destination = fos.getChannel();) { |
| 175 | destination.transferFrom(source, 0, source.size()); |
| 176 | } catch (IOException e) { |
| 177 | String error = Messages.XmlUtils_ErrorCopyingFile; |
| 178 | Activator.logError(error, e); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | Map<String, File> files = XmlUtils.listFiles(); |
| 184 | for (File xmlFile : files.values()) { |
| 185 | processFile(xmlFile); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Notifies the main XML analysis module that the executable modules list |
| 191 | * may have changed and needs to be refreshed. |
| 192 | */ |
| 193 | public static void notifyModuleChange() { |
| 194 | fModules = null; |
| 195 | TmfAnalysisManager.refreshModules(); |
| 196 | } |
| 197 | |
| 198 | } |