Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output plugin core #40

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/henplus/HenPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import henplus.commands.properties.SessionPropertyCommand;
import henplus.io.ConfigurationContainer;
import henplus.logging.Logger;
import henplus.property.MetaDataPropertyHolder;
import henplus.util.StringUtil;

import java.io.BufferedReader;
Expand All @@ -39,6 +40,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

Expand Down Expand Up @@ -145,6 +147,39 @@ public void readConfiguration(final InputStream in) throws Exception {
setDefaultPrompt();
}

public static class OutputTypePropertyHolder extends MetaDataPropertyHolder {
public OutputTypePropertyHolder (final String... vals) {
super(vals);
}

public OutputTypePropertyHolder (final Collection<String> vals) {
super(vals);
}

public OutputTypePropertyHolder (final Map<String, Map<String, String>> vals) {
super(vals.keySet());
}
@Override
public void enumeratedPropertyChanged(final int index, final String value) {
}
@Override
public String getDefaultValue() {
return "table";
}
@Override
public String getShortDescription () {
return "set the output type";
}
@Override
public String getLongDescription () {
return "Set the output type";
}
@Override
public Map<String, String> getMetaData() {
return getMetaData(getValue());
}
}

public void initializeCommands(final String[] argv) {
_henplusProperties = new PropertyRegistry();
_henplusProperties.registerProperty("comments-remove", _commandSeparator.getRemoveCommentsProperty());
Expand All @@ -156,6 +191,9 @@ public void initializeCommands(final String[] argv) {
_dispatcher = new CommandDispatcher(_settingStore);
_objectLister = new ListUserObjectsCommand(this);
_henplusProperties.registerProperty("echo-commands", new EchoCommandProperty(_dispatcher));
MetaDataPropertyHolder renderers = new OutputTypePropertyHolder("table");
renderers.getMetaData().put("renderer-class", "henplus.commands.ResultSetRenderer");
_henplusProperties.registerProperty("output", renderers);

_dispatcher.register(new HelpCommand());

Expand Down Expand Up @@ -400,6 +438,14 @@ public String getPartialLine() {
return _historyLine.toString() + Readline.getLineBuffer();
}

/**
* Get a reference to this HenPlus instance's internal
* PropertyRegistry.
*/
public PropertyRegistry getRegistry() {
return _henplusProperties;
}

public void run() {
String cmdLine = null;
String displayPrompt = _prompt;
Expand Down
4 changes: 2 additions & 2 deletions src/henplus/PropertyRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void registerProperty(final String name, final PropertyHolder holder) thr
_namedProperties.put(name, holder);
}

public void unregisterProperty(final String name) {
_namedProperties.remove(name);
public PropertyHolder unregisterProperty(final String name) {
return _namedProperties.remove(name);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/henplus/commands/PluginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.SortedMap;
Expand Down Expand Up @@ -101,7 +102,14 @@ private Command loadPlugin(final String className) throws ClassNotFoundException
IllegalAccessException {
Command plugin = null;
final Class<?> pluginClass = Class.forName(className);
plugin = (Command) pluginClass.newInstance();
try {
plugin = (Command) pluginClass.getDeclaredConstructor(HenPlus.class).newInstance(_henplus);
_henplus.getDispatcher().register(plugin);
return plugin;
}
catch (Exception e) {
}
plugin = (Command) pluginClass.newInstance();
_henplus.getDispatcher().register(plugin);
return plugin;
}
Expand Down Expand Up @@ -154,6 +162,7 @@ public int execute(final SQLSession currentSession, final String cmd, final Stri
try {
plugin = loadPlugin(pluginClass);
} catch (final Exception e) {
e.printStackTrace(System.err);
HenPlus.msg().println("couldn't load plugin: " + e.getMessage());
return EXEC_FAILED;
}
Expand All @@ -180,6 +189,11 @@ public int execute(final SQLSession currentSession, final String cmd, final Stri
} else {
final Command c = _plugins.remove(pluginClass);
_henplus.getDispatcher().unregister(c);
try {
Class.forName(pluginClass).getDeclaredMethod("unregister").invoke(c);
}
catch (Exception e) {
}
}
}
return SUCCESS;
Expand Down
22 changes: 11 additions & 11 deletions src/henplus/commands/ResultSetRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
*/
public class ResultSetRenderer implements Interruptable {

private final ResultSet _rset;
private final ResultSetMetaData _meta;
private final TableRenderer _table;
private final int _columns;
private final int[] _showColumns;

private boolean _beyondLimit;
private long _firstRowTime;
private final long _clobLimit = 8192;
private final int _rowLimit;
private volatile boolean _running;
protected final ResultSet _rset;
protected final ResultSetMetaData _meta;
protected final TableRenderer _table;
protected final int _columns;
protected final int[] _showColumns;

protected boolean _beyondLimit;
protected long _firstRowTime;
protected final long _clobLimit = 8192;
protected final int _rowLimit;
protected volatile boolean _running;

public ResultSetRenderer(final ResultSet rset, final String columnDelimiter, final boolean enableHeader,
final boolean enableFooter, final int limit, final OutputDevice out, final int[] show) throws SQLException {
Expand Down
24 changes: 22 additions & 2 deletions src/henplus/commands/SQLCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import henplus.AbstractCommand;
import henplus.CommandDispatcher;
import henplus.HenPlus;
import henplus.OutputDevice;
import henplus.PropertyRegistry;
import henplus.SQLSession;
import henplus.SigIntHandler;
import henplus.logging.Logger;
import henplus.property.BooleanPropertyHolder;
import henplus.property.PropertyHolder;
import henplus.property.MetaDataPropertyHolder;
import henplus.view.util.CancelWriter;
import henplus.view.util.NameCompleter;

Expand Down Expand Up @@ -58,6 +60,7 @@ public String[] getCommandList() {
private boolean _showFooter;
private volatile boolean _running;
private StatementCanceller _statementCanceller;
private PropertyRegistry _registry;

protected SQLCommand(final ListUserObjectsCommand tc) {
_columnDelimiter = "|";
Expand All @@ -73,6 +76,7 @@ public SQLCommand(final ListUserObjectsCommand tc, final PropertyRegistry regist
_rowLimit = 2000;
_showHeader = true;
_showFooter = true;
_registry = registry;
registry.registerProperty("column-delimiter", new SQLColumnDelimiterProperty());
registry.registerProperty("sql-result-limit", new RowLimitProperty());
registry.registerProperty("sql-result-showheader", new ShowHeaderProperty());
Expand All @@ -83,6 +87,10 @@ public SQLCommand(final ListUserObjectsCommand tc, final PropertyRegistry regist
new Thread(_longRunningDisplay).start();
}

public PropertyRegistry getRegistry() {
return _registry;
}

/**
* don't show the commands available in the toplevel command completion list ..
*/
Expand Down Expand Up @@ -244,8 +252,20 @@ public int execute(final SQLSession session, final String cmd, final String para
do {
rset = _stmt.getResultSet();
ResultSetRenderer renderer;
renderer = new ResultSetRenderer(rset, getColumnDelimiter(), isShowHeader(), isShowFooter(), getRowLimit(),
HenPlus.out());
renderer =
(ResultSetRenderer)
Class
.forName(((MetaDataPropertyHolder)getRegistry().getPropertyMap().get("output"))
.getMetaData()
.get("renderer-class"))
.getDeclaredConstructor(ResultSet.class,
String.class,
boolean.class,
boolean.class,
int.class,
OutputDevice.class)
.newInstance(rset, getColumnDelimiter(), isShowHeader(), isShowFooter(), getRowLimit(),
HenPlus.out());
SigIntHandler.getInstance().pushInterruptable(renderer);
final int rows = renderer.execute();
SigIntHandler.getInstance().popInterruptable();
Expand Down
4 changes: 2 additions & 2 deletions src/henplus/property/EnumeratedPropertyHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
public abstract class EnumeratedPropertyHolder extends PropertyHolder {

private final String[] _values;
private final NameCompleter _completer;
protected final String[] _values;
protected final NameCompleter _completer;

/**
* create a new EnumeratedPropertyHolder that gets an array of Strings with possible values of this property.
Expand Down
68 changes: 68 additions & 0 deletions src/henplus/property/MetaDataPropertyHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package henplus.property;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* A PropertyHolder, that can change its values to a fixed set of values.
*
* In addition, for each of those fixed set of values, it can store a
* Map of String-String pairs, as additional meta-data.
*/
public abstract class MetaDataPropertyHolder extends EnumeratedPropertyHolder {
protected final Map<String, Map<String, String>> _metaDataValues = new HashMap<String, Map<String, String>>();


/**
* Create a new MetaDataPropertyHolder that gets an array of
* Strings with possible values for this property.
*/
public MetaDataPropertyHolder (final String... vals) {
super(vals);
for (String val : vals) {
_metaDataValues.put(val, new HashMap<String, String>());
}
if (vals!=null && vals.length>0) {
try {
setValue(vals[0]);
}
catch (Exception e) {}
}
}

/**
* Same with Collection as input
*/
public MetaDataPropertyHolder (final Collection<String> vals) {
super(vals);
for (String val : vals) {
_metaDataValues.put(val, new HashMap<String, String>());
}
}

public MetaDataPropertyHolder (final Map<String, Map<String, String>> vals) {
super(vals.keySet());
_metaDataValues.putAll(vals);
}

@Override
public void enumeratedPropertyChanged(final int index, final String value) {
}

/**
* Get the full Map of meta-data, for the default property.
*/
public Map<String, String> getMetaData() {
return getMetaData(getValue());
}

/**
* Get the full Map of meta-data, for a particular enumerated
* property.
*/
public Map<String, String> getMetaData(String val) {
return _metaDataValues.get(val);
}
}