66 */
77
88import java .io .File ;
9+ import java .io .IOException ;
910import java .nio .file .NotDirectoryException ;
1011import java .util .HashMap ;
1112import java .util .Map ;
1213import javax .xml .parsers .DocumentBuilder ;
1314import javax .xml .parsers .DocumentBuilderFactory ;
1415import org .apache .commons .collections .MapUtils ;
16+ import org .jenkinsci .remoting .RoleChecker ;
1517import org .w3c .dom .Document ;
1618import org .w3c .dom .Element ;
1719import org .w3c .dom .Node ;
1820import org .w3c .dom .NodeList ;
1921import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
22+ import hudson .FilePath ;
23+ import hudson .FilePath .FileCallable ;
24+ import hudson .remoting .VirtualChannel ;
2025
2126public class MatlabReleaseInfo {
22- private String matlabRoot ;
27+ private FilePath matlabRoot ;
2328 private static final String VERSION_INFO_FILE = "VersionInfo.xml" ;
2429 private static final String VERSION_INFO_ROOT_TAG = "MathWorks_version_info" ;
2530 private static final String RELEASE_TAG = "release" ;
@@ -36,6 +41,10 @@ public class MatlabReleaseInfo {
3641 private Map <String , String > versionInfoCache = new HashMap <String , String >();
3742
3843 public MatlabReleaseInfo (String matlabRoot ) {
44+ this .matlabRoot = new FilePath (new File (matlabRoot ));
45+ }
46+
47+ public MatlabReleaseInfo (FilePath matlabRoot ) {
3948 this .matlabRoot = matlabRoot ;
4049 }
4150
@@ -73,33 +82,11 @@ public boolean verLessThan(double version) throws MatlabVersionNotFoundException
7382 private Map <String , String > getVersionInfoFromFile () throws MatlabVersionNotFoundException {
7483 if (MapUtils .isEmpty (versionInfoCache )) {
7584 try {
76- File versionFile = new File (this .matlabRoot + File .separator + VERSION_INFO_FILE );
77- if (versionFile .isFile ()) {
78- DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance ();
79- DocumentBuilder dBuilder = dbFactory .newDocumentBuilder ();
80- Document doc = dBuilder .parse (versionFile );
81-
82- doc .getDocumentElement ().normalize ();
83- NodeList nList = doc .getElementsByTagName (VERSION_INFO_ROOT_TAG );
84-
85- for (int temp = 0 ; temp < nList .getLength (); temp ++) {
86- Node nNode = nList .item (temp );
87- if (nNode .getNodeType () == Node .ELEMENT_NODE ) {
88-
89- Element eElement = (Element ) nNode ;
90-
91- versionInfoCache .put (RELEASE_TAG , eElement .getElementsByTagName (RELEASE_TAG )
92- .item (0 ).getTextContent ());
93- versionInfoCache .put (VERSION_TAG , eElement .getElementsByTagName (VERSION_TAG )
94- .item (0 ).getTextContent ());
95- versionInfoCache .put (DESCRIPTION_TAG , eElement
96- .getElementsByTagName (DESCRIPTION_TAG ).item (0 ).getTextContent ());
97- versionInfoCache .put (DATE_TAG ,
98- eElement .getElementsByTagName (DATE_TAG ).item (0 ).getTextContent ());
99- }
100- }
85+ FilePath versionFile = new FilePath (this .matlabRoot , VERSION_INFO_FILE );
86+ if (versionFile .exists ()) {
87+ versionInfoCache .putAll (versionFile .act (new RemoteFileOperation ()));
10188 }
102- else if (!new File ( this .matlabRoot ) .exists ()){
89+ else if (!this .matlabRoot .exists ()){
10390 throw new NotDirectoryException ("Invalid matlabroot path" );
10491 }else {
10592 versionInfoCache .putAll (VERSION_OLDER_THAN_17A );
@@ -112,4 +99,56 @@ else if(!new File(this.matlabRoot).exists()){
11299 }
113100 return versionInfoCache ;
114101 }
102+
103+ /*
104+ * Static File Callable to perform File operations on specific remote nodes.
105+ */
106+
107+ private static final class RemoteFileOperation implements FileCallable <Map <String , String >> {
108+
109+ private static final long serialVersionUID = 1L ;
110+
111+ private Map <String , String > versionInfoCache = new HashMap <String , String >();
112+
113+ @ Override
114+ public void checkRoles (RoleChecker checker ) throws SecurityException {
115+
116+ }
117+
118+ @ SuppressFBWarnings (value = "REC_CATCH_EXCEPTION" ,
119+ justification = "Irrespective of exception type, intention is to handle it in same way. Also, there is no intention to propagate any runtime exception up in the hierarchy." )
120+ @ Override
121+ public Map <String , String > invoke (File versionFile , VirtualChannel channel )
122+ throws IOException , InterruptedException {
123+
124+ try {
125+
126+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance ();
127+ DocumentBuilder dBuilder = dbFactory .newDocumentBuilder ();
128+ Document doc = dBuilder .parse (versionFile );
129+ doc .getDocumentElement ().normalize ();
130+ NodeList nList = doc .getElementsByTagName (VERSION_INFO_ROOT_TAG );
131+
132+ for (int temp = 0 ; temp < nList .getLength (); temp ++) {
133+ Node nNode = nList .item (temp );
134+ if (nNode .getNodeType () == Node .ELEMENT_NODE ) {
135+
136+ Element eElement = (Element ) nNode ;
137+
138+ versionInfoCache .put (RELEASE_TAG , eElement .getElementsByTagName (RELEASE_TAG )
139+ .item (0 ).getTextContent ());
140+ versionInfoCache .put (VERSION_TAG , eElement .getElementsByTagName (VERSION_TAG )
141+ .item (0 ).getTextContent ());
142+ versionInfoCache .put (DESCRIPTION_TAG , eElement
143+ .getElementsByTagName (DESCRIPTION_TAG ).item (0 ).getTextContent ());
144+ versionInfoCache .put (DATE_TAG ,
145+ eElement .getElementsByTagName (DATE_TAG ).item (0 ).getTextContent ());
146+ }
147+ }
148+ } catch (Exception e ) {
149+ throw new IOException ("Error in reading MATLAB VersionInfo file" ,e );
150+ }
151+ return versionInfoCache ;
152+ }
153+ }
115154}
0 commit comments