Thursday, July 29, 2010

Startup scripts

Sample startup file, place in /etc/init.d:

cat /etc/init.d/archiva
#! /bin/sh
# chkconfig: 345 90 10
# description: Archiva server

# uncoment to set JAVA_HOME as the value present when Continuum installed
export JAVA_HOME=/opt/SDK/jdk
export ARCHIVA=/opt/archiva/current/bin/archiva

case "$1" in

'start')
su - archiva -c "$ARCHIVA start"

;;

'stop')
su - archiva -c "$ARCHIVA stop"

;;

'restart')
su - archiva -c "$ARCHIVA stop"
sleep 20
su - archiva -c "$ARCHIVA start"

;;

*)
echo "Usage: $0 { start | stop | restart }"
exit 1
;;
esac

exit 0


To add it to startup, execute as root:
chkconfig --add archiva


Done.




Another example, using Geronimo:

[geronimo@mypc bin]$ ./geronimo.sh --help
Using GERONIMO_HOME: /home/geronimo/geronimo-tomcat6-javaee5-2.1.6
Using GERONIMO_TMPDIR: var/temp
Using JRE_HOME: /opt/SDK/jdk/jre
Usage: geronimo.sh command [geronimo_args]
commands:
debug Debug Geronimo in jdb debugger
jpda run Start Geronimo in foreground under JPDA debugger
jpda start Start Geronimo in background under JPDA debugger
run Start Geronimo in the foreground
start Start Geronimo in the background
stop Stop Geronimo
stop --force Stop Geronimo (followed by kill -KILL)




Create /etc/init.d/geronimo as follows:

#! /bin/sh
# chkconfig: 345 90 10
# description: geronimo server

# uncoment to set JAVA_HOME as the value present when Continuum installed
export JAVA_HOME=/opt/SDK/jdk
export GERONIMO=/opt/geronimo/current/bin/geronimo.sh

case "$1" in

'start')
su - geronimo -c "$GERONIMO start"

;;

'stop')
su - geronimo -c "$GERONIMO stop"

;;

'restart')
su - geronimo -c "$GERONIMO stop"
sleep 20
su - geronimo -c "$GERONIMO start"

;;

'debug')
su - geronimo -c "$GERONIMO debug"

;;

'jpdarun')
su - geronimo -c "$GERONIMO jpda run"

;;

'jpdastart')
su - geronimo -c "$GERONIMO jpda start"

;;

'jpdastop')
su - geronimo -c "$GERONIMO stop"

;;



*)
echo "Usage: $0 { start | stop | restart | debug | jpdarun | jpdastart | jpdastop }"
exit 1
;;

esac

exit 0



Test each entry:

/etc/init.d/geronimo
Usage: /etc/init.d/geronimo { start | stop | restart | debug | jpdarun | jpdastart | jpdastop }

/etc/init.d/geronimo start

/etc/init.d/geronimo stop
(login/pass = system / manager)

/etc/init.d/geronimo debug


# add to startup
chkconfig --add geronimo

# check that the file exists in the startup location, ex:
ls -la /etc/rc.d/init.d/geronimo

Friday, July 16, 2010

How to load properties files in Oracle's Weblogic 11G

PropertiesLoader propertiesLoader = new PropertiesLoader(propertiesFileName);
if (propertiesLoader.thePropertiesFileExists()) {
System.out.println("Properties file exists: " + propertiesFileName);
} else { ... }


String serverName = propertiesLoader.getProperty("serverName");


...


And the properties loader class:




package alan.lupsha.properties;

import org.apache.log4j.Logger;
import java.util.Properties;
import java.io.InputStream;

public class PropertiesLoader {
    private static final Logger logger = Logger.getLogger(PropertiesLoader.class);

    private Properties props = null;
    private String propertiesFileName = null;
    private boolean propertiesFileExists = false;

    public PropertiesLoader(String propertiesFileName) {
        this.propertiesFileName = propertiesFileName;
        try {
            props = new Properties();
            ClassLoader cl = this.getClass().getClassLoader();
            // does not work in 11G: java.net.URL url = cl.getResource(propertiesFileName);

            InputStream in = cl.getResourceAsStream( propertiesFileName );
            if( in != null )
            {
                props.load(in);
                setPropertiesFileExists(true);
            }
            else
            {
                logger.warn("InputStream is null while trying to load properties file: " + propertiesFileName );
            }
        } catch (Exception e) {
            logger.error("Error while loading properties from file "
                    + propertiesFileName + ". Error is: " + e.toString());
            System.out.println("Error while loading properties from file "
                    + propertiesFileName + ". Error is: " + e.toString());
        }
    }

    public String getProperty(String propertyName) {
        String returnStr = "";
        if (props == null) {
            logger.error("Sorry, your props file couldn't be loaded: " + propertiesFileName);
        } else {
            returnStr = props.getProperty(propertyName);
            if (returnStr == null) {
                returnStr = "";
            }
        }
        return returnStr;
    }

    public void setPropertiesFileExists(boolean propertiesFileExists) {
        this.propertiesFileExists = propertiesFileExists;
    }

    public boolean thePropertiesFileExists() {
        return propertiesFileExists;
    }
}