HSAdmin Backend Domains, E-Mail, Datenbanken
Peter Hormanns
2013-04-03 d4457e3d53c76ea9677383460cfb73d74975cc03
close #102 Logging, Queue
3 files modified
135 ■■■■■ changed files
hsarback/src/de/hsadmin/core/qserv/CommandShell.java 24 ●●●●● patch | view | raw | blame | history
hsarback/src/de/hsadmin/core/qserv/QueueServer.java 108 ●●●● patch | view | raw | blame | history
hsarback/src/de/hsadmin/core/util/Config.java 3 ●●●● patch | view | raw | blame | history
hsarback/src/de/hsadmin/core/qserv/CommandShell.java
@@ -6,8 +6,13 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CommandShell {
    private static final Logger logger = Logger.getLogger("de.hsadmin.core.qserv");
    
    public static String execute(String command) throws ShellException {
        return execute(command, null);
@@ -23,10 +28,7 @@
                logCommand += "<<EOF\n" + stdInput + "EOF";
            }
            logCommand += "\n";
            // TODO logging
            System.out.println("--- shell command: ---");
            System.out.println(logCommand);
            logger.log(Level.INFO, logCommand);
            String[] cmdArray = { "/bin/bash", "-c", command };
            backend = Runtime.getRuntime().exec(cmdArray);
            if (stdInput != null) {
@@ -38,22 +40,12 @@
            }
            callOutput = readProcessStream(backend.getInputStream());
            exitCode = backend.waitFor();
            // TODO logging
            System.out.println("RET: " + exitCode);
            logger.log(Level.INFO, "Return-Code: " + exitCode);
            if (exitCode != 0) {
                String aErr = readProcessStream(backend.getErrorStream());
                // TODO logging
                System.out.println("ERR: " + aErr);
                System.out.println("--- done. ---");
                logger.log(Level.SEVERE, aErr);
                throw new ShellException(aErr);
            }
            // TODO logging
            System.out.println("--- done. ---");
        } catch (IOException e) {
            throw new ShellException(e);
        } catch (InterruptedException e) {
hsarback/src/de/hsadmin/core/qserv/QueueServer.java
@@ -2,9 +2,9 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
@@ -28,6 +28,7 @@
    
    private static final String VERSION_NO = "1.1";
    
    private Logger logger;
    private QueueConnection conn;
    private QueueSession queueSession;
    private String jmsStatusQueue;
@@ -35,57 +36,39 @@
    private String jmsUserName;
    private String jmsSystemQueue;
    private String jmsFactory;
    private String serviceEMail;
    /** Runs the QueueServer, using the arguments as ConnectionFactory
    /**
     * Runs the QueueServer, using the arguments as ConnectionFactory
     *  and Topic names. 
     */
    public static void main(String[] args) throws Exception {
        File propFile = new File(System.getProperty("user.dir"), "conf/qserv.properties");
        if (args.length == 1) {
            propFile = new File(args[0]);
        } else {
            if (args.length != 0) {
                throw new Exception(userHelp(propFile));
        }
        else if (args.length != 0) {
            throw new Exception(
                    "Wrong number of arguments.\n"
                            + "With no arguments '"
                            + propFile
                            + "' will be used as config file.\n"
                            + "Or give a properties file as single argument.\n\n"
                            + "Example config file:\n\n"
                            + "hsadmin.jms.factory=QueueCF\n"
                            + "hsadmin.jms.system-queue=hive-h01\n"
                            + "hsadmin.jms.status-queue=queue/hsadminStatus\n"
                            + "hsadmin.jms.username=hive-h01\n"
                            + "hsadmin.jms.password=geheimeskennwort\n");
        }
        FileInputStream propStream = new FileInputStream(propFile);
        Properties props = new Properties(System.getProperties());
        props.load(propStream);
        propStream.close();
        String stdout = System.getProperty("hsadmin.stdout");
        if (stdout != null && stdout.length() > 0)
            System.setOut(new PrintStream(new FileOutputStream(stdout)));
        String stderr = System.getProperty("hsadmin.stderr");
        if (stderr != null && stderr.length() > 0)
            System.setErr(new PrintStream(new FileOutputStream(stderr)));
        System.setProperty("java.util.logging.config.file", props.getProperty("hsadmin.log.file", System.getProperty("user.home") + "/.hsadmin.log.properties"));
        final QueueServer qServ = new QueueServer();
        qServ.setJmsFactory(props.getProperty("hsadmin.jms.factory"));
        qServ.setJmsSystemQueue(props.getProperty("hsadmin.jms.system-queue"));
        qServ.setJmsStatusQueue(props.getProperty("hsadmin.jms.status-queue"));
        qServ.setJmsUserName(props.getProperty("hsadmin.jms.username"));
        qServ.setJmsPassWord(props.getProperty("hsadmin.jms.password"));
        qServ.setServiceEMail(props.getProperty("hsadmin.email.service"));
        System.out.println("==================================================");
        System.out.println("hsadmin-qserv " + VERSION_NO + " started using:");
        System.out.println("queue server: " + props.getProperty("hsadmin.jms.factory"));
        System.out.println("system queue: " + props.getProperty("hsadmin.jms.system-queue"));
        System.out.println("status queue: " + props.getProperty("hsadmin.jms.status-queue"));
        System.out.println("queue user:   " + props.getProperty("hsadmin.jms.username"));
        System.out.println("==================================================");
        qServ.setServiceEMail(props.getProperty("hsadmin.log.email"));
        Logger logger = Logger.getLogger("de.hsadmin.core.qserv");
        logger.log(Level.CONFIG, "hsadmin-qserv " + VERSION_NO + " started using:"
         + "\nqueue server: " + props.getProperty("hsadmin.jms.factory")
         + "\nsystem queue: " + props.getProperty("hsadmin.jms.system-queue")
         + "\nstatus queue: " + props.getProperty("hsadmin.jms.status-queue")
         + "\nqueue user:   " + props.getProperty("hsadmin.jms.username"));
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
@@ -100,11 +83,29 @@
        }
    }
    private void setServiceEMail(String property) {
        if (property == null) {
    private static String userHelp(File propFile) {
        return "Wrong number of arguments.\n"
                + "With no arguments '"
                + propFile
                + "' will be used as config file.\n"
                + "Or give a properties file as single argument.\n\n"
                + "Example config file:\n\n"
                + "hsadmin.jms.factory=QueueCF\n"
                + "hsadmin.jms.system-queue=hive-h01\n"
                + "hsadmin.jms.status-queue=queue/hsadminStatus\n"
                + "hsadmin.jms.username=hive-h01\n"
                + "hsadmin.jms.password=geheimeskennwort\n";
    }
    public QueueServer() {
        logger = Logger.getLogger("de.hsadmin.core.qserv");
    }
    private void setServiceEMail(String emailAddress) {
        if (emailAddress == null || emailAddress.isEmpty()) {
            serviceEMail = "peter@ostwall195.de";
        } else {
            serviceEMail = property;
            serviceEMail = emailAddress;
        }
    }
@@ -123,26 +124,24 @@
            receiver.setMessageListener(this);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            logger.log(Level.WARNING, e.getMessage(), e);
            return false;
        }
    }
    public synchronized void onMessage(Message jmsMessage) {
        // TODO logging
        System.out.println("\nonMessage(" + jmsMessage);
        logger.log(Level.INFO, jmsMessage.toString());
        QueueTask task = null;
        try {
            ObjectMessage jmsObjectMessage = (ObjectMessage) jmsMessage;
            task = (QueueTask) jmsObjectMessage.getObject();
            Processor processor = task.getProcessor();
            System.out.println("processing (" + task.getTitle() + " | started("
            logger.log(Level.INFO, "processing (" + task.getTitle() + " | started("
                    + task.getStarted() + ") |" + task.getDetails() + "|"
                    + processor + ")");
            System.out.println("with " + processor);
            try {
                processor.process();
                System.out.println("done");
                logger.log(Level.INFO, "done");
            } catch (ProcessorException e) {
                logException(e);
                task.setException(e);
@@ -158,19 +157,16 @@
    }
    private void logException(Throwable t) {
        System.err.println("exception " + t);     // TODO: logging
        t.printStackTrace(System.err);
        if (t.getCause() != null) {
            System.err.println("caused by exception "
                    + t.getCause());             // TODO: logging
            t.getCause().printStackTrace(System.err);
        } else
            System.err.println("no further cause available");
        logger.log(Level.SEVERE, t.getMessage(), t);
        Throwable cause = t.getCause();
        if (cause != null) {
            logger.log(Level.SEVERE, "cause: " + t.getMessage(), t);
        }
    }
    @Override
    public void onException(JMSException e) {
        System.out.println("Exception: " + e.getMessage());
        logger.log(Level.WARNING, e.getMessage(), e);
        close();
        while (!connect()) {
            try {
@@ -197,22 +193,18 @@
        Session statusSession = null;
        Connection statusConnection = null;
        try {
            System.out.println("sendStatus( " + queueMessage + ")");
            logger.log(Level.INFO, "sendStatus(" + queueMessage + ")");
            Context ctx = new InitialContext();
            System.out.println("Created InitialContext");
            ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(jmsFactory);
            System.out.println("connectionFactory= " + connectionFactory.toString());
            Destination queue = (Destination) ctx.lookup(jmsStatusQueue);
            statusConnection = connectionFactory.createConnection(jmsUserName, jmsPassWord);
            statusSession = statusConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = statusSession.createProducer(queue);
            ObjectMessage statusMessage = statusSession.createObjectMessage(queueMessage);
            System.out.println("send( " + statusMessage + ")");
            logger.log(Level.INFO, "send(" + statusMessage + ")");
            producer.send(statusMessage);
        } catch (Exception statusException) {
            // TODO: log exception
            System.out.println("failed");
            statusException.printStackTrace(System.err);
            logger.log(Level.SEVERE, statusException.getMessage(), statusException);
        } finally {
            // close JMS
            try { producer.close(); } catch (Exception exc) { }
hsarback/src/de/hsadmin/core/util/Config.java
@@ -43,7 +43,8 @@
    }
    
    public String getProperty(String propertyName) {
        return props.getProperty(propertyName).trim();
        String property = props.getProperty(propertyName);
        return property;
    }
    
    public String getProperty(String propertyName, String defaultValue) {