Saturday, February 19, 2011

Set Tomcat's Default Locale

If for whatever reason you want to set the locale in Tomcat you simply need to add two jvm arguments in the catalina.sh file found in the "bin" directory of the Tomcat installation. I was able to set the default locale for Tomcat to US/en on a Mac running Mac OS X Leopard with Tomcat 7.0.8 installed. These instructions should work for any *nix machine, but I can't guarantee it. If you're using windows, these instructions are probably also relevant, but instead you'd change catalina.bat.

Step 1: Open up the catalina.sh file with any text editor.


Step 2: Search for "JAVA_OPTS=" in the file and in particular locate the following lines of code:
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi

Step 3: At the end of the JAVA_OPTS definitions add "-Duser.language=en -Duser.region=US".
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Duser.language=en -Duser.region=US"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER -Duser.language=en -Duser.region=US"
fi

Step 4: Save the file and restart Tomcat.

Piece of Cake!!

Sunday, February 13, 2011

My Mac OS X MySQL Cheat Sheet

Using MySQL and Java? Check out an easier way: Yank

Following is my Mac OS X MySQL cheat sheet. Normally I use a great open source SQL client called Sequel Pro for my SQL work, but every now and then I need to log into MySQL directly using Terminal for some advanced options.

Log In

/usr/local/mysql/bin/mysql -u root -p

Create Database

create database DATABASE_NAME;

Show Databases

show databases;

Use Table

use TABLE_NAME;

Indexes

show index from TABLE_NAME;

Drop Table

drop table TABLE_NAME;

Truncate

truncate table TABLE_NAME;

See also: Uninstall MySQL on Mac OS X
And also: Install MySQL on Mac OS X
And also: Installing and Running Tomcat on Mac OS X
And also: Set MySQL Server System Variables on Mac OS X

Saturday, February 12, 2011

Set MySQL Server System Variables on Mac OS X

Using MySQL and Java? Check out an easier way: Yank

If you need to set a system variable for the MySQL server, it's done by adding a name-value pair in a file called my.cnf. Where is my.cnf you're wondering? MySQL will look inside the /etc for "my.cnf" file. This file does not exist by default. The following steps describe how to create the my.cnf file used to confugure MySQL.

Step 1: Log into mysql. Fire up the terminal and enter:
/usr/local/mysql/bin/mysql -u root -p
Edit path and user appropriately.

Step 2: View MYSQL's current values:
show variables;
I'm interested in overriding the ft_min_word_len variable, and I see it's currently set to 4.

Step 3: Create /etc/my.cnf:
sudo vim /etc/my.cnf
and add (for example):

[mysqld]
default-time-zone='+00:00'
ft_min_word_len=1

to the file. Exit vim (esc :x).

Step 4: Restart MYSQL:
sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart

Step 5: Log into MYSQL again and verify the change occured.

Piece of cake!!!


References

MySQL 5.5 system variables
The MySQL Configuration File


See also: Uninstall MySQL on Mac OS X
And also: Install MySQL on Mac OS X
And also: Installing and Running Tomcat on Mac OS X
And also: My Mac OS X MySQL Cheat Sheet

My Mac OS X Filesystem Permissions Cheat Sheet

With Mac OS X being a Unix-based Operating System, we can use the Terminal application to get under the hood of the OS and perform many advanced operations. One the the most basic Unix or Linux command line operations or set of operations deals with file permissions. Here, I have accumulated revavent information and commands to filesystem permissions general to *nix and also specific to a mac.

Users and Groups

Listing

dscl . -list /Groups PrimaryGroupID
dscl . -list /Users UniqueID

Creating a non-standard user

First, choose a User ID and a Group ID which is not already in use, which must be a positive integer, lower than 500.
sudo dscl . -create /Groups/_tomcat PrimaryGroupID 107
sudo dscl . -create /Groups/_tomcat RealName "Tomcat Users"
sudo dscl . -create /Groups/_tomcat Password \*
sudo dscl . -create /Users/_tomcat UniqueID 107
sudo dscl . -create /Users/_tomcat PrimaryGroupID 107
sudo dscl . -create /Users/_tomcat HomeDirectory /usr/local/tomcat
sudo dscl . -create /Users/_tomcat UserShell /usr/bin/false
sudo dscl . -create /Users/_tomcat RealName "Tomcat Administrator"
sudo dscl . -create /Users/_tomcat Password \*
Choosing /usr/bin/false as the UserShell, and setting the Password to “*” turns this account unusable as a standard user account. The _name convention is used for non-standard accounts.

List Files - ls


ls -lhFa

Arguments

-l long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename
-F appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix.
-a lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list.
-R recursively lists subdirectories. The command ls -R / would therefore list all files.
-d shows information about a symbolic link or directory, rather than about the link's target or listing the contents of a directory.
-t sort the list of files by modification time.
-h print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.)

Explained

drwxr-xr-x@ 14 root wheel 476B Feb 12 00:12 ./
drwxr-xr-x 6 root wheel 204B Feb 10 22:11 ../
-rw-r--r--@ 1 root wheel 55K Feb 4 13:51 LICENSE
-rw-r--r--@ 1 root wheel 1.2K Feb 4 13:51 NOTICE
-rw-r--r--@ 1 root wheel 8.6K Feb 4 13:51 RELEASE-NOTES
-rw-r--r--@ 1 root wheel 6.5K Feb 4 13:51 RUNNING.txt
drwxr-xr-x@ 26 root wheel 884B Feb 12 00:55 bin/
drwxr-xr-x@ 9 root wheel 306B Feb 12 01:07 conf/
drwxr-xr-x@ 21 root wheel 714B Feb 10 23:40 lib/
drwxrws---@ 24 _tomcat admin 816B Feb 12 00:55 logs/
drwxrws--- 6 _tomcat admin 204B Feb 12 00:12 static/
drwxrws---@ 6 _tomcat admin 204B Feb 12 12:40 temp/
drwxrws---@ 12 _tomcat admin 408B Feb 12 00:49 webapps/
drwxrws---@ 4 _tomcat admin 136B Feb 12 01:08 work/
d=directory, -=normal file, l=symbolic link
rwx=red/write/execute, rwx truple=user/group/others
@=attributes, use xattr -l filename to view them
next comes the user owner, then the group owner, then file size, then last modified, then name

CHOWN


Arguments

-R recursive

Explained

sudo chown root file.txt
sudo chown -R root:wheel .
sudo chown root:_tomcat conf/tomcat-users.xml
sudo chown _tomcat:admin logs temp webapps work
root=user, wheel=group

CHMOD

Arguments

-R recursive

Explained

sudo chmod 644 conf/*
sudo chmod 640 conf/tomcat-users.xml
sudo chmod 2770 logs temp webapps work
sudo chmod -R 2770 /usr/local/tomcat/static
rwx=red/write/execute, rwx truple=user/group/others
7 full
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
0 none
2770, the first bit (2) in a 4-bytle chmod is for setuid and setgid permissions. See: techrepublic.com


References:

Joel's Writings
ls-Wikipedia
anselmbradford
chown-Wikipedia
chmod
setuid

Friday, February 11, 2011

Debugging a Launchd Configuration on Mac OS X

Recently I set up a launchd configuration on my macbook that starts Tomcat up automatically during bootup. Of course it didn't work until after man hours of trial and error and combing Google for answers with a fine toothed comb. During part of the ordeal I came across a couple terminal commands for debugging Launchd. Here they are:

1. set the launcdctr log level to debug
sudo launchctl log level debug (debug, info, notice, warning, error, critical, alert, emergency)

2. Tail the system.log
tail -f /var/log/system.log

3. set the launcdctr log level back to error
sudo launchctl log level error

The possible log levels are as follows: debug, info, notice, warning, error, critical, alert, emergency.

Useful links I came across:
Creating launchd Daemons and Agents
Joel's Writings