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 | 22 | |
97ed0cf0 FW |
23 | import javax.xml.parsers.ParserConfigurationException; |
24 | ||
0281ea1c GB |
25 | import org.eclipse.core.runtime.FileLocator; |
26 | import org.eclipse.core.runtime.IConfigurationElement; | |
97ed0cf0 | 27 | import org.eclipse.core.runtime.IPath; |
4884b393 | 28 | import org.eclipse.core.runtime.ISafeRunnable; |
0281ea1c | 29 | import org.eclipse.core.runtime.Platform; |
4884b393 | 30 | import org.eclipse.core.runtime.SafeRunner; |
aa353506 | 31 | import org.eclipse.jdt.annotation.NonNull; |
0dcf01f1 | 32 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator; |
6eca054d GB |
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; | |
2bdf0193 AM |
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; | |
0281ea1c | 38 | import org.osgi.framework.Bundle; |
97ed0cf0 FW |
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 | |
0dcf01f1 | 49 | * @since 2.0 |
97ed0cf0 FW |
50 | */ |
51 | public class XmlAnalysisModuleSource implements IAnalysisModuleSource { | |
52 | ||
0281ea1c GB |
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 | ||
2aebf5f3 BH |
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 | ||
aa353506 | 68 | private static List<@NonNull IAnalysisModuleHelper> fModules = null; |
97ed0cf0 | 69 | |
048bde85 GB |
70 | /** |
71 | * Constructor. It adds the new module listener to the analysis manager. | |
72 | */ | |
73 | public XmlAnalysisModuleSource() { | |
03b8cd0e | 74 | |
048bde85 GB |
75 | } |
76 | ||
97ed0cf0 FW |
77 | @Override |
78 | public synchronized Iterable<IAnalysisModuleHelper> getAnalysisModules() { | |
aa353506 AM |
79 | List<@NonNull IAnalysisModuleHelper> modules = fModules; |
80 | if (modules == null) { | |
81 | modules = new ArrayList<>(); | |
82 | fModules = modules; | |
0281ea1c | 83 | populateBuiltinModules(); |
97ed0cf0 FW |
84 | populateAnalysisModules(); |
85 | } | |
aa353506 | 86 | return modules; |
97ed0cf0 FW |
87 | } |
88 | ||
0281ea1c GB |
89 | private static void processFile(File xmlFile) { |
90 | if (!XmlUtils.xmlValidate(xmlFile).isOK()) { | |
97ed0cf0 FW |
91 | return; |
92 | } | |
97ed0cf0 | 93 | |
0281ea1c | 94 | try { |
f10786aa | 95 | Document doc = XmlUtils.getDocumentFromFile(xmlFile); |
0281ea1c GB |
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); | |
97ed0cf0 | 101 | |
0281ea1c GB |
102 | IAnalysisModuleHelper helper = new TmfAnalysisModuleHelperXml(xmlFile, node, XmlAnalysisModuleType.STATE_SYSTEM); |
103 | fModules.add(helper); | |
104 | } | |
38e2a2e9 JCK |
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 | } | |
0281ea1c GB |
114 | } catch (ParserConfigurationException | SAXException | IOException e) { |
115 | Activator.logError("Error opening XML file", e); //$NON-NLS-1$ | |
116 | } | |
117 | } | |
97ed0cf0 | 118 | |
0281ea1c GB |
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)) { | |
4884b393 MAL |
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 | |
4dfd44fe | 133 | public void run() throws IOException { |
4884b393 MAL |
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 | } | |
0281ea1c GB |
144 | } |
145 | } | |
4884b393 MAL |
146 | |
147 | @Override | |
148 | public void handleException(Throwable exception) { | |
149 | // Handled sufficiently in SafeRunner | |
150 | } | |
151 | }); | |
97ed0cf0 FW |
152 | } |
153 | } | |
154 | } | |
155 | ||
0281ea1c GB |
156 | private static void populateAnalysisModules() { |
157 | IPath pathToFiles = XmlUtils.getXmlFilesPath(); | |
bc21b431 MK |
158 | File folder = pathToFiles.toFile(); |
159 | if (!(folder.isDirectory() && folder.exists())) { | |
0281ea1c GB |
160 | return; |
161 | } | |
2aebf5f3 BH |
162 | /* |
163 | * Transfer files from Linux Tools directory. | |
164 | */ | |
bc21b431 MK |
165 | File oldFolder = XML_DIRECTORY_LEGACY.toFile(); |
166 | final File[] oldAnalysisFiles = oldFolder.listFiles(); | |
167 | if (oldAnalysisFiles != null) { | |
168 | for (File fromFile : oldAnalysisFiles) { | |
2aebf5f3 BH |
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 | } | |
537572cd BH |
183 | Map<String, File> files = XmlUtils.listFiles(); |
184 | for (File xmlFile : files.values()) { | |
185 | processFile(xmlFile); | |
0281ea1c GB |
186 | } |
187 | } | |
188 | ||
97ed0cf0 FW |
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 | } |