SuperX-Kernmodul
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

620 lines
21 KiB

package de.superx.bin;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import de.memtext.util.CryptUtils;
import de.memtext.util.GetOpts;
import de.memtext.widgets.LabeledComboBox;
import de.memtext.widgets.LabeledTextField;
import de.memtext.widgets.RadioButtonGroup;
import de.memtext.widgets.VerticalBox;
import de.memtext.widgets.WarningMessage;
import de.superx.util.ConnectionDialogCommon;
import de.superx.util.SqlStringUtils;
public class PropAdmin extends JFrame implements ActionListener, ItemListener {
static Container cp;
static LinkedList logLevels = new LinkedList();
static JTextField tAdminPasswd, tdriver, turl, tadminUser, tRestrictedUser,
tRestrictedPassword;
static LabeledTextField ltfMaskCache;
static LabeledTextField ltfUserCache;
static JComboBox tname;
static LabeledComboBox sqlLogLevel, xmlLogLevel;
static LabeledTextField maxActive;
static LabeledTextField minIdle;
static LabeledTextField maxIdle;
static RadioButtonGroup rbEntwicklungsmodus = new RadioButtonGroup();
static private String defaultDBDriver = "com.informix.jdbc.IfxDriver";
static private String defaultConnection = "jdbc:informix-sqli://<<hostname>>:<<Portnr>>:informixserver=<<informixserver>>;database=superx";
static private String defaultUser = "superx";
private static String dbpropfile = "db.properties";
static private String connTypes[][];
private static boolean isGuiWanted = true;
static Properties props = new Properties();
static byte key[] = { (byte) 255, (byte) 221, (byte) 127, (byte) 109,
(byte) 129 };
static int keyLength = key.length;
private static String usage = "-------------------------------------\nGebrauch: java de.superx.bin.PropAdmin -dbproperties:<<Pfad zu den db-Properties>>(optional) \n---------------------------------------------------";
// private static String newAdminPassword;
public PropAdmin() {
super("DB-Properties Admin @version@");
ltfMaskCache = new LabeledTextField("Masken, die im Cache sein sollen",
"select tid from maskeninfo where 1=0", 50);
ltfUserCache = new LabeledTextField("User, die im Cache sein sollen",
"select tid from userinfo where 1=0", 50);
maxActive = new LabeledTextField("maxActive", 3);
minIdle = new LabeledTextField("minIdle", 3);
maxIdle = new LabeledTextField("maxIdle", 3);
sqlLogLevel = new LabeledComboBox("Log Level SQL", logLevels);
xmlLogLevel = new LabeledComboBox("Log Level XML", logLevels);
sqlLogLevel.setSelectedIndex(1);
xmlLogLevel.setSelectedIndex(1);
rbEntwicklungsmodus.add(new JLabel("Entwicklungsmodus "));
rbEntwicklungsmodus.add("an");
rbEntwicklungsmodus.add("aus");
rbEntwicklungsmodus.setSelection("an");
JButton btnTestAdmin = new JButton("Verbindung testen");
btnTestAdmin.addActionListener(this);
cp = this.getContentPane();
cp.setLayout(new BorderLayout());
JPanel titel = new JPanel();
JLabel ltitel = new JLabel("DB-Properties Admin für " + dbpropfile);
ltitel.setFont(new Font("Courier", Font.BOLD, 14));
titel.add(ltitel);
cp.add(titel, "North");
VerticalBox center = new VerticalBox();
JPanel p0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
tname = new JComboBox();
connTypes = ConnectionDialogCommon.getTypes();
for (int i = 0; i < connTypes.length; i++) {
tname.addItem(connTypes[i][0]);
}
// controls.add(types);
JLabel lname = new JLabel(" Driver :");
lname.setFont(new Font("Courier", Font.BOLD, 12));
// tname=new JTextField(30);
JLabel c_name = new JLabel("(mögliche Datenbanksysteme für SuperX)");
p0.add(lname);
p0.add(tname);
p0.add(c_name);
center.add(p0);
// JPanel center=new JPanel(new GridLayout(0,1));
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel ldriver = new JLabel(" Driver Class:");
ldriver.setFont(new Font("Courier", Font.BOLD, 12));
tdriver = new JTextField(30);
JLabel c_driver = new JLabel("(muss im CLASSPATH stehen!)");
p1.add(ldriver);
p1.add(tdriver);
p1.add(c_driver);
center.add(p1);
JPanel p1b = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel lurl = new JLabel(" Connection URL:");
lurl.setFont(new Font("Courier", Font.BOLD, 12));
turl = new JTextField(50);
p1b.add(lurl);
p1b.add(turl);
center.add(p1b);
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel luser = new JLabel(" Username:");
luser.setFont(new Font("Courier", Font.BOLD, 12));
tadminUser = new JTextField(10);
JLabel lpasswd = new JLabel(" Password:");
lpasswd.setFont(new Font("Courier", Font.BOLD, 12));
tAdminPasswd = new JPasswordField(10);
p3.add(luser);
p3.add(tadminUser);
p3.add(lpasswd);
p3.add(tAdminPasswd);
p3.add(btnTestAdmin);
center.add(p3);
JPanel p3b = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel lbl33 = new JLabel(" Eingeschränkter User:");
lbl33.setFont(new Font("Courier", Font.BOLD, 12));
p3b.add(lbl33);
tRestrictedUser = new JTextField(10);
p3b.add(tRestrictedUser);
tRestrictedPassword = new JPasswordField(10);
p3b.add(new JLabel("Passwort"));
p3b.add(tRestrictedPassword);
JButton btnTest2 = new JButton("Verb.testen");
btnTest2.addActionListener(this);
JButton btnSelectRights = new JButton("select-Rechte auf alle Tabellen");
btnSelectRights.addActionListener(this);
p3b.add(btnTest2);
p3b.add(btnSelectRights);
center.add(p3b);
center.add(sqlLogLevel);
center.add(xmlLogLevel);
center.addWithLeftAlignment(rbEntwicklungsmodus);
center
.addWithLeftAlignment(new JLabel(
"<html>(Im Entwicklungsmodus werden alle SQL-Befehle von Abfragen einzeln an die Datenbank geschickt.<br>Das dauert etwas l&auml;nger, erm&ouml;glicht aber bessere Fehlermeldungen.)<br>"));
center.add(ltfMaskCache);
center.add(ltfUserCache);
JPanel p4 = new JPanel();
p4
.add(new JLabel(
"<html>Der Apache ConnectionPool verwaltet die Anzahl benötigter Verbindungen dynamisch.<br>min/max idle gibt an wieviele Connections ständig bereit gehalten werden sollen.<br>maxActive gibt an wieviele Connections maximal gleichzeitig aktiv sein sollen."));
center.add(p4);
JPanel p5 = new JPanel();
p5.add(minIdle);
p5.add(maxIdle);
p5.add(maxActive);
center.add(p5);
cp.add(center, "Center");
JButton OK = new JButton("Speichern");
OK.addActionListener(this);
JPanel unten = new JPanel();
unten.add(OK);
cp.add(unten, "South");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tname.addItemListener(this);
this.pack();
}
private static void properties_einlesen() throws IOException {
if (!isGuiWanted)
System.out.println("Lese ein: " + dbpropfile);
props = new Properties();
FileInputStream is = new FileInputStream(dbpropfile);
if (is != null) {
props.load(is);
is.close();
} else {
if (isGuiWanted)
JOptionPane.showMessageDialog(null,
"Kann Properties nicht einlesen.", "DB-Prop Admin",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("kann properties nicht einlesen");
}
// System.out.println(CryptUtils.decryptSimple(props.getProperty(
// "connectionPassword")));
}
private static void initFormFromProps() {
String tdriverclass = props.getProperty("driverName");
tdriver.setText(tdriverclass);
// if(props.getProperty( "connectionURL" ).equals(""))
// {
// Default-Url:
for (int i = 0; i < connTypes.length; i++) {
if (tdriverclass.equals(connTypes[i][1])) {
tname.setSelectedIndex(i);
}
}
if (props.getProperty("connectionURL") != null)
turl.setText(props.getProperty("connectionURL"));
// }
tadminUser.setText(props.getProperty("connectionName"));
tRestrictedUser.setText(props.getProperty("restrictedConnectionName"));
if (props.getProperty("logLevelSQL") != null)
sqlLogLevel.setSelectedItem(props.getProperty("logLevelSQL"));
if (props.getProperty("logLevelXML") != null)
xmlLogLevel.setSelectedItem(props.getProperty("logLevelXML"));
if (props.getProperty("maskCache") != null)
ltfMaskCache.setValue(props.getProperty("maskCache"));
if (props.getProperty("userCache") != null)
ltfUserCache.setValue(props.getProperty("userCache"));
if (props.getProperty("minIdle") != null)
minIdle.setValue(props.getProperty("minIdle"));
else
minIdle.setValue("5");
if (props.getProperty("maxIdle") != null)
maxIdle.setValue(props.getProperty("maxIdle"));
if (props.getProperty("maxActive") != null)
maxActive.setValue(props.getProperty("maxActive"));
if (props.getProperty("developmentMode") == null
|| props.getProperty("developmentMode").equals("true"))
rbEntwicklungsmodus.setSelection("an");
else
rbEntwicklungsmodus.setSelection("aus");
}
public void actionPerformed(ActionEvent event) {
String cmd = event.getActionCommand();
if (cmd.equals("Speichern"))
{
try {
formValuesToProps();
saveProps();
System.exit(0);
} catch (Exception e) {
System.out.println("Es ist ein Fehler aufgetreten.");
e.printStackTrace();
WarningMessage.show(null, "Fehler: " + e, "SuperX");
}
}
if (cmd.equals("Verbindung testen")) {
try {
formValuesToProps();
boolean isOk = testConnection(props
.getProperty("connectionName"), tAdminPasswd.getText());
} catch (Exception e) {
WarningMessage.show(null, "Fehler:" + e, "PropAdmin");
}
}
if (cmd.equals("Verb.testen")) {
try {
formValuesToProps();
testConnection(props.getProperty("restrictedConnectionName"),
tRestrictedPassword.getText());
} catch (Exception e) {
WarningMessage.show(null, "Fehler:" + e, "PropAdmin");
}
}
if (cmd.equals("select-Rechte auf alle Tabellen")) {
try {
if (tRestrictedUser.getText() == null
|| tRestrictedUser.getText().trim().equals(""))
throw new IllegalArgumentException(
"Eingeschränkter user muss angegeben sein");
if (tadminUser.getText() == null
|| tadminUser.getText().trim().equals(""))
throw new IllegalArgumentException(
"User muss angegeben sein");
if (tAdminPasswd.getText() == null
|| tAdminPasswd.getText().trim().equals(""))
throw new IllegalArgumentException(
"Userpassword muss angegeben sein");
// int result=JOptionPane.showConfirmDialog(this,
// "Wollen Sie dem eingeschränktem User select-Rechte auf alle Tabellen geben?"
// ,"PropAdmin",JOptionPane.YES_NO_OPTION);
// if (result==JOptionPane.YES_OPTION)
grantSelectToRestrictedUser();
} catch (Exception e) {
WarningMessage.show(null, "Fehler:" + e, "PropAdmin");
}
}
}
private void grantSelectToRestrictedUser() throws Exception {
Class.forName(props.getProperty("driverName"));
Connection conn = DriverManager.getConnection(props
.getProperty("connectionURL"), tadminUser.getText(),
tAdminPasswd.getText());
Statement stm = conn.createStatement();
ResultSet rs = null;
if (props.getProperty("driverName").indexOf("postgres") > -1)
rs = conn.getMetaData().getTables(null, null, null, null);
else
rs = conn.getMetaData().getTables("superx", "superx", null, null);
while (rs.next()) {
System.out.println(rs.getObject(3).toString());
if (rs.getString(4) != null
&& (rs.getString(4).equals("TABLE") || rs.getString(4)
.equals("VIEW")))
stm.execute("grant select on " + rs.getObject(3).toString()
+ " to " + tRestrictedUser.getText() + ";");
}
rs.close();
stm.close();
conn.close();
}
private static boolean testConnection(String username, String password) {
boolean result = false;
try {
Class.forName(props.getProperty("driverName"));
if (props.getProperty("driverName").indexOf("postgres") > -1) {
props.put("charSet", SqlStringUtils.getEncoding().equals(
"xUTF-8") ? "UTF-8" : "Latin-1");
props.put("DateStyle", "German, DMY");
} else // Informix
{
props.put("GL_DATETIME", "%d.%m.%Y %T");
props.put("CLIENT_LOCALE", SqlStringUtils.getEncoding().equals(
"xUTF-8") ? "UTF-8" : "de_de.8859-1");
}
Connection conn = DriverManager.getConnection(props
.getProperty("connectionURL"), username, password);
DatabaseMetaData dbmd = conn.getMetaData();
/*
* am 19.1.2006 auskommentiert, weil propadmin auch für DBen ausser
* superx genutzt wird. dq Statement stm=conn.createStatement(); if
* (props.getProperty("driverName").indexOf("postgres")>-1)
*
* stm.executeQuery("select date('1.1.2005');"); else
* stm.executeQuery("select 'xx' from xdummy");
*/
String msg = "Verbindung mit Datenbank " + conn.getCatalog() + " ("
+ dbmd.getDatabaseProductName() + " "
+ dbmd.getDatabaseProductVersion() + ") als " + username
+ " erfolgreich aufgebaut";
if (isGuiWanted)
JOptionPane.showMessageDialog(null, msg, "DB-Prop Admin",
JOptionPane.INFORMATION_MESSAGE);
else
System.out.println(msg);
// stm.close();
conn.close();
result = true;
} catch (Exception e) {
String msg = "Ein Fehler ist aufgetreten.\n" + e.toString();
if (props.getProperty("driverName").indexOf("postgres") > -1
&& e.toString().indexOf("Date Style") > -1) {
msg += "\nPrüfen Sie,ob Date Style auf dem Server auf German, DMY steht.";
}
if (isGuiWanted)
JOptionPane.showMessageDialog(null, msg, "DB-Prop Admin",
JOptionPane.WARNING_MESSAGE);
else
System.out.println("Fehler: " + e.toString());
}
return result;
}
private static void formValuesToProps() throws Exception {
String driver = tdriver.getText();
String name = tadminUser.getText();
String passwd = tAdminPasswd.getText();
String url = turl.getText();
if (driver == null || name == null || passwd == null || url == null
|| driver.equals("") || name.equals("") || url.equals("")) {
if (isGuiWanted)
JOptionPane.showMessageDialog(null,
"Bitte alle Felder ausfüllen!", "DB-Prop Admin",
JOptionPane.INFORMATION_MESSAGE);
return;
}
props.setProperty("connectionPassword", "sx_des"
+ CryptUtils.encryptStringDES(tAdminPasswd.getText()));
props.setProperty("connectionName", name);
props
.setProperty("restrictedConnectionName", tRestrictedUser
.getText());
if (tRestrictedPassword.getText() != null
&& tRestrictedPassword.getText().length() > 1)
props.setProperty("restrictedConnectionPassword", "sx_des"
+ CryptUtils
.encryptStringDES(tRestrictedPassword.getText()));
props.setProperty("connectionURL", url);
props.setProperty("driverName", driver);
props.setProperty("minIdle", (String) minIdle.getValue());
props.setProperty("maxIdle", (String) maxIdle.getValue());
props.setProperty("maxActive", (String) maxActive.getValue());
props.setProperty("maskCache", (String) ltfMaskCache.getValue());
props.setProperty("userCache", (String) ltfUserCache.getValue());
props.setProperty("logLevelSQL", sqlLogLevel.getSelectedItem()
.toString());
props.setProperty("logLevelXML", xmlLogLevel.getSelectedItem()
.toString());
props.setProperty("developmentMode", rbEntwicklungsmodus
.getSelectedName().equals("an") ? "true" : "false");
}
private static void saveProps() {
try {
props.remove("charSet");
props.remove("DateStyle");
props.remove("GL_DATETIME");
props.remove("CLIENT_LOCALE");
OutputStream os = new FileOutputStream(dbpropfile);
props.store(os, "SuperX DB.properties");
os.close();
System.out.println("Änderung gespeichert in " + dbpropfile);
} catch (IOException e) {
String fehler = "Konnte db.properties-Datei nicht speichern:"
+ e.toString();
if (isGuiWanted)
JOptionPane.showMessageDialog(null, fehler, "DB-Prop Admin",
JOptionPane.WARNING_MESSAGE);
System.out.println(e.toString());
System.exit(1);
}
}
public static void main(String args[]) {
logLevels.add("SEVERE");
logLevels.add("WARNING");
logLevels.add("INFO");
logLevels.add("FINE");
logLevels.add("FINER");
logLevels.add("FINEST");
System.out.println("Umgebungsvariable LANG: "
+ System.getProperty("file.encoding"));
if (System.getProperty("file.encoding").toLowerCase().endsWith("utf8")) {
System.out
.println("Achtung: Ihre Default-Dateicodierung nutzt einen Zeichensatz (UTF8), der die Verschlüsselung nicht unterstützt.");
System.out
.println("Bitte setzen Sie Ihre Umgebungsvariable LANG auf eine deutsche LOCALE (z.B. de_DE@euro");
System.exit(1);
}
GetOpts.setOpts(args);
PropAdmin propAdmin = null;
if (GetOpts.isPresent("-dbproperties"))
dbpropfile = GetOpts.getValue("-dbproperties");
if (GetOpts.isPresent("-nogui") || GetOpts.isPresent("-no-gui")
|| GetOpts.isPresent("--no-gui")) {
isGuiWanted = false;
} else {
try {
// wenn keine graphische Umgebung verfügbar ist
// tritt ein Fehler auf.
JFrame f = new JFrame();
} catch (Throwable e) {
System.out
.println("Keine graphische Umgebung verfuegbar - starte Shell-Modus");
isGuiWanted = false;
}
if (isGuiWanted)
propAdmin = new PropAdmin();
}
try {
properties_einlesen();
if (isGuiWanted) {
initFormFromProps();
}
} catch (IOException e) {
String fehler = "Konnte db.properties-Datei nicht finden.\nEs wird eine neue in \n"
+ dbpropfile + " \nangelegt.";
if (isGuiWanted) {
JOptionPane.showMessageDialog(null, fehler, "DB-Prop Admin",
JOptionPane.INFORMATION_MESSAGE);
System.out.println(fehler);
tdriver.setText(defaultDBDriver);
tadminUser.setText(defaultUser);
turl.setText(defaultConnection);
} else {
System.out
.println("Sie koennen nur bestehende db.properties Dateien bearbeiten.");
System.out
.println("Geben Sie den Parameter -dbproperties:/home/superx/../db.properties an");
System.out.println(e);
System.exit(1);
}
}
if (isGuiWanted) {
propAdmin.show();
} else {
noGuiEdit();
}
}
private static void noGuiEdit() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out
.println("Die meisten Paramter koennen Sie mit dem vi bearbeiten.");
System.out
.print("Passwort für uneingeschränkten User (erscheint auf Bildschirm!): ");
String newAdminPassword = br.readLine();
System.out.println();
// System.out.println("you entered: " + pass);
if (testConnection(props.getProperty("connectionName"),
newAdminPassword)) {
System.out.print("Einen Moment - Verschlüsselung läuft ...");
props.setProperty("connectionPassword", "sx_des"
+ CryptUtils.encryptStringDES(newAdminPassword));
System.out.println(" OK");
saveProps();
}
if (props.getProperty("restrictedConnectionName") != null) {
System.out
.print("Passwort für eingeschraenkten User (erscheint auf Bildschirm!): ");
String newPassword = br.readLine();
System.out.println();
if (testConnection(props
.getProperty("restrictedConnectionName"), newPassword)) {
System.out
.print("Einen Moment - Verschlüsselung läuft ...");
props.setProperty("restrictedConnectionPassword", "sx_des"
+ CryptUtils.encryptStringDES(newPassword));
System.out.println(" OK");
saveProps();
}
}
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void itemStateChanged(ItemEvent e) {
String s = (String) e.getItem();
for (int i = 0; i < connTypes.length; i++) {
if (s.equals(connTypes[i][0])) {
tdriver.setText(connTypes[i][1]);
// if(turl.getText().indexOf( ">>") < 0)
turl.setText(connTypes[i][2]);
}
}
}
}