Monday, August 2, 2010

Updating Existing Applications via WebSphere Administrative Console (Simple Update)

The purpose of this entry is to provide fellow developers where I work a quick review of how you can deploy an application using the WebSphere Administrative Console. The first step is to bring up the WebSphere Administrative Console using SSL, which can be accessed by going to https://{serverName}:9043/ibm/console, where you replace {serverName} with a dns entry or the IP address of the websphere server. When security is enabled for the console, you will be prompted to enter your user name and password.

Once the home page is displayed you will need to click on "Applications" on the left hand menu and then "Enterprise Applications" which will be displayed after you click on "Applications."

A new screen will be displayed which lists all of the current applications installed on that server, which may actually include applications that are deployed websphere instances on that server (NOTE: A single physical websphere instance can have more than one logical websphere server instances installed on it). To see all of the logical server instances on the box you will click on "Servers" on the left hand navigation pane and then click on "Application Servers."

On the "Enterprise Application" screen you will click on the Checkbox next to the project name you are updating then click on the "Stop" button at the top of the datagrid.

Once the application is stopped you will click on the check mark next to its name again and then click on the "Update" button.

On the "Preparing for the application installation" page you will
  • "Replace the Entire Application" and upload the new EAR file."
  • Select "Prompt me only when additional information is required"
  • Click "Next"
On the next screen you do not need to change any of the settings, just accept the default and click "Next"

Click "Finish" so that the installation actually begins

The screen will then present console output until the installation is complete and then you'll be able to "Save Master Configuration" which will end up taking you back to the "Enterprise Applications" screen.  (NOTE:  If you are working in a clustered environment, then you will want to click on "Rollout Update")

You will need to click on the check mark next to the application you just installed and then click on the "Start" button.

If the application starts successfully you will be presented with a message stating so.  If it does not then you will want to look at the websphere log files which will be in a location like:


\\{physical server name}\e$\WebSphere\AppServer\Profiles\AppSrv\logs\{logical websphere server name}\


If trace is enabled then you will be able to check out the trace.log file to see the error.  If not then the SystemErr.log file will have the error message.

Tuesday, June 22, 2010

Using String.format to easily output arguments like .NET or C++ allows

When I started programming using Java I was surprised when I didn't find an equivalent to the functionality provided by String.format in .NET (or similar functions in C++) which allowed you easily to mix string output and variables without having to deal with manually concatenating the data which to me always makes the code look very very ugly. For example, if you wanted to output the arguments passed to a function in .NET then you could do something like the following

function doSomething(arg1 as String, arg2 as String, arg3 as string) as Object
    logger.finest("Arguments passed are arg1: {0} arg2: {1} arg3: {3}", arg1, arg2, arg3)
    ...
    ...
end function

If we executed the above function as follows:

doSomething("foo", "bar", "donkey")

then the output would be:

Arguments passed are arg1 foo arg2 bar arg3 donkey

To me this solution helps to minimize the compexity of the code and just looks pretty.

In java traditionaly I would traditionally have implemented the same functionality as follows:

function Object doSomething(arg1 as String, arg2 as String, arg3 as string){
    logger.finest("Arguments passed are arg1: " + arg1 + " arg2: " + arg2 + " arg3: " + arg3);
    ...
    ...
}

Besides always having to deal with eclipse trying to insert extra " characters it just looks ugly (especially if you are opening the file in something that doesn't do syntax highlighting.

Thankfully today I discovered that as of Java 1.5 the String.format function does provide the same functionality as .NET (joy)

The code can now be written as:

function Object doSomething(arg1 as String, arg2 as String, arg3 as string){
    logger.finest("Arguments passed are arg1: %s arg2: %s arg3: %s", arg1, arg2, arg3);
    ...
    ...
}

Now doesn't that just look beautiful.

More information can be found at: http://www.rgagnon.com/javadetails/java-0463.html

Friday, April 30, 2010

Delayed environment variable expansion in Windows

Today I got to learn the wonders of "Delayed Environment Variable Expansion" in windows.... this one really hurt my head :)

As a basis for this entry I'm going to use information from another article as the starting point (http://stackoverflow.com/questions/305605/weird-scope-issue-in-bat-file). After I read the article for the first time I still wasn't sure what was going on so I'm going to explain in a little more detail below.

For the example we'll start with the code below:

set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)

When I first looked at the code I throught that I would see "If you see this, it worked" in the command prompt window when the code is ran, but unfrotunately that is not how bat files work... (this really surprised me).

What is really happening is as follows:

1) Windows loads "before" into the variable
2) Windows loads the entire if into memory
3) Windows sets all instances of %VAR% to before
4) when the code hits the following line "if "%VAR%" == "after" @echo If you see this, it worked" the value in %VAR% is "before" because the value was replaced prior to the "set VAR=after" statement and %VAR% will only be equal to "after" once the program exits the if statement's scope

To take this example one step further I added "@Echo %VAR%" at the end of the code (after the if statement) as described below.

set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)
@Echo %VAR%


When the code above executes you get the following output to the command window:

if "before" == "before" (
set VAR=after
if "before" == "after"
)
after

Please note that once the program gets out of the if statement scope the value in %VAR% IS set to "after".


From the articles I've read thus far the system works like this because this is how it was originally implemented. However, there is something called "delayed environment variable expansion" in windows that makes the command file work as we expect. The code with delayed environment variable expansion implemented is provided below:

SETLOCAL EnableDelayedExpansion
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)
@Echo %VAR%


The most important difference with the code provided above is the statement "SETLOCAL EnableDelayedExpansion". When this is set all variables marked with '!' instead of '%' are evaluated at runtime when they are used rather than when the block of code they are in is evaluated.

So with that being said, when the code above is executed you'll get the following output.

set VAR=after
if "!VAR!" == "after"
)
If you see this, it worked
after

Friday, April 23, 2010

Spring 3.0 Restful Web Services -- Flickr Style URLs

I just recently started to use the Spring Framework to create RESTful web services. We chose the Spring Framework because we are going to run in production with WebSphere v6.1 without any of the feature packs installed (ugh... one of these days maybe we'll get a modern application server) and WebSphere v6.1 didn't support the full J2EE spec for Java 1.5 when it was released. In order to support features like EJB 3.0 you must install the feature packs, but I digress.

Also, when I started to use the Spring Framework all of the RESTful web service tutorials used URL templates like the following where 1234233 is the index that identifies a particular book.

http://www.mydomain.com/books/1234233

However I wanted to do something like this:

http://www.mydomain.com?method=genInfo&bookId=1234233

I'm not sure why all of springs tutorials used the first style, but a lot of the RESTful web services that I looked into (ex: Flickr and Twitter) both support the second style.

So, with that being said, how do you create a class that allows you to use URL templates like Twitter.

@Controller
@RequestMapping(value = "/bookController.*")
public class BookController {
    @RequestMapping(value = "?method={method}&bookId={bookId}", params = {
"bookId"})
    public ModelAndView getBookDetails(
        @RequestParam("method") String method,
        @RequestParam("bookId") String bookId) {
        // business logic
    }
}

Please note the use of the @RequestParam attribute in the above example. You cannot use @PathVariable. Also, use params to help the system understand what is required or what shouldn't be there. In the following example I show how to tell the system that it shouldn't allow a particular argument.

@Controller
@RequestMapping(value = "/bookController.*")
public class BookController {
    @RequestMapping(value = "?method={method}&genre={genre}", params = {
"genre", "!publisherId"})
    public ModelAndView getBooksByGenre(
        @RequestParam("method") String method,
        @RequestParam("genre") String genre) {
            // busines logic
    }


    @RequestMapping(value = "?method={method}&genre={genre}&publisherId={publisherId}", params = {
"genre", "publisherId"})
    public ModelAndView getBooksByGenre(
        @RequestParam("method") String method,
        @RequestParam("genre") String genre, 
        @RequestParam("publisherId") String publisherId) {
            // busines logic
    }
}


From what I've found thus far if you have the code like above (pretty much allowing overloaded methods) you must tell the system not to execute the first method if it contains the publisherId parameter.  This is accomplished by using '!publisherId'.

More information about the use of params can be found at Stackoverflow and also in the javadoc for the @RequestMapping annotation.

Thursday, April 22, 2010

Making Criteria Queries Case Sensative in SQL Server 2005

I recently recieved a problem ticket at work because we were showing incorrect data for a particular user. The problem was caused by the fact that the process that creates ids considers ID1234 to be different than id1234. (one of those small requirements issues that gets missed... woops) Anyway I did some searching online and found the COLLATE keyword for SQL Server. The COLLATE keyword allows you to create a SQL statement that overrides the entire databases case sensitivity rules. I have provided an examples below:

Select * from FOO where id ='AbC' COLLATE SQL_Latin1_General_CP1_CS_AS

This would return all rows in the table that had ABC as the ID, but more importantly it would require that A and C be uppercase while b is lowercase. Please note that you could have placed the COLLATE SQL_Latin1_General_CP1_CS_AS text directly after ID instead of including it after the literal.

In addition you could do the following and use the 'IN' keyword.

Select * from FOO where id COLLATE SQL_Latin1_General_CP1_CS_AS IN ('AbC')

The above query again requires that the ID match 'AbC' using case sensative rules. However notice that the COLLATE SQL_Latin1_General_CP1_CS_AS is placed prior to the 'IN' keyword. I have not been able to get SQL Statements that include the 'IN' keyword to work if you put COLLATE SQL_Latin1_General_CP1_CS_AS after the literal.


Some resources I used during my research for this issue are:

Wednesday, March 17, 2010

Forays into JavaScript development: Aptana -- JavaScript plug-in for eclipse

As I have been reintroducing myself to JavaScript development I have been on the prowl for any good JavaScript editors and I think I may have found one. It is an eclipse plug-in called aptana. Some of the features it provides are:
  • Syntax Highlighting

  • Code Completion (In a single JavaScript file or across multiple in the same folder)… NOTE: JSDT allows code completion within a single file, but breaks down when you have multiple JavaScript files.

  • Validation

  • Support for jQuery, Prototype, YUI, Dojo, etc
I have found that it does work well, but there are some caveats when it comes to code completion. In general it requires that you format your classes in a particular way in order for code completion to work properly. For example when using a namespace (without a class (JSON style)) the following works (when you have one JavaScript file or multiple):

Example #1 that works

var foo = {
myMethod: function(){
},
anotherMehtod: function(){
}
}

Also, if you setup all of your methods to be global then it will pick up those methods as well (not recommended).
Another way you can do it is to create a class such as:

Example #2 that works


function foo() {
}
function bar(a, b){
};
foo.prototype.b = bar;
var x = new foo();
x.b

In the above example the key parts to recognize is that you have to define the class and function separately and then relate them using prototype as in foo.prototype.b = bar, where b will be the name of the method when accessing it via the class and bar is the name of the original method. Although I don’t like the extra work required to setup the class, since it provides me with code completion (a feature I dearly love) I find the pain to be sufferable.
So now that I have told you what works, what doesn’t work when declaring namespaces and functions? I have provided examples of those below:

Example #1 that doesn’t work


var foo = new function(value){
this.myMethod= function(){
}
}


Example #2 that doesn’t work


function foo(value){
this.myMethod= function(){
}
}


Example #3 that doesn’t work


var foo = new function() {
}
function bar(a, b){
};
foo.prototype.b = bar;
var x = new foo();
x.b

Saturday, March 13, 2010

jQuery -- Returning XML with ajax

Over the past week I have been working on a project where I am consuming RESTful web services with jQuery's ajax feature (http://api.jquery.com/jQuery.ajax/). Because I don't have a lot of experience with jQuery and RESTful web services I decided that I would use jsMockito (http://jsmockito.org/) and Qunit (jQuery's unit testing framework) to verify that I can successfully consume the web service with jQuery. When I was running my unit tests I started seeing weird error messages in firefox. The error was something like:

XML parsing error: no element found {file name} Line Number 1, Column 1

I started up fiddler in firefox so that I could see the data being sent to and from the server and was able to identify that the XML was actually being returned and that it was valid XML. After some additional research online I found out that jQuery doesn't allow you to retrieve XML from a domain other than teh one you are running on. This means that if you are running your application on http://myCompany then you will not be able to retrieve data (using XML as the dataType) from http://anotherCompany/. After additional analysis I found that you can set the dataType to either script or json in order to be able to retrieve data from a service that is not on the same domain.