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