Sunday, April 27, 2008

check if a website is responding script

recently i needed to check if a website was responding, and if not, page myself (by sending an email to my pager). Here is the unix shell script i used to make this happen.

Required programs:
wget
working mail (i use xmail on solaris 8 in this script)

script:

#!/bin/sh
## Script will check if the "host" is up, if the host is down, send an email
## You should cron it every 5 mins or so.

#uncomment to debug:
#set -x

## change these:
host="http://dogself.com"
email="my-pager-number@skytel.com,my-real-email@gmail.com"

## locations of stuff:
mailx="/usr/bin/mailx"
wget="/usr/bin/wget"
log="/path/to/a/writable/log/file.log"

now=`date '+%m/%d/%Y %H:%M:%S'`
rm ${log}
#when checking connection, do 2 tries, and time out in 7 seconds
${wget} -O /dev/null -t 2 -T 7 ${host} -o ${log}
grep "saved \[" ${log}
if [ $? = "1" ];
then
echo "site:[${host}] is down"
${mailx} -s "PRODUCTION is DOWN at ${now}" ${email} < ${log}
else
echo "site:[${host}] is up"
fi

appfuse 1.8.1 ehcache NoSuchMethodError

Problem:
I installed appfuse-light-all 1.8.1, installed acegi security, and ajax. After doing a build i get the following error when Spring starts up:

Error creating bean with name 'userManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]
.... bunch of other errors ...
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: net.sf.ehcache.Cache.<init>

Solution:
The problem here is that i am using an older version of ehcache for some reason. Doing a bit more digging shows that i have the following versions of ehcache in my M2 repository:
[M2_REPO]/net/sf/ehcache/ehcache/1.2.3/
[M2_REPO]/net/sf/ehcache/ehcache/1.3.0/

version 1.2.3 is what is messing things up for me, it gets loaded by Spring first and causes the error. Deleting that directory does not work because "ant deploy" does the following if the dependency is missing:

Buildfile: build.xml
[artifact:dependencies] Downloading: net/sf/ehcache/ehcache/1.2.3/ehcache-1.2.3.jar
[artifact:dependencies] Downloading: net/sf/ehcache/ehcache/1.2.3/ehcache-1.2.3.jar

Now, since i dont know how to use MAVEN, i cant remove the code that fetches the old version, so i do the evil hack to prevent it from being created:

remove the directory "1.2.3", make a copy of "1.3.0" directory, call it "1.2.3". rename all the files inside to say 1.3.0 instead of 1.2.3.

Also, if you are deploying to tomcat as a directory, you might need to go into the webapps directory in tomcat, locate your app, and its WEB-INF and delete ehcache-1.2.3.jar thats is probably there now.

Now you can use appfuse.

if anyone knows of a better way to solve this, leave a comment.

Friday, April 25, 2008

firefox: tab and enter do not work

I have been effected by the evil firefox bug which causes tab and enter to stop working. As of this writing there is no fix for this.

Here is what i have found out about living with this bug and other info:

Workarounds:
1. shift-enter works instead of enter. No replacement for tab exists.
2. opening a new windows or restarting firefox fixes this problem for a while
- people suggested using session fix addon to remember your tabs while you restart firefox

other info:
1. people claim its a firebug issue or a web developer issue. i have both of those installed, by the way
2. there is a firefox bug report on this here
3. some posts on message boards which might eventually find a fix for this problem are:
http://www.nabble.com/Enter-and-Tab-keys-stop-working-td14302706i20.html
http://support.mozilla.com/tiki-view_forum_thread.php?comments_parentId=38626&forumId=1


at this point the best solution seems to use that session fix plugin, or maybe do a reinstall.

shell scripting with /bin/sh

I found this half decent tutorial / how-to on shell scripting with /bin/sh which i do lots but suck at.

here it is:
http://ooblick.com/text/sh/

also, to check if a file DOES NOT exist:

if [ ! -e /path/to/file ];
then
# do something
fi

ok i am done!

Tuesday, April 15, 2008

common regex cookbook

I see nice regexes on the java forums from time to time, i am going to add them all here for use!

Thursday, April 10, 2008

Escape cp-1252 chars in HTML

This is for when people paste crap from MS Word into html and add all those funky characters that looks horrible.

My friend gave me this perl one liner (ok, you can use it as a one liner if you wanted to) to escape the the evil CP-1252 characters in your HTML.

Notes for porting:
ord($1) converts capture group 1 to ASCII.
assumes $str has your html

Bonus!
the above regex replaces control chars!

Wednesday, April 09, 2008

Teamsite DCR java parser


Working with teamsite DCRs is painful. This is my attempt to make it a little bit less painful.

I made this program in order to make it easier to edit and parse teamsite data capture records. I should have done this a long time ago, and actually this is the first step towards freeing myself from OpenDeploy.

Anyway, here is the code for the parser:



Note: The code above does not yet truly support replicants, as in, you cant say 'give me the 3rd text field from the 2nd replicant..' unless you can figure out the xpath for it, which somewhat defeats the purpose of having a DCR parser.


Here is a driver class that uses my parser in order to do something silly.



in order to use this parser you will need the following libs:
- JDOM
- JAXEN

Thursday, March 13, 2008

email regex that doesnt suck

why is it so hard to find a good email java regex on google? They all suck.

Here are a few regexes that i would use in a production system to check if an email is valid:

"([\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+\\w{2,7})\\b"


Something a little more light weight:

"[^\\s]+@[^\\s]+([.]\\w{2,3})+"


and if all else fails, this might be useful:
Apache Commons EmailValidator class API

Friday, March 07, 2008

Path does not start with a "/" character

Problem:
I get this error when using the tiles plugin when building out some tiles:

java.lang.IllegalArgumentException: Path .some.tiles.path does not start with a "/" character

first, lets pretend that the tile in question looks like this:
<definition name=".some.tiles.path" extends=".some.other.tiles.path"> ..some stuff.. </definition>

Solution:
ive noticed that the reason for this error can be one of the following:

1. if you look at the tile that this tile extends, and what that tile extends, until you find the tile that extends nothing, that tile should have a definition like this:
<definition name=".some.base.tile" path="/a/real/path/to/some/jsp/somejsp.jsp"> ..some stuff.. </definition>
the key thing, is that the path of the parent tile starts with a "/" character

2. if #1 is not your problem, then you might have a duplicate definition of the tile name which the error talks about.

3. Another thing that can happen is that your tiles path that you are referencing in struts-config.xml does not exist at all, because maybe you misspelled it, in which case that same error is thrown.

Friday, February 22, 2008

B. Fleischmann - Gain Lyrics

I have looked everywhere for the lyrics of the song "Gain" by B. Fleischmann which is found here on you tube by the way. They dont seem to exist on teh intarwebs.

Anyway, i have transcribed them as best as i can, if anyone thinks i got something wrong, please leave a comment.


B. Fleischmann - Gain Lyrics

lets face another day
lets see another fence
lets have another crash
lets get another chance

where all reason ends, i put my heart
where foresight ends, i want to start
as long as i haven't tried to gain
i will not stop, will not complain

lets give it one more try
don't want to make you cry
how else can we survive
unless we dry our eyes try our lives

where all reason ends, i put my heart
where foresight ends, i want to start
as long as i haven't tried to gain
i will not stop, will not complain

Thursday, February 21, 2008

JSP syntax: empty checks, params, misc

This stuff is so elementary that i am always annoyed to forget it every time i dont use jsp for a few months.

1. Get params from the url request, ie http://dogself.com?param1=test&something=poop
<c:out value="param1 is: ${param.param1} something is:${param.something}"/>

also there is ${params} for getting lists, but i would need to read up on that

2. checking if a request attribute is null or empty:
<c:if test="${param.page != null && not empty param.page}">
<c:import url="${base_html_dir}${param.page}"></c:import>
</c:if>


3. setting variables:
<c:set var="thingToSet" value="${1+ 3}">
this by default has the scope of 'page', also can be 'session' defined by attribute 'scope'

ill add more as i go

Wednesday, February 13, 2008

Document base /path/to/ROOT does not exist or is not a readable directory

I recently installed tomcat 5.5 on my slicehost, and it was somewhat painful, probably because i deviated from the basic setup so much.
Anyway, one last error (in /var/log/tomcat5.5/catalina.out) that i was getting was this:

SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base /home/mk/public_html/ROOT does not exist or is not a readable directory

I was setting up my app context path using CATALINA_HOME/conf/Catalina/localhost/ROOT.xml and so it was strange that tomcat was looking for a directory called ROOT.

Solution:

I was overlooking this warning before the error:
WARNING: A docBase /home/mk/public_html/dogself.com inside the host appBase has been specified, and will be ignored

the problem was that i set my appBase inside of /etc/tomcat5.5/conf/server.xml to be /home/mk/public_html/ and this was a parent directory of app's docBase.

Changing the appBase to /home/mk/public_html/webapps solved this problem.

Wednesday, February 06, 2008

Music for february

this place has lots of rock from back when i used to listen to rock:
http://saintsmythe.illemonati.com/tsx/

Recently i've been interested in a progressive metal band called "Sonata Arctica", this place as a decent selection of them:

http://www.rockandroll-server.com.ar/GOD/mp3/Sonata/


a ton of DnB:
http://www.riotnerd.com/nmp3s/

thats all until i find more betterer stuff

Thursday, January 31, 2008

find duplicate files on your machine

ive had need for this before. A program to find duplicate files on your computer. i have been told by a very reliable source that this is good:

http://www.beautylabs.net/software/dupseek.html

so i am going to write it down so that next time i need this, i will have it

dupes are evil

Thursday, January 24, 2008

Java: reading / writing international characters

It is actually very easy to support international characters in java. The only trick is knowing the charset that the text is encoded in. Most of the time is a safe bet to try UTF-8 if you dont know. If you use Apache's HTTPClient it has methods to get the charset of the stream you are reading from.


Tuesday, January 22, 2008

Music for January

this place has lots of kraftwerk and other interesting stuff:

http://elf.art.pl/

this place has a BUNCH of music, lots of normal music with weird Japanese bands i've never heard of:

http://side7.homeunix.net/s/mp3/

Unix: Compress or delete files older then X days

Problem:
I have some logs in some directories that i need to back up if the file is at least 12 days old, if the file is older then 24 days, i need to delete it

Solution:

to compress:
find <path within you're looking> -xdev -mtime +<number of days> -exec /usr/bin/compress {} \;

to delete:
find <path within you're looking> -xdev -mtime +<number of days> -exec /usr/bin/rm -f {} \;


Note: This was tested on Solaris 8

Examples:

find /usr/local/apache/logs/ -xdev -name "sar.*" -mtime +24 -exec /usr/bin/rm -f {} \;
find /usr/local/apache/logs/ -xdev -name "sar.*" -mtime +12 -exec /usr/bin/compress {} \;

Tuesday, January 15, 2008

Cooking: How to make sushi rice

Making perfect sushi rice is hard, it has to be cooked though, soft and sticky. Ive always had trouble doing it because i would end up burning the rice on the bottom of the pot (i dont have a rice cooker). My rice wouldnt be sticky enough to stick to the seaweed. My friend Sam always emphasized to me how important it was to make the rice "just right" when making sushi.

Enough, babble! i found a blog entry that makes cooking sushi rice easy! it actually came out great the first time i tried the oven method.

http://beyondsalmon.blogspot.com/2006/02/technique-of-week-how-to-make-sushi.html

Thursday, January 03, 2008

how to: use cron

ok, now that i really know how to use the cron i should blog it incase i forget

this assumes you have nothing in your cron / or your crontab is in sync with hostname.cron because you always use this method to modify the crontab.

0. run crontab -l to make sure your crontab is empty, if its not, paste what it has to the bottom of the file you make in step 1

1. make a file called hostname. cron and add this into the top of it for later:
#########################################
# #
# Cron file for [username] #
# #
#########################################
# min hour dom month dow usr cmd
# use '*' as skipped element

2. add your stuff to it, here is a better explanation on what to add:
01 * * * * echo "This command is run at one min past every hour"
17 8 * * * echo "This command is run daily at 8:17 am"
17 20 * * * echo "This command is run daily at 8:17 pm"
00 4 * * 0 echo "This command is run at 4 am every Sunday"
* 4 * * Sun echo "So is this"
42 4 1 * * echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * echo "This command is run hourly on the 19th of July"

3. save your file.

4. run this:
crontab < hostname.cron

done!

useful info happily stolen from here: http://www.unixgeeks.org/security/newbie/unix/cron-1.html