Monday, February 22, 2010

Weblogic 9 setup

  • Create domain directory

    1. Go to {weblogic.dir}\weblogic92\common\bin and run config.cmd
    2. Select Create a new Weblogic domain
    3. Select the source (automatic or with template)
    4. Input admin username and password
    5. Choose startup mode and JDK. Use JRocket for JDK.
    6. Enter domain name & location and click Create
    7. Check Start Admin Server and click Done

  • Change port number

    1. Go to http://{hostname/localhost}:7001/console and login
    2. Navigate to {domain name}->Environment-Servers. Select AdminServer(admin)
    3. Change the Listen Port and save changes.,/li>
    4. Activate changes

  • Add the db2jcc jars for the DB2 driver classes

    1. Go to your DB2 installation or <sqllib_home>\java directory
    2. Copy the following jars to your <weblogic_domain>\lib directory:
      • db2jcc.jar
      • db2jcc_license_cu.jar
      • db2jcc_license_cisuz.jar
    3. Restart server
  • Add JDBC Data Source

    1. Navigate to {domain name}->Services->JDBC->Data Sources. Add new data source.
    2. Set Data Source properties and click Next
      Name: db2Pool
      JNDI Name: db2Pool
      Database Type: DB2
      Database Driver: IBM's DB2 Driver(Type 2)
    3. Select transaction options (use defaults)
    4. Set connection properties and click Next
      Database Name: db2test
      Host Name: 
      Port:
      Database User Name: coreusrt
      Password:
    5. Test Database Connection. Update the following:
      Driver Class Name: com.ibm.db2.jcc.DB2Driver
      URL: jdbc:db2://db2t.vigslbp.bear.com:5510/DB2TEST
      click Test Configuration and Next
    6. Select the server and click Finish
    7. Activate changes

  • Deploy Web App
    Note: Make sure that the WEB-INF/lib of the webapp contains all needed lib (ex. by copy.runtime.lib)

    1. Navigate to {domain name}->Deployments. Click Install
    2. Select the path of your application root directory (webapp directory). Click Next
    3. Select Install this deployment as an application and click Next
    4. Set settings (use default) and click Next
    5. Review choices and click Finish
    6. Activate changes
    7. Navigate back to {domain name}->Deployments. Select webapp and click Start servicing all requests. Start deployment

  • Finish! You may now go to your webapp.
    Sample: http://pbctx60.bsna.bsroot.bear.com:7831/acctsvcs-nae/nae/acctvwup?action=showPage

Tuesday, February 16, 2010

Java Annotations

  • provides data about a program but do not directly affect the program semantics

  • defined similar to interface
    @interface Author {
    String author();
    String date();
    String[] reviewers(); // Note use of array
    }

  • sample use:
    @Author(
    name = "Benjamin Franklin",
    date = "3/27/2003",
    reviewers = {"Alice", "Bob", "Cindy"} // Note array notation
    )
    class MyClass() { }


3 Predefined Annotation Types:
1) @Deprecated - should also be documented using the Javadoc @deprecated tag
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated

2) @Override

3) @SuppressWarnings
@SuppressWarnings("deprecation") //deprecation warning - suppressed
@SuppressWarnings({"unchecked", "deprecation"}) // legacy code & deprecation warning - suppressed

Detailed explanation here: http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

Tuesday, September 22, 2009

Java Enum Type

How do you represent enumerated constants in your java code? It probably will look something like this:
public static final int STATUS_ACTIVE = 0;
public static final int STATUS_PENDING = 1;
public static final int STATUS_CLOSED = 2;



This way of representation poses some problems. So in Java 5.0 Enum Type is introduced. Now you can write your code as,
public enum Status { ACTIVE, PENDING, CLOSED }
and refer to it by Status.ACTIVE, Status.PENDING and Status.CLOSED. The reserved word enum is used like class.


The links below are some discussions on what, how and when they should be used.

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
http://www.ajaxonomy.com/2007/java/making-the-most-of-java-50-enum-tricks

Monday, July 27, 2009

Java nested classes

I usually get confused with nested classes so I'll note some important points here.

Nested classes
- is also a member of enclosing class like variables and methods
- can use all access modifiers like variables and methods

Types of nested classes
1) static
- like a static method, it is not associated to any instance of enclosing class and has access only to all the static members
- syntax: Outer.Inner i = new Outer.Inner();

2) non-static / inner
- has access to other members of enclosing class even if declared private
- like non-static methods, it cannot define any static members as static members only belong to the (outer) class
- syntax: Outer.Inner i = new Outer().new Inner();

3) local inner - declared and known only within the body of method/block

4) anonymous inner - no name and known only within the statement in w/c they are defined

Thursday, July 9, 2009

Java modifiers summary

ModifierClassInterfaceInner ClassInner InterfaceVariableMethodConstructorFree-Floating Block
publicyesyesyesyesyesyesyesno
protectednonoyesyesyesyesyesno
none or package or defaultyesyesyesyesyesyesyesyes
privatenonoyesyesyesyesyesno
finalyesnoyesnoyesyesnono
abstractyesyes/noyesyes/nonoyesnono
staticnonoyesyesyesyesnoyes
nativenononononoyesnono
transientnonononoyesnonono
volatilenonononoyesnonono
synchronizednononononoyesnoyes
strictfpyesyesyesyesnoyesyesno

Points:
  • all access modifiers can be used to classes and members except for protected and private which cannot be used to outer classes and interfaces

  • free-floating block cannot have access modifiers. it can only use static or synchronized

  • constructors can only use access modifiers and strictfp

  • native is only for methods

  • transient and volatile are only for variables. Transient indicates that it is not serializable. Volatile indicates that it can be modified simultaneously by many threads.

  • synchronized can only be used on methods or free-floating blocks

  • strictfp can't be used on variables or free-floating blocks

    Using strictfp ensures that you get the same result of floating-point expressions across multiple platforms. But it may also result to overflow or underflow hence the expression, "Write-Once-Get-Equally-Wrong-Results-Everywhere". If you don't use strictfp, JVM can calculate floating-point expressions however it want and thus could produce more accurate results.

Thursday, July 2, 2009

Getting started with Java

1) download the Java EE Software Development Kit (SDK)
- download link: http://www.oracle.com/technetwork/java/javaee/downloads/index.html

What Java Do I Need?
You must have a copy of the JRE (Java Runtime Environment) on your system to run Java applications and applets. To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.

What's the difference between J2SE and J2EE?
J2SE has access to all of the SE libraries. However, EE adds a set of libraries for dealing with enterprise applications such as Servlets, JSP and Enterprise
Javabeans.


2) install JDK
- Windows instructions/troubleshooting: http://java.sun.com/javase/6/webnotes/install/jdk/install-windows.html

Why shouldn't I install in "C:\Program Files\Java"?
Some apps (e.g. maven, plugins) uses your Java path without considering potential whitespace on the path causing "C:\Program Files\Java" to become "C:\Program" w/c leads to errors. So either use a path w/out whitespace or set you path to JAVA_HOME=C:\Progra~1\Java not JAVA_HOME=C:\Program Files\Java


3) update environment variables (NOT case-sensitive under Windows)
PATH
- defines the search paths for executable programs (with file extension of ".exe", ".bat" or ".com" for Windows systems) invoked from a command shell ("cmd.exe")
- allows the use of javac and java
- if not set, you need to specify the full path to the executable every time you run it, such as: C:\Program Files\Java\jdk1.6.0\bin\javac MyClass.java
- so add here the JDK binary (bin) directory (e.g., "c:\jdk1.6\bin")
- Note: The JDK binary directory should be listed before "c:\windows\system32" and "c:\windows" in the PATH. This is because some Windows systems provide their own Java runtime (which is often outdated) in these directories (try search for "java.exe" in your computer!).

CLASSPATH
- defines the directories and Java's jar-files for searching for the Java classes referenced in a Java program
- normally, no explicit CLASSPATH setting is required
- if not set, default is current working directory (since JDK 1.3)
- if set, include the current working directory '.'
- link: How Classes are found

JAVA_HOME
- needed for running Tomcat and many Java applications
- set here the JDK installation directory, e.g., "c:\jdk1.6

How to set environment variables in Mac/Unix
1. Set environment variables for your user in ~/.bash_profile (will affect bash shells only).
Create the file if it does not exist:
touch ~/.bash_profile
Open the file:
open ~/.bash_profile
Add environment variables in the file:
export JAVA_HOME=$(/usr/libexec/java_home)
export JRE_HOME=$(/usr/libexec/java_home)
2. Set temporary environment variables for the current bash shell. Just type the same command in 1. to the current bash shell.

Ref: http://www3.ntu.edu.sg/home/ehchua/programming/howto/environment_variables.html

Wednesday, April 22, 2009

Design Patterns

Definitions on this blog came from the book Head First Design Patterns.

  • Strategy pattern - defines a family of algorithms, ancapsulate each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

  • From http://www.ida.liu.se

  • Observer pattern - defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
    - think Swing/GUI

  • From http://kur2003.if.itb.ac.id

    Using Java's built-in Observer pattern (allows observer to pull data from observable)...
    From http://kur2003.if.itb.ac.id


  • Decorator pattern - attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
    - think Java I/O

  • From http://oreilly.com



Ref:
Head First Design Patterns by By Eric Freeman, Elisabeth Robson, Kathy Sierra, and Bert Bates