Skip to content

Commit

Permalink
More work on refactoring configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
johncurrier committed Dec 5, 2006
1 parent 37693d5 commit 757a0f3
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 49 deletions.
123 changes: 116 additions & 7 deletions src/net/sourceforge/schemaspy/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sourceforge.schemaspy;

import java.beans.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
Expand All @@ -17,16 +18,20 @@ public class Config
private static final long serialVersionUID = 1L;
private List options;
private Map dbSpecificOptions;
private HashMap originalDbSpecificOptions;
private Map originalDbSpecificOptions;
private boolean helpRequired;
private boolean dbHelpRequired;
private File outputDir;
private Pattern inclusions;
private Pattern exclusions;
private String dbType;
private String schema;
private String user;
private String password;
private String db;
private String host;
private Integer port;
private String server;
private Pattern inclusions;
private Pattern exclusions;
private String userConnectionPropertiesFile;
private Properties userConnectionProperties;
private Integer maxDbThreads;
Expand All @@ -45,6 +50,7 @@ public class Config
private Boolean numRowsEnabled;
private Boolean meterEnabled;
private Boolean evaluteAll;
private String schemaSpec; // used in conjunction with evaluateAll

/**
* Default constructor. Intended for when you want to inject properties
Expand Down Expand Up @@ -132,16 +138,58 @@ public String getDbType() {
return dbType;
}

public void setDb(String db) {
this.db = db;
}

public String getDb() {
if (db == null)
db = pullParam("-db");
return db;
}

public void setSchema(String schema) {
this.schema = schema;
}

public String getSchema() {
if (schema == null) {
if (schema == null)
schema = pullParam("-s", false, true);
return schema;
}

public void setHost(String host) {
this.host = host;
}

public String getHost() {
if (host == null)
host = pullParam("-host");
return host;
}

public void setPort(int port) {
this.port = new Integer(port);
}

public Integer getPort() {
if (port == null)
try {
port = Integer.valueOf(pullParam("-port"));
} catch (Exception notSpecified) {}
return port;
}

public void setServer(String server) {
this.server = server;
}

public String getServer() {
if (server == null) {
server = pullParam("-server");
}

return schema;
return server;
}

public void setUser(String user) {
Expand Down Expand Up @@ -413,6 +461,23 @@ public boolean isEvaluateAllEnabled() {
return evaluteAll.booleanValue();
}

/**
* When -all (evaluateAll) is specified then this is the regular
* expression that determines which schemas to evaluate.
*
* @param schemaSpec
*/
public void setSchemaSpec(String schemaSpec) {
this.schemaSpec = schemaSpec;
}

public String getSchemaSpec() {
if (schemaSpec == null)
schemaSpec = pullParam("-schemaSpec");

return schemaSpec;
}

/**
* Returns <code>true</code> if the options indicate that the user wants
* to see some help information.
Expand Down Expand Up @@ -492,7 +557,7 @@ public List getRemainingParameters()

/**
* Options that are specific to a type of database. E.g. things like <code>host</code>,
* <code>port</code> or <code>db</code>.
* <code>port</code> or <code>db</code>, but <b>don't</b> have a setter in this class.
*
* @param dbSpecificOptions
*/
Expand Down Expand Up @@ -584,7 +649,7 @@ protected List fixupArgs(List args) {
for (Iterator iter = args.iterator(); iter.hasNext(); ) {
String arg = iter.next().toString();
int indexOfEquals = arg.indexOf('=');
if (indexOfEquals != -1) {
if (indexOfEquals != -1 && indexOfEquals -1 != arg.indexOf("\\=")) {
expandedArgs.add(arg.substring(0, indexOfEquals));
expandedArgs.add(arg.substring(indexOfEquals + 1));
} else {
Expand Down Expand Up @@ -699,6 +764,31 @@ protected void dumpUsage(String errorMessage, boolean detailedDb) {
System.out.flush();
}

/**
* Get the value of the specified parameter.
* Used for properties that are common to most db's, but aren't required.
*
* @param paramName
* @return
*/
public String getParam(String paramName) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(Config.class);
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
if (prop.getName().equalsIgnoreCase(paramName)) {
Object result = prop.getReadMethod().invoke(this, null);
return result == null ? null : result.toString();
}
}
} catch (Exception failed) {
failed.printStackTrace();
}

return null;
}

/**
* Return all of the configuration options as a List of Strings, with
* each parameter and its value as a separate element.
Expand Down Expand Up @@ -778,6 +868,25 @@ public List asList() throws IOException {
list.add("-connprops");
list.add(value);
}
value = getDb();
if (value != null) {
list.add("-db");
list.add(value);
}
value = getHost();
if (value != null) {
list.add("-host");
list.add(value);
}
if (getPort() != null) {
list.add("-port");
list.add(getPort().toString());
}
value = getServer();
if (value != null) {
list.add("-server");
list.add(value);
}
list.add("-i");
list.add(getInclusions().pattern());
list.add("-x");
Expand Down
7 changes: 2 additions & 5 deletions src/net/sourceforge/schemaspy/Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package net.sourceforge.schemaspy;

import java.util.*;


/**
* @author John Currier
*/
public class Main {
public static void main(String[] argv) throws Exception {
SchemaAnalyzer analyzer = new SchemaAnalyzer();
System.out.println(Arrays.asList(argv));
System.exit(analyzer.analyze(new Config(argv), argv));

System.exit(analyzer.analyze(new Config(argv)));
}
}
23 changes: 13 additions & 10 deletions src/net/sourceforge/schemaspy/SchemaAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author John Currier
*/
public class SchemaAnalyzer {
public int analyze(Config config, String[] argv) throws Exception {
public int analyze(Config config) throws Exception {
try {
if (config.isHelpRequired()) {
config.dumpUsage(null, false);
Expand All @@ -34,9 +34,8 @@ public int analyze(Config config, String[] argv) throws Exception {
Properties properties = config.getDbProperties(config.getDbType());

ConnectionURLBuilder urlBuilder = new ConnectionURLBuilder(config, properties);

//TODO
//String schemaSpec = getParam(args, "-schemaSpec");
if (config.getDb() == null)
config.setDb(urlBuilder.getConnectionURL());

config.populate(); // force options to be evaluated
if (config.getRemainingParameters().size() != 0) {
Expand All @@ -56,7 +55,7 @@ public int analyze(Config config, String[] argv) throws Exception {
return 3;

DatabaseMetaData meta = connection.getMetaData();
String dbName = urlBuilder.getDbName();
String dbName = config.getDb();
String schema = config.getSchema();
File outputDir = config.getOutputDir();

Expand All @@ -65,16 +64,20 @@ public int analyze(Config config, String[] argv) throws Exception {
Iterator iter = urlBuilder.getOptions().iterator();
while (iter.hasNext()) {
DbOption option = (DbOption)iter.next();
args.add("-" + option.name);
args.add(option.value);
if (!args.contains("-" + option.name)) {
args.add("-" + option.name);
args.add(option.value);
}
}

yankParam(args, "-o"); // param will be replaced by something appropriate
yankParam(args, "-s"); // param will be replaced by something appropriate
args.remove("-all"); // param will be replaced by something appropriate
if (schema == null)
schema = properties.getProperty("schemaSpec", ".*");
return MultipleSchemaAnalyzer.getInstance().analyze(dbName, meta, schema, args, config.getUser(), outputDir, config.getLoadedFromJar());

String schemaSpec = config.getSchemaSpec();
if (schemaSpec == null)
schemaSpec = properties.getProperty("schemaSpec", ".*");
return MultipleSchemaAnalyzer.getInstance().analyze(dbName, meta, schemaSpec, args, config.getUser(), outputDir, config.getLoadedFromJar());
}

if (schema == null && meta.supportsSchemasInTableDefinitions())
Expand Down
46 changes: 19 additions & 27 deletions src/net/sourceforge/schemaspy/util/ConnectionURLBuilder.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.sourceforge.schemaspy.util;

import java.io.*;
import java.io.File;
import java.util.*;
import net.sourceforge.schemaspy.*;
import net.sourceforge.schemaspy.Config;

/**
* @author John Currier
Expand All @@ -11,7 +11,6 @@ public class ConnectionURLBuilder {
private final String type;
private String description;
private final String connectionURL;
private String dbName = null;
private final List options = new ArrayList();
public class DbOption {
public final String name;
Expand All @@ -27,11 +26,9 @@ public DbOption(String name, String value, String description) {

/**
* @param config
* @param args
* @param properties
* @throws IOException
*/
public ConnectionURLBuilder(Config config, Properties properties) throws IOException {
public ConnectionURLBuilder(Config config, Properties properties) {
this.type = config.getDbType();

List opts = new ArrayList();
Expand All @@ -43,11 +40,7 @@ public ConnectionURLBuilder(Config config, Properties properties) throws IOExcep
}
opts.addAll(config.getRemainingParameters());

connectionURL = parseParameters(opts, properties);

if (dbName == null)
dbName = connectionURL;

connectionURL = parseParameters(opts, properties, config);
description = properties.getProperty("description");

List remaining = config.getRemainingParameters();
Expand Down Expand Up @@ -88,11 +81,11 @@ Properties load(String dbType) {
}
}

parseParameters(null, new DbPropLoader().load(type));
parseParameters(null, new DbPropLoader().load(type), null);
connectionURL = null;
}

private String parseParameters(List args, Properties properties) {
private String parseParameters(List args, Properties properties, Config config) {
StringBuffer url = new StringBuffer();
boolean inParam = false;

Expand All @@ -105,9 +98,7 @@ private String parseParameters(List args, Properties properties) {
inParam = false;
} else {
if (inParam) {
String paramValue = getParam(args, token, properties);
if (token.equals("db") || token.equals("-db"))
dbName = paramValue;
String paramValue = getParam(args, token, properties, config);
url.append(paramValue);
} else
url.append(token);
Expand All @@ -121,26 +112,27 @@ public String getConnectionURL() {
return connectionURL;
}

public String getDbName() {
return dbName;
}

public List getOptions() {
return options;
}

private String getParam(List args, String paramName, Properties properties) {
private String getParam(List args, String paramName, Properties properties, Config config) {
String param = null;
int paramIndex = args != null ? args.indexOf("-" + paramName) : -1;
String description = properties.getProperty(paramName);

if (args != null) {
if (paramIndex < 0)
throw new Config.MissingRequiredParameterException(paramName, description, true);

args.remove(paramIndex);
param = args.get(paramIndex).toString();
args.remove(paramIndex);
if (paramIndex < 0) {
if (config != null)
param = config.getParam(paramName); // not in args...might be one of
// the common db params
if (param == null)
throw new Config.MissingRequiredParameterException(paramName, description, true);
} else {
args.remove(paramIndex);
param = args.get(paramIndex).toString();
args.remove(paramIndex);
}
}

options.add(new DbOption(paramName, param, description));
Expand Down

0 comments on commit 757a0f3

Please sign in to comment.