control: Support recordings snapshot for multiple sessions from menu
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / module / XmlAnalysisModuleSource.java
CommitLineData
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 10package org.eclipse.tracecompass.tmf.analysis.xml.core.module;
97ed0cf0
FW
11
12import java.io.File;
2aebf5f3 13import java.io.FileInputStream;
4884b393 14import java.io.FileNotFoundException;
2aebf5f3 15import java.io.FileOutputStream;
97ed0cf0 16import java.io.IOException;
0281ea1c 17import java.net.URL;
2aebf5f3 18import java.nio.channels.FileChannel;
97ed0cf0
FW
19import java.util.ArrayList;
20import java.util.List;
21
22import javax.xml.parsers.DocumentBuilder;
23import javax.xml.parsers.DocumentBuilderFactory;
24import javax.xml.parsers.ParserConfigurationException;
25
0281ea1c
GB
26import org.eclipse.core.runtime.FileLocator;
27import org.eclipse.core.runtime.IConfigurationElement;
97ed0cf0 28import org.eclipse.core.runtime.IPath;
4884b393 29import org.eclipse.core.runtime.ISafeRunnable;
0281ea1c 30import org.eclipse.core.runtime.Platform;
4884b393 31import org.eclipse.core.runtime.SafeRunner;
aa353506 32import org.eclipse.jdt.annotation.NonNull;
0dcf01f1
GB
33import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
34import org.eclipse.tracecompass.tmf.analysis.xml.core.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType;
2bdf0193 35import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
2bdf0193
AM
36import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper;
37import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleSource;
38import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
0281ea1c 39import org.osgi.framework.Bundle;
97ed0cf0
FW
40import org.w3c.dom.Document;
41import org.w3c.dom.Element;
42import org.w3c.dom.NodeList;
43import 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 */
52public 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
129 public void run() throws Exception {
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();
154 File fFolder = pathToFiles.toFile();
155 if (!(fFolder.isDirectory() && fFolder.exists())) {
156 return;
157 }
2aebf5f3
BH
158
159 /*
160 * Transfer files from Linux Tools directory.
161 */
162 File fOldFolder = XML_DIRECTORY_LEGACY.toFile();
163 if ((fOldFolder.isDirectory() && fOldFolder.exists())) {
164 for (File fromFile : fOldFolder.listFiles()) {
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 }
179
0281ea1c
GB
180 for (File xmlFile : fFolder.listFiles()) {
181 processFile(xmlFile);
182 }
183 }
184
97ed0cf0
FW
185 /**
186 * Notifies the main XML analysis module that the executable modules list
187 * may have changed and needs to be refreshed.
188 */
189 public static void notifyModuleChange() {
190 fModules = null;
191 TmfAnalysisManager.refreshModules();
192 }
193
194}
This page took 0.074533 seconds and 5 git commands to generate.