org.flexdock.util.OsInfo Maven / Gradle / Ivy
The newest version!
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.flexdock.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* @author Christopher Butler
*/
public class OsInfo {
private static final OsInfo SINGLETON = new OsInfo();
private static final String XML_RESOURCE = "org/flexdock/util/os-info.xml";
private String osArch;
private List osNameList;
private List osLibraryList;
private List prefixOsLibraryList;
public static OsInfo getInstance() {
return SINGLETON;
}
public OsInfo() {
this(null);
}
public OsInfo(Properties systemProps) {
if(systemProps==null) {
systemProps = System.getProperties();
}
Document doc = ResourceManager.getDocument(XML_RESOURCE);
osArch = getSystemArch(doc, systemProps);
osNameList = Collections.unmodifiableList(getOSChain(doc, systemProps));
ArrayList libList = new ArrayList(osNameList.size());
ArrayList prefixLibList = new ArrayList(osNameList.size());
for(Iterator it=osNameList.iterator(); it.hasNext();) {
String osName = (String)it.next();
libList.add(osName + "-" + osArch);
}
libList.addAll(osNameList);
for(Iterator it=libList.iterator(); it.hasNext();) {
String prefixed = "-" + it.next();
prefixLibList.add(prefixed);
}
osLibraryList = Collections.unmodifiableList(libList);
prefixOsLibraryList = Collections.unmodifiableList(prefixLibList);
}
private String getSystemArch(Document doc, Properties systemProps) {
String archTag = "arch";
String sysArch = format(systemProps.getProperty("os.arch"));
Element archElem = findElementByName(doc, archTag, sysArch);
while(isNested(archElem, archTag)) {
archElem = (Element)archElem.getParentNode();
}
return archElem==null? sysArch: archElem.getAttribute("name");
}
private List getOSChain(Document doc, Properties systemProps) {
String osTag = "os";
String osName = format(systemProps.getProperty("os.name"));
Element osElem = findElementByName(doc, osTag, osName);
ArrayList list = new ArrayList();
while(osElem!=null && osTag.equals(osElem.getTagName())) {
list.add(osElem.getAttribute("name"));
osElem = (Element)osElem.getParentNode();
}
if(list.isEmpty()) {
list.add(osName);
}
return list;
}
private Element findElementByName(Document doc, String tagName, String nameAttrib) {
NodeList list = doc.getElementsByTagName(tagName);
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy