Skip to content

By Programmer For Programmer

Here I lay down the useful tips, tricks and utilities for programmers like myself.

Archive

Category: Code Snippets

Given a class that is mapped in Hibernate, you can get the table name that it is mapped to with the following piece of code.

import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.Joinable;

public String getTableName(SessionFactory sessionFactory, Class<?> mappedClass)
{
    ClassMetadata cmd = sessionFactory.getClassMetadata(mappedClass);

    //check that the class is mapped to something with a table name
    if (cmd == null || !Joinable.class.isInstance(cmd))
        return null;

    return Joinable.class.cast(cmd).getTableName();
}

Hey everyone,

I created a bash script to update an index created by SupoSE, keeping track of what revision was last successfully updated. I liked this better than their built-in scheduler because it has to continuously be running, and if for some reason our servers had to be restarted, it wouldn’t automatically start up again. Thus, I made a script to set up in cron to run hourly.

To use this script:

  • Create an index normally with SupoSE
  • Create a file to hold the last successfully indexed revision, and put this number in the file as the only text
  • Set the variables in the script below for your environment
  • Use bash 3.0 or higher, since I’m using regular expressions. If this isn’t an option, you may just need to rewrite the parsing of “svn info” to get the HEAD revision.

Enjoy!

#!/bin/sh

# Variables
SUPOSE_EXE="supose"
SUPOSE_LOC="/usr/local/supose/bin"
SVN_INDEX="/var/lib/supose/index/myindex"
SVN_URL="http://subversionurl/repo"
SVN_USER="username"
SVN_PASS="password"
LASTREV_FILE="/var/lib/supose/index/last.rev"

# Get the last successfully indexed revision
LASTREV=""
read -r LASTREV < "$LASTREV_FILE"
if [ "$LASTREV" = "" ]; then
        echo "Please specify the last successfully indexed revision in $LASTREV_FILE"
        exit 1;
fi

# Get the "to" revision
TOREV=""
SVNINFO_CMD="svn info $SVN_URL"
SVNINFO=`$SVNINFO_CMD`
if [[ "$SVNINFO" =~ 'Revision: ([0-9]+)' ]]; then
        TOREV=${BASH_REMATCH[1]}
fi
if [ "$TOREV" = "" ]; then
        echo "**** ERROR ****"
        echo "Could not read the HEAD revision number from command: $SVNINFO_CMD"
        echo "Has subversion been updated with a different format?"
        echo "Output from command:"
        echo $SVNINFO
        exit 1;
fi

# Print some output
echo "Updating subversion: $SVN_URL"
echo "Last successful revision: $LASTREV"
echo "HEAD revision: $TOREV"
echo "Index location: $SVN_INDEX"
echo "..."

if [ "$LASTREV" -ge "$TOREV" ]; then
        echo "No revisions to update... skipping process"
        exit 0;
fi

# Add one to to get the FROM revision
let FROMREV=$LASTREV+1
if [ "$FROMREV" = "" ]; then
        echo "Error adding one to last successfully indexed revision: $LASTREV"
        exit 1;
fi

# Run the SupoSE command
SUPOSE_CMD="./$SUPOSE_EXE scan \
                --url $SVN_URL \
                --username $SVN_USER \
                --password $SVN_PASS \
                --fromrev $FROMREV \
                --torev $TOREV \
                --index $SVN_INDEX"
pushd $SUPOSE_LOC
echo "Running $SUPOSE_CMD"
$SUPOSE_CMD
popd

# Was there an error running SupoSE?
SUPOSE_STATUS="$?"
if [ "$SUPOSE_STATUS" -gt "0" ]; then
        echo "**** Error ****"
        echo "$SUPOSE_EXE returned status code: $SUPOSE_STATUS (not successful)"
        echo "File $LASTREV_FILE will not be updated with latest revision"
        echo "Command that was run:"
        echo $SUPOSE_CMD
        exit $SUPOSE_STATUS;
fi

# If no error, update last.rev file
echo "**** Operation completed successfully ****"
echo "Updating $LASTREV_FILE with revision: $TOREV"
echo "$TOREV" > "$LASTREV_FILE"
exit 0