Over the course of my career I have found many useful code snippets, utilities, and ideas that have made my software projects successful. Through this site I intend to give back some of the things that I think you may find useful.

Isaac Asimov - "What Is Intelligence, Anyway?"

Tonight I ran across the following piece written by Isaac Asimov, and felt is was worth sharing:

What is intelligence, anyway? When I was in the army, I received the kind of aptitude test that all soldiers took and, against a normal of 100, scored 160. No one at the base had ever seen a figure like that, and for two hours they made a big fuss over me. (It didn't mean anything. The next day I was still a buck private with KP - kitchen police - as my highest duty.)

All my life I've been registering scores like that, so that I have the complacent feeling that I'm highly intelligent, and I expect other people to think so too. Actually, though, don't such scores simply mean that I am very good at answering the type of academic questions that are considered worthy of answers by people who make up the intelligence tests - people with intellectual bents similar to mine?

The Six Rules Jack Welch Lives By

Recently, in an MBA class I was taking (Human Resources Management), I came across the 6 rules Jack Welch lives by.

For those who have no idea who Jack Welch is, he was Chairman and CEO of General Electric between 1981 and 2001. He practiced some unique leadership strategies during his tenure at GE, and increased its market capitalization by over $400 billion

I felt that the 6 rules are worth sharing with the the world and am publishing them below through this blog. By the way, I just finished the program and earned the degree! Yippee for me!

The six rules are:

1. Face reality as it is, not as it was or as you wish it to be.

View your credit report for free...Put a fraud alert on your credit.

Recently I added a fraud alert to my credit report to see what exactly it does.

I went to http://www.experian.com/fraud and added an Initial Security Alert (90 days) to my credit report. Surprisingly, this was very easy to do, and only took me about 10 minutes. In any case, I got a free peek at who's been viewing my credit, what accounts I have open, etc. I was able to verify that there are no other accounts in my name (thankfully, there were none), and I even submitted a dispute that a card that I am no longer an authorized purchaser on is still listed in my credit. Within 2 weeks that card was removed from my credit report. It is important to note that you will not be shown your FICO score when submitting a fraud alert. You can pay a small amount to have it displayed though.

Oracle - Determine Objects Using The Most Space And Clean Them Up (Pack Tables and Rebuild Indexes)

So you keep extending your Oracle datafiles, but do you know what objects are the true space hogs? I use the query below, to look at the size of the objects in a specific tablespace.

SELECT owner,
       segment_name,
       segment_type,
       tablespace_name,
       bytes
FROM DBA_SEGMENTS
WHERE TABLESPACE_NAME = 'DEVELOPMENT'
ORDER BY bytes desc

This will give me a list of my tables, indexes, and other objects in the specified tablespace in order by their size in bytes. The largest objects will be displayed first based on my sort criteria.

From this output, I usually see various tables that I can start purging, such as log tables, temp data tables, etc. This part will be specific to your own environment, but you'll want to delete data you don't need anymore.

I hate plumbing....

I apologize for the lack of posts, but this past week has been a week of hell dealing with plumbing...

First off, I found out that my shower had a leak last Monday. I decided to replace the fixture, but it was some goofy fixture that was actually all attached inside of the walls and the job required the walls to be opened up. I decided to not do that one myself and hired a plumber. I called 5 different plumbers, and only 1 decided to call me back and bid on the job. They were able to get it done, as well as extract $670 from my wallet.

Now, today (Sunday), I found out my garbage disposal has been leaking. A bunch of water has started ruining the finish of the base of the cabinet under the sink. Oh yeah, and my favorite part was the nasty smell and slimy fungus growing under the sink from the water that has been dripping for who knows how long.

Using the ‘expect’ Command To Automate Interaction With Programs

From the expect man page:
Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

At the institution for which I work, I am responsible for our Sungard HE Banner ERP system. One of the development tasks that I was presented with was to automatically run a binary file at 10pm every night. The challenge was that this program was interactive and required the user to input answers to prompts. This sounded like a perfect use of the Unix program expect.

Restoring Quickbooks Loan Manager From Backup

Today I had a slight crisis…I had to reinstall QuickBooks 2006 on a new computer and restore the backup file that QuickBooks creates with the “.qbb” extension.

Everything worked great, and it was literally as simple as popping in the QuickBooks CD, installing the software, launching the program, and then following the steps from the “File->Restore menu”.

However, once I went into the Loan Manager to make a loan payment, I discovered none of the loans that I setup were there. I learned the hard way that the Loan Manager data is not backed up with your QuickBooks backup file. I really did not want to re-key in my loans, figure out what payment number I was on, determine principal and interest components, etc…Needless to say I was a little sad and looked for a way to get my Loan Manager data back.

Migrate Your Firefox Saved Passwords and Bookmarks to a New Computer

I just got a new computer and had to figure out how to bring over all of my Firefox bookmarks and saved passwords over to it from my old computer.

For reference, my old and new computers are both running Windows XP, and my old computer is running Mozilla Firefox 2.0.0.3.

In order to bring over all of my personal settings to my new computer I performed the following actions:

  1. First install Firefox on the new computer.
  2. On your old computer, navigate to the directory containing all of your personal settings. We want to copy this directory from your old computer to your new one.

Quick Start Guide to Setting up Apache Tomcat on Linux

I recently setup Apache Tomcat 6.0.10 and in this post I will share the steps that I went through to install it on my RedHat Linux AS 4 server.

The Basics – Download and Install The Software

  1. First make sure you have a Java Development Kit installed on your server. I have a write up on how to do this at http://timarcher.com/?q=node/59.

Installing the JDK on a RedHat Linux System

By default, RedHat Linux AS and AS4 servers don’t come with a JDK (Java Development Kit) installed on them. Depending on your install, if you run the java command you may get some sort of error message or a file not found message.

In this post I will describe how to install a JDK on your RedHat Linux server. It should also work on Fedora, however I have not tried it personally.

  1. First we need to download a JDK. I am downloading JDK 6u1 from http://java.sun.com/javase/downloads/index.jsp. Go to that page and click the Download button. On the new page that loads, we will download the Linux RPM in self-extracting file.

Setup Your Linux Box as an NTP Server

I believe that every organization should have a NTP/time server if they have more than one computer on site. Having an NTP server will allow you to keep the times on all of your computers in sync. This helps when comparing the logs from various servers to trace through various events that happened. It’s nice to be assured that the event really happened at the time specified in the log file regardless of what server you’re on.

In this post I will show you how to setup your RedHat Linux AS 3 or 4 machine as an NTP server. You could then take all of your other servers and workstations and have them synchronize their time from your NTP server.

By default, the files necessary to run the ntpd service are installed on a RedHat machine if you installed everything. If you did not install everything, go grab the ntp package off your install CDs or download it directly from RedHat. The name of the package that is installed on my RedHat AS4 server is ntp-4.2.0.a.20040617-4

Now we have to edit the /etc/ntp.conf file to plug in the NTP servers that we’ll synchronize with and setup the permissions of who can synchronize with us. To do this follow my steps below.

  1. First make sure your computer's clock is set to something sensible (within a few minutes of the 'true' time). If it isn’t, NTP may never get them synchronized. On RedHat AS4, you can use the command system-config-date, and I believe on RedHat AS3, the command is redhat-config-date.
  2. Open up /etc/ntp.conf in your favorite editor.
    vi /etc/ntp.conf
    
  3. Plug in the timeservers that we’ll synchronize with. I use the servers from pool.ntp.org. The 0, 1 and 2.pool.ntp.org names point to a random set of servers that will change every hour.

    server 0.pool.ntp.org
    server 1.pool.ntp.org
    server 2.pool.ntp.org
    
  4. Restrict the access that the pool.ntp.org servers have to our system. By putting the below restrictions in your ntp.conf file, the servers are not allowed to modify the run-time configuration or query your Linux NTP server.

    restrict 0.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict 1.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict 2.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    
  5. Allow systems on your own network to query your NTP server. We have to define the networks from which this server will accept NTP synchronization requests. To do this we add another restrict statement that does not have the noquery restriction. Since my home network contains IP addresses in the range of 192.168.1.1-255, I plug in the following network:

    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
    

    We could easily add many networks, or even individual hosts by using the mask 255.255.255.255.

  6. We also want to permit full access to our NTP server from the localhost. Add a line like the following in your ntp.conf file if it is not already in there.
    # Permit all access over the loopback interface.  This could
    # be tightened as well, but to do so would effect some of
    # the administrative functions.
    restrict 127.0.0.1
    
  7. The last thing we want to do is to setup ntpd to automatically start at system boot.

    chkconfig --level 35 ntpd on
    
  8. And then verify that your system is set to start ntpd on system boot for runlevels 3 and 5. Run the command:

    chkconfig --list ntpd
    

    And your output should look like:

    ntpd            0:off   1:off   2:off   3:on    4:off   5:on    6:off
    
  9. Now your NTP config should be all setup. Let’s manually stop and then start it. Run the /etc/init.d/ntpd stop and then the /etc/init.d/ntpd start commands.

    root@as1 filmoregroup.com [asdb] > /etc/init.d/ntpd stop
    Shutting down ntpd: [  OK  ]
    
    root@as1 filmoregroup.com [asdb] > /etc/init.d/ntpd start
    ntpd: Synchronizing with time server: [  OK  ]
    Starting ntpd: [  OK  ]
    
  10. At your shell prompt you can run the ntpq command to check the synchronization status of your server with the external time servers we specified in the ntp.conf file.

    As root, run the command:

    ntpq -p
    

    And your output should look something like:

    root@as1 filmoregroup.com [asdb] > ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    +ns1.dns.pciwest 204.123.2.5      2 u  562 1024  377   76.131   -2.912   1.129
    +dns1.tjgroup.no 192.43.244.18    2 u  550 1024  377  143.463    1.390   0.422
    *time4.stupi.se  .PPS.            1 u  604 1024  377  122.685    0.087   0.492
     LOCAL(0)        LOCAL(0)        10 l   35   64  377    0.000    0.000   0.001
    

    If the jitter column has a value of 4000.00 for any of the servers then it is usually a sign that your server is not able to synchronize with that server specified on the line with the bad jitter value. You may have a firewall or other internet connectivity problem then.

  11. Your NTP server should now be setup. You can now tell other machines on your network how synchronize with your NTP server.


Synchronize Your Other Linux Clients

In order to point other Linux boxes on your network to your new NTP server, you’d follow the instructions above. However, instead of synchronizing with the pool.ntp.org servers you’d synchronize with the hostname of your NTP server. You also WOULD NOT need to grant access from other machines on the network to your server as we did by adding the command:

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

Alternatively, you can use the command system-config-time on RedHat AS 4, or redhat-config-time on RedHat AS 3 to use a GUI to specify the NTP server to synchronize with. I have a document on doing it this way at http://timarcher.com/?q=node/16

Enabling FTP on a RedHat AS3 or AS4 Server

By default FTP comes disabled on stock RedHat AS3 and AS4 server installs. For the most part you should not be using it anyways, and instead using something more secure like SFTP. However, in some rare occasions I find the need to enable FTP. Below I will show you how to do it:

  1. Become the root user on your server.
  2. Change to the /etc/xinetd.d directory
    cd /etc/xinetd.d/
    
  3. Edit the gssftp file

    vi gssftp
    
  4. Change the line that says disable = yes to disable = no. Also remove the –a option in the server_args line.

Sending Files via FTP From Your Java Applications - Part 2 of 2 - Using Jsch for SFTP

Yesterday I introduced how I send files via FTP and SFTP from my java applications. In that post I showed you how to use the Jakarta Commons Net package to FTP files. Today I will show you how to use the JSch (Java Secure Channel) library to do SFTP transfers.



Part 2 of 2 - Using JSch (Java Secure Channel)

Sending Files via FTP From Your Java Applications - Part 1 of 2 - Using Commons Net For FTP

Within the enterprise applications that I design and develop, a requirement always comes up to integrate it in some way with an external, 3rd party system. For example, in writing an insurance policy management system, the carriers often want policy details FTP’ed to them. Logistics companies typically FTP EDI files around all day. And most recently at the University for which I work, we have integrated our systems to transmit financial aid data to the government, send information related to book store vouchers to our outsourced bookstore, and we have our student data geocoded to help us analyze what profile of students are enrolling. All of these external providers want the data to be FTP’ed to them.

Why TimArcher.com Got Created

Well, it’s been about one month now since I started this blog, and so far all I’ve done are tech posts. However, since I have never really introduced myself or described why this blog was created, I will use today’s post to do just that.

It all started back in January of 2006. I saw the domain timarcher.com become available and since my name is in fact Tim Archer, I thought it would be a good idea to register the domain. Then it sat idle for over a year as I was in the middle of my MBA program and had no time to do anything with it. Besides, I had no idea what I should even do with the domain anyway.

Using Javamail to Send Emails (Includes SMTP Auth and Attachment Support)

Last night I presented my article discussing how to use JavaMail to check a POP3 mail account and retrieve its messages. Tonight I am going to follow that up with a summary of my EmailDelivery class. This is a simple class I wrote to help me easily send emails from my Java applications. A few highlights of the class are that it:

  • Supports relaying through SMTP servers that require authentication.
  • Has convenience methods for easily adding file attachments.
  • Has a method to set the message priority (high, normal, low).
  • Allows you to easily set the to, from, cc, and bcc email addresses. Also supports passing in a comma separated list into any of these methods and have it parsed automatically for easy sending to multiple recipients.
  • Allows you to easily add header name/value pairs in the message.
  • Suppports easily setting the value stored in the Message-ID header tag of the message.

To use my classes you will first need to download the JavaBeans Activation Framework, and the Javamail libraries. Unzip the downloaded archives and put the appropriate jar file into your classpath. My example also uses Log4j to log its output. If you don’t want to configure log4j, then replace all of my log.info and log.debug calls with a call to System.out.println.

You also need Jakarta Commons Codec library from http://jakarta.apache.org/site/downloads/downloads_commons-codec.cgi. I downloaded the file labeled 1.3.zip Binary. Uncompress the zip file and put the commons codec jar file into your classpath. The name of the jar file in my example is commons-codec-1.3.jar. The Commons Codec jar file is what supports the Base 64 encoding of the file attachments.

Next, you will need to have my EmailMimeMessage class. Take the class below which is a simple extension of the standard JavaMail MimeMessage class. This class exists for the sole purpose of supporting a user defined Message-ID header tag in my examples.

package com.tima.edelivery.util;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

import org.apache.log4j.*;

/**
 * Extended MIME Message class with method to set the message ID 
 * header tag, and the updateHeaders method which ensures the 
 * Message ID gets set in the message.
 *
 * @author  Tim Archer 09/30/2003
 * @version $Revision: 1.1 $
 */
class EmailMimeMessage extends MimeMessage {
    static Logger log = Logger.getLogger(EmailMimeMessage.class);
    
    /** The value stored in the Message-ID header tag for the message.*/
    protected String messageID = "";
    
    /**
     * Create a new EmailMimeMessage object.
     * @param session The javax.mail.Session object the mail message is for.
     *
     */        
    public EmailMimeMessage(javax.mail.Session session) {
        super(session);
    }
    
    /**
     * Set the value stored in the Message-ID header tag for the message.
     * @param p_value The value of the Message-ID header tag.
     *
     */        
    void setMessageID(String p_value) {
        messageID = p_value;
    }
    
    /**
     * Calls the super.updateHeaders() method, and also ensures 
     * that the Message-ID header tag
     * gets set if the setMessageID method was called.
     *
     * @throws MessagingException If an error occurs.
     */        
    protected void updateHeaders() throws MessagingException {
        super.updateHeaders();
        if (messageID != null && messageID.length() > 0) {
            setHeader("Message-ID", messageID);
        }
    }
}



Now that you have the EmailMimeMessage class, take the code below and create the EmailDelivery class. This class has the real functionality we're after.

package com.tima.edelivery.util;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.*;

/**
 * Create a multipart message with the second block of the 
 * message being the given file for an attachment.
 * 
 * Has support for SMTP authentication to authenticate to an 
 * SMTP server to send the message.
 * Utilizes the apache commons codec library to Base64 encode the file attachment.
 *
 * @author  Tim Archer 09/30/2003
 * @version $Revision: 1.1 $
 */
public class EmailDelivery {
    static Logger log = Logger.getLogger(EmailDelivery.class);
    
    /** Static variable representing a high priority message. */
    public final static String HIGH_PRIORITY = "1";
    /** Static variable representing a normal priority message. */
    public final static String NORMAL_PRIORITY = "3";
    /** Static variable representing a low priority message. */
    public final static String LOW_PRIORITY = "5";
    
    EmailMimeMessage msg = null;
    Session session = null;
    Properties props = null;
    Multipart mp = null;
        
    /** The SMTP host used to send the message. */
    String smtp_host = new String("");
    /** If using SMTP authentication, then this is the username to login to the SMTP server as. */
    String smtp_username = null;
    /** If using SMTP authentication, then this is the password for the username to login to the SMTP server as. */
    String smtp_password = null;
    /** The port the SMTP server is listening on. */
    int smtp_port = 25;
    
    /** If we are to use SMTP authentication when sending the message. */
    boolean usingAuthentication = false;
    
    
    /**
     * Create a new object to send email messages.
     *
     */      
    public EmailDelivery() {
        props = System.getProperties();
        session = Session.getDefaultInstance(props, null);
        session.setDebug(false);
        msg = new EmailMimeMessage(session);
        mp = new MimeMultipart();
    }
    
    /**
     * Set the SMTP host used for sending the mail message.
     *
     * @param p_host The SMTP host to use for sending the messgae.
     * @throws MessagingException If an error occurs.
     *
     */      
    public void setSMTPHost(String p_host) throws MessagingException {
        this.setSMTPHost(p_host, null, null);
    }
    
    /**
     * Set the SMTP host used for sending the mail message.
     * This method allows a username and password to be specified for SMTP authentication.
     *
     * @param p_host The SMTP host to use for sending the messgae.
     * @param p_username The username to log into the SMTP server with.
     * @param p_password The password for the username to log into the SMTP server with.
     * @throws MessagingException If an error occurs.
     *
     */      
    public void setSMTPHost(String p_host, String p_username, String p_password) throws MessagingException {
        log.debug("EmailDelivery in setSMTPHost Method. Host: "+p_host+" Username: "+p_username);
        smtp_host = p_host;
        smtp_username = p_username;
        smtp_password = p_password;

        props.put("mail.smtp.host", smtp_host);
        
        //
        //Determine if we are using authentication or not
        //
        if ((smtp_username != null && smtp_username.length() > 0) ||
             (smtp_password != null && smtp_password.length() > 0)) {
            props.put("mail.smtp.auth", "true");
            usingAuthentication = true;
        }
        else {
            usingAuthentication = false;
        }
        log.debug("EmailDelivery in setSMTPHost Method. UsingAuthenticaton: "+usingAuthentication);
    }
 
    /**
     * Set the port that the SMTP server is listening on.
     *
     * @param p_port The port the SMTP server is listening on.
     * @throws Exception If an error occurs.
     *
     */      
    public void setSMTPPort(int p_port) throws Exception {
        smtp_port = p_port;
        props.put("mail.smtp.port", String.valueOf(smtp_port));
    }    

    /**
     * Set who the email message is to. 

Java Class To Check a POP3 Mail Account And Retrieve Its Messages

Recently I wrote a java daemon process that monitors a POP3 email box for messages, automatically downloads the messages, and then processes them. Basically my daemon process looks for returned and bounced emails in a POP3 mailbox and then updates certain flags in our ERP system so we don't send to that email address again. In some cases people reply to the messages our ERP system sends out, and in that case my daemon process forwards those messages to an appropriate user for manual review and handling.

I’m not going to go into the details of how this daemon process work since it goes far beyond the scope of this post (and it is a huge piece of software in itself). However, I am going to share the basic java class that I use to check the POP3 mail account, download messages, and then loop through them for processing.

Simple Java Class to DES Encrypt Strings (Such as Passwords and Credit Card Numbers)

In my Java based applications I usually have the need to encrypt a string before storing it somewhere. Most commonly, I need to encrypt user passwords and credit card information before storing it in a character field in the database. I don’t want people to be able to walk away with a cleartext list of passwords or credit card numbers just by doing a simple select. Instead they’ll have to work a little harder to decrypt the values stored in the database.

To do this, I wrote a very simple utility class that takes a String, runs it through a javax.crypto.Cipher object to DES encrypt it, and finally passes it through the Jakarta Commons Codec class which encodes the encrypted bytes into a Base64 (org.apache.commons.codec.binary.Base64) byte array. I wanted to encode the encrypted string in Base64 to ensure it only contains ASCII characters before storing it in my database. The decryption just follows the previous explanation in reverse: decode base 64, unencrypt string, return unencrypted string.

Changing The Oracle Listener Port

On the Oracle systems that I manage I typically like to add a simple layer of security by changing the port that the listener listens on. Given that I work for an institution that allows the public to connect to its internal network, and since that network has an Oracle Database server on it, I wanted to make it just a little bit harder to find our databases. I recognize that a good hacker will find our Oracle Database servers regardless of the port that they run on. However, by changing the listening port I hope to eliminate the potential for a member of the public bringing in an infected computer that tries to do something malicious to Oracle databases.

Oracle Database Init Script - /etc/init.d/dbora

Many Oracle shops want their database to automatically start when their server boots up, and to automatically shutdown when they shutdown the server.

Below I will share with you the Oracle init script that I use on my server. It has been tested with Oracle 10gR2 on RedHat Linux AS3.

  1. Login as the root user on your server.
  2. Put the following script in the file named /etc/init.d/dbora:
    #!/bin/sh
    # chkconfig: 345 99 10
    # description: Oracle auto start-stop script.
    #
    # Change the value of ORACLE_HOME to specify the correct Oracle home
    # directory for your installation.
    
Syndicate content
v2.0