Saturday, October 30, 2010

On Communication

Spaceship Earth

This fall for my fifth wedding anniversary to my lovely wife, we went to Disney World and Universal's  Islands of Adventure (to go to the Wizarding World of Harry Potter).  While at Epcot we went on Spaceship Earth, the newly redone ride in the big sphere.  The ride was very interesting as it tells the history of the mankind.

The theme running through the ride (at least to me) was that of the evolution of communication.
Spaceship Earth starts with the dawn of human kind, humans hunting by themselves, it then goes on to say the first breakthrough was people working together to hunt.  From groups hunting we have tribes, then towns, cities, and countries forming.  All-a-long this forming of larger groups of people we find people sharing information as key to progress.  This progress spans from: early cave drawings, to papias writings, books, universities, libraries, and finally computers with large databases and other means of storage.  A very interesting way of looking at the history of mankind.

Basic Patterns of Communication

Spaceship Earth got me thinking again about something I've been thinking about since my undergrad at NIU, there are really only two forms of communications, information sharing and information downloading.

Information Sharing

Information sharing, to me, is anytime you have more than one entity sharing information.  Example of this are a conversation among two people.  A conversation is when all parties are speaking and thinking about what each other are saying and are forming a pool of shared meaning and knowledge, all parties gain knowledge and insight from this conversation and leave it with more knowledge than before it took place.


In the picture above we have two people, Jim and Cindy (my parents) communicating back and forth with each other.  We could easily abstract this to two computer systems communicating as shown below.


There is no difference in the pattern between Jim and Cindy speaking and Chrome and Google communicating, they both share information back and forth and leave the conversation with more knowledge than when they entered it (true you could say that Chrome and Google do not gain anything, but they could if they wanted to and sometimes do, i.e. browser cookies and web server analytics).

Information Downloading

Information downloading, again to me, is anytime one entity is obtain information from an information storage.  This storage can take any form: a book, database, or even a teacher.  In this form of communication only one entity gains anything from the communication, the storage gains nothing.  An example of this would be a presentation, in the typical presentation the presenter gains no new knowledge on the presentation they are giving, but the observers of the presentation gain the knowledge being presented.


In this picture we have myself presenting a topic to an audience.  Since I am just presenting, I am not gaining any new knowledge on the topic while (I hope) the audience is gaining new knowledge about the topic being presented.  This can be abstracted to web service and a database as shown below.


There is no difference between Mike and the audience or GetParty web service and the Party Details database, both the audience and GetParty are downloading information from Mike and Party Details.  In both cases Mike and the Party Details database gain nothing from the exchange.

Saturday, October 2, 2010

How to format the Date and Time in an Ant Script

Configuration Nightmare

On the current project I am working on, we have people in three different timezones working on the same code and environments.  We have a fairly normal testing environments set up of: Unit, Integration, System, Acceptance, and Regression levels.  Our current deployment strategy calls for each WebService to have everything it needs in one package, meaning that if WebService A and B both use the same Data Access jar, then both WebService packages will have their own versions of the jar.  As you may have guess this works fine for the most part if that Data Access jar is not changing, but if it does change then both WebServices will have to be redeployed if they both want to use any changes to the Data Access jar.

This strategy can lead to the following issues (shown in this UML Deployment Diagram):



If you look closely you will see that in GetAccount WebService in the Integration level we have version 1 of the DataAccess jar, if all our testing validates that this service works but we want the new functionality of version 2 of the DataAccess jar, we will have to retest with version 2 to validate that the new jar does not cause issues.  This issue is easy enough for the Environment Manager, all they have to do is deploy the GetAccount WebService with version 2 of the DataAccess jar.  The Environment Manager will just have to go to the CMS and get version 2 of the jar, unfortantly the CMS is designed to be accessed by dates of atrifacts and has a totally different concept of versions.  To make matters worst the Environment Manager is in a different timezone than the developers and the CMS they use.

This was the kind of position I found myself in recently.  The MAINIFEST.MF in the jar was using a date and time format that was in the European style (day/month/year) with a time without a timezone.  The Ant script was creating a MAINIFEST.MF file with attributes for the data and time that confused everyone, so I went in and changed the script.  That is when I found out there was no single document that covered the Ant TStamp task in full.  I hope the rest of this blog will solve that issue.

Ant TStamp Task

TStamp Task has the following format:

AttributeDescriptionRequired
propertyThe property to receive the date/time string in the given pattern.Yes
patternThe date/time pattern to be used. The values are as defined by the Java SimpleDateFormat class.Yes
timezoneThe timezone to use for displaying time. The values are as defined by the Java TimeZone class.No
offsetThe numeric offset to the current timeNo
unitThe unit of the offset to be applied to the current time. Valid Values are
  • millisecond
  • second
  • minute
  • hour
  • day
  • week
  • month
  • year
No
localeThe locale used to create date/time string. The general form is "language, country, variant" but either variant or variant and country may be omitted. For more information please refer to documentation for the Locale class.No

An example would be:
<tstamp>
  <format property="datetime" pattern="MM/dd/yyyy hh:mm"/>
</tstamp>

I find this pattern for the format of the date and time to be a bit elitist.  Meaning people use to the European style of dates might get confuse, plus there is no timezone on the time which could lead to even more confusion.  Instead I think that the month should be spelled out and the time should have a timezone.  We are lucky since the pattern that Ant's TStamp Task uses is the same pattern used for Java's SimpleDateFormat class.

Java SimpleDateFormat Pattern

Java's SimpleDateFormat uses the follow pattern:


LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
yYearYear199696
MMonth in yearMonthJulyJul07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesdayTue
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard TimePSTGMT-08:00
ZTime zoneRFC 822 time zone-0800

Look at the table above we see that M can be used to show the month and z can be used to show the timezone.  Looking at the examples given in the Java doc we see the following:

Date and Time PatternResult
"yyyy.MM.dd G 'at' HH:mm:ss z"2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"Wed, Jul 4, '01
"h:mm a"12:08 PM
"hh 'o''clock' a, zzzz"12 o'clock PM, Pacific Daylight Time
"K:mm a, z"0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"010704120856-0700

When M is used three or more times, we get a text representation of the month, which is what we want.  Further, with a z we get the timezone and also with an a we get the AM/PM marker (which is good since we have options for 24 hour days starting with either 0 or 1, so we'll do the easy thing and use AM/PM).

TStamp Task Solution

I changed the date and time MANIFEST attribute to be:
<tstamp>
  <format property="datetime" 
pattern="EEE, MMM dd, yyyy hh:mm:ss a z"/>
</tstamp>

which gives the follow:

Wed, Jul 04, 2001 12:08:56 PM PDT

Which I find much more readable and understandable to people across the world.


Monday, September 20, 2010

How to view the MANIFEST.MF file of a jar

If you do not work with the command line with jars a lot it is easy to forget what the options are. I was surprise to learn that if you do a search for "How to view the MANIFEST.MF file of a jar" you do not find anything that simply tells you how (or even how to in a round about way). I hope this post will solve that.

Say you have a jar file like http.jar from the NetBeans Flower Store example.

View contents of jar

If you want to view the contents of the jar simply use jar tf  jar
mike@mike-laptop:~$ jar tf /home/mike/NetBeansProjects/FlowerRest/build/web/WEB-INF/lib/http.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/sun/
com/sun/net/
com/sun/net/httpserver/
com/sun/net/httpserver/spi/
com/sun/net/httpserver/spi/HttpServerProvider$1.class
com/sun/net/httpserver/spi/HttpServerProvider.class
com/sun/net/httpserver/HttpServer.class
com/sun/net/httpserver/HttpsServer.class
com/sun/net/httpserver/HttpHandler.class
com/sun/net/httpserver/HttpContext.class
com/sun/net/httpserver/HttpsConfigurator.class
com/sun/net/httpserver/HttpExchange.class
com/sun/net/httpserver/Filter$Chain.class
com/sun/net/httpserver/Filter.class
com/sun/net/httpserver/Authenticator$Result.class
com/sun/net/httpserver/Authenticator$Failure.class
com/sun/net/httpserver/Authenticator$Success.class
com/sun/net/httpserver/Authenticator$Retry.class
com/sun/net/httpserver/Authenticator.class
com/sun/net/httpserver/HttpsParameters.class
com/sun/net/httpserver/Headers.class
com/sun/net/httpserver/HttpPrincipal.class
com/sun/net/httpserver/HttpsExchange.class
com/sun/net/httpserver/BasicAuthenticator.class
com/sun/net/httpserver/Base64.class
sun/
sun/net/
sun/net/httpserver/
sun/net/httpserver/ChunkedOutputStream.class
sun/net/httpserver/ExchangeImpl.class
sun/net/httpserver/Request$ReadStream.class
sun/net/httpserver/Request$WriteStream.class
sun/net/httpserver/Request.class
sun/net/httpserver/HttpConnection.class
sun/net/httpserver/LeftOverInputStream.class
sun/net/httpserver/PlaceholderOutputStream.class
sun/net/httpserver/ServerImpl$DefaultExecutor.class
sun/net/httpserver/ServerImpl$Dispatcher.class
sun/net/httpserver/ServerImpl$Exchange$LinkHandler.class
sun/net/httpserver/ServerImpl$Exchange.class
sun/net/httpserver/ServerImpl$ServerTimerTask.class
sun/net/httpserver/ServerImpl$1.class
sun/net/httpserver/ServerImpl.class
sun/net/httpserver/TimeSource.class
sun/net/httpserver/HttpContextImpl.class
sun/net/httpserver/SSLStreams$Parameters.class
sun/net/httpserver/SSLStreams$WrapperResult.class
sun/net/httpserver/SSLStreams$BufType.class
sun/net/httpserver/SSLStreams$EngineWrapper.class
sun/net/httpserver/SSLStreams$InputStream.class
sun/net/httpserver/SSLStreams$OutputStream.class
sun/net/httpserver/SSLStreams$1.class
sun/net/httpserver/SSLStreams.class
sun/net/httpserver/ContextList.class
sun/net/httpserver/Event.class
sun/net/httpserver/AuthFilter.class
sun/net/httpserver/SelectorCache$1.class
sun/net/httpserver/SelectorCache$SelectorWrapper.class
sun/net/httpserver/SelectorCache$CacheCleaner.class
sun/net/httpserver/SelectorCache.class
sun/net/httpserver/WriteFinishedEvent.class
sun/net/httpserver/HttpError.class
sun/net/httpserver/DefaultHttpServerProvider.class
sun/net/httpserver/FixedLengthInputStream.class
sun/net/httpserver/UndefLengthOutputStream.class
sun/net/httpserver/Code.class
sun/net/httpserver/ServerConfig.class
sun/net/httpserver/StreamClosedException.class
sun/net/httpserver/UnmodifiableHeaders.class
sun/net/httpserver/HttpExchangeImpl.class
sun/net/httpserver/HttpsExchangeImpl.class
sun/net/httpserver/HttpServerImpl.class
sun/net/httpserver/HttpsServerImpl.class
sun/net/httpserver/ChunkedInputStream.class
sun/net/httpserver/FixedLengthOutputStream.class

There it is the MANIFEST.MF file, I've highlighted it in red.

Get MANIFEST.MF from jar

To extract the MANIFEST.MF file simply use jar xf  jar META-INF/MANIFEST.MF
mike@mike-laptop:~$ jar xf /home/mike/NetBeansProjects/FlowerRest/build/web/WEB-INF/lib/http.jar META-INF/MANIFEST.MF

View MANIFEST.MF from jar

To view the file use cat META-INF/MANIFEST.MF or edit META-INF/MANIFEST.MF in Windows.
mike@mike-laptop:~$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.5.0 (Sun Microsystems Inc.)

Easy enough.  The xf option on the jar, extracts the file so you may want to delete the extracted META-INF/MANIFEST.MF afterwards.

Tuesday, September 14, 2010

Complex SQL Updates with the Case Statement

One of the most important rules-of-thumb for database performance and data quality is to do as much as you can in as few transactions as possible.  When making Updates to data sometimes you need to do different things based on different conditions.  In order to make these complex Updates and to do them in as few transaction as possible, you'll need some way to make a decision.  One way to make a decision in SQL is by using the Case statement.

Given the following table:
idcountrystatezip
2USAIL60191
3UKAA0
5USACA90210
7UK?99999

Say the state and zip columns are now made nullable so that garbage data will stop being put into it (e.g., UK having state AA and ?, and zip of 0 and 99999).  You'll want to correct the data you have so that you no longer have garbage data in the table.  To do so what you'll want to do is null the state and zip when the country is not USA.


update location
  set
    state = case
      when country = 'USA' then state
      when country <> 'USA' then null
    end,
    zip = case
      when country = 'USA' then zip
      when country <> 'USA' then null
    end


or you can do


update location
  set
    state = case
      when country = 'USA' then state
      else null
    end,
    zip = case
      when country = 'USA' then zip
      else null
    end


or you can replace null with ''.


The table should look like this now:
idcountrystatezip
2USAIL60191
3UK(null)(null)
5USACA90210
7UK(null)(null)

You can also used any other value you can obtain from the table in the Update statement, like this.

Given the following table again:
idcountrystatezip
2USAIL60191
3UKAA0
5USACA90210
7UK?99999

We can do something like this (assuming that state and zip are Character data types).

update location
  set
    state = case
      when country = 'USA' then state
      when country <> 'USA' then country
    end,
    zip = case
      when country = 'USA' then zip
      when country <> 'USA' then country
    end


The table should look like this now:
idcountrystatezip
2USAIL60191
3UKUKUK
5USACA90210
7UKUKUK

Remember if you can picture what you want the data to look like then you can do it in SQL, it is a declarative programming language after all.

Saturday, September 11, 2010

Tracking New Referrals Traffic Through Google Analytics

If you read this blog on a regular basis (thank you) you might notice two tags at the bottom of the site.  These tags have placed this blog on two different blog referral sites.  Since I use Google Analytics to monitor the site, I figured I'd set up Segments to see if the blog referral sites are actually sending traffic to the site.  The goal is for these sites to send 100 new visitors in a month, if they do not then the tags will be removed.

To set up a Segment in Google Analytics you do the following.

1) Click on "Advance Segments" under "My Customizations" on the left.

2) Click the "Create new custom segment" button

3) Drag the "Dimensions" and "Metrics" you want to use to create the Segment


4) Add the Segments to the view by clicking on the "View" button on the top right


5) Now the Segments on show along with everything else on the dashboard.


That is it.  Now I can easily see if these sites are sending any traffic my way.  Feel free to comment.