@Override public Location read(JsonReader in) throws IOException { return Locations.getLocationFromAbsolutePath(runtimeContext.getLocationFactory(), in.nextString()); } })
/** * Unpack a jar file in the given location to a directory. * * @param jarLocation Location containing the jar file * @param destinationFolder Directory to expand into * @return The {@code destinationFolder} * @throws IOException If failed to expand the jar */ public static File unJar(Location jarLocation, File destinationFolder) throws IOException { Preconditions.checkArgument(jarLocation != null); return unJar(Locations.newInputSupplier(jarLocation), destinationFolder); }
/** * Converts the given file into a local {@link Location}. */ public static Location toLocation(java.nio.file.Path path) { return toLocation(path.toFile()); }
@Override public void cancel() { Closeables.closeQuietly(writer); Locations.deleteQuietly(Locations.getParent(eventFile), true); }
/** * Creates a temp copy of the program jar. * * @return a new {@link Location} which contains the same content as the program jar */ private Location copyProgramJar(Location targetDir) throws IOException { Location programJarCopy = targetDir.append("program.jar"); ByteStreams.copy(Locations.newInputSupplier(programJarLocation), Locations.newOutputSupplier(programJarCopy)); LOG.debug("Copied Program Jar to {}, source: {}", programJarCopy, programJarLocation); return programJarCopy; }
private StreamPositionTransformFileReader(StreamFileOffset offset) throws IOException { this.reader = StreamDataFileReader.createWithOffset(Locations.newInputSupplier(offset.getEventLocation()), Locations.newInputSupplier(offset.getIndexLocation()), offset.getOffset()); this.offset = new StreamFileOffset(offset); this.partitionLocation = Locations.getParent(offset.getEventLocation()); LOG.trace("Stream reader created for {}", offset.getEventLocation()); }
return scriptURI; return Locations.linkOrCopy(locationFactory.create(scriptURI), pythonFile).toURI(); return Locations.linkOrCopy(Locations.toLocation(new File(scriptURI.getPath())), pythonFile).toURI();
public static void deleteQuietly(Location location) { deleteQuietly(location, false); }
/** * Copies the artifact jar to the given target file. * * @param artifactId artifact id of the artifact to be copied * @param artifactDetail detail information of the artifact to be copied * @param targetFile target file to copy to * @throws IOException if the copying failed */ protected void copyArtifact(ArtifactId artifactId, ArtifactDetail artifactDetail, File targetFile) throws IOException { Locations.linkOrCopy(artifactDetail.getDescriptor().getLocation(), targetFile); }
@Test public void testParentHDFS() { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://1.2.3.4:8020"); LocationFactory locationFactory = new FileContextLocationFactory(conf, "abc"); Location location = locationFactory.create("def"); Assert.assertEquals("def", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertEquals("abc", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertTrue(Locations.isRoot(location)); Assert.assertTrue(location.getName().isEmpty()); Assert.assertNull(Locations.getParent(location)); }
@Override public Location call() throws Exception { Location baseLocation = StreamUtils.createGenerationLocation(config.getLocation(), generation); Locations.mkdirsIfNotExists(baseLocation); return baseLocation; } });
/** * Gets the stream name given a stream's base directory. * @param streamBaseLocation the location of the stream's directory * @return name of the stream associated with the location */ public static String getStreamNameFromLocation(Location streamBaseLocation) { // streamBaseLocation = /.../<namespace>/streams/<streamName>, // or /customLocation/streams/<streamname> // as constructed by FileStreamAdmin#getStreamConfigLocation Location streamsDir = Locations.getParent(streamBaseLocation); Preconditions.checkNotNull(streamsDir, "Streams directory of stream base location %s was null.", streamBaseLocation); return streamBaseLocation.getName(); }
Locations.mkdirsIfNotExists(archiveDir); Files.copy(uploadedFile, Locations.newOutputSupplier(tmpLocation));
private void writeConfig(StreamConfig config) throws IOException { Location configLocation = config.getLocation().append(CONFIG_FILE_NAME); Location tmpConfigLocation = configLocation.getTempFile(null); CharStreams.write(GSON.toJson(config), CharStreams.newWriterSupplier( Locations.newOutputSupplier(tmpConfigLocation), Charsets.UTF_8)); try { // Windows does not allow renaming if the destination file exists so we must delete the configLocation if (OSDetector.isWindows()) { configLocation.delete(); } tmpConfigLocation.renameTo(getConfigLocation(config.getStreamId())); } finally { Locations.deleteQuietly(tmpConfigLocation); } }
private void cleanup() { try { Locations.processLocations(credentialsDir, false, new Processor<LocationStatus, Runnable>() {
/** * Creates a {@link Location} instance which represents the parent of the given location. * Note: CDAP-13765 this method can return an invalid location if the parent is the root directory depending * on the implementation of the input Location. Consider calling {@link #isRoot(Location)} before * calling this. * * @param location location to extra parent from. * @return an instance representing the parent location or {@code null} if there is no parent. */ @Nullable public static Location getParent(Location location) { // If it is root, return null if (isRoot(location)) { return null; } URI source = location.toURI(); URI resolvedParent = URI.create(source.toString() + "/..").normalize(); // TODO: (CDAP-13765) move this logic to a better place // NOTE: if there is a trailing slash at the end, rename(), getName() and other operations on file // does not work in MapR. so we remove the trailing slash (if any) at the end. // However, don't remove the trailing slash if this is a root directory, as a URI without a path is an invalid // location if (resolvedParent.toString().endsWith("/") && !"/".equals(resolvedParent.getPath())) { String parent = resolvedParent.toString(); resolvedParent = URI.create(parent.substring(0, parent.length() - 1)); } return location.getLocationFactory().create(resolvedParent); }
return scriptURI; return Locations.linkOrCopy(locationFactory.create(scriptURI), pythonFile).toURI(); return Locations.linkOrCopy(Locations.toLocation(new File(scriptURI.getPath())), pythonFile).toURI();
public static void deleteQuietly(Location location) { deleteQuietly(location, false); }
/** * Copies the artifact jar to the given target file. * * @param artifactId artifact id of the artifact to be copied * @param artifactDetail detail information of the artifact to be copied * @param targetFile target file to copy to * @throws IOException if the copying failed */ protected void copyArtifact(ArtifactId artifactId, ArtifactDetail artifactDetail, File targetFile) throws IOException { Locations.linkOrCopy(artifactDetail.getDescriptor().getLocation(), targetFile); }
@Test public void testRootParentLocal() throws IOException { LocationFactory locationFactory = new LocalLocationFactory(new File(File.separator)); Location location = locationFactory.create("abc"); location = location.append("def"); Assert.assertEquals("def", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertEquals("abc", location.getName()); location = Locations.getParent(location); Assert.assertNotNull(location); Assert.assertTrue(Locations.isRoot(location)); Assert.assertTrue(location.getName().isEmpty()); Assert.assertNull(Locations.getParent(location)); }