Showing posts with label tech. Show all posts
Showing posts with label tech. Show all posts

Wednesday, February 4, 2009

How to invoke maven to perform a jar library install

Invoke this installjar with:
sudo -u maven /home/maven/installjar $1 $2 $3 $4
Don't forget to add your user to the sudoers file.


> cat installjar
#!/bin/sh

# ==========================================
# by Alan Lupsha, 02/04/2009
# Installs a jar into the Maven2 repository
# ==========================================

GROUP=$1
ARTIFACT=$2
VERSION=$3
JARPATH=$4

MAVEN_DIR=/home/maven
export JAVA_HOME=/opt/SDK/jdk

checkUserArgs()
{
if [ "$GROUP" = "" ] ; then
syntax
fi
if [ "$ARTIFACT" = "" ] ; then
syntax
fi
if [ "$VERSION" = "" ] ; then
syntax
fi
if [ "$JARPATH" = "" ] ; then
syntax
fi

if [ -s "$JARPATH" ] ; then
echo "[ALAN] OK: file exists: $JARPATH"
else
echo "[ALAN] Error: file does not exist: $JARPATH"
echo "[ALAN] Make sure to upload the file there first, before executing the script"
RETURN_CODE=1
exit $RETURN_CODE
fi
}

syntax()
{
echo "[ALAN] ERROR: Please use the following syntax: ./installjar group artifact version path"
echo "[ALAN] "
echo "[ALAN] EXAMPLE: "
echo "[ALAN] To install jar file called servlet1.3.jar from your local directory, by placing it "
echo "[ALAN] in group basej2ee with the name servlet and version 1.3, do the following: "
echo "[ALAN] 1. FTP your jar into your directory, example: /home/smith_j/servlet1.3.jar"
echo "[ALAN] 2. Execute: ./installjar basej2ee servlet 1.3 ~/servlet1.3.jar "
echo "[ALAN] "
echo "[ALAN] 3. In your project's pom.xml file add the following dependency: "
echo "[ALAN] <dependency>"
echo "[ALAN] <groupId>basej2ee</groupId>"
echo "[ALAN] <artifactId>servlet</artifactId>"
echo "[ALAN] <version>1.3</version>"
echo "[ALAN] </dependency>"
echo "[ALAN] "
echo "[ALAN] Good luck."
RETURN_CODE=1
exit $RETURN_CODE
}

installFile()
{
/bin/mkdir $MAVEN_DIR/projects/
cd $MAVEN_DIR/projects/

COMMAND="/maven/maven-2.0.6/bin/mvn install:install-file -DgroupId=$GROUP -DartifactId=$ARTIFACT -Dversion=$VERSION -Dpackaging=jar -Dfile=$JARPATH"
echo "[ALAN] Executing command: "
echo "[ALAN] $COMMAND"

/maven/maven-2.0.6/bin/mvn install:install-file -DgroupId=$GROUP -DartifactId=$ARTIFACT -Dversion=$VERSION -Dpackaging=jar -Dfile=$JARPATH

echo "[ALAN] "
echo "[ALAN] Don't forget to add the following dependency in your project's pom.xml file:"
echo " <dependency>"
echo " <groupId>$GROUP</groupId>"
echo " <artifactId>$ARTIFACT</artifactId>"
echo " <version>$VERSION</version>"
echo " </dependency>"
echo "[ALAN] "
echo "[ALAN] If the install was successful, then you can remove your jar file from: $JARPATH "
}


checkUserArgs
installFile
echo "[ALAN] Finished! (questions, comments, concerns - email "

mySQL versus Oracle data types





Shell script to use Maven2 to build jar libraries

Invoke this bldjar with:
sudo -u maven /home/maven/bldjar $1 $2 $3 $4
Don't forget to add your user to the sudoers file.

> cat bldjar



#!/bin/sh
# ===================================================================
# This script builds a maven2 jar project, commits the new jar to CVS
# and optionally registers the new jar as a reusable library in maven.
# Alan Lupsha
# ====================================================================

# environment variables
export JAVA_HOME=/opt/SDK/jdk
export CVSROOT=/maven/cvsroot

PROJECT=$1
REGISTER_JAR=$2
GROUP=$3
ARTIFACT=$4
MAVEN_DIR=/home/maven
LAST_UPDATE="2009.02.04"
OK_TO_REGISTER_JAR="no"

check_user_arguments()
{
if [ "$PROJECT" = "" ] ; then
echo "[ALAN] You must pass the project name as an argument! ex: sudo -u maven /home/maven/bldjar my-abc"
PROJECT="noProjectSpecified"
RETURN_CODE=1
OK_TO_REGISTER_JAR="no"
exit $RETURN_CODE
fi

if [ "$REGISTER_JAR" = "registerjar" ] ; then
if [ "$GROUP" = "" ] ; then
echo "[ALAN] Error: invalid group. You asked to register the jar in group ->$GROUP<-"
echo "[ALAN] Syntax: bldjar registerjar GROUP ARTIFACT"
RETURN_CODE=1
OK_TO_REGISTER_JAR="no"
exit $RETURN_CODE
else
echo "[ALAN] Registering jar: group is ok: $GROUP"
fi
if [ "$ARTIFACT" = "" ] ; then
echo "[ALAN] Error: invalid artifact. You asked to register the jar using artifact ->$ARTIFACT<-"
echo "[ALAN] Syntax: bldjar registerjar GROUP ARTIFACT"
RETURN_CODE=1
OK_TO_REGISTER_JAR="no"
exit $RETURN_CODE
else
echo "[ALAN] Registering jar: artifact is ok: $ARTIFACT"
OK_TO_REGISTER_JAR="yes"
fi
else
REGISTER_JAR="dontregisterjar"
fi
echo "[ALAN] Your saved arguments are: $PROJECT $REGISTER_JAR $GROUP $ARTIFACT"
}


set_up_directories()
{
# set up the directories, copy source
cd $MAVEN_DIR
mkdir $MAVEN_DIR/projects/
cd $MAVEN_DIR/projects
cvs checkout $PROJECT
cd $MAVEN_DIR/projects/$PROJECT/
mkdir TEMP
mv src/* TEMP/
mkdir src/main/
mkdir src/main/java
mv TEMP/* src/main/java
}


perform_build()
{
# perform the build
/maven/maven-2.0.6/bin/mvn --offline clean package > $MAVEN_DIR/projects/$PROJECT/build.log
cat $MAVEN_DIR/projects/$PROJECT/build.log
cvs add -m 'your build log' build.log
cvs commit -m 'your build log' build.log
}


commit_the_jar()
{
# go into the target dir, there may or may NOT be something in there
mkdir $MAVEN_DIR/projects/$PROJECT/target/
cd $MAVEN_DIR/projects/$PROJECT/target/
JAR_NAME=`ls *.jar`
echo "[ALAN] Your jar file name is $JAR_NAME"

# just in case this doesn't exist
mkdir $MAVEN_DIR/projects/$PROJECT/dist/

# if the jar already existed, commit it
if [ -f "$MAVEN_DIR/projects/$PROJECT/dist/$JAR_NAME" ] ; then
rm $MAVEN_DIR/projects/$PROJECT/dist/$JAR_NAME
cp $MAVEN_DIR/projects/$PROJECT/target/$JAR_NAME $MAVEN_DIR/projects/$PROJECT/dist/
cd $MAVEN_DIR/projects/$PROJECT/dist/
cvs commit -m 'newly built' $JAR_NAME

# if the jar is brand new, add it to CVS and commit it
else
echo "[ALAN] Adding new file to CVS - first time ever"
cp $MAVEN_DIR/projects/$PROJECT/target/$JAR_NAME $MAVEN_DIR/projects/$PROJECT/dist/

cd $MAVEN_DIR/projects/$PROJECT/dist/
cvs add -m 'newly added' $JAR_NAME
cvs commit -m 'newly-added now committed by bldjar script' $JAR_NAME
fi
}

perform_cleanup()
{
# clean up
echo "[ALAN] Now cleaning up, removing $MAVEN_DIR/projects/$PROJECT/"
rm -Rf $MAVEN_DIR/projects/$PROJECT/

echo "[ALAN] Script version was last updated on: $LAST_UPDATE"
echo "[ALAN] Any questions, comments, concerns? Email me at: "
}

register_jar_as_library()
{
# check if we need to register the jar
if [ "$OK_TO_REGISTER_JAR" = "yes" ] ; then
echo "[ALAN] Now registering your jar as a dependency in the maven lib directory structure..."
extract_rvl_from_jar_name
/maven/maven-2.0.6/bin/mvn install:install-file -DgroupId=$GROUP -DartifactId=$ARTIFACT -Dversion=$RVL -Dpackaging=jar -Dfile=$MAVEN_DIR/projects/$PROJECT/dist/$JAR_NAME
echo "[ALAN] If the above registration was successful, don't forget to add this to your pom.xml:"
echo " <dependency>"
echo " <groupId>$GROUP</groupId>"
echo " <artifactId>$ARTIFACT</artifactId>"
echo " <version>$RVL</version>"
echo " </dependency>"
echo " "
else
echo "[ALAN] I'm not registering your jar as a library. Syntax for that is: ./bldjar YOUR-PROJECT registerjar GROUP ARTIFACT"
fi
}


extract_rvl_from_jar_name()
{
STR=$JAR_NAME
len=${#STR}
REV=""
for (( i=$len ; i>0 ; i-- ))
do
REV=$REV""${STR:$i-1:$i}
STR=${STR%${STR:$i-1:$i}}
done
#echo "Reversed string $REV"
#ex: raj.3.2.1-reganamytiruces-ped

NAME=`echo $REV | cut -f1 -d'-'`
#echo "$NAME"
#ex: raj.3.2.1

STR=$NAME
len=${#STR}
REV=""
for (( i=$len ; i>0 ; i-- ))
do
REV=$REV""${STR:$i-1:$i}
STR=${STR%${STR:$i-1:$i}}
done
#echo "Reversed string $REV"
#ex: 1.2.3.jar

FULLLEN=${#REV}
ENDPOS=FULLLEN-4
RVL=${REV:0:ENDPOS}
}


check_user_arguments
set_up_directories
perform_build
commit_the_jar
register_jar_as_library
perform_cleanup



Thursday, January 29, 2009

How to fix the jsr173 missing artifact

How to fix the jsr173 missing artifact problem during a Maven build:

==========================================================
[ERROR] BUILD ERROR
[INFO] -------------------
[INFO] Failed to resolve artifact.

Missing:
----------
1) javax.xml:jsr173:jar:1.0

Try downloading the file manually from:
http://ftpna2.bea.com/pub/downloads/jsr173.jar
==========================================================


1. Go to /tmp
cd /tmp

2. Download the 14MB zip file:
http://cvs.apache.org/dist/incubator/beehive/v1.0-beta/bin/apache-beehive-incubating-beta.zip

wget http://cvs.apache.org/dist/incubator/beehive/v1.0-beta/bin/apache-beehive-incubating-beta.zip

3. Unzip the file
unzip apache-beehive-incubating-beta.zip

3. After unzipping, make sure that the jsr jar exists:
ls -la apache-beehive-incubating-beta/lib/common/jsr173_1.0_api.jar

Example output:
-rw-r--r-- 1 maven maven 26396 Mar 25 2005 apache-beehive-incubating-beta/lib/common/jsr173_1.0_api.jar

4. As user maven, install the file (use the right path to your mvn executable):
/maven/maven-2.0.6/bin/mvn install:install-file -DgroupId=javax.xml -DartifactId=jsr173 -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true -Dfile=/tmp/apache-beehive-incubating-beta/lib/common/jsr173_1.0_api.jar

Example output:

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'install'.
[INFO] ----------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [install:install-file] (aggregator-style)
[INFO] ----------------------------------------------------------------------------
[INFO] [install:install-file]
[INFO] Installing /tmp/apache-beehive-incubating-beta/lib/common/jsr173_1.0_api.jar to /maven/lib/javax/xml/jsr173/1.0/jsr173-1.0.Jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Thu Jan 29 09:56:38 EST 2009
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
[maven@myserver tmp]$

5. rebuild the project

cd /path/to/project-directory
/maven/maven-2.0.6/bin/mvn --offline

...
[INFO] BUILD SUCCESSFUL
...

Done.

Tuesday, January 27, 2009

How to parse Apache log files

How to parse Apache log files using the cat, grep and awk command, in order to see what a particular person was browsing

To view the Apache access log file:
cat /var/log/apache/access_log

Log lines look as follows:
74.6.72.36 - - [01/Nov/2006:02:10:33 -0500] "GET /~lupsha/artificialintelligence/artificialintelligence.htm HTTP/1.0" 200 116 "-" "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)"

To only view log lines that contain "Nov/2006" and "lupsha" and Mary's ip of "141.213.178.207", and to save all the contents of the log in another file called mary.txt, use grep as follows:
cat /var/log/apache/access_log | grep Nov/2006 | grep lupsha | grep 141.213.178.207 > /tmp/lupsha/mary.txt

The contents of /tmp/lupsha/mary.txt are now:
141.213.178.207 - - [10/Nov/2006:22:47:13 -0500] "GET /~lupsha/personal/pictures/2000.08.Trip.to.Crete/x-rethimnon-artistic.jpg HTTP/1.1" 200 7996 "http://socr.uwindsor.ca/~lupsha/personal/pictures/2000.08.Trip.to.Crete/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; InfoPath.1)"
141.213.178.207 - - [10/Nov/2006:22:47:13 -0500] "GET /~lupsha/personal/pictures/2000.08.Trip.to.Crete/x-rethimnon-artistic-2.jpg HTTP/1.1" 200 5503 "http://socr.uwindsor.ca/~lupsha/personal/pictures/2000.08.Trip.to.Crete/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; InfoPath.1)"
... etc

Take a log line as the one above, for example:
141.213.178.207 - - [10/Nov/2006:22:47:13 -0500] "GET /~lupsha/personal/pictures/2000.08.Trip.to.Crete/x-rethimnon-artistic.jpg HTTP/1.1" 200 7996 "http://socr.uwindsor.ca/~lupsha/personal/pictures/2000.08.Trip.to.Crete/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; InfoPath.1)"

We wish to take the path
/~lupsha/personal/pictures/2000.08.Trip.to.Crete/x-rethimnon-artistic.jpg
and convert it to:
<a href ="http://socr.uwindsor.ca/~lupsha/personal/pictures/2000.08.Trip.to.Crete/x-rethimnon-artistic.jpg"> [10/Nov/2006:22:47:13 </a>
so that a user can click on the link with the date in order to see what files were browsed.

We use the awk command as follows:
awk '{print "<a href=\"http://socr.uwindsor.ca"$7"\">" $4 "</a>"}' mary.txt > mary.html
The awk command above takes argument 7 and 4 from the log line and concatenates it to a custom html string, creating a link for the user.

by Alan Lupsha

Monday, January 26, 2009

How to get an email when someone logs into a shell account

Add the following 2 lines at the bottom of the .bashrc file. Replace YOURUSERNAME with the shell login and replace YOUR@EMAIL.ADDRESS with a valid email address:


echo "Today is $(date) User is: `who | grep YOURUSERNAME | awk '{ print $6 }'`" > ~/lastuser.txt

mail -s 'user YOURUSERNAME logged into server XYZ' YOUR@EMAIL.ADDRESS < ~/lastuser.txt