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