private static void wrapIOException( IOException e ) throws CommandFailed { throw new CommandFailed( format( "unable to dump database: %s: %s", e.getClass().getSimpleName(), e.getMessage() ), e ); }
public static void wrapIOException( IOException e ) throws CommandFailed { throw new CommandFailed( format( "unable to load database: %s: %s", e.getClass().getSimpleName(), e.getMessage() ), e ); }
private static long parsePid( Args args ) throws CommandFailed { if ( args.has( PID_KEY ) ) { try { return Long.parseLong( args.get( PID_KEY, "" ) ); } catch ( NumberFormatException e ) { throw new CommandFailed( "Unable to parse --" + PID_KEY, e ); } } return NO_PID; }
throw new CommandFailed( "archive does not exist: " + archive, e ); throw new CommandFailed( "database already exists: " + database, e ); throw new CommandFailed( "you do not have permission to load a database -- is Neo4j running as a " + "different user?", e ); throw new CommandFailed( "Not a valid Neo4j archive: " + archive, incorrectFormat );
@Override public void execute( String[] args ) throws IncorrectUsage, CommandFailed { try { setPassword( arguments.parse( args ).get( 0 ) ); } catch ( IncorrectUsage | CommandFailed e ) { throw e; } catch ( Throwable throwable ) { throw new CommandFailed( throwable.getMessage(), new RuntimeException( throwable ) ); } }
@Override public void execute( String[] args ) throws IncorrectUsage, CommandFailed { try { setDefaultAdmin( arguments.parse( args ).get( 0 ) ); } catch ( IncorrectUsage | CommandFailed e ) { throw e; } catch ( Throwable throwable ) { throw new CommandFailed( throwable.getMessage(), new RuntimeException( throwable ) ); } }
private Config getConfig( File configFile ) throws CommandFailed { if ( !fs.fileExists( configFile ) ) { throw new CommandFailed( "Unable to find config file, tried: " + configFile.getAbsolutePath() ); } try { return Config.fromFile( configFile ).withHome( homeDir ).withConnectorsDisabled().build(); } catch ( Exception e ) { throw new CommandFailed( "Failed to read config file: " + configFile.getAbsolutePath(), e ); } }
Importer getImporterForMode( String mode, Args parsedArgs, Config config, OutsideWorld outsideWorld ) throws IncorrectUsage, CommandFailed { Importer importer; switch ( mode ) { case "database": importer = new DatabaseImporter( parsedArgs, config, outsideWorld ); break; case "csv": importer = new CsvImporter( parsedArgs, config, outsideWorld ); break; default: throw new CommandFailed( "Invalid mode specified." ); // This won't happen because mode is mandatory. } return importer; } }
private void dump( String database, DatabaseLayout databaseLayout, Path transactionalLogsDirectory, Path archive ) throws CommandFailed { Path databasePath = databaseLayout.databaseDirectory().toPath(); try { File storeLockFile = databaseLayout.getStoreLayout().storeLockFile(); dumper.dump( databasePath, transactionalLogsDirectory, archive, path -> Objects.equals( path.getFileName().toString(), storeLockFile.getName() ) ); } catch ( FileAlreadyExistsException e ) { throw new CommandFailed( "archive already exists: " + e.getMessage(), e ); } catch ( NoSuchFileException e ) { if ( Paths.get( e.getMessage() ).toAbsolutePath().equals( databasePath ) ) { throw new CommandFailed( "database does not exist: " + database, e ); } wrapIOException( e ); } catch ( IOException e ) { wrapIOException( e ); } }
private Config getConfig( File configFile, String databaseName ) throws CommandFailed { if ( !outsideWorld.fileSystem().fileExists( configFile ) ) { throw new CommandFailed( "Unable to find config file, tried: " + configFile.getAbsolutePath() ); } try { return Config.fromFile( configFile ).withHome( homeDir ).withSetting( active_database, databaseName ).withConnectorsDisabled().build(); } catch ( Exception e ) { throw new CommandFailed( "Failed to read config file: " + configFile.getAbsolutePath(), e ); } }
private static void checkDbState( DatabaseLayout databaseLayout, Config additionalConfiguration ) throws CommandFailed { try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction(); JobScheduler jobScheduler = createInitialisedScheduler(); PageCache pageCache = ConfigurableStandalonePageCacheFactory.createPageCache( fileSystem, additionalConfiguration, jobScheduler ) ) { RecoveryRequiredChecker.assertRecoveryIsNotRequired( fileSystem, pageCache, additionalConfiguration, databaseLayout, new Monitors() ); } catch ( RecoveryRequiredException rre ) { throw new CommandFailed( rre.getMessage() ); } catch ( Exception e ) { throw new CommandFailed( "Failure when checking for recovery state: '%s'." + e.getMessage(), e ); } }
private static void checkDbState( DatabaseLayout databaseLayout, Config additionalConfiguration ) throws CommandFailed { try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction(); JobScheduler jobScheduler = createInitialisedScheduler(); PageCache pageCache = ConfigurableStandalonePageCacheFactory .createPageCache( fileSystem, additionalConfiguration, jobScheduler ) ) { assertRecoveryIsNotRequired( fileSystem, pageCache, additionalConfiguration, databaseLayout, new Monitors() ); } catch ( RecoveryRequiredException rre ) { throw new CommandFailed( rre.getMessage() ); } catch ( Exception e ) { throw new CommandFailed( "Failure when checking for recovery state: '%s'." + e.getMessage(), e ); } }
@Override public void execute( String[] args ) throws IncorrectUsage, CommandFailed { final Path databaseDirectory = arguments.parse( args ).getMandatoryPath( "store" ); Validators.CONTAINS_EXISTING_DATABASE.validate( databaseDirectory.toFile() ); try ( DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction(); JobScheduler jobScheduler = createInitialisedScheduler(); PageCache pageCache = StandalonePageCacheFactory.createPageCache( fileSystem, jobScheduler ) ) { DatabaseLayout databaseLayout = DatabaseLayout.of( databaseDirectory.toFile() ); final String storeVersion = new StoreVersionCheck( pageCache ) .getVersion( databaseLayout.metadataStore() ) .orElseThrow( () -> new CommandFailed( String.format( "Could not find version metadata in store '%s'", databaseDirectory ) ) ); final String fmt = "%-30s%s"; out.accept( String.format( fmt, "Store format version:", storeVersion ) ); RecordFormats format = RecordFormatSelector.selectForVersion( storeVersion ); out.accept( String.format( fmt, "Store format introduced in:", format.introductionVersion() ) ); findSuccessor( format ) .map( next -> String.format( fmt, "Store format superseded in:", next.introductionVersion() ) ) .ifPresent( out ); } catch ( Exception e ) { throw new CommandFailed( e.getMessage(), e ); } }
public static void checkLock( StoreLayout storeLayout ) throws CommandFailed { try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction(); StoreLocker storeLocker = new GlobalStoreLocker( fileSystem, storeLayout ) ) { storeLocker.checkLock(); } catch ( StoreLockException e ) { throw new CommandFailed( "the database is in use -- stop Neo4j and try again", e ); } catch ( IOException e ) { wrapIOException( e ); } }
@Override public void execute( String[] stringArgs ) throws IncorrectUsage, CommandFailed { Args args = Args.withFlags( "list", "to", "verbose", "force", PID_KEY ).parse( stringArgs ); verbose = args.has( "verbose" ); jmxDumper = new JMXDumper( homeDir, fs, out, err, verbose ); pid = parsePid( args ); boolean force = args.has( "force" ); DiagnosticsReporter reporter = createAndRegisterSources(); Optional<Set<String>> classifiers = parseAndValidateArguments( args, reporter ); if ( !classifiers.isPresent() ) { return; } DiagnosticsReporterProgress progress = buildProgress(); // Start dumping Path destinationDir = new File( destinationArgument.parse( args ) ).toPath(); try { Path reportFile = destinationDir.resolve( getDefaultFilename() ); out.println( "Writing report to " + reportFile.toAbsolutePath().toString() ); reporter.dump( classifiers.get(), reportFile, progress, force ); } catch ( IOException e ) { throw new CommandFailed( "Creating archive failed", e ); } }
@Test void shouldNotPrintTheStacktraceWhenTheCommandFailsIfTheDebugFlagIsNotSet() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); CommandFailed exception = new CommandFailed( "" ); AdminCommand command = args -> { throw exception; }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld, never() ).printStacktrace( exception ); }
throw new CommandFailed( "database does not exist: " + database, e ); throw new CommandFailed( "the database is in use -- stop Neo4j and try again", e ); throw new CommandFailed( "you do not have permission to dump the database -- is Neo4j running as a different user?", e );
@Test void shouldProvideFeedbackIfTheCommandFails() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = args -> { throw new CommandFailed( "the-failure-message" ); }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld ).stdErrLine( "command failed: the-failure-message" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldPrintTheStacktraceWhenTheCommandFailsIfTheDebugFlagIsSet() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); CommandFailed exception = new CommandFailed( "" ); AdminCommand command = args -> { throw exception; }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, true ) .execute( null, null, "exception" ); verify( outsideWorld ).printStacktrace( exception ); }
private void setPassword( String password ) throws Throwable { Config config = loadNeo4jConfig(); FileSystemAbstraction fileSystem = outsideWorld.fileSystem(); if ( realUsersExist( config ) ) { File authFile = CommunitySecurityModule.getUserRepositoryFile( config ); throw new CommandFailed( realUsersExistErrorMsg( fileSystem, authFile ) ); } else { File file = CommunitySecurityModule.getInitialUserRepositoryFile( config ); if ( fileSystem.fileExists( file ) ) { fileSystem.deleteFile( file ); } FileUserRepository userRepository = new FileUserRepository( fileSystem, file, NullLogProvider.getInstance() ); userRepository.start(); userRepository.create( new User.Builder( INITIAL_USER_NAME, LegacyCredential.forPassword( UTF8.encode( password ) ) ) .withRequiredPasswordChange( false ) .build() ); userRepository.shutdown(); outsideWorld.stdOutLine( "Changed password for user '" + INITIAL_USER_NAME + "'." ); } }