Friday, May 8, 2009

Yesterday I found a useful script that will delete all tables and constraints from a sql server database. Replace "dbo" or "oup" with the appropriate value.


/* Drop all Foreign Key constraints */

DECLARE @name VARCHAR(128)

DECLARE @constraint VARCHAR(254)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

WHILE @name is not null

BEGIN

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

WHILE @constraint IS NOT NULL

BEGIN

SELECT @SQL = 'ALTER TABLE [oup].[' + RTRIM(@name) +'] DROP CONSTRAINT ' + RTRIM(@constraint)

EXEC (@SQL)

PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

END

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

END

GO

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/* Drop all Primary Key constraints */

DECLARE @name VARCHAR(128)

DECLARE @constraint VARCHAR(254)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

WHILE @name IS NOT NULL

BEGIN

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

WHILE @constraint is not null

BEGIN

SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT ' + RTRIM(@constraint)

EXEC (@SQL)

PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

END

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

END

GO


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


/* Drop all tables */

DECLARE @name VARCHAR(128)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL

BEGIN

SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'

EXEC (@SQL)

PRINT 'Dropped Table: ' + @name

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])

END

GO

Thursday, February 5, 2009

Logging

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


private Log logger = LogFactory.getLog(Class.class);


# Custom API
log4j.appender.custom=org.apache.log4j.RollingFileAppender
log4j.appender.custom.File=@log_path_custom@
log4j.appender.custom.MaxFileSize=50MB
# Keep three backup files.
log4j.appender.custom.MaxBackupIndex=3
# Pattern to output: date priority [category] {TH thread}- message
log4j.appender.custom.layout=org.apache.log4j.PatternLayout
log4j.appender.custom.layout.ConversionPattern=%d %p [%c] {TH %t} - %m%n
# Categories
log4j.logger.com.penton=debug,custom

Advantage of a scripting language

I can drop the script into a client environment and run it without having to build. This could be useful in debugging production issues.

Thursday, January 15, 2009

Unix 'Tree' Command

Have you ever wanted to recursively view a path? Forget ls or find. Try the tree command instead.

If you have the LS_COLORS environmental variable set and use the -C switch then the tree command will print the output to the terminal with colors. But wait, it get's better. Using the -d option will print only directories. You can also filter the output with regular expressions, print file permissions and size.

Tuesday, January 13, 2009

Deploying the Shibboleth Web App

First thing to note is that the web application does not follow maven standards. So, you'll have to move the java source files into src/main/java. Then next thing you'll have to do is modify sys.properties. You'll need to modify everything in that file. Then you'll be able to deploy it.

James Gathings

One Line Unix Scripts

There are many positive aspects of one line unix scripts. One line unix scripts are quite similar to good, witty conversation. They tease your brain a bit because one liner's are similar to a puzzle. You also get great satisfaction when you solve the puzzle. Unix offers a plethora of one-line tools to play. You have AWK, SED, Bash, etc. at your fingertips. What follows are a few of my favorite scripts.

Jar Finder:

Have you ever had to look for a class in your classpath? Here's a script, my favorite script of all, that accomplishes that task.

#!/bin/sh

for JAR_FILE in `find $1 -name \*.jar`
do
echo $JAR_FILE
jar tvf $JAR_FILE | grep $2
done

To execute this script execute:

jgrep /home/jgathings/ String

What this does is search for the class "String" in all jars in /home/jgathings/ path.

This is not a one-liner, but it is a useful script. Here's the one-liner

find . -name \*.jar | awk '{print "echo "$1"; jar tvf "$1"| grep ApacheConf.class"}' | sh

A bit confusing? Well, let's break it down. First, note that the one-liner is composed of several tokens delimeted by the unix pipe. The first token uses 'find' to find all jars. The second token uses awk to construct a dynamic command. This token is semicolon delimeted. First AWK prints the name of the jar including it's path. The second AWK token uses jar to list the items in the jar. The output of this token is piped to grep which checks to see if the specified class is in the jar. This example searches for ApacheConf.class. Next the AWK command is closed with the }. Finally the entire command is piped to the shell with 'sh' and the output is printed to the screen. The output could be appended to a file with the > operator. Here's an output snippet:

./server/lib/tomcat-util.jar
./server/lib/catalina-storeconfig.jar
./server/lib/servlets-invoker.jar
./server/lib/tomcat-ajp.jar
12497 Mon Jan 28 13:36:16 GMT-05:00 2008 org/apache/jk/config/ApacheConfig.class
./server/lib/catalina-ant-jmx.jar
./server/lib/tomcat-coyote.jar
./server/lib/catalina-cluster.jar
./server/lib/tomcat-apr.jar
./bin/bootstrap.jar

James Gathings


Monday, January 12, 2009

Creating a Shibboleth Environment

1) Download and install shibboleth 1.3. Shibboleth can be downloaded here: http://shibboleth.internet2.edu/v1.3.html. The best instructions I found are here: https://spaces.internet2.edu/display/SHIB/JKIdPInstall. There is a README that will guide you through the install process. Be sure that tomcat and shibboleth have the same write privileges. There doesn't appear to be a way to test the installation. Going to /shibboleth-ip returned a 404 to me. However, I was able to go to any jsp in the webapp (/shibboleth-ip/sample.jsp).

Please note that if you are running this on a linux system then it is easiest to install all components (shibboleth-idp,tomcat, etc) as the same user. I found some permissions problems when having apache run as root and tomcat as a different user.

2) Download the tomcat apache connector (mod_jk). CD to native. Type ./configure --with-apxs=/usr/local/apache/bin/apxs --with-java-home=/usr/java/jdk1.5.0_14/ --enable-EAPI. Then type make. mod_jk.so should be in tomcat-connectors-1.2.27-src/apache-1.3/.

Create an ajp13 workers.properties file:
# Define 1 real worker using ajp13
worker.list=ajp13
# Set properties for the ajp13 worker
worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009

Add the following to httpd.conf:
LoadModule jk_module libexec/mod_jk.so

JkWorkersFile /usr/local/tomcat/conf/jk/workers.properties
JkLogFile /usr/local/tomcat/logs/mod_jk.log
JkLogLevel debug

JkMount /shibboleth-idp/* ajp13
JkMount /jsp-examples/* ajp13

3) At this point apache and tomcat should be able to communicate to each other.
4) Go to shibboleth web application. For example, http://sorcerer.internal.emeta.com/shibboleth-idp/SSO.
5) You'll receive a Shibboleth error page (Shibboleth Identity Error Page to be specific). This is an expected sign. It means that shibboleth is now ready to be configured for the federation.
6) The next step is to use openssl to generate a key and a certificate:
  1. openssl genrsa -out idp.key 2048
  2. openssl req -new -key idp.key -x509 -days 365 -out idp.crt
7) Repeat the same steps on the erights server.
8) In the IDP server directory copy etc/example-metadata.xml to etc/partner-metadata.xml.