下面列出了java.nio.file.Path#isAbsolute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private SecretModel getBallerinaConfSecret(String configFilePath, String serviceName) throws
KubernetesPluginException {
//create a new config map model with ballerina conf
SecretModel secretModel = new SecretModel();
secretModel.setName(getValidName(serviceName) + "-ballerina-conf" + SECRET_POSTFIX);
secretModel.setMountPath(BALLERINA_CONF_MOUNT_PATH);
Path dataFilePath = Paths.get(configFilePath);
if (!dataFilePath.isAbsolute()) {
dataFilePath = KubernetesContext.getInstance().getDataHolder().getSourceRoot().resolve(dataFilePath)
.normalize();
}
String content = Base64.encodeBase64String(KubernetesUtils.readFileContent(dataFilePath));
Map<String, String> dataMap = new HashMap<>();
dataMap.put(BALLERINA_CONF_FILE_NAME, content);
secretModel.setData(dataMap);
secretModel.setBallerinaConf(configFilePath);
secretModel.setReadOnly(false);
return secretModel;
}
@Override
public void visitPath(Path path) throws IOException {
if (path.isAbsolute()) {
stream.writeBoolean(true);
Path cellPath = rootCellPath;
Optional<String> cellName = Optional.empty();
for (Map.Entry<AbsPath, Optional<String>> candidate : cellMap.entrySet()) {
if (path.startsWith(candidate.getKey().getPath())) {
cellPath = candidate.getKey().getPath();
cellName = candidate.getValue();
}
}
ValueTypeInfoFactory.forTypeToken(new TypeToken<Optional<String>>() {})
.visit(cellName, this);
writeRelativePath(cellPath.relativize(path));
} else {
stream.writeBoolean(false);
writeRelativePath(path);
}
}
public static Path subtractPathOrNull(Path parent, Path sub)
{
if (!parent.endsWith(sub)) {
throw new IllegalArgumentException(String.format(
"%s is not a parent path of %s", parent, sub));
}
if (parent.getNameCount() == sub.getNameCount()) {
return parent.getRoot(); // NOTE: return null if parent has no root
}
Path res = parent.subpath(0,
parent.getNameCount() - sub.getNameCount());
if (parent.isAbsolute()) {
return parent.getRoot().resolve(res);
} else {
return res;
}
}
/** Checks that the given file can be deleted, throwing an exception if it can't. */
private void checkDeletable(File file, DeleteMode mode, Path path) throws IOException {
if (file.isRootDirectory()) {
throw new FileSystemException(path.toString(), null, "can't delete root directory");
}
if (file.isDirectory()) {
if (mode == DeleteMode.NON_DIRECTORY_ONLY) {
throw new FileSystemException(path.toString(), null, "can't delete: is a directory");
}
checkEmpty(((Directory) file), path);
} else if (mode == DeleteMode.DIRECTORY_ONLY) {
throw new FileSystemException(path.toString(), null, "can't delete: is not a directory");
}
if (file == workingDirectory && !path.isAbsolute()) {
// this is weird, but on Unix at least, the file system seems to be happy to delete the
// working directory if you give the absolute path to it but fail if you use a relative path
// that resolves to the working directory (e.g. "" or ".")
throw new FileSystemException(path.toString(), null, "invalid argument");
}
}
/**
* Adds the Path to the hasher as unencoded chars. Roughly equivalent to {@code
* hasher.putUnencodedChars(path.toString())}.
*/
public static Hasher hashPathFast(Hasher hasher, Path path) {
if (!(path instanceof BuckUnixPath)) {
return hasher.putUnencodedChars(path.toString());
}
if (path.isAbsolute()) {
hasher.putChar('/');
}
for (int i = 0; i < path.getNameCount(); i++) {
if (i != 0) {
hasher.putChar('/');
}
hasher.putUnencodedChars(FastPaths.getNameString(path, i));
}
return hasher;
}
/**
* Determines if a directory contains a valid Apache FlexJS SDK.
*/
public static boolean isValidSDK(Path absolutePath)
{
if(absolutePath == null || !absolutePath.isAbsolute())
{
return false;
}
File file = absolutePath.toFile();
if(!file.isDirectory())
{
return false;
}
Path sdkDescriptionPath = absolutePath.resolve(FLEX_SDK_DESCRIPTION);
file = sdkDescriptionPath.toFile();
if(!file.exists() || file.isDirectory())
{
return false;
}
Path compilerPath = absolutePath.resolve(JS).resolve(BIN).resolve(ASJSC);
file = compilerPath.toFile();
if(!file.exists() || file.isDirectory())
{
return false;
}
return true;
}
Path outputDirectory(JigConfig config) {
Project project = getProject();
Path path = Paths.get(config.getOutputDirectory());
if (path.isAbsolute()) return path;
return project.getBuildDir().toPath().resolve("jig");
}
@Override
public void deleteFile(Path path) throws IOException {
checkClosed();
if (path.isAbsolute()) {
Files.delete(path);
}
final MCRStoredNode storedNode = resolve(path);
final MCRPath mcrPath = getCurrentSecurePath(storedNode);
storedNode.delete();
MCRPathEventHelper.fireFileDeleteEvent(mcrPath);
}
private String getEscapedRelativeWebpackPath(Path path) {
if (path.isAbsolute()) {
return FrontendUtils.getUnixRelativePath(webpackConfigPath, path);
} else {
return FrontendUtils.getUnixPath(path);
}
}
/**
* Filter files under the project root, and convert to canonical relative path style. For example,
* the project root is /project, 1. file path /project/./src/com/facebook/./test/../Test.java will
* be converted to src/com/facebook/Test.java 2. file path
* /otherproject/src/com/facebook/Test.java will be ignored.
*/
static ReferencedFiles getCanonicalFilesUnderProjectRoot(
AbsPath projectRoot, Iterable<String> nonCanonicalFilePaths) throws IOException {
// toRealPath() is used throughout to resolve symlinks or else the Path.startsWith() check will
// not be reliable.
ImmutableSet.Builder<RelPath> projectFiles = ImmutableSet.builder();
ImmutableSet.Builder<AbsPath> nonProjectFiles = ImmutableSet.builder();
AbsPath normalizedRoot = projectRoot.toRealPath();
for (String filePath : nonCanonicalFilePaths) {
Path canonicalFullPath = Paths.get(filePath);
if (!canonicalFullPath.isAbsolute()) {
canonicalFullPath = projectRoot.resolve(canonicalFullPath).getPath();
}
if (!canonicalFullPath.toFile().exists()) {
nonProjectFiles.add(AbsPath.of(canonicalFullPath));
continue;
}
canonicalFullPath = canonicalFullPath.toRealPath();
// Ignore files that aren't under project root.
if (canonicalFullPath.startsWith(normalizedRoot.getPath())) {
Path relativePath =
canonicalFullPath.subpath(
normalizedRoot.getPath().getNameCount(), canonicalFullPath.getNameCount());
projectFiles.add(RelPath.of(relativePath));
} else {
nonProjectFiles.add(AbsPath.of(canonicalFullPath));
}
}
return new ReferencedFiles(projectFiles.build(), nonProjectFiles.build());
}
@Override
public Path getDataDirectory() {
if (configuration!=null && configuration.getArchivaRuntimeConfiguration()!=null && StringUtils.isNotEmpty(configuration.getArchivaRuntimeConfiguration().getDataDirectory())) {
Path dataDir = Paths.get(configuration.getArchivaRuntimeConfiguration().getDataDirectory());
if (dataDir.isAbsolute()) {
return dataDir;
} else {
return getAppServerBaseDir().resolve(dataDir);
}
} else {
return getAppServerBaseDir().resolve("data");
}
}
private Path makeAbsolute(Path path) {
if (path.isAbsolute()) {
return path;
} else {
return workdir.resolve(path);
}
}
@Override
public Iterable<Path> visitPath(Path value) {
if (value.isAbsolute()) {
return ImmutableList.of(value);
}
return ImmutableList.of();
}
/** @return a valid {@link Location} from given path.
* @throws CommonException if a valid Location could not be created.
* Given errorMessagePrefix will be used as a prefix in {@link CommonException}'s message. */
public static Location createValidLocation(Path path, String errorMessagePrefix) throws CommonException {
if(!path.isAbsolute()) {
throw new CommonException(errorMessagePrefix + "Path `"+ path.toString()+"` is not absolute.");
}
return new Location(path);
}
private void onChangedSymbolicLink(BlockingQueue<Update> queue, Path path) throws IOException {
Path symlink = Files.readSymbolicLink(path);
String targetPath;
if (symlink.isAbsolute()) {
targetPath = path.getParent().toAbsolutePath().relativize(symlink).toString();
} else {
// the symlink is already relative, so we can leave it alone, e.g. foo.txt
targetPath = symlink.toString();
}
String relativePath = toRelativePath(path);
log.trace("Symlink {}, relative={}, target={}", path, relativePath, targetPath);
put(queue, Update.newBuilder().setPath(relativePath).setSymlink(targetPath).setModTime(lastModified(path)).setLocal(true).build());
}
public static String findOutputDirectory(String mainFile, String outputValue, boolean isSWF)
{
if(outputValue == null)
{
if(mainFile == null)
{
return System.getProperty("user.dir");
}
Path mainFilePath = Paths.get(mainFile);
if(!mainFilePath.isAbsolute())
{
mainFilePath = Paths.get(System.getProperty("user.dir"), mainFile);
}
Path mainFileParentPath = mainFilePath.getParent();
if(mainFileParentPath == null)
{
return System.getProperty("user.dir");
}
if(!isSWF)
{
//Royale treats these directory structures as a special case
String mainFileParentPathAsString = mainFileParentPath.toString();
if(mainFileParentPathAsString.endsWith("/src") ||
mainFileParentPathAsString.endsWith("\\src"))
{
mainFileParentPath = mainFileParentPath.resolve("../");
}
else if(mainFileParentPathAsString.endsWith("/src/main/flex") ||
mainFileParentPathAsString.endsWith("\\src\\main\\flex") ||
mainFileParentPathAsString.endsWith("/src/main/royale") ||
mainFileParentPathAsString.endsWith("\\src\\main\\royale"))
{
mainFileParentPath = mainFileParentPath.resolve("../../../");
}
try
{
return mainFileParentPath.toFile().getCanonicalPath();
}
catch(IOException e)
{
return null;
}
}
return mainFileParentPath.toString();
}
Path outputPath = Paths.get(outputValue);
if(!outputPath.isAbsolute())
{
outputPath = Paths.get(System.getProperty("user.dir"), outputValue);
}
if(!isSWF)
{
return outputPath.toString();
}
Path outputValueParentPath = outputPath.getParent();
return outputValueParentPath.toString();
}
public AbsPathImpl(Path path) {
super(path);
if (!path.isAbsolute()) {
throw new IllegalArgumentException("path must be absolute: " + path);
}
}
BasicFileAttributeViewImpl(Path path) {
this.path = MCRPath.toMCRPath(path);
if (!path.isAbsolute()) {
throw new InvalidPathException(path.toString(), "'path' must be absolute.");
}
}
private Path resolveNodePath(String pathString) {
Path path = context.fileSystem().getPath(pathString);
return path.isAbsolute() ? path : context.pathInNodeUnderVespaHome(path);
}
private String compactName(Path name) {
Path relative = FileUtils.relativize(context.getConfiguration().getBasePath(), name);
return relative.isAbsolute() ? relative.getFileName().toString() : relative.toString();
}