abstractfilestore.java
来自「world wind java sdk 源码」· Java 代码 · 共 818 行 · 第 1/2 页
JAVA
818 行
/*Copyright (C) 2001, 2006 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.cache;import gov.nasa.worldwind.avlist.*;import gov.nasa.worldwind.data.*;import gov.nasa.worldwind.util.*;import java.io.File;import java.util.logging.Level;/** * @author tag * @version $Id: AbstractFileStore.java 9774 2009-03-30 01:13:51Z tgaskins $ */public class AbstractFileStore implements FileStore{ protected static class StoreLocation extends AVListImpl { protected boolean markWhenUsed = false; public StoreLocation(java.io.File file, boolean isInstall) { this.setValue(AVKey.FILE_STORE_LOCATION, file); this.setValue(AVKey.INSTALLED, isInstall); } public StoreLocation(java.io.File file) { this(file, false); } public java.io.File getFile() { Object o = this.getValue(AVKey.FILE_STORE_LOCATION); return (o != null && o instanceof java.io.File) ? (java.io.File) o : null; } public void setFile(java.io.File file) { this.setValue(AVKey.FILE_STORE_LOCATION, file); } public boolean isInstall() { Object o = this.getValue(AVKey.INSTALLED); return (o != null && o instanceof Boolean) ? (Boolean) o : false; } public void setInstall(boolean isInstall) { this.setValue(AVKey.INSTALLED, isInstall); } public boolean isMarkWhenUsed() { return markWhenUsed; } public void setMarkWhenUsed(boolean markWhenUsed) { this.markWhenUsed = markWhenUsed; } } protected final java.util.List<StoreLocation> readLocations = new java.util.ArrayList<StoreLocation>(); protected StoreLocation writeLocation = null; private final Object fileLock = new Object(); //**************************************************************// //******************** File Store Configuration **************// //**************************************************************// protected void initialize(java.io.InputStream xmlConfigStream) { javax.xml.parsers.DocumentBuilderFactory docBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); try { javax.xml.parsers.DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); org.w3c.dom.Document doc = docBuilder.parse(xmlConfigStream); // The order of the following two calls is important, because building the writable location may entail // creating a location that's included in the specified read locations. this.buildWritePaths(doc); this.buildReadPaths(doc); if (this.writeLocation == null) { Logging.logger().warning("FileStore.NoWriteLocation"); } if (this.readLocations.size() == 0) { // This should not happen because the writable location is added to the read list, but check nonetheless String message = Logging.getMessage("FileStore.NoReadLocations"); Logging.logger().severe(message); throw new IllegalStateException(message); } } catch (javax.xml.parsers.ParserConfigurationException e) { String message = Logging.getMessage("FileStore.ExceptionReadingConfigurationFile"); Logging.logger().severe(message); throw new IllegalStateException(message, e); } catch (org.xml.sax.SAXException e) { String message = Logging.getMessage("FileStore.ExceptionReadingConfigurationFile"); Logging.logger().severe(message); throw new IllegalStateException(message, e); } catch (java.io.IOException e) { String message = Logging.getMessage("FileStore.ExceptionReadingConfigurationFile"); Logging.logger().severe(message); throw new IllegalStateException(message, e); } } protected void buildReadPaths(org.w3c.dom.Node dataFileStoreNode) { javax.xml.xpath.XPathFactory pathFactory = javax.xml.xpath.XPathFactory.newInstance(); javax.xml.xpath.XPath pathFinder = pathFactory.newXPath(); try { org.w3c.dom.NodeList locationNodes = (org.w3c.dom.NodeList) pathFinder.evaluate( "/dataFileStore/readLocations/location", dataFileStoreNode.getFirstChild(), javax.xml.xpath.XPathConstants.NODESET); for (int i = 0; i < locationNodes.getLength(); i++) { org.w3c.dom.Node location = locationNodes.item(i); String prop = pathFinder.evaluate("@property", location); String wwDir = pathFinder.evaluate("@wwDir", location); String append = pathFinder.evaluate("@append", location); String isInstall = pathFinder.evaluate("@isInstall", location); String isMarkWhenUsed = pathFinder.evaluate("@isMarkWhenUsed", location); String path = buildLocationPath(prop, append, wwDir); if (path == null) { Logging.logger().log(Level.WARNING, "FileStore.LocationInvalid", prop != null ? prop : Logging.getMessage("generic.Unknown")); continue; } StoreLocation oldStore = this.storeLocationFor(path); if (oldStore != null) // filter out duplicates continue; // Even paths that don't exist or are otherwise problematic are added to the list because they may // become readable during the session. E.g., removable media. So add them to the search list. java.io.File pathFile = new java.io.File(path); if (pathFile.exists() && !pathFile.isDirectory()) { Logging.logger().log(Level.WARNING, "FileStore.LocationIsFile", pathFile.getPath()); } boolean pathIsInstall = isInstall != null && (isInstall.contains("t") || isInstall.contains("T")); StoreLocation newStore = new StoreLocation(pathFile, pathIsInstall); // If the input parameter "markWhenUsed" is null or empty, then the StoreLocation should keep its // default value. Otherwise the store location value is set to true when the input parameter contains // "t", and is set to false otherwise. if (isMarkWhenUsed != null && isMarkWhenUsed.length() > 0) newStore.setMarkWhenUsed(isMarkWhenUsed.toLowerCase().contains("t")); this.readLocations.add(newStore); } } catch (javax.xml.xpath.XPathExpressionException e) { String message = Logging.getMessage("FileStore.ExceptionReadingConfigurationFile"); Logging.logger().severe(message); throw new IllegalStateException(message, e); } } @SuppressWarnings({"ResultOfMethodCallIgnored"}) protected void buildWritePaths(org.w3c.dom.Node dataFileCacheNode) { javax.xml.xpath.XPathFactory pathFactory = javax.xml.xpath.XPathFactory.newInstance(); javax.xml.xpath.XPath pathFinder = pathFactory.newXPath(); try { org.w3c.dom.NodeList locationNodes = (org.w3c.dom.NodeList) pathFinder.evaluate( "/dataFileStore/writeLocations/location", dataFileCacheNode.getFirstChild(), javax.xml.xpath.XPathConstants.NODESET); for (int i = 0; i < locationNodes.getLength(); i++) { org.w3c.dom.Node location = locationNodes.item(i); String prop = pathFinder.evaluate("@property", location); String wwDir = pathFinder.evaluate("@wwDir", location); String append = pathFinder.evaluate("@append", location); String create = pathFinder.evaluate("@create", location); String path = buildLocationPath(prop, append, wwDir); if (path == null) { Logging.logger().log(Level.WARNING, "FileStore.LocationInvalid", prop != null ? prop : Logging.getMessage("generic.Unknown")); continue; } Logging.logger().log(Level.FINER, "FileStore.AttemptingWriteDir", path); java.io.File pathFile = new java.io.File(path); if (!pathFile.exists() && create != null && (create.contains("t") || create.contains("T"))) { Logging.logger().log(Level.FINER, "FileStore.MakingDirsFor", path); pathFile.mkdirs(); } if (pathFile.isDirectory() && pathFile.canWrite() && pathFile.canRead()) { Logging.logger().log(Level.FINER, "FileStore.WriteLocationSuccessful", path); this.writeLocation = new StoreLocation(pathFile); // Remove the writable location from search path if it already exists. StoreLocation oldLocation = this.storeLocationFor(path); if (oldLocation != null) this.readLocations.remove(oldLocation); // Writable location is always first in search path. this.readLocations.add(0, this.writeLocation); break; // only need one } } } catch (javax.xml.xpath.XPathExpressionException e) { String message = Logging.getMessage("FileStore.ExceptionReadingConfigurationFile"); Logging.logger().severe(message); throw new IllegalStateException(message, e); } } protected static String buildLocationPath(String property, String append, String wwDir) { String path = propertyToPath(property); if (append != null && append.length() != 0) path = appendPathPart(path, append.trim()); if (wwDir != null && wwDir.length() != 0) path = appendPathPart(path, wwDir.trim()); return path; } protected static String appendPathPart(String firstPart, String secondPart) { if (secondPart == null || secondPart.length() == 0) return firstPart; if (firstPart == null || secondPart.length() == 0) return secondPart; firstPart = WWIO.stripTrailingSeparator(firstPart); secondPart = WWIO.stripLeadingSeparator(secondPart); return firstPart + System.getProperty("file.separator") + secondPart; } protected static String propertyToPath(String propName) { if (propName == null || propName.length() == 0) return null; String prop = System.getProperty(propName); if (prop != null) return prop; if (propName.equalsIgnoreCase("gov.nasa.worldwind.platform.alluser.store")) return determineAllUserLocation(); if (propName.equalsIgnoreCase("gov.nasa.worldwind.platform.user.store")) return determineSingleUserLocation(); return null; } protected static String determineAllUserLocation() { if (gov.nasa.worldwind.Configuration.isMacOS()) { return "/Library/Caches"; } else if (gov.nasa.worldwind.Configuration.isWindowsOS()) { String path = System.getenv("ALLUSERSPROFILE"); if (path == null) { Logging.logger().severe("generic.AllUsersWindowsProfileNotKnown"); return null; } return path + "\\Application Data"; } else if (gov.nasa.worldwind.Configuration.isLinuxOS() || gov.nasa.worldwind.Configuration.isUnixOS() || gov.nasa.worldwind.Configuration.isSolarisOS()) { return "/var/cache/"; } else { Logging.logger().warning("generic.UnknownOperatingSystem"); return null; } } protected static String determineSingleUserLocation() { String home = getUserHomeDir(); if (home == null) { Logging.logger().warning("generic.UsersHomeDirectoryNotKnown"); return null; } String path = null; if (gov.nasa.worldwind.Configuration.isMacOS()) { path = "/Library/Caches"; } else if (gov.nasa.worldwind.Configuration.isWindowsOS()) { // This will produce an incorrect path with duplicate parts, // like "C:\Users\PatC:\Users\Pat\Application Data". //path = System.getenv("USERPROFILE"); //if (path == null) //{ // Logging.logger().fine("generic.UsersWindowsProfileNotKnown"); // return null; //} //path += "\\Application Data"; path = "\\Application Data"; } else if (gov.nasa.worldwind.Configuration.isLinuxOS() || gov.nasa.worldwind.Configuration.isUnixOS() || gov.nasa.worldwind.Configuration.isSolarisOS()) { path = "/var/cache/"; } else { Logging.logger().fine("generic.UnknownOperatingSystem"); } if (path == null) return null; return home + path; } protected static String getUserHomeDir() { return System.getProperty("user.home"); } //**************************************************************// //******************** File Store Locations ******************// //**************************************************************// public java.util.List<? extends java.io.File> getLocations() { java.util.ArrayList<java.io.File> locations = new java.util.ArrayList<java.io.File>(); for (StoreLocation location : this.readLocations) { locations.add(location.getFile()); } return locations; } public java.io.File getWriteLocation() { return (this.writeLocation != null) ? this.writeLocation.getFile() : null; } public void addLocation(String newPath, boolean isInstall) { this.addLocation(this.readLocations.size(), newPath, isInstall); } public void addLocation(int index, String newPath, boolean isInstall) { if (newPath == null || newPath.length() == 0) { String message = Logging.getMessage("nullValue.FileStorePathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (index < 0) { String message = Logging.getMessage("generic.InvalidIndex", index); Logging.logger().fine(message); throw new IllegalArgumentException(message); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?