IDMLlib für Java mit PHP

Go to the english version of this article.

Da wir zur Zeit einige Anfragen von PHP-Entwicklern bezüglich der Arbeit mit IDML, der IDMLib und PHP bekommen, haben wir einen Blick auf das PHP/Java Bridge Projekt bei Sourceforge geworfen, und es scheint zu funktionieren. Wir werden mit ein paar komplexeren Beispielen testen, wie stabil alles läuft.

Hier ist mein Vorschlag, die IDMLlib für Java mit Apache 2, Tomcat 6, Java 6 und PHP 5 zum Laufen zu bringen.

Um die IDMLlib für Java mit PHP nutzen zu können, benötigen Sie folgende Komponenten:

  • Die IDMLlib für Java, die Sie über http://idmllib.com beziehen können
  • Ein laufendes Apache 2 und PHP 5 Setup mit einer Tomcat 6 Installation
  • Und natürlich die PHP/Java Bridge

Für diese Anleitung werde ich ein Debian 5 Lenny Setup nutzen. Aber Sie können ebenso die Konfigurationshinweise für beispielsweise Windows, Mac OS X, Redhat Linux, Suse Linux oder wo auch immer Java, Apache 2, Tomcat 6 und PHP laufen, nutzen.

1 Installation Debian Linux

Installieren Sie eine einfache debian Lenny Distribution.

Stellen Sie sicher, dass Sie ‘non-free‘ in Ihren apt.sources Liste stehen haben.

deb http://debian.uni-essen.de/debian/ lenny main non-free
deb-src http://debian.uni-essen.de/debian/ lenny main non-free

deb http://security.debian.org/ lenny/updates main contrib non-free
deb-src http://security.debian.org/ lenny/updates main contrib non-free

2 Installation Apache 2 and PHP 5

 apt-get install apache2 php5 libapache2-mod-php5 php5-cli php5-common php5-cgi

3 Installation Tomcat 6

Da es kein Tomcat 6 debian Packet gibt, installieren wir Tomcat 6 von Hand.

apt-get install sun-java6-jdk
echo 'JAVA_HOME="/usr/lib/jvm/java-6-sun"' | tee -a /etc/environment

Nach ‘wget’ die URL zur letzten Tomcat Version einfügen!

wget http://tomcat.apache.org/download-60.cgi
tar xfvz apache-tomcat-6.0.24.tar.gz
mkdir /opt/tomcat6
groupadd -g 109 tomcat6
useradd -b /opt/tomcat6 -g tomcat6 -s /bin/false -u 109 tomcat6

Jakarta Commons Daemon installieren.

apt-get install jsvc

Ein Initscript erstellen, um Tomcat 6 zu starten und zu stoppen.

/etc/init.d/tomcat6

#!/bin/sh
#
# /etc/init.d/tomcat6 -- startup script for the Tomcat 5 servlet engine
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian GNU/Linux by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Modified for Tomcat by Stefan Gybas <sgybas@debian.org>.
# Modified for Tomcat 6 by ohaeuser - 16.02.2009
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Tomcat.
# Description:       Start the Tomcat servlet engine.
### END INIT INFO

set -e

PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=tomcat6
DESC="Tomcat servlet engine"
DAEMON=/usr/bin/jsvc
CATALINA_HOME=/opt/$NAME
DEFAULT=/etc/default/$NAME

if [ `id -u` -ne 0 ]; then
        echo "You need root privileges to run this script"
        exit 1
fi

# Make sure tomcat is started with system locale
if [ -r /etc/default/locale ]; then
        . /etc/default/locale
        export LANG
fi

. /lib/lsb/init-functions
. /etc/default/rcS

# The following variables can be overwritten in $DEFAULT

# Run Tomcat 5 as this user ID
TOMCAT6_USER=tomcat6

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/java-6-sun /usr/lib/jvm/java-1.5.0-sun /usr/lib/j2sdk1.5-sun /usr/lib/j2sdk1.5-ibm /usr/lib/j2sdk1.4-sun /usr/lib/j2sdk1.4-blackdown
/usr/lib/j2se/1.4 /usr/lib/j2sdk1.4-ibm /usr/lib/j2sdk1.3-sun /usr/lib/j2sdk1.3-blackdown /usr/lib/jvm/java-gcj /usr/lib/kaffe"

# Look for the right JVM to use
for jdir in $JDK_DIRS; do
    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
        JAVA_HOME_TMP="$jdir"
        # checks for a real JDK like environment, needed to check if
        # really the java-gcj-compat-dev package is installed
        if [ -r "$jdir/bin/jdb" ]; then
            JAVA_HOME="$JAVA_HOME_TMP"
        fi
    fi
done
export JAVA_HOME

# Directory for per-instance configuration files and webapps
CATALINA_BASE=/opt/$NAME

# Use the Java security manager? (yes/no)
TOMCAT6_SECURITY=yes

# Timeout in seconds for the shutdown of all webapps
TOMCAT6_SHUTDOWN=30

# End of variables that can be overwritten in $DEFAULT

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
        . "$DEFAULT"
fi

test -f $DAEMON || exit 0

[ -z "$TOMCAT6_USER" ] && TOMCAT6_USER=tomcat6

# Check if we use gij
gij=no
"$JAVA_HOME/bin/java" -version 2>&1 | grep -q "^gij (GNU libgcj)" && gij=yes

# gij doesn't support a security manager yet (see bug #399595)
if [ "$gij" = "yes" ]; then
    printf "The java-gcj-compat-dev environment currently doesn't\nsupport a security manager. See README.Debian." | logger -p daemon.warning -t $NAME
fi

# Set java.awt.headless=true if JAVA_OPTS is not set so the
# Xalan XSL transformer can work without X11 display on JDK 1.4+
# It also looks like the default heap size of 64M is not enough for most cases
# se the maximum heap size is set to 128M
if [ -z "$JAVA_OPTS" ]; then
        JAVA_OPTS="-Djava.awt.headless=true -Xmx128M"
fi

JAVA_OPTS="$JAVA_OPTS -Djava.endorsed.dirs=$CATALINA_HOME/common/endorsed -Dcatalina.base=$CATALINA_BASE -Dcatalina.home=$CATALINA_HOME
-Djava.io.tmpdir=$CATALINA_BASE/temp"

# Set the JSP compiler if set in the tomcat6.default file
if [ -n "$JSP_COMPILER" ]; then
        JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=$JSP_COMPILER"
fi

if [ "$TOMCAT6_SECURITY" = "yes" ]; then
        JAVA_OPTS="$JAVA_OPTS -Djava.security.manager -Djava.security.policy=$CATALINA_BASE/conf/catalina.policy"
fi

# juli LogManager disabled if running under gij (see bug #395167)
if [ "$gij" != "yes" ]; then
        JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
                   -Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
fi

# Define other required variables
CATALINA_PID="/var/run/$NAME.pid"
BOOTSTRAP_CLASS=org.apache.catalina.startup.Bootstrap
JSVC_CLASSPATH="/usr/share/java/commons-daemon.jar:$CATALINA_HOME/bin/bootstrap.jar"

# Look for Java Secure Sockets Extension (JSSE) JARs
if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then
    JSSE_HOME="${JAVA_HOME}/jre/"
fi
export JSSE_HOME

echo $JSSE_HOME

case "$1" in
  start)
        if [ -z "$JAVA_HOME" ]; then
                log_failure_msg "no JDK found - please set JAVA_HOME"
                exit 1
        fi

        if [ ! -d "$CATALINA_BASE/conf" ]; then
                log_failure_msg "invalid CATALINA_BASE specified"
                exit 1
        fi

 log_daemon_msg "Starting $DESC" "$NAME"
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                --user $TOMCAT6_USER --startas "$JAVA_HOME/bin/java" \
                >/dev/null; then

                # Create catalina.policy (for the security manager)
                rm -f "$CATALINA_BASE/conf/catalina.policy"
                umask 022
                echo "// AUTO-GENERATED FILE -- DO NOT EDIT!" \
                        > "$CATALINA_BASE/conf/catalina.policy"
                echo "// Edit the files in /opt/tomcat6/conf/policy.d/ instead" \
                        >> "$CATALINA_BASE/conf/catalina.policy"
                echo ""  >> "$CATALINA_BASE/conf/catalina.policy"
                cat /opt/tomcat6/conf/policy.d/*.policy \
                        >> "$CATALINA_BASE/conf/catalina.policy"

                # Clean up and set permissions on required files
                find "$CATALINA_BASE"/temp/ -mindepth 1 -exec rm -fr {} +
                REQUIRED_FILES='"$CATALINA_BASE/conf"
                        "$CATALINA_BASE/logs" "$CATALINA_BASE/temp"
                        "$CATALINA_BASE/webapps" "$CATALINA_BASE/work"'
                if [ -e "$CATALINA_BASE/conf/tomcat-users.xml" ]; then
                        REQUIRED_FILES=$REQUIRED_FILES'
                                "$CATALINA_BASE/conf/tomcat-users.xml"'
                fi
                eval chown --dereference "$TOMCAT6_USER" $REQUIRED_FILES \
                        || true

                cd "$CATALINA_BASE/temp"

                $DAEMON -user "$TOMCAT6_USER" -cp "$JSVC_CLASSPATH" \
                    -outfile SYSLOG -errfile SYSLOG \
                    -pidfile "$CATALINA_PID" $JAVA_OPTS "$BOOTSTRAP_CLASS"

else
                log_progress_msg "(already running)"
        fi
        log_end_msg 0
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                --user "$TOMCAT6_USER" --startas "$JAVA_HOME/bin/java" \
                >/dev/null; then
                log_progress_msg "(not running)"
        else
                $DAEMON -cp "$JSVC_CLASSPATH" -pidfile "$CATALINA_PID" \
                     -stop "$BOOTSTRAP_CLASS"
        fi
        log_end_msg 0
        ;;
   status)
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                --user $TOMCAT6_USER --startas "$JAVA_HOME/bin/java" \
                >/dev/null; then

                if [ -f "$CATALINA_PID" ]; then
                    log_success_msg "$DESC is not running, but pid file exists."
                else
                    log_success_msg "$DESC is not running."
                fi
        else
                log_success_msg "$DESC is running with pid `cat $CATALINA_PID`"
        fi
        ;;

restart|force-reload)
        if start-stop-daemon --test --stop --pidfile "$CATALINA_PID" \
                --user $TOMCAT6_USER --startas "$JAVA_HOME/bin/java" \
                >/dev/null; then
                $0 stop
                sleep 1
        fi
        $0 start
        ;;
  try-restart)
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
                --user $TOMCAT6_USER --startas "$JAVA_HOME/bin/java" \
                >/dev/null; then
                $0 start
        fi
        ;;
  *)
        log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
        exit 1
        ;;
esac

exit 0

Grundlegende Einstellungen unter /etc/defaults/tomcat6:

# Modified for tomcat6 16.02.2009 - ohaeuser
# Run Tomcat as this user ID. Not setting this or leaving it blank will use the
# default of tomcat6.
#TOMCAT6_USER=tomcat6

# The home directory of the Java development kit (JDK). You need at least
# JDK version 1.4. If JAVA_HOME is not set, some common directories for
# the Sun JDK, various J2SE 1.4 versions, and the free runtimes
# java-gcj-compat-dev and kaffe are tried.
JAVA_HOME=/usr/lib/jvm/java-6-sun

# Directory for per-instance configuration files and webapps. It contain the
# directories conf, logs, webapps, work and temp. See RUNNING.txt for details.
# Default: /var/lib/tomcat5.5
CATALINA_BASE=/opt/tomcat6

# Arguments to pass to the Java virtual machine (JVM).
#JAVA_OPTS="-Djava.awt.headless=true -Xmx128M"
JAVA_OPTS="-Djava.awt.headless=true -Xms256m -Xmx512m -XX:MaxPermSize=256m -Dlog.home=$CATALINA_BASE -Dsolr.solr.home=/home/solr -Dcom.idmllib.license=ILJD10-xxxx-xxxx-xxxx-xxxx"

# Java compiler to use for translating JavaServer Pages (JSPs). You can use all
# compilers that are accepted by Ant's build.compiler property.
#JSP_COMPILER=jikes

# Use the Java security manager? (yes/no, default: yes)
# WARNING: Do not disable the security manager unless you understand
# the consequences!
# NOTE: java-gcj-compat-dev currently doesn't support a security
# manager.
TOMCAT6_SECURITY=no
#Export for AWT
#export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/

Die wichtigste Zeile dabei ist folgende:

JAVA_OPTS=”-Djava.awt.headless=true -Xms256m -Xmx512m -XX:MaxPermSize=256m -Dlog.home=$CATALINA_BASE -Dsolr.solr.home=/home/solr -Dcom.idmllib.license=ILJD10-xxxx-xxxx-xxxx-xxxx”

Mit dieser werden Speichereinstellungen festgelegt, außerdem setzt sie den IDMLlib Lizenzcode.

-Dcom.idmllib.license=ILJD10-xxxx-xxxx-xxxx-xxxx

Sie benötigen einen gültigen Lizenzkey, den Sie unter http://idmllib.com bekommen können.

4 Überprüfen, ob das grundlegende Setup läuft

Erstellen Sie eine info.php Datei in Ihrem Apache 2 web root…

 <?php phpinfo() ?>

… und testen Sie, ob Apache 2 und PHP laufen: http://server_ip/index.php

Wenn Sie die PHP Informationsseite sehen, sollte alles funktionieren!

phpinfo Apache2 status page

Nun testen Sie, ob der Tomcat 6 installiert ist: http://server_ip:8080/

Wenn Sie die Standard Tomcat Statusseite sehen, können wir damit anfangen, die PHP/Java Bridge aufzusetzen.

5 PHP/Java Bridge Templatedatei erstellen

Dies ist die Hauptseite des PHP/Java Bridge Projektes: http://php-java-bridge.sourceforge.net/

Sie benötigen eine JavaBridgeTemplate.war Datei: http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.1.2.1/JavaBridgeTemplate6121.war/download

Benennen Sie diese Datei um in IDML.war und kopieren die diese in Ihren Webapps Ordner. Tomcat sollte es so automatisch nutzen können.

Kopieren Sie die idmllib.jar Datei nach /opt/tomcat6/webapps/IDML/WEB-INF/lib

olitest:/root/# ls -l /opt/tomcat6/webapps/IDML/WEB-INF/lib/
total 4040
-rw-r--r-- 1 root    root    3554086 2010-05-20 11:34 idmllib-1.1.106.jar
-rw-r--r-- 1 tomcat6 tomcat6  405003 2010-05-02 13:22 JavaBridge.jar
-rw-r--r-- 1 tomcat6 tomcat6   41347 2010-05-02 13:22 php-script.jar
-rw-r--r-- 1 tomcat6 tomcat6  114991 2010-05-02 13:22 php-servlet.jar

6 Konfiguration Apache und PHP

URL_FOPEN auf ‘On’ stellen.

;Whether to allow the treatment of URLs (like http:// or ftp://) as files.
allow_url_fopen = On
; Whether to allow include/require to open URLs (like http:// or ftp://) as files.
allow_url_include = On

Sie können Ihr Setup mit suhosin schützen, so dass URL_FOPEN auf eine spezifische Adresse beschränkt ist:

suhosin.executor.include.whitelist=http://localhost:8080

Besuchen Sie http://www.hardened-php.net/suhosin/configuration.html für weitere interessanten Einstellungen.

7 Testen Sie die IDMLlib

Wenn Sie alle vorhergehenden Schritte ausgeführt haben, starten Sie Adobe InDesign, nehmen Sie irgendein InDesign Dokument und exportieren Sie es über Datei/Export als IDML Datei. Sie können auch ein neues InDesign Dokument erstellen, allerdings benötigen Sie zumindest eine Textbox mit Inhalt.

Laden Sie die IDML Datei in ihren Webordner und steuern Sie es mit einem kleinen PHP Script an:

<?php
require_once("http://localhost:8080/IDML/java/Java.inc");

echo "This is a Test - this is PHP";

$idml = new java("de.fhcon.idmllib.api.elements.Idml", "/var/www/java/text.idml");
$document = $idml->getDocument();
$cmykProfile = $document->getCMYKProfile();
$rgbProfile = $document->getRGBProfile();

echo "<br>This is the cmyk Profile: ".$cmykProfile;
echo "<br>And the RGB Profile: ".$rgbProfile;

$story = $idml->getAbstractDocument()->getStoryById("uec");
$content  = java("de.fhcon.idmllib.api.util.StoryUtil")->getContent($story);
echo "<br>This is the Content from the Textbox: <br>".$content;
echo "<br>";
?>

Achtung: Sie müssen einen Pfad für die IDML Datei zur Verfügung stellen. Außerdem fragen wir nicht alle Stories ab, sondern “schummeln” ein wenig und stellen nur die korrekte Story ID aus der IDML Datei zur Verfügung. Entpacken Sie die IDML Datei und navigieren Sie zum Stories Ordner. Natürlich gibt es auch  einige nützliche Funktionen in der IDMLlib, um die Story IDs aus den Spreads zu bekommen und über sie zu iterieren.

olitest:/var/www/java/test# ls -l
total 84
-rw-r--r-- 1 root root 24210 2010-01-22 15:50 designmap.xml
drwxr-xr-x 2 root root  4096 2010-05-20 13:46 MasterSpreads
-rw-r--r-- 1 root root 30060 2010-05-20 11:51 mein.zip
drwxr-xr-x 2 root root  4096 2010-05-20 13:46 META-INF
-rw-r--r-- 1 root root    43 2010-01-22 15:50 mimetype
drwxr-xr-x 2 root root  4096 2010-05-20 13:46 Resources
drwxr-xr-x 2 root root  4096 2010-05-20 13:46 Spreads
drwxr-xr-x 2 root root  4096 2010-05-20 13:46 Stories
drwxr-xr-x 2 root root  4096 2010-05-20 13:46 XML
olitest:/var/www/java/test# ls -l Stories/
total 4
-rw-r--r-- 1 root root 924 2010-01-22 15:50 Story_uec.xml

Demnächst werden wir Ihnen zeigen, wie Sie Story Inhalte modifizieren und in eine IDML Datei zurückschreiben können.

Tags: , ,

Hinterlasse eine Antwort