/** * Initializes configuration if required. * Returns non-zero if something goes wrong. */ private int initConfigIfRequired(CommandScope requiredCommandScope, File localDir) throws ConfigException { switch (requiredCommandScope) { case INITIALIZED_LOCALDIR: if (!ConfigHelper.configExists(localDir)) { return showErrorAndExit("No repository found in path, or configured plugin not installed. Use 'sy init' to create one."); } config = ConfigHelper.loadConfig(localDir); if (config == null) { return showErrorAndExit("Invalid config in " + localDir); } break; case UNINITIALIZED_LOCALDIR: if (ConfigHelper.configExists(localDir)) { return showErrorAndExit("Repository found in path. Command can only be used outside a repository."); } break; case ANY: default: break; } return 0; }
private void initLocalDir(OptionSet options, OptionSpec<File> optionLocalDir) throws ConfigException, Exception { // Find config or use --localdir option if (options.has(optionLocalDir)) { localDir = options.valueOf(optionLocalDir); } else { File currentDir = new File(".").getAbsoluteFile(); localDir = ConfigHelper.findLocalDirInPath(currentDir); // If no local directory was found, choose current directory if (localDir == null) { localDir = currentDir; } } }
logger.log(Level.INFO, "Loading config from {0} ...", localDir); ConfigTO configTO = ConfigHelper.loadConfigTO(localDir); RepoTO repoTO = ConfigHelper.loadRepoTO(localDir, configTO);
/** * Loads the repository transfer object from the local directory. */ public static RepoTO loadRepoTO(File localDir, ConfigTO configTO) throws ConfigException { File appDir = new File(localDir, Config.DIR_APPLICATION); File repoFile = new File(appDir, Config.FILE_REPO); if (!repoFile.exists()) { throw new ConfigException("Cannot find repository file at "+repoFile+". Try connecting to a repository using 'connect', or 'init' to create a new one."); } try { if (CipherUtil.isEncrypted(repoFile)) { return loadEncryptedRepoTO(repoFile, configTO); } else { return loadPlaintextRepoTO(repoFile, configTO); } } catch (Exception e) { throw new ConfigException("Cannot load repo file: "+e.getMessage(), e); } }
private void startWatchOperations(Map<File, FolderTO> newWatchedFolderTOs) throws ConfigException, ServiceAlreadyStartedException { for (Map.Entry<File, FolderTO> folderEntry : newWatchedFolderTOs.entrySet()) { File localDir = folderEntry.getKey(); try { Config watchConfig = ConfigHelper.loadConfig(localDir); if (watchConfig != null) { logger.log(Level.INFO, "- Starting watch operation at " + localDir + " ..."); WatchOperationOptions watchOptions = folderEntry.getValue().getWatchOptions(); if (watchOptions == null) { watchOptions = new WatchOperationOptions(); } WatchRunner watchRunner = new WatchRunner(watchConfig, watchOptions, daemonConfig.getPortTO()); watchRunner.start(); watchOperations.put(localDir, watchRunner); } else { logger.log(Level.INFO, "- CANNOT start watch, because no config found at " + localDir + " ..."); } } catch (Exception e) { logger.log(Level.SEVERE, " + Cannot start watch operation at " + localDir + ". IGNORING.", e); } } }
@Override public GenlinkOperationResult execute() throws Exception { logger.log(Level.INFO, ""); logger.log(Level.INFO, "Running 'GenLink'"); logger.log(Level.INFO, "--------------------------------------------"); if (configTO == null) { configTO = ConfigHelper.loadConfigTO(config.getLocalDir()); } ApplicationLink applicationLink = new ApplicationLink(configTO.getTransferSettings(), options.isShortUrl()); if (configTO.getMasterKey() != null) { String encryptedLinkStr = applicationLink.createEncryptedLink(configTO.getMasterKey()); return new GenlinkOperationResult(encryptedLinkStr, true); } else { String plaintextLinkStr = applicationLink.createPlaintextLink(); return new GenlinkOperationResult(plaintextLinkStr, false); } } }
@Test public void testConfigHelperLoadConfig() throws Exception { // Setup Config testConfig = TestConfigUtil.createTestLocalConfig(); // Run Config loadedConfig = ConfigHelper.loadConfig(testConfig.getLocalDir()); // Test assertNotNull(loadedConfig); assertEquals(testConfig.getAppDir(), loadedConfig.getAppDir()); assertEquals(testConfig.getCacheDir(), loadedConfig.getCacheDir()); assertNotNull(loadedConfig.getChunker()); assertEquals(testConfig.getChunker().toString(), loadedConfig.getChunker().toString()); assertNotNull(loadedConfig.getCache()); assertNotNull(loadedConfig.getConnection()); assertEquals(testConfig.getDatabaseDir(), loadedConfig.getDatabaseDir()); assertEquals(testConfig.getDatabaseFile(), loadedConfig.getDatabaseFile()); assertEquals(testConfig.getDisplayName(), loadedConfig.getDisplayName()); assertEquals(testConfig.getLocalDir(), loadedConfig.getLocalDir()); assertEquals(testConfig.getLogDir(), loadedConfig.getLogDir()); assertEquals(testConfig.getMachineName(), loadedConfig.getMachineName()); assertEquals(testConfig.getMasterKey(), loadedConfig.getMasterKey()); assertNotNull(loadedConfig.getMultiChunker()); assertNotNull(loadedConfig.getRepoId()); assertNotNull(loadedConfig.getTransformer()); // Tear down TestConfigUtil.deleteTestLocalConfigAndData(testConfig); }
@Test public void testConfigHelperLoadConfigTO() throws Exception { // Setup Config testConfig = TestConfigUtil.createTestLocalConfig(); // Run ConfigTO loadedConfigTO = ConfigHelper.loadConfigTO(testConfig.getLocalDir()); // Test assertNotNull(loadedConfigTO); assertEquals(testConfig.getDisplayName(), loadedConfigTO.getDisplayName()); assertEquals(testConfig.getMachineName(), loadedConfigTO.getMachineName()); assertEquals(testConfig.getMasterKey(), loadedConfigTO.getMasterKey()); // Tear down TestConfigUtil.deleteTestLocalConfigAndData(testConfig); }
ConfigTO loadedConfigTO = ConfigHelper.loadConfigTO(testConfig.getLocalDir()); RepoTO repoConfigTO = ConfigHelper.loadRepoTO(testConfig.getLocalDir(), loadedConfigTO);
@Test public void testConfigHelperFindLocalDirInPathNotExistent() throws IOException { File startingPath = EnvironmentUtil.isWindows() ? new File("C:\\does\\not\\exist") : new File("/does/not/exist"); // Run File actualLocalDir = ConfigHelper.findLocalDirInPath(startingPath); // Test assertNull(actualLocalDir); }
@Test public void testConfigHelperFindLocalDirInPathTwoLevelsDown() throws Exception { // Setup Config testConfig = TestConfigUtil.createTestLocalConfig(); new File(testConfig.getAppDir()+"/some/folder").mkdirs(); File startingPath = new File(testConfig.getAppDir()+"/some/folder"); // Run File actualLocalDir = ConfigHelper.findLocalDirInPath(startingPath); // Test assertNotNull(actualLocalDir); assertEquals(testConfig.getLocalDir(), actualLocalDir.getCanonicalFile()); // Tear down TestConfigUtil.deleteTestLocalConfigAndData(testConfig); }
@Test public void testConfigHelperFindLocalDirInPath() throws Exception { // Setup Config testConfig = TestConfigUtil.createTestLocalConfig(); File startingPath = testConfig.getLocalDir(); // Run File actualLocalDir = ConfigHelper.findLocalDirInPath(startingPath); // Test assertNotNull(actualLocalDir); assertEquals(testConfig.getLocalDir(), actualLocalDir.getCanonicalFile()); // Tear down TestConfigUtil.deleteTestLocalConfigAndData(testConfig); }