1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
;
13 import java
.io
.FileInputStream
;
14 import java
.io
.FileNotFoundException
;
15 import java
.io
.FileOutputStream
;
16 import java
.io
.IOException
;
18 import java
.nio
.channels
.FileChannel
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
23 import javax
.xml
.parsers
.ParserConfigurationException
;
25 import org
.eclipse
.core
.runtime
.FileLocator
;
26 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
27 import org
.eclipse
.core
.runtime
.IPath
;
28 import org
.eclipse
.core
.runtime
.ISafeRunnable
;
29 import org
.eclipse
.core
.runtime
.Platform
;
30 import org
.eclipse
.core
.runtime
.SafeRunner
;
31 import org
.eclipse
.jdt
.annotation
.NonNull
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
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
;
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
;
38 import org
.osgi
.framework
.Bundle
;
39 import org
.w3c
.dom
.Document
;
40 import org
.w3c
.dom
.Element
;
41 import org
.w3c
.dom
.NodeList
;
42 import org
.xml
.sax
.SAXException
;
45 * Analysis module source who creates helpers for the analysis modules described
46 * in the imported XML files
48 * @author Geneviève Bastien
51 public class XmlAnalysisModuleSource
implements IAnalysisModuleSource
{
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$
57 private static final String XML_FILE_ATTRIB
= "file"; //$NON-NLS-1$
60 * Legacy (Linux Tools) XML directory.
61 * TODO Remove once we feel the transition phase is over.
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$
68 private static List
<@NonNull IAnalysisModuleHelper
> fModules
= null;
71 * Constructor. It adds the new module listener to the analysis manager.
73 public XmlAnalysisModuleSource() {
78 public synchronized Iterable
<IAnalysisModuleHelper
> getAnalysisModules() {
79 List
<@NonNull IAnalysisModuleHelper
> modules
= fModules
;
80 if (modules
== null) {
81 modules
= new ArrayList
<>();
83 populateBuiltinModules();
84 populateAnalysisModules();
89 private static void processFile(File xmlFile
) {
90 if (!XmlUtils
.xmlValidate(xmlFile
).isOK()) {
95 Document doc
= XmlUtils
.getDocumentFromFile(xmlFile
);
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
);
102 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.STATE_SYSTEM
);
103 fModules
.add(helper
);
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
);
111 IAnalysisModuleHelper helper
= new TmfAnalysisModuleHelperXml(xmlFile
, node
, XmlAnalysisModuleType
.PATTERN
);
112 fModules
.add(helper
);
114 } catch (ParserConfigurationException
| SAXException
| IOException e
) {
115 Activator
.logError("Error opening XML file", e
); //$NON-NLS-1$
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
)) {
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() {
133 public void run() throws IOException
{
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
);
141 URL locatedURL
= FileLocator
.toFileURL(xmlUrl
);
142 processFile(new File(locatedURL
.getFile()));
148 public void handleException(Throwable exception
) {
149 // Handled sufficiently in SafeRunner
156 private static void populateAnalysisModules() {
157 IPath pathToFiles
= XmlUtils
.getXmlFilesPath();
158 File folder
= pathToFiles
.toFile();
159 if (!(folder
.isDirectory() && folder
.exists())) {
163 * Transfer files from Linux Tools directory.
165 File oldFolder
= XML_DIRECTORY_LEGACY
.toFile();
166 final File
[] oldAnalysisFiles
= oldFolder
.listFiles();
167 if (oldAnalysisFiles
!= null) {
168 for (File fromFile
: oldAnalysisFiles
) {
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
);
183 Map
<String
, File
> files
= XmlUtils
.listFiles();
184 for (File xmlFile
: files
.values()) {
185 processFile(xmlFile
);
190 * Notifies the main XML analysis module that the executable modules list
191 * may have changed and needs to be refreshed.
193 public static void notifyModuleChange() {
195 TmfAnalysisManager
.refreshModules();