schemacrawler.tools.executable.CommandRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of schemacrawler Show documentation
Show all versions of schemacrawler Show documentation
SchemaCrawler is an open-source Java API that makes working with database metadata as easy as working with plain old Java objects. SchemaCrawler is also a database schema discovery and comprehension, and schema documentation tool. You can search for database schema objects using regular expressions, and output the schema and data in a readable text format. The output is designed to be diff-ed against other database schemas.
/*
* SchemaCrawler
* http://sourceforge.net/projects/schemacrawler
* Copyright (c) 2000-2014, Sualeh Fatehi.
* This library is free software; you can redistribute it and/or modify it under
* the terms
* of the GNU Lesser General Public License as published by the Free Software
* Foundation;
* either version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package schemacrawler.tools.executable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.tools.options.OutputOptions;
/**
* Command registry for mapping commands to executable.
*
* @author Sualeh Fatehi
*/
public final class CommandRegistry
implements Iterable
{
private static Map loadCommandRegistry()
throws SchemaCrawlerException
{
final List commandProviders = new ArrayList(Arrays
.asList(new SchemaExecutableCommandProvider("list"),
new SchemaExecutableCommandProvider("brief"),
new SchemaExecutableCommandProvider("schema"),
new SchemaExecutableCommandProvider("details"),
new ExecutableCommandProvider("count",
"schemacrawler.tools.text.operation.OperationExecutable"),
new ExecutableCommandProvider("dump",
"schemacrawler.tools.text.operation.OperationExecutable"),
new ExecutableCommandProvider("script",
"schemacrawler.tools.integration.scripting.ScriptExecutable"),
new ExecutableCommandProvider("graph",
"schemacrawler.tools.integration.graph.GraphExecutable")));
try
{
final ServiceLoader serviceLoader = ServiceLoader
.load(CommandProvider.class);
for (final CommandProvider commandRegistryEntry: serviceLoader)
{
final String executableCommand = commandRegistryEntry.getCommand();
LOGGER.log(Level.FINER, "Loading executable, " + executableCommand
+ "="
+ commandRegistryEntry.getClass().getName());
commandProviders.add(commandRegistryEntry);
}
}
catch (final Exception e)
{
throw new SchemaCrawlerException("Could not load extended command registry",
e);
}
final Map commandRegistry = new HashMap<>();
for (final CommandProvider commandProvider: commandProviders)
{
commandRegistry.put(commandProvider.getCommand(), commandProvider);
}
return commandRegistry;
}
private static final Logger LOGGER = Logger.getLogger(CommandRegistry.class
.getName());
private final Map commandRegistry;
public CommandRegistry()
throws SchemaCrawlerException
{
commandRegistry = loadCommandRegistry();
}
public String getHelpResource(final String command)
{
final String helpResource;
if (commandRegistry.containsKey(command))
{
helpResource = commandRegistry.get(command).getHelpResource();
}
else
{
helpResource = null;
}
return helpResource;
}
public boolean hasCommand(final String command)
{
return command != null && commandRegistry.containsKey(command);
}
@Override
public Iterator iterator()
{
return lookupAvailableCommands().iterator();
}
Executable configureNewExecutable(final String command,
final SchemaCrawlerOptions schemaCrawlerOptions,
final OutputOptions outputOptions)
throws SchemaCrawlerException
{
final CommandProvider commandProvider;
if (commandRegistry.containsKey(command))
{
commandProvider = commandRegistry.get(command);
}
else
{
commandProvider = new ExecutableCommandProvider(command,
"schemacrawler.tools.text.operation.OperationExecutable");
}
return commandProvider.configureNewExecutable(schemaCrawlerOptions,
outputOptions);
}
private Collection lookupAvailableCommands()
{
final List availableCommands = new ArrayList<>(commandRegistry.keySet());
Collections.sort(availableCommands);
return availableCommands;
}
}