255671adbc48141f1df47f44bf1b330937322874
[scilab.git] / scilab / modules / jvm / src / java / org / scilab / modules / jvm / LoadClassPath.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - DIGITEO - Clement DAVID
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.jvm;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
23 import javax.xml.xpath.XPath;
24 import javax.xml.xpath.XPathConstants;
25 import javax.xml.xpath.XPathExpressionException;
26 import javax.xml.xpath.XPathFactory;
27
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31 import org.xml.sax.SAXException;
32
33 import org.scilab.modules.commons.xml.ScilabDocumentBuilderFactory;
34
35 /**
36  * Utility class to ease the jar loading management.
37  */
38 public final class LoadClassPath {
39         /** Scilab environment variable */
40         private static final String SCI = System.getenv("SCI");
41         /** The classpath.xml file path */
42         private static final String CLASSPATH_PATH = SCI + "/etc/classpath.xml";
43         /** The expression to get the jars */
44         private static final String XPATH_EXPRS = "//classpaths/path[@load='onUse']/load";
45         
46         /** Cache the already loaded libraries */
47         private static Set<String> loadedModules = new HashSet<String>(); 
48         
49         /**
50          * Load the module on the classpath
51          * 
52          * The module have to be declared in the $SCI/etc/classpath.xml file.
53          * 
54          * @param module the module to be loaded
55          */
56         public static void loadOnUse(String module) {
57                 if (loadedModules.contains(module)) {
58                         return;
59                 }
60                 
61                 // define XPath expression
62                 String xpathExpression = XPATH_EXPRS + "[@on='" + module + "']";
63                 
64                 // Initialize xpath
65                 XPathFactory factory = XPathFactory.newInstance();
66                 XPath xpath = factory.newXPath();
67                 
68                 // initialize document factory
69                 DocumentBuilderFactory domFactory = ScilabDocumentBuilderFactory.newInstance();
70                 domFactory.setValidating(false);
71             domFactory.setNamespaceAware(true);
72            
73             // Parse Classpath file
74             Document doc = null;
75             try {
76                         DocumentBuilder builder = domFactory.newDocumentBuilder();
77                         doc = builder.parse(CLASSPATH_PATH);
78             } catch (ParserConfigurationException e) {
79                         e.printStackTrace();
80                         return;
81                 } catch (SAXException e) {
82                         e.printStackTrace();
83                         return;
84                 } catch (IOException e) {
85                         e.printStackTrace();
86                         return;
87                 }
88             
89                 // Load JARs
90                 try {
91                         NodeList result = (NodeList) xpath.evaluate(xpathExpression, doc,
92                                                 XPathConstants.NODESET);
93                         
94                         for (int i = 0; i < result.getLength(); i++) {
95                                 Node n = result.item(i).getParentNode();
96                                 
97                                 String jar = n.getAttributes().getNamedItem("value").getNodeValue();
98                                 ClassPath.addFile(jar.replace("$SCILAB", SCI), 0);
99                                 loadedModules.add(module);
100                         }
101                         
102                 } catch (XPathExpressionException e) {
103                         e.printStackTrace();
104                 } catch (FileNotFoundException e) {
105                         e.printStackTrace();
106                 } catch (IOException e) {
107                         e.printStackTrace();
108                 }
109         }
110         
111         /**
112          * This class is a static singleton
113          */
114         private LoadClassPath() { }
115 }