org.apache.brooklyn.util.os.Os.tmp()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(93)

本文整理了Java中org.apache.brooklyn.util.os.Os.tmp()方法的一些代码示例,展示了Os.tmp()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Os.tmp()方法的具体详情如下:
包路径:org.apache.brooklyn.util.os.Os
类名称:Os
方法名:tmp

Os.tmp介绍

[英]returns the best tmp dir to use; see TmpDirFinder for the logic (and the explanation why this is needed!)
[中]返回要使用的最佳tmp目录;请参阅TmpDirFinder了解逻辑(以及为什么需要它的解释!)

代码示例

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

public ShellAbstractTool(File localTempDir) {
  if (localTempDir == null) {
    localTempDir = new File(Os.tmp(), "tmpssh-"+Os.user());
    if (!localTempDir.exists()) localTempDir.mkdir();
    Os.deleteOnExitEmptyParentsUpTo(localTempDir, new File(Os.tmp()));
  }
  this.localTempDir = localTempDir;
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-utils-common

/** writes given contents to a temporary file which will be deleted on exit */
public static File writeToTempFile(InputStream is, String prefix, String suffix) {
  return writeToTempFile(is, new File(Os.tmp()), prefix, suffix);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-utils-common

public static File writePropertiesToTempFile(Properties props, String prefix, String suffix) {
  return writePropertiesToTempFile(props, new File(Os.tmp()), prefix, suffix);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-utils-common

public void testTmp() {
  log.info("tmp dir is: "+Os.tmp());
  Assert.assertNotNull(Os.tmp());
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-utils-common

/** creates a private temp file which will be deleted on exit;
 * either prefix or ext may be null; 
 * if ext is non-empty and not > 4 chars and not starting with a ., then a dot will be inserted;
 * if either name part is too long it will be shortened to prevent filesystem errors */
public static File newTempFile(String prefix, String ext) {
  String sanitizedPrefix = (Strings.isNonEmpty(prefix) ? Strings.makeValidFilename(prefix) + "-" : "");
  if (sanitizedPrefix.length()>101) sanitizedPrefix = sanitizedPrefix.substring(0, 100)+"--";
  String extWithPrecedingSeparator = (Strings.isNonEmpty(ext) ? ext.startsWith(".") || ext.length()>4 ? ext : "."+ext : "");
  if (extWithPrecedingSeparator.length()>13) sanitizedPrefix = sanitizedPrefix.substring(0, 12)+"--";
  try {
    File tempFile = File.createTempFile(sanitizedPrefix, extWithPrecedingSeparator, new File(tmp()));
    tempFile.deleteOnExit();
    return tempFile;
  } catch (IOException e) {
    throw Exceptions.propagate(e);
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-utils-common

/** creates a temp dir which will be deleted on exit */
public static File newTempDir(String prefix) {
  String sanitizedPrefix = (prefix==null ? "" : Strings.makeValidFilename(prefix) + "-");
  String tmpParent = tmp();
  
  //With lots of stale temp dirs it is possible to have 
  //name collisions so we need to retry until a unique 
  //name is found
  for (int i = 0; i < TEMP_DIR_ATTEMPTS; i++) {
    String baseName = sanitizedPrefix + Identifiers.makeRandomId(4);
    File tempDir = new File(tmpParent, baseName);
    if (!tempDir.exists()) {
      if (tempDir.mkdir()) {
        Os.deleteOnExitRecursively(tempDir);
        return tempDir;
      } else {
        log.warn("Attempt to create temp dir failed " + tempDir + ". Either an IO error (disk full, no rights) or someone else created the folder after the !exists() check.");
      }
    } else {
      log.debug("Attempt to create temp dir failed, already exists " + tempDir + ". With ID of length 4 it is not unusual (15% chance) to have duplicate names at the 2000 samples mark.");
    }
  }
  throw new IllegalStateException("cannot create temporary folders in parent " + tmpParent + " after " + TEMP_DIR_ATTEMPTS + " attempts.");
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups = "Integration")
public void testInstallClasspathCopyTo() throws Exception {
  File dest = new File(Os.tmp(), "sshMachineLocationTest_dir/");
  dest.mkdir();
  try {
    int result = host.installTo("classpath://brooklyn/config/sample.properties", Urls.mergePaths(dest.getAbsolutePath(), "sample.properties"));
    assertEquals(result, 0);
    String contents = ArchiveUtils.readFullyString(new File(dest, "sample.properties"));
    assertTrue(contents.contains("Property 1"), "contents missing expected phrase; contains:\n"+contents);
  } finally {
    dest.delete();
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups = "Integration")
public void testInstallUrlTo() throws Exception {
  File dest = new File(Os.tmp(), "sshMachineLocationTest_dir/");
  dest.mkdir();
  try {
    int result = host.installTo("https://raw.github.com/brooklyncentral/brooklyn/master/README.md", Urls.mergePaths(dest.getAbsolutePath(), "README.md"));
    assertEquals(result, 0);
    String contents = ArchiveUtils.readFullyString(new File(dest, "README.md"));
    assertTrue(contents.contains("http://brooklyncentral.github.com"), "contents missing expected phrase; contains:\n"+contents);
  } finally {
    dest.delete();
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups = {"Integration"})
public void testOutputAsExpected() throws Exception {
  final String CONTENTS = "hello world\n"
    + "bye bye\n";
  execCommands("cat > "+Os.mergePaths(Os.tmp(), "test1")+" << X\n"
    + CONTENTS
    + "X\n");
  String read = execCommands("echo START_FOO", "cat "+Os.mergePaths(Os.tmp(), "test1"), "echo END_FOO");
  log.debug("read back data written, as:\n"+read);
  String contents = Strings.getFragmentBetween(read, "START_FOO", "END_FOO");
  Assert.assertEquals(CONTENTS.trim(), contents.trim());
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups = "Integration")
public void testCopyStreamTo() throws Exception {
  String contents = "abc";
  File dest = new File(Os.tmp(), "sshMachineLocationTest_dest.tmp");
  try {
    host.copyTo(Streams.newInputStreamWithContents(contents), dest.getAbsolutePath());
    assertEquals("abc", Files.readFirstLine(dest, Charsets.UTF_8));
  } finally {
    dest.delete();
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

public static File getOsgiCacheDir(ManagementContext mgmt) {
  StringConfigMap brooklynProperties = mgmt.getConfig();
  String cacheDir = brooklynProperties.getConfig(BrooklynServerConfig.OSGI_CACHE_DIR);
  
  // note dir should be different for each instance if starting multiple instances
  // hence default including management node ID
  
  cacheDir = TemplateProcessor.processTemplateContents(cacheDir, mgmt, 
    MutableMap.of(BrooklynServerConfig.MGMT_BASE_DIR.getName(), getMgmtBaseDir(mgmt),
      BrooklynServerConfig.MANAGEMENT_NODE_ID_PROPERTY, mgmt.getManagementNodeId(),
      Os.TmpDirFinder.BROOKLYN_OS_TMPDIR_PROPERTY, Os.tmp()));
  cacheDir = resolveAgainstBaseDir(mgmt.getConfig(), cacheDir);
  
  return new File(cacheDir);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups="Integration") // integration only because non-reusable OSGi takes ~200ms
public void testOsgiPathCustom() {
  BrooklynProperties bp = BrooklynProperties.Factory.newEmpty();
  String randomSeg = "osgi-test-"+Identifiers.makeRandomId(4);
  bp.put(BrooklynServerConfig.OSGI_CACHE_DIR, "${brooklyn.os.tmpdir}"+"/"+randomSeg+"/"+"${brooklyn.mgmt.node.id}");
  mgmt = LocalManagementContextForTests.builder(true).enableOsgiNonReusable().useProperties(bp).build();
  String path = BrooklynServerPaths.getOsgiCacheDir(mgmt).getAbsolutePath();
  Os.deleteOnExitRecursivelyAndEmptyParentsUpTo(new File(path), new File(Os.tmp()+"/"+randomSeg));
  
  Assert.assertTrue(path.startsWith(Os.tmp()), path);
  Assert.assertTrue(path.contains(mgmt.getManagementNodeId()), path);
  
  assertExistsThenIsCleaned(path);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups="Integration") // integration only because non-reusable OSGi takes ~200ms
public void testOsgiPathCustomWithoutNodeIdNotCleaned() {
  BrooklynProperties bp = BrooklynProperties.Factory.newEmpty();
  String randomSeg = "osgi-test-"+Identifiers.makeRandomId(4);
  bp.put(BrooklynServerConfig.OSGI_CACHE_DIR, "${brooklyn.os.tmpdir}"+"/"+randomSeg+"/"+"sample");
  mgmt = LocalManagementContextForTests.builder(true).enableOsgiNonReusable().useProperties(bp).build();
  String path = BrooklynServerPaths.getOsgiCacheDir(mgmt).getAbsolutePath();
  Os.deleteOnExitRecursivelyAndEmptyParentsUpTo(new File(path), new File(Os.tmp()+"/"+randomSeg));
  
  Assert.assertTrue(path.startsWith(Os.tmp()), path);
  Assert.assertFalse(path.contains(mgmt.getManagementNodeId()), path);
  
  assertExistsThenCorrectCleanedBehaviour(path, false);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-utils-common

@Test
public void testDeleteRecursivelyNonExistantDir() throws Exception {
  DeletionResult result = Os.deleteRecursively(Os.mergePaths(Os.tmp(), Identifiers.makeRandomId(8)));
  assertTrue(result.wasSuccessful());
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups = {"Integration"})
public void testUsesCustomLocalTempDir() throws Exception {
  class SshjToolForTest extends SshjTool {
    public SshjToolForTest(Map<String, ?> map) {
      super(map);
    }
    public File getLocalTempDir() {
      return localTempDir;
    }
  };
  
  final SshjToolForTest localtool = new SshjToolForTest(ImmutableMap.<String, Object>of("host", "localhost"));
  assertNotNull(localtool.getLocalTempDir());
  assertEquals(localtool.getLocalTempDir(), new File(Os.tidyPath(SshjTool.PROP_LOCAL_TEMP_DIR.getDefaultValue())));
  
  String customTempDir = Os.tmp();
  final SshjToolForTest localtool2 = new SshjToolForTest(ImmutableMap.of(
      "host", "localhost", 
      SshjTool.PROP_LOCAL_TEMP_DIR.getName(), customTempDir));
  assertEquals(localtool2.getLocalTempDir(), new File(customTempDir));
  
  String customRelativeTempDir = "~/tmp";
  final SshjToolForTest localtool3 = new SshjToolForTest(ImmutableMap.of(
      "host", "localhost", 
      SshjTool.PROP_LOCAL_TEMP_DIR.getName(), customRelativeTempDir));
  assertEquals(localtool3.getLocalTempDir(), new File(Os.tidyPath(customRelativeTempDir)));
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

public void testResolvesLocalTempDir() throws Exception {
  String localTempDir = Os.mergePaths(Os.tmp(), "testResolvesUsernameAtHost");
  brooklynProperties.put("brooklyn.location.byon.localTempDir", localTempDir);
  FixedListMachineProvisioningLocation<MachineLocation> byon = resolve("byon(hosts=\"1.1.1.1\",osFamily=\"windows\")");
  MachineLocation machine = byon.obtain();
  assertEquals(machine.getConfig(SshMachineLocation.LOCAL_TEMP_DIR), localTempDir);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-software-base

@Test(groups = "Integration")
public void testStartsInDifferentRunAndInstallSpecifiedDirectories() throws Exception {
  String dir1 = Os.mergePathsUnix(Os.tmp(), "/brooklyn-test-"+Strings.makeRandomId(4));
  String dir2 = Os.mergePathsUnix(Os.tmp(), "/brooklyn-test-"+Strings.makeRandomId(4));
  app.config().set(BrooklynConfigKeys.INSTALL_DIR, dir1);
  app.config().set(BrooklynConfigKeys.RUN_DIR, dir2);
  doTestSpecifiedDirectory(dir1, dir2);
  Os.deleteRecursively(dir1);
  Os.deleteRecursively(dir2);
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-software-base

@Test(groups = "Integration")
public void testStartsInMgmtSpecifiedDirectory() throws Exception {
  String dir = Os.mergePathsUnix(Os.tmp(), "/brooklyn-test-"+Strings.makeRandomId(4));
  tearDown();
  mgmt = new LocalManagementContextForTests();
  mgmt.getBrooklynProperties().put(BrooklynConfigKeys.ONBOX_BASE_DIR, dir);
  setUp();
  doTestSpecifiedDirectory(dir, dir);
  Os.deleteRecursively(dir);
}

相关文章