java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 26 Feb 2019 18:19:49 +0100
branchv_0
changeset 236 a3ec71fa8e17
parent 188 54bacc7ed42b
permissions -rw-r--r--
Avoid reusing/rewriting the DB connection properties. There was weird random errors while testing connection to multiple DB in parallel when one of them was meta connection to same DB connection. Two kinds of exception: 1) missing password 2) „Passing DB password as CLI parameter is insecure!“
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk.configuration;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
30
b7ea47b2d4ca class for xmlns constants
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    20
import static info.globalcode.sql.dk.Xmlns.CONFIGURATION;
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import info.globalcode.sql.dk.formatting.Formatter;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import info.globalcode.sql.dk.formatting.FormatterContext;
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    23
import info.globalcode.sql.dk.formatting.FormatterException;
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.lang.reflect.Constructor;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import java.lang.reflect.InvocationTargetException;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
import javax.xml.bind.annotation.XmlElement;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
/**
155
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    29
 * Configured (but not yet instantiated) formatter.
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 * @author Ing. František Kučera (frantovo.cz)
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 */
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
public class FormatterDefinition implements NameIdentified {
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	private String name;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	private String className;
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    37
	private Properties properties = new Properties();
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    39
	public FormatterDefinition() {
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    40
	}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    41
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    42
	public FormatterDefinition(String name, String className) {
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    43
		this.name = name;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    44
		this.className = className;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    45
	}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    46
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    47
	public FormatterDefinition(String name, String className, Properties properties) {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    48
		this(name, className);
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    49
		this.properties = properties;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    50
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    51
30
b7ea47b2d4ca class for xmlns constants
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    52
	@XmlElement(name = "name", namespace = CONFIGURATION)
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	@Override
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	public String getName() {
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		return name;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	}
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	public void setName(String name) {
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		this.name = name;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	/**
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	 * Filter's class. Must implement the
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	 * <code>info.globalcode.sql.dk.formatting.Formatter</code> interface.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	 * Subclassing the
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	 * <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	 * The constructor must accept one parameter:
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	 * <code>info.globalcode.sql.dk.formatting.FormatterContext</code>
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	 * @return fully qualified class name
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	 */
30
b7ea47b2d4ca class for xmlns constants
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    72
	@XmlElement(name = "class", namespace = CONFIGURATION)
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	public String getClassName() {
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		return className;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	}
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	public void setClassName(String className) {
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		this.className = className;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	}
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    81
	@XmlElement(name = "property", namespace = CONFIGURATION)
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    82
	public Properties getProperties() {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    83
		return properties;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    84
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    85
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    86
	public void setProperties(Properties properties) {
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    87
		this.properties = properties;
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    88
	}
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    89
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	/**
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	 * @param context
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	 * @return
188
54bacc7ed42b jdbc-dk-driver: include main project sources
František Kučera <franta-hg@frantovo.cz>
parents: 160
diff changeset
    93
	 * @throws FormatterException
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	 */
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    95
	public Formatter getInstance(FormatterContext context) throws FormatterException {
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    96
		context.getProperties().setDefaults(properties);
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		try {
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
			Constructor constructor = Class.forName(className).getConstructor(context.getClass());
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
			Object instance = constructor.newInstance(context);
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
			if (instance instanceof Formatter) {
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
				return (Formatter) instance;
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
			} else {
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   104
				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
			}
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		} catch (ClassNotFoundException e) {
160
84ea4a819fb2 InfoLister: option --list-formatters also tests, if formatter class can be instantiated (thus is valid)
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   107
			throw new FormatterException("Formatter class does not exist: " + className, e);
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		} catch (NoSuchMethodException e) {
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   109
			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   111
			throw new FormatterException("Formatter's constructor caused an error: " + className, e);
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
		}
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
	}
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
}