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