Sunday, December 17, 2006

Call web service from C# client using basic authentication

The C# class Credentials is used to put authentication information into the SOAP header, not the HTTP header, which would lead to some problem when calling a java-based web service.
In order to invoke web service protected by basic authentication from a C# client, we must override the GetWebRequest() method.
The solution is as follows:
protected override System.Net.WebRequest GetWebRequest(Uri uri){
System.Net.WebRequest request = base.GetWebRequest (uri);
string username = "Test";
string password = "Test";
string auth = username + ":" + password;
byte[] binaryData = new Byte[auth.Length];
binaryData = Encoding.UTF8.GetBytes(auth);auth = Convert.ToBase64String(binaryData);
auth = "Basic " + auth;
request.Headers["AUTHORIZATION"] = auth;
return request;
}
2 related posts can be found at: thescripts developer network , Clinton Davidson's blog .

Thursday, November 02, 2006

Run windows programs in "Run..."

I just got a link to an article that summarizes most programs that could be run using the "start-->Run..." menu.
Some of the important tasks include: compmgmt.msc, services.msc, msconfig, odbccp32.cpl, gpedit.msc, secpol.msc, perfmon, mstsc, and so on.
The link is http://tech.sina.com.cn/s/2006-04-27/0909920026.shtml .
Besides, the "net start" command can be used to run system services, and details can be found on the link http://eastday.yesky.com/net/428/2283928.shtml?287 .

Sunday, October 08, 2006

Displaying Chinese characters using utf-8 code

I took the task of building a website for Association of Chinese Scholars and Students in the Netherlands, Enschede, about 2 years ago. The web server is provided by the Student Network of Twente (SNT), University of Twente. As they didn't have the GB2321 installed on the server, I went through a long way to get the Chinese words display correctly on the website. But I didn't record what I did to make it done at that time.
Several weeks ago, I passed the task of building and maintaining the website to another Chinese student in the computer science department. She found that the website she built could not be displayed correctly, and she asked me for help.
The first thing I did is to check whether she added "<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">" in every page that contains Chinese characters.
Then I checked if she put the .htaccess file that contains the following statements:
AddDefaultCharset off
AddType text/html;charset=utf-8 html
That was one problem for her website, because she didn't use this configuration file. (The usage of .htaccess file is available in http://www.andsky.com/show.php?id=6207 )
After I correct the above issue, the Chinese characters cannot be displayed, still. Finally I asked how she built the website, and she told me that she just used the DreamWeaver under the WinXP OS (Chinese version, I believe). Suddenly I realized that the files of the website must be stored without using UTF-8 code. I used the notepad to open all the web pages and resaved them using UTF-8 code.
Then, the problem is solved! http://www.student.utwente.nl/~acss .

Tuesday, October 03, 2006

J2EE (3): Naming and Directory Services

Naming and Directory Services
A Naming Service provides a mechanism for giving names to objects so that you can retrieve and use those objects without knowing the location of the object.
A Directory Service associates names with objects but provides additional information by associating attributes with the objects.

Why Use a Naming Service
Naming Services provide an indispensable mechanism for decoupling the provider of a service from the consumer of the service. Users of the service need only know the name of the service to use it.

What is JNDI?
JNDI is a Java API that defines an interface to Naming and Directory Services for Java programs. To use JNDI, an implementation of a Naming and Directory must be available. JNDI provides a service-independent interface to underlying Service Provider implementation. Naming services can be plugged into the JNDI by implementing the Service Provider Interface (SPI) for JNDI. Common naming services include DNS, NDS (Novell Directory Services), NIS (Network Information Service), LDAP, CORBA naming systems, and Windows Active Directory.
Use JNDI
Running a JNDI-aware program requires a JNDI service to be running and the classes for that service to be available to the program. By default, running a J2EE server starts a naming service on the same machine.
Obtaining an Initial Context
The first step in using the JNDI name service is to get a context in which to add or find names. The context that represents the entire namespace is called the Initial Context and is represented by a class called javax.naming.InitialContext and is a sub-class of the javax.naming.Context class.
A Context object represents a context that you can use to look up objects or add new objects to the namespace. You can also interrogate the context to get a list of objects bound to that context.
The following code createa an initial context using the default JNDI service information:
Context ctx = new InitialContext();
If something goes wrong when creating the initial context, a NamingException is thrown.
Initial Context Naming Exceptions
The runtime system reports errors in using JNDI as a subclass of NamingException. The exceptions most likely to occur for accessing the initial context include: CommunicationException, NoInitialContextException, and ServiceUnavailableException.
(1) Defining the JNDI Service.
Parameters used to define for the JNDI service include: JNDI service classname, server's DNS host name, socket port number.
There are several ways of defining the JNDI service properties for a program including:
- Add the properties to the JNDI properties file in the Java runtime home directory;
- Provide an application resource file for the program. An application resource file called jndi.properties defines the JNDI service. The JNDI system automatically reads the application resource files from all components in the program's CLASSPATH and from lib/jndi.properties in the Java runtime home directory (jre sub-directory of the JDK home directory). An example file of jndi.properties is as follows:
java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
java.naming.provider.url = localhost:1099
java.naming.factory.url.pkgs = com.sun.enterprise.naming
We can either configure every client's Java home directory to include the necessary JNDI properties or include a suitable JNDI properties file with the client program and distribute everything as a JAR file;
- Specify command-line parameters to be passed to an application. Using the -D option, you can supply the JNDI properties on the java command line of an application. Here's an example:
java -D java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory -D java.naming.provider.url = localhost:1099 MyClass
- Specify parameters to be passed into an applet. For example:
<applet code="MyApplet.class" width="640" height="480">
<param name="java.naming.factory.initial" value="com.sun.enterprise.naming.SerialInitContextFactory>
<param name="java.naming.provider.url" value="localhost:1099">
</applet>
- Hard-code the parameters into the program. This is the least desirable way to specify the JNDI properties. The mechanism for defining the service in code is via a hash table of properties passed into the InitialContext constructor. Here is an example:
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
Context ctx = new InitialContext(env);
Here the code uses symbolic constants from the Context class rather than using strings representing the properties (such as "java.naming.factory.initial"), which makes the code more portable if the property names change in the future versions of Java.
The first two options are the most suited to production environments. They both require you to distribute simple text configuration files with the program.
Binding JNDI Objects
After the initial JNDI context has been obtained, a program can look up existing objects and bind new objects to the context. Binding an object means adding a name to the JNDI service and associating that name with a Java object. The name and object are bound to a context. Here's an example of binding a text message to a name:
import javax.naming.*;
public class JNDIBind{
private final static String JNDI = "Yelei/j2ee";
public static void main(String[] args){
try{
Context ic = new InitialContext();
ic.bind(JNDI, "Learning J2EE as quick as possible");
System.out.println("Bound "+JNDI);
}catch(NamingException ex){
System.err.println(ex);
System.exit(1);
}
}
}
Note that the object to be bound must implement the Serializable interface so that the name server can store a copy of the object.
A bound object normally remains in the namespace until it is unbound. If the bound name remains across server restarts, it is said to be persistent. Commercial servers like NDS, Active Directory, and LDAP are persistent name servers and store the bound names and ancilliary information on disk. Usually the naming services for J2EE product is transient, and it reloads bound objects from configuration files in the SDK home directory whenever it is restarted. The naming service will not retain objects bound with the Context.bind() method when server restarts.
We can use the rebind() method to solve the problem of bind() failing if a name is already bound. For example:
ic.rebind("Yelei/j2ee", "Learn J2EE as quick as possible");
The code unbinds any existing object bound to that name and replace it with the new object.
We can remove an object from a namespace by using the Context.unbind() method. Another use for unbind() is to test if a name is already in use and unbind the old object before binding a new object. The advantage of using unbind() over use rebind() is that you can verify that the object to be unbound is at least of the same type as the new object to be bound:
String JNDI = "Yelei/j2ee";
try{
Object o = ic.lookup(JNDI);
if(o instanceof String)
ic.unbind(JNDI);
}catch(NameNotFoundException nnfe){}
ic.bind(JNDI, "Learn J2EE as quick as possible");
We can rename objects using Context.rename() by specifying the old name and then the new name as parameters. An object must be bound to the old name and the new name must not have a bound object. Otherwise, a NamingException is thrown.
ic.rename("Yelei/j2ee", "Yelei/learnJ2EE");
JNDI Name Lookup
The most common use of JNDI is to look up objects that have been bound to a name. To do this, you require two pieces of information: the JNDI name, and the class of the bound object. Here's an example:
import javax.naming.*;
public class JNDILookup{
private final static String JNDI = "Yelei/j2ee";
public static void main(String[] args){
try{
Context ic = new InitialContext();
String name = (String)ic.lookup(JNDI);
System.out.println(JNDI+" = "+name);
}catch(NamingException ne){
... ...
}catch(ClassCastException cce){
... ...
}
}
}
The above example is an example of a Composite Name. If you need to look up many names in the same context of a composite name (names of the form Yelei/...), it's better to change to sub-context and look up the simple name with that context. Here's an example:
import javax.naming.*;
public class JNDILookupYelei{
public static void main(String[] args){
try{
Context ic = new InitialContext();
Context ctx = (Context)ic.lookup("Yelei");
String name = (String)ctx.look("j2ee");
System.out.println(name);
}catch(NamingException ne){}catch(ClassCastException cce){}
}
}
When dealing with RMI over IIOP object, we cannot cast the returned object the required class; instead, we must narrow it. The implementation of J2EE requires the use of RMI-IIOP to implement the remote interfaces to EJB components. RMI-IIOP uses a portable remote object to encapsulate information about the real remote object. A portable remote object contains information about the real bound object in a portable format that can be interogated by the recipient to find the real remote object. This process of obtaining the real object from the portable remote object is called narrowing.
We can use the PortableRemoteObject.narrow() method in the javax.rmi package to narrow a portable remote object to obtain the actual remote object. This method takes 2 parameters: the object to narrow, a java.lang.Class object defining the real remote object's class. For example:
InitialContext ic = new InitialContext();
Object lookup = ic.lookup("java:com/env/ejb/Agency");
AgencyHome home = (AgencyHome) PortableRemoteObject.narrow(lookup, AgencyHome.class);
Contexts
Contexts provide a hierarchical structure to JNDI names, and composite names group together related names. The initial context provides a top-level view of the namespace and any sub-contexts reflect the hierarchical composite name structure.
You can obtain a listing of the names in a context by using Context.list(). This method returns a list of name and class bindings of type javax.naming.NamingEnumeration, where each element in the enumeration is a javax.NameClassPair object. Here's an example:
import javax.naming.*;
public class JNDIList {

public static void main(String[] args){
try{
Context ctx=new InitialContext();
NamingEnumeration list = ctx.list("Yelei");
while(list.hasMore()){
NameClassPair item=(NameClassPair)list.next();
String cl= item.getClassName();
String name=item.getName();
System.out.println(cl+":"+name);
}
}catch(NamingException ne){
ne.printStackTrace();
System.exit(1);
}
}
}
The parameter to the list() method defines the name of the context to list. It this is the empty string, the method lists the current context. If you want to list the initial context of the J2EE RI namespace, you must have the J2EE RI classes in your search path. Otherwise, you will get an org.omg.CORBA.BAD_PARAM exception caused by another CORBA exception.
The list() method returns the name and the bound object's class name, but not the object itself. It is a lightweight interface designed for browsing the namespace. A second method, called Context.listBindings(), retrieve the object itself. This method returns a NamingEnumeration, where each element is of type javax.naming.Binding. Here's an example:
import javax.naming.*;
public class JNDITree{
public static void main(String[] args){
Context ctx = null;
try{
ctx = new InitialContext();
listContext(ctx, "");
}catch(NamingException ne){
ne.printStackTrace();
System.exit(1);
}
}
private static void listContext(Context ctx, String indent){
try{
NamingEnumeration list = ctx.listBindings("");
while(list.hasMore()){
Binding item = (Binding)list.next();
String className = item.getClassName();
String name = item.getName();
System.out.println(indent+className+" "+name);
Object o = item.getObject();
if(o instanceof javax.naming.Context)
listContext((Context)o, indent+"");
}
}catch(NamingException ex){
System.err.println("List error: "+ex);
System.exit(1);
}
}
}
Binding a composite name will automatically create an intermediate sub-context required to bind the name. For example, binding the name Yelei/j2ee creates sub-contexts "Yelei" and then "Yelei/j2ee" if they don't already exist. We can also create contexts using the Context.createSubcontext() method. This method require a single parameter, the name of the context. If this is a composite name, all intermediate contexts must already exist.
The Context.destroySubcontext() method can destroy contexts. The method removes from the namespace any bound names and sub-contexts with the destroyed context.

More on JNDI Names
It is conventional to use composite names to group related names. As an example, JDBC data sources take names of jdbc/xxx, and EJBs uses the form ejb/xxx.
Incidentally, JNDI calls structured names like the DNS and LDAP compound names. JNDI does not interpret compound names, but simply passes them through to corresponding name service providers.
Besides forward slash (/), JNDI also treats backslash (\), single quote ('), and double quote (") as special characters. If a compound name or a component of a name contains any of these characters, they must be escaped using the backslash character.
You can specify a URL as a parameter to the lookup() and bind() methods in the Context class. For example:
Context ic = new InitialContext();
Object obj = ic.lookup("ldap://localhost:389/cn=Winston,dc=my-domain,dc=com");
This overrides the default context and forces JNDI to perform the lookup against the specified server. Here the class path must contain the necessary service provider classes, and these must be able to process the request bind or lookup operation. In practice, this means that the URL must use the same service provider classes as the initial context.

Attributes
Attributes are a feature of a Directory service and are not available with simple name servers. Typically, you use attributes with an LDAP server. CORBA name server does not support attributes.
An attribute is additional information stored with a name. Directory services usually support searching for names (objects) that have certain attributes defined (or not defined). LDAP uses a schema system to control which attributes an object must define and those that it may define. Any attributes that you add or delete must not break the schema's requirements.
Overview of LDAP X.500 Names
LDAP names conform to the X.500 standard that requires a hierarchical namespace. A Distinguished Name (DN) unambiguously identifies each entry in the directory. The DN consists of the concatenation of the names from the root of the directory tree down to the specific entry. Different implementations of X.500 may use different syntax for representing object names.
LDAP uses a comma-separated list of names with the names specified from the lowest entry up the tree to the higher entries. Names consist of name and value pairs with names typically being those in the following list:
- c (countryName): ISO two letter code for country such as cn, us.
- o (organizationName): organization or company name.
- ou (organizationUnitName): orgainational unit, typically a division or departement within an organization.
- l (localityName): typically defines a location within an organizational unit.
- cn (commonName): sometimes called personal name, usually the name of the user or client.
- dc (domainComponent): a component part of a domain name.
- uid (userid): typically represents a login name.
An example LDAP DN looks like the following:
cn=Yelei Zhang, ou=Developers, o=Tick, c=cn
Using an LDAP Directory Service requires the JNDI properties to specify the JNDI Service Provider from Sun Microsystems and to have an LDAP server running. To configure the JNDI properties to use LDAP, the simplest way is to create an empty jndi.properties file in current directory and add the following lines to this file:
java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
java.naming.provider.url=ldap://localhost:389
The operations on LDAP service are similar to above examples except that there are more operations like manipulating attributes, searching objects, and so forth.

More on Objects
Loading Classes from a Code Base
Java class files can be made available via a web server. A javaCodebase attribute supplies the details of the web server location when binding the object into the JNDI directory namespace. Here is an example of binding a Book object to the LDAP namespace (Book class file is available via a web server):
import javax.naming.*;
import javax.naming.directory.*;
public class JNDICodebase{
private final static String JNDI = "cn=book, o=Agency, c=us";
private final static String codeURL = "http://localhost:8000/classes";
public static void main(String[] args){
try{
DirContext ic = new InitialDirContext();
Book book = new Book("J2EE");
Attributes attrs = new BasicAttributes();
attrs.put("javaCodebase", codeURL);
attrs.put("cn", "book");
ic.rebind(JNDI, book, attrs);
System.out.println("Bound "+JNDI);
}catch(NamingException ex){
System.err.println(ex);
System.exit(1);
}
}
}
With the name registered and the HTTP server running, the client can look up the bound object without having the Book class file in the search path.
References
Sometimes, storing a serialized copy of an object in the Directory Service is inappropriate. JNDI references provide a mechanism for storing an object by reference rather than by value. This mechanism only works if the underlying JNDI Service Provider supports Referenceable objects. A reference to an object in this case require 2 related classes:
- the Object class that must implement the javax.naming.Referenceable interface.
- a Factory class that can create the required objects.

What Else can JNDI Do?
JNDI Events
JNDI supports an event model similar to the event listeners in the Java GUI programming. However, the underlying JNDI service provider must also provide support for this model for a client to register event handlers.
The javax.naming.event package support two types of JNDI event listeners: NamespaceChangeListener (reports on changes of namespace), and ObjectChangeListener (reports on changes on object bindings). You use the EventContext.addNamingListener() method to register a NamingListener object against a context. Adding and removing a listener requires the context to implement the EventContext. Here is an example:
Context ic = new InitialContext();
EventContext ctx = (EventContext)ic.lookup("Yelei");
NamingListener listener = new MyNamingListener();
ctx.addNamingListener("book", EventContext.ONELEVEL_SCOPE, listener);
Security
JNDI security depends on the underlying service provider. Simple services such as RMI and CORBA name service do not support security.
Most live J2EE implementations will make use of LDAP to provide a naming service that supports security. LDAP security is based on 3 categories: anonymous, simple, and Simple Authentication and Security Layer (SASL). The security information is provided in the JNDI properties java.naming.security.authentication, java.naming.security.principal, java.naming.security.credentials.
If you do not define any of these properties, the implementation uses anonymous authentication. You can use a JNDI properties file to supply client authentication information, but more usually you code the information within the client program. If you use strong authentication, the java.naming.security.authentication value can consist of a space-separated list of authentication mechanisms. JNDI can support authentication schems such as external, GSSAPI (Kerberos v5), and Digest MD5.

Friday, September 08, 2006

About .net framework

Source: http://msdn.microsoft.com

Microsoft® .NET is a set of Microsoft software technologies for connecting information, people, systems, and devices. It enables a high level of software integration through the use of Web services—small, discrete, building-block applications that connect to each other as well as to other, larger applications over the Internet.
The .NET Framework consists of two main parts:
the common language runtime (CLR) and a unified, hierarchical class library that includes a revolutionary advance to Active Server Pages (ASP.NET®), an environment for building smart client applications (Windows Forms), and a loosely-coupled data access subsystem (ADO.NET®).

The Microsoft .NET Framework Software Development Kit (SDK) includes the .NET Framework, as well as everything you need to write, build, test, and deploy .NET Framework applications: documentation, samples, and command-line tools and compilers.
The .NET Framework SDK also provides the .NET Framework class library, which is organized into a single hierarchical tree of namespaces.
At the root is the System namespace, which contains objects (including predefined types, such as classes and interfaces) that can be used from any supported language. System objects, which are contained in Mscorlib.dll, are used by all applications. The .NET Framework class library also includes namespaces for abstract base classes and derived class implementations, including those for file I/O, messaging, networking, and security. You can use these classes as is or derive your own classes from them.
Runtime-based class libraries are organized into hierarchical namespaces, and namespaces are stored in portable executable (PE) files — typically DLL and EXE files. You can have several namespaces — including nested namespaces — in one PE file, and you can split a namespace across multiple PE files.
One or more PE files are combined together to create an assembly, which is a physical unit that can be deployed, version numbered, and reused. The runtime uses assemblies to locate and bind to the referenced types.

Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. An assembly provides the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly.

Metadata is binary information describing your program that is stored either in a common language runtime portable executable (PE) file or in memory. When you compile your code into a PE file, metadata is inserted into one portion of the file, while your code is converted to Microsoft intermediate language (MSIL) and inserted into another portion of the file. Every type and member defined and referenced in a module or assembly is described within metadata. When code is executed, the runtime loads metadata into memory and references it to discover information about your code's classes, members, inheritance, and so on.
Metadata describes every type and member defined in your code in a language-neutral manner. Metadata stores the following information:
Description of the assembly: Identity (name, version, culture, public key); The types that are exported; Other assemblies that this assembly depends on; Security permissions needed to run. Description of types: Name, visibility, base class, and interfaces implemented; Members (methods, fields, properties, events, nested types).
Attributes: Additional descriptive elements that modify types and members.

To ensure that your managed code is accessible to developers using any programming language, the .NET Framework provides the Common Language Specification (CLS), which describes a fundamental set of language features and defines rules for how those features are used.
Common Language Specification (CLS) compliance generally refers to the claim that CLS rules and restrictions are being followed.

Thursday, August 24, 2006

Cool Google Tools

I found another interesting site provided by Google called Google Code .
Google released a new product called Project Hosting for open source community to share codes and facilitate software development.
Source: Google Project Hosting .

Wednesday, August 23, 2006

Language Learning

Machine Learning is a very interesting topic for computer scientists.

Source: http://staff.science.uva.nl/~pietera/ALS/

The well-known specialist in neural networks Kohonen developed the idea of Dynamically Expanding Context (DEC) grammars. He applied the concept successfully to, among other things, musical composition. The basic idea behind DEC-grammars is the notion that the occurrence of a symbol in a string is determined by its predecessors. A DEC-grammar consists of rules that determine which symbol can occur in which context. A context is a string symbols that precedes a symbol. A rule of a DEC-grammar has the following form:
X => y
Here ‘X’ is a context, ‘y’ is a symbol. These rules can be used to generate a string of symbols: in context ‘X’ write symbol ‘y’.

The algorithm to learn a DEC-grammar (i.e. to find a DEC-Grammar which describes a particular string) is very simple. Say you want to find a DEC-Grammar for the string ‘^abae’. You start to read the first symbol of the string, and you form the rule:
^ => a
Then you read the next symbol (which is ‘b’) and you form the rule:
a => b
You proceed along these lines, generating rules, until you obtain the grammar:
^ => a
a => b
b => a
a => e
This grammar is not deterministic anymore, because there are two possible successors of ‘a’, namely ‘b’ and ‘e’. In order to repair this you update the rules in the grammar that violate the deterministic structure by expanding their context as necessary: (a => b) is replaced by (^a => b) and (a => e) is replaced by (ba => e). The final grammar becomes:
^ => a
^a => b
b => a
ba => e

Under such a grammar, the algorithm to generate a string (with a maximum length of 200) is:
Begin Pseudo-Code:
Input: DEC-grammar G
CONTEXT := ^
COUNT := 0
While ((COUNT < 200) AND (G contains a rule (X => y) such that there is a (possible empty) string S such that S+X = CONTEXT)) do
COUNT := COUNT + 1
CONTEXT := CONTEXT + y
End While
End Pseudo-Code:


When I was searching some stuff related to generic classes in Java, I found a good web site http://www.java2s.com/ , which contains a lot of useful code in several languages.

Wednesday, August 16, 2006

Creating a Thread Pool with Java

I've mentioned before that J2SE5 proposed a new feature called ThreadPool, but this class cannot really make a pool with a fixed size.
There's good article about creating a thread pool with Java, which I believe it was written before the release of JDK1.5, because it says there's no such a feature in Java. This article is available in Creating a Thread Pool with Java .

Monday, August 14, 2006

Design patterns for J2EE and XML

The Java BluePrints is the main source of J2EE design patterns.

Presentation Layer Patterns
Patterns in presentation layer
(1) the Decorating Filter Pattern involves applying inbound and outbound filters to modify client request and response data across distinct request types. It useful when a general piece of logic needs to be applied globally to many types of user interactions.
(2) the Model-View-Controller Pattern, which uses controller to collect client requests, and update backend data model, and then dispatch the request to the view component which renders the user interface.

Application and Service Layer Patterns
These patterns focus on increasing the flexibility, maintainability, and modularity of the internal application components.
(1) the Service Locator Pattern hides the complexity of locating remote services. The concept behind this pattern is to provide a simple API for obtaining a reference to a service component.
(2) the Business Delegate Pattern is employed to simplify client access to a particular application service. A business delegate object acts as a proxy between application clients and the service, and it's often a dependent object used by servlet and EJBs.
(3) the Value Object Pattern, a value object is a serializable representation of a complex set of data suitable for passing between application tiers via RMI.
(4) the Data Access Object Pattern is useful for interacting synchronously with a persistent data store. It's commonly used to decouple application components, such as EJBs and servlets, from an underlying database.

Issues with system integration

Systems integration refers to the work of gluing together independent applications, services, and data stores.

4 Levels of Integration
Data Level Integration: it's the simplest architectural pattern for systems integration and the quickest to implement by sharing access to an enterprise data store with one or more other applications.
Message Level Integration: it's the most flexible and best performing of the system integration patterns. Data is packaged and transmitted over the network asynchronously for remote processing.
Procedure Level Integration: it's an extension of message level integration that supports synchronous interaction between system. It's generally referred to as remote procedure calls (RPC) or remote function calls (RFC).
Object Level Integration: it can be used between systems that support a common distributed object architecture, such as CORBA or DCOM, in which some well-know objects act as intermediaries between client and server software object. This intermediary (ORB in CORBA) provides a set of services that clients can use to locate a desired server and invoke it. Once the client locates the service, the server object provides a remote stub of itself to the client that it can use locally. On the server side, there's a client skeleton providing the same proxy functionality to the server object.

SOAP
The SOAP specification addresses 4 main areas: message envelope, encoding rules, RPC support and HTTP binding. A basic SOAP message contains SOAP envelope (the wrapping structure), SOAP header (which contains meta-information specific to a SOAP message itself), SOAP body (which contains the payload of the message, such as an application specific request, response, or an asynchronous message). Both the header and the body can be further structured into SOAP blocks. SOAP can be extended to accomodate non-SOAP attachments to basic SOAP messages.

WSDL
A WSDL file contains 5 groups of information:
(1) data types: the <types> element allows you to derive new XML Schema data types for elements used in your WSDL descriptions.
(2) messages: the <message> element defines messages including parameters.
(3) port types: the <portType> elements are abstract constructs that allow grouping of message definitions into operations. They define request/response interaction without regard to the specifics of the protocol used to exchange the messages.
(4) bindings: a <binding> element maps a port type to a communication mechanism, like SOAP.
(5) web service instances: a <service> element describes an instance of a web service, including where it is located (URI), what its interface is (port), and how to interact with it (binding).

Saturday, August 12, 2006

JSP Core

I collected and summarized the main points about JSP, which are listed below:

Standard JSP Statements
1. JSP comments: <%-- comments --%> , it is not visible to the clients
2. JSP declaration: <%! int k = 5; %>
3. JSP expressions and scriptlets: <%= someValue %>, <% java code %> .

JSP Directives
1. include: <%@include file="relativeURLspec" %> .
2. page: <%@page language/ extends/ import/ session/ buffer/ autoFlush/ isThreadSafe/ info/ errorPage/ contentType/ isErrorPage %> .
3. tag lib: <%@taglib uri="tagLibURI" prefix="tagPrefix" %> .

JSP Actions
1. <jsp:forward page="relativeURL" >
<jsp:param name="pName" value="pValue" />
</jsp:forward >
2. <jsp:include page="relativeURL" flush="true" >
<jsp:param name="pName" value="pValue" />
</jsp:include >

Note the value of flush must be set to true. The difference between action of include and directive of include is that jsp include action is executed at run time, while the directive is executed at compile time.
3. <jsp:plugin type="typebean"/ code="classFile"/ codebase="classDir">
</jsp:plugin >
jsp:plugin is used to download the JavaBeans or Applet from the server and execute them at the client's browser.
4. <jsp:useBean id="name" class="className" scope="page request session application" typeSpec >
</jsp:useBean >
jsp:useBean is the core of the java component technology, and it's used to instantiated one or more JavaBean components.
5. <jsp:setProperty name="BeanName" PropertyExpr/>
jsp:setProperty is used to set the property of a JavaBean.
6. <jsp:getProperty name="BeanName" property="propertyName"/ >.

JSP Internal Objects
1. out: it's used to output data to the clients, and manage the output buffer of the application server. Its base class is javax.servlet.jsp.JspWriter.
2. request: it's used to get the input message from the clients. Its base class is javax.servlet.http.HttpServletRequest.
3. response: it deals with http connection messages, such as cookies and HTTP headers. One important method of Response is sendRedirect, to redirect the clients to other pages:
<%response.sendRedirect("http://zhangyelei.blogspot.com");%> .
The repsonse object can also redirect a page by setting the header information:
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn="/newpath/index.html";
response.setHeader("Location",newLocn);
%>
4. exception: it's used to deal with the errors and exceptions during the execution of JSP programms. And it can only be used in a page with a page directive specifying isErrorPage true. The exception object is an instantiated object of the java.lang.Throwable class.
5. config: it's a message sent by the JSP engine to a servlet program when it is initialized. The information includes the parameters for initialization and server information. Use its getServletContext() method can return an application object. The config object is an instantiated object of the javax.servlet.ServletConfig interface.
6. page: it refers to the current JSP program, and it's analogous to "this" in the class file. It's an instantiated object of the java.lang.Object class.
7. application: it's an instantiated object of the javax.servlet.ServletContext interface.
8. session: it's an instantiated object of the javax.servlet.http.HttpSession interface. It's used to store user information.
9. pageContext: it's an intantiated object of the javax.servlet.jsp.PageContext class. It provides accesses to all the JSP objects and namespaces.
10. global.asa file is used to execute some programm when initializing or destroying application and session objects. It has 4 main methods to be defined: applicationInit(), sessionInit(), applicationDestroy(), sessionDestroy().

Servlet
In fact, JSP is developed on the basis of servlet technology, and it's a high-level wrapping of the servlet technolgoy.
Servlet lifecycle:
1. web server loads a servlet.
2. web server instantiates a servlet.
3. web server calls the init() method of the servlet.
4. when the server receives a client request, it create an object of HttpServletRequest, and then an object of HttpServletResponse.
5. web server activates the service() method of the servlet and pass those 2 created objects as parameters.
6. for more requests, web server creates new request and response objects, but still calls the service() method of the same servlet.
7. when the server shuts down, it calls the destroy() method of the servlet.
8. note the init() and destroy() methods only execute once.
Server side include (SSI):
The file use SSI has an extension .shtml, and it's actually a text file containing the include and html tags. SHTML supports 2 kinds of tags: <servlet> and <include> .
The javax.servlet package:
It contains 4 packages: javax.servlet, javax.servlet.http, javax.servlet.jsp and javax.servlet.jsp.tagext. The first one is the basis of the whole servlet API; the second one is the HTTP extension of servlet; the third one is the JSP extension of the servlet; the last one is used to support Tag Library.
javax.servlet: contains ServletConfig interface, ServletContext interface, ServletRequest interface, ServletResponse interface, RequestDispatcher interface, ServletInputStream interface, and ServletOutputStream interface.
javax.servlet.http: contains HttpServletRequest interface, HttpServletResponse interface, HttpSession interface, HttpServlet class, Cookie class, and HttpUtils class.

Friday, August 11, 2006

Concepts in J2EE development

Middleware: it exists to overcome the difference between different computing platforms, and exposes common set of services across platforms and provides a homogeneous computing environment in which distributed application can be built, such as JNDI, JDBC, JMS, JTA, JAF, JAXP and JAAS.

Open System: a system in which components can be implemented in different ways and executed in a variety of environments.

Development Methodology: it defines a process for building software, including the steps to be taken and roles to be played by project team members.
RUP: Rational Unified Process provides a detailed process for object-oriented development, defining a complicated web of processes, activities and tasks to be undertaken by team members in clearly defined roles. It's used for developing a complicated system over a longer timeframe. It might be too thick for J2EE.
EXtreme Programming (XP): it's a lightweight methodology for hackers. It goes from requirements gathering to coding test cases to coding functionality. The number of user stories implemented and the percentage of test cases running successfully at the moment are the measure of success. It's often not appropriate for large development projects. Once concer with XP is that it does not produce sufficient analysis and design documentattion, whcih can be essential in the ongoing maintenance of a system.

J2EE Development Tools:
(1) analysis and design tools: 2 of the most common choices are Rational Rose by Rational Software and Together Control Center by TogetherSoft Corporation.
(2) development tools: 3 of the most commercial IDEs used on J2EE projects are WebGain Studio (integrating best with BEA System's WebLogic development environment), Borland's JBuilder, and IBM's Visual Age (best suitable for WebSphere). If you are using an open source server like JBoss or Enhydra, the most important feature of an IDE may be its ability to integrate with the Ant build tool.
(3) build tools: the deployment of J2EE components involves compiling the components and their related classes, creating deployment descriptors, and packaging the components into JAR, WAR or EAR files. Build tools controls the whole deployment process.The most significant build tool is the Antbuild tool, part of the Aparche Software Foundation's Jakarta open source effort. One of the nicest things about Ant is its protability between operating systems.
(4) source code control tools: the Concurrent Versioning System (CVS) is an open source versioning system used widely throughout the industry.
(5) testing tools: testing can be categorized as unit/functional testing (testing individual components to ensure proper behavior), system testing (testing the functionality of the entire application, including the interactions between components and sub-systems), integration testing (involves testing functionality of the integration between your application and external systems), performance/load testing (used to determine the scalability and discover potential bottlenecks) and user acceptance testing (UAT, getting real users to try the system, examince its functionality, and report gaps between functionality delivered and original requirements). JUnit is a popular open source testing package for Java components, and it has also been extended to do more specific types of testing such as for automated Web testing, there is HTTPUnit, for server-side J2EE testing, there are JUnitEE and Apache's Cactusproject. Testing also involves the use of logging tools like Apache Log4j (logging is built within J2EE methods).
(6) problem tracking tools.

XML-Related Technologies:
(1) XML validation technologies: DTD and XML schema.
(2) XML parsing technologies: an XML parser makes data contained in an XML data structure available to the application that needs to use it. There are 2 distinct modes for XML parsing, event-based model, and document object model (DOM). The first mode is used by the Simple API for XML (SAX). A SAX-based parser reads in the XML data source and makes callbacks to its client application whenever it encounters a distinct section of the XML document, and it cannot look forward in the document during parsing. There are no predefined relationships between nodes in the document. While in the DOM model, the parser will read in an entire XML data source and construct a treelike representation of it in memory.
(3) XML translation technologies: XML translation means to convert and XML data set from one form to another in a generic manner, such as XSLT (eXtensible Stylesheet Language for Transformations). The most popular open source XSLT engine for Java is the Apache Software Foundation's Xalan project.
(4) messaging technologies: SOAP (Simple Object Access Protocol) is a messaging specification describing data encoding and packaging rules for XML-based communication. WSDL (Web Services Description Language) is used to describe web services. UDDI (Universal Description, Discovery and Integration) aims to enable the online registration and lookup of web services via a publicly available repository.
(5) data manipulation and retrieval technologies: XPath, XPointer, XInclude, XLink, XBase, and XQuery.
(6) data storage technologies: storing XML in a textual, unparsed format is inefficient, an alternative mechnism to storing text files is the Persistent Document Object Model (PDOM). PDOM implements the W3C DOM specification but stores the parsed XML document in binary format on the file system. Another alternative to static file system storage is the use of native-XML databases, such as Software AG's Tamino.

Java APIs for XML:
(1) JAXP (Java API for XML Parsing) provides implementation-neutral access to XML parsers and XSLT processors.
(2) JDOM (Java Document Object Model) provides a java-centric, object-oriented implementation of the DOM framework. The use of JDOM does not subvert the JAXP architecture, but builds upon it.
(3) JAXB (Java API for XML Binding) defines a two-way persistent mapping between XML structures and Java objects and enables you to work with XML documents as if they were Java objects. The JAXB development process requires the creation of a DTD and a binding schema- an XML document that defines the mapping between a Java object and its XML schema, which are fed into a schema compiler to generate Java source code. The compiled Java classes handle the details of the XML-Java conversion process. JAXB shows improved performance over SAX and DOM parsers because it's lightweighted and precompiled, but one tradeoff to consider is a loss of system flexibility since any change in your XML or object structures requires recompilation of JAXB classes.
(4) Long Term Java-Beans Persistence provides XML serialization for JavaBean components, which is similar to JAXB. It leverages the JavaBeans component contract instead of a bing schema to define the mapping from Java to XML. Since JavaBeans must define get and set methods for each of their publicly accessible properties, it was possible to develop XML-aware components that can serialize JavaBeans to XML without a binding schema.
(5) JAXM (Java API for XML Messaging) enables the use of SOAP messaging in Java applications, using resource factories in a manner similar to the Java Messaging Service (JMS). The 2 main components of the JAXM architecture are the JAXM Client and Provider. The client provides access to JAXM services from within the application. The provider is responsible for sending and receiving SOAP messages.
(6) JAX-RPC is an XML-RPC implementation API for Java, which is similar to JAXM. Using JAX-RPC, you can expose methods of the beans running in your EJB container to remote Java and non-Java clients.
(7) JAXR (Java API for XML Repositories) provides implementation-neutral access to XML repositories like ebXML and UDDI. It is expected to handle everythign from executing complex registry queries to submitting and updating your own data to a particular registry system.

Source: Kurt A. Gabrick, David, B. Weiss, J2EE and XML development.

Thursday, August 10, 2006

Using Ant in Eclipse

Ant is a widely used compiling tool for many distributed web application. Eclipse provides supports to the Ant tool in the development environment. In order to use Ant Editor in Eclipse, file association should be set first. Or you can just create a new file named "build.xml", the Ant Editor will be the default editor for that file.
There's a basic tutorial which introduces several concepts in Ant and Eclipse, such as ant configurations, ant view, etc. It's available in http://individual.utoronto.ca/kia/ .
Another article from onJava.com: Integrating Ant with Eclipse .
Link from Eclipse online help on this topic: Eclipse Help about Ant .
Sometimes you need to prepare the MANIFEST.MF file to deploy your programs, a tutorial about this is available in Packaging Programs in JAR Files .

Eclipse tricks

1. After setting up the default comment, sometimes you find it does not take effect. The solution to this is to select Window --> Preferences --> Java --> Code Style, and tick "Automatically add comments for new methods and types".
2. When you install eclipse plugins to measure software metrics, you need to enable it first in the Property page of the project.
3. When you want to update your JRE from 1.4 to 1.5 in Eclipse, just modify Window --> Preferences --> Java --> Build Path does not suffice. You also need to modify Window --> Preferences --> Java --> compiler to make it compatible with Java 5.0.

JUnit, it's all about testing

Writing test code sometimes is very annoying for programmers. Many experienced developers turn to a testing framework like JUnit. I read several article about JUnit today, and picked some useful ones to list below:
JUnit Test Infected: Programmers Love Writing Tests , and it's from the JUnit website. I believe it's one of the first articles that teach people about JUnit framework.
Eclipse fans like me can use the following sources:
Using JUnit with Eclipse IDE, it's from http://www.onjava.com .
JUnit Test Tutorial from File systems and Storage Lab (FSL) of CS Department, Stony Brook University.
Unit Testing in Eclipse Using JUnit, from CS Department of North Carolina State University.

Some key points:
Different kinds of tests include:
Unit tests: to check the correctness of several modules (classes).
Customer's tests: functional, system and acceptance tests. All these tests check the behavior of the system as whole. In the XP methodology, these tests are written by the customer, given the program skeleton.
Integration tests: to help test the interaction of several levels of the application.
Developer tests: tests used by developers to verify the whole code, new pieces of code and/or new functions.
JUnit is very useful when dealing with developer's test.

Monday, August 07, 2006

How to calculate execution time

Execution time is a very important measure to evaluate the system performance. The execution time can be computed by reduce the ending time using the starting time of the program.
But how to get the starting and ending time?
I'm used to imagine a solution and check it in Java API. So it's very natural for me to consider the Date class. But unfortunately, Date does not provide a static method for us to get the current time. So I have to create a new Date object whenever I want to retrieve the point of time. I've always been very curious about why Java does not provide a static method.
Today, I happened to find another method to get such a time in a book. The System object has a static method called currentTimeMillis(), which returns the current system time. I finally realize that I have used the previous stupid method for so long!

Thursday, August 03, 2006

Thread pool, J2SE 5's new feature

I just implemented a route searching engine who's able to figure out the shortest path in a maze. As there are thousands of threads running in the program, a thread pool might be a good choice. Thread pool is a new feature of J2SE 5. In my opinion, it's a much better feature than CachedRowSet, which is also a new feature in J2SE5 advertised by SUN.
A thread pool can be acquired by using the following statement:
ExecutorServer threadPool = Executors.newFixedThreadPool(1000);
Then, use threadPool to execute threads:
threadPool.execute(seekerThread1);
I compared 2 implementations using JRE 1.4 and JRE 1.5. The one using thread pool is much more efficient than the one without using thread pool. Maybe it's especially useful for application with a lot of threading work.

Wednesday, August 02, 2006

Apache Tomcat Developer's Guide

Source: http://tomcat.apache.org/tomcat-4.1-doc/appdev/index.html

Standard Directory Layout
root/ -- .html, .jsp files
--- WEB-INF/ -- web.xml
--- classes/ -- .class files
--- lib/ -- .jar files as library

Setup of Ant and Tomcat for Development


  • Configure the Ant custom tasks: The implementation code for the Ant custom tasks is in a JAR file named $CATALINA_HOME/server/lib/catalina-ant.jar. This file must be copied into the lib directory of your Ant installation.
  • Define one or more Tomcat users. --> Use the Manager web application, only user that has the security role manager can login. Tomcat users and roles are specified in a "database" called a Realm. The realm defines the interface between a servlet container and the associated user and role information. Tomcat4 defines a Java interface org.apache.catalina.Realm that can be implemented by "plug in" components to establish the connection between a servlet container and some existing authentication database or mechanism that already exists in the production environment. There are 4 ways to access different sources of authentication information: DataSourceRealm, JDBCRealm, JNDIRealm, and MemoryRealm ($CATALINA_HOME/conf/tomcat-users.xml). Source: http://tomcat.apache.org/tomcat-4.1-doc/realm-howto.html .


Create Project Source Code Directory
mkdir myapp
cd myapp
mkdir docs
mkdir src
mkdir web
cd web
mkdir WEB-INF
cd ..
cvs import -m "Initial Project Creation" myapp myapp start
This set of commands creates the project directory, and defines it within your CVS repository. In the last line, the "Initial Project Creation" is a log message, the first myapp specifies the directory $CVSROOT/myapp to store the project file. The second myapp is a vendor tag, and the start is a release tag. These tags may fill no purpose in this context, but cvs requires them to be present.
The doc directory contains static documentation files to be copied to the docs sub-directory of the distribution. The src directory contains the Java source code to be compiled to the WEB-INF/classes sub-directory of the web application. The web directory contains static web page files.
Later, the project can be exported from the cvs directory using command:
cvs checkout myapp
When some modifications are made to the project files, these changes can be done on the repository using commands like this:
cvs add build.xml
cvs commit
The changes are local to your own development directory until you perform the commit.

Customize Project Properties
The customization is done by creating a file named build.properties in the project's top-level directory. The supported properties are listed in the build.xml file. A sample of this file can be got from this link. At a minimum, you will generally need to define the catalina.home property defining where Tomcat4 is installed, and the manager application username and password. You might end up with something like this:
# Context path to install this application on
app.path=/myapp
# Tomcat4 installation directory
catalina.home=C:\\local\\jakarta-tomcat-4.1.31
# Manager webapp username and password
manager.username=tomcat
manager.password=tomcat
In general, you will NOT want to check the build.properties file in to the CVS repository, because it is unique to each developer's environment. Note the DOUBLE slash in the fourth line, because the catalina.home variable is referenced as string in the build.xml file.

Describe the Web Application
A sample web descriptor can be found here.
cvs add web.xml
cvs commit

Edit Source Code and Pages
Whenever you wish to refresh your development directory to reflect the work performed by other developers, you can use the following cvs commands:
cd myapp
cvs update -dP
The commands to add new files to the repository are already listed above.

Build the Web Application
The Ant tool executes the default "compile" target in the build.xml file, which will compile any new or updated Java code.
cd myapp
ant
To force the recompilation of the entire application, do this:
cd myapp
ant all

Test the Web Application
The quickest way to test the application is to use the custom Ant tasks that are included in the sample build.xml script.
Compile the application: use ant compile or ant (default).
Install the application: use ant install command. This tells Tomcat to immediately start running your application on the context path defined in app.path build property. Tomcat does NOT have to be restarted for this to take effect.
Test the application: use the browser or other testing tools to test the functionality of the application.
Modify and rebuild as needed: when you discover that some changes are required, make those changes in the original source files, not in the output build directory, and reissue the ant compile command. This ensures that your changes will be available to be saved later on, and the output build directory is deleted and recreated as necessary.
Reload the application: Tomcat will recognize changes in JSP pages automatically, but it will continue to use the old versions of any servlet or JavaBean classes until the application is reloaded. You can trigger this by executing the ant reload command.
Remove the application: you can use the ant remove command.
Check the applications: you can use the ant list command to list all the applications deployed on the server.

Creating a Release
Finally, it is time to create the distributed version of your web application that can be deployed on the production server. The following general steps are required:
(1) Issue the command ant all from the project source directory, to rebuild everything from scratch one last time.
(2) Use the cvs tag command to create an identifier for all of the source files utilized to create this release. This allows you to reliably reconstruct a release at a later time.
(3) Issue the command ant dist to create a distributable web application archive (WAR) file, as well as a JAR file containing the corresponding source code.
(4) Package the contents of the dist directory using the tar or zip utility, accroding to the standard release procedures.

Additional Information about CVS
cvs can access a repository by a variety of means. In order to distinguish various ways to access a repository, the repository name can start with an access method. The access method :local: means to access a repository directory. In Unix system, if the repository starts with '/', then :local: is assumed. Otherwise, either :ext: or :server: is assumed. But in Windows OS, you must specify the access method, like :local:C:/local/cvsroot.
To create a repository under windows, we can use:
cvs -d :local:C:/local/cvsroot/ init
Access Remote Repositories
The format for the name of the remote repository is:
[:method:][[user][:password]@]hostname[:[port]]/remote_path

Monday, July 24, 2006

Running Tomcat

Tomcat is a free servlet/JSP container. The use of Tomcat on Windows does not need installation, so I just downloaded it and unpackaged it on the lab computer.
But there was a problem when start running the Tomcat server. When I ran the server using "startup.bat", there appeared an error saying " JAVA_HOME is not defined correctly, it should point to jdk instead to jre" and " the system cannot find file -Dsun.io.useCanonCaches=false". It was ridiculous for the server to give such an error message, because I was 100% sure that the variable was pointing to the JDK directory. So there must be any other missing in the environmental variable. Was it the CATALINA_HOME variable? I tried but it still didn't work.
Finally I found that the classpath variable was not defined in the windowXP system. I added this variable and made it point to the tool.jar in the jdk lib directory. And I found the tomcat server could be run.
So it's really important to check all the environment variables of jdk to make the tomcat work. But the error message in tomcat server is really misleading!

Wednesday, July 12, 2006

Forgot to use "analyze"

I was really unsatisfied about the performance of my event stream processing engine using Threshold Algorithm. Today, I suddenly realized that I didn't update database statistics after I created the indexes, and loaded some data. As a consequence, database could not use the indexes to optimize the query execution plan.
Statistics can be updated using "analyze" command in PostgreSQL and MySQL. After I executed the command, I'm really glad to see that the performance is twice as good as before!

A good article about DB2 performance tuning

Today I took a look at some database performance tuning techniques, and I got a good series of articles about UDB performance tuning written by Roger Sanders in DB2 Magazine.
The links are:
Basic Performance Tuning, part 1
Basic Performance Tuning, part 2
Basic Performance Tuning, part 3
These 3 parts focus on tuning rules, system configuration and database design respectively, and the content is easy to read and comprehend.

Other sources include:
SQL Server Performance Tuning in SQL-Server-Performance.Com
Database Administration with Weblogic , which I believe focuses on the application design domain. (didn't read it yet)
PostgreSQL Performance Tuning, I'm a little bit familiar with it.
Another Performance Tuning of PostgreSQL, everybody likes free products!
SQL Performance Tuning Using Indexes , it seems that the author has a misunderstanding between clustered index and integrated index. Or maybe it's a product-specific issue.
Best Practice for Tuning DB2 UDBv8.1 in IBM Developer Networks.
Getting Started with DB2 Application Tuning, it contains some sources for other technical papers.

Interesting sites:
IBM Information Management Products Training
Oracle Database Site
Oracle 9i Documentation collected by Leiden University
DB2 Magazine
SQL-Server-Performance

Friday, July 07, 2006

Referential Integrity in MySQL

When I work with database products like MS SQL Server, MySQL, PostgreSQL, I have to check the manual very frequently, because the DDL and DML for these products are not exactly the same. Even these statements are universal, it's also hard for me to memorize them all.
User manual of MySQL is not so good for us to immediately get a solution, because there are not many examples and they just provide long, long definitions.
These days I'm working on 2 tables in MySQL which need referential integrity. The statement is too long for me to recall, so I just put it down here for later copying&pasting. The following is a table creation statement which enforces referential integrity to another table:
create table channels(ChannelID integer primary key auto_increment not null, Title varchar(1000) not null, URL varchar(1000) not null, LastUpdated datetime not null, FolderID integer, index (FolderID), foreign key (FolderID) references folders(FolderID) on delete restrict on update restrict);

A source for referential integrity in MySQL: http://www.databasejournal.com/features/mysql/article.php/2248101 .

Thursday, July 06, 2006

First sight at Visual C#

As my laptop crashed several months ago and I have not had it repaired yet, I just do most of my work in the lab. Today I asked the administrator to install the visual C# for me because I would like to play with it and gain some experience with this Java-like language.
The installation process was very very very long (including .net framework, sdk and C# express), like other new products with the "Microsoft" label, although the PC I use has a 3.2 GHz CPU and 1 G memory. It seems that Microsoft can always find the way to consume the hardware resources no matter how much hardware capability has been improved.

However, whenI tried to run it for the first time (again, the first-time run is very slow), I found I really liked it, partly because of its fresh green appearance, just like my blog. After playing with Java for so long, I feel that I'm still fond of the design of the tools from Microsoft. But the price for enjoying developing with Microsoft tools is that you should pay both Microsoft and hardware vendors like Intel first.

This C# Express edition is offered without any costs, which is not usual for Microsoft, who's always trying to sell everything he's got. Anybody who wants to try it can download and install it if your hardwares meet the requirements. Besides, you need to register the product within 30 days. The address for free download is http://msdn.microsoft.com/vstudio/express/visualcsharp/ .
A guy called Bob Tabor provides some video to support the learning of Visual studio products, in the address: http://www.learnvisualstudio.net/ . So, start to enjoy it!

Monday, July 03, 2006

Good article about C# and Java

I just knew that "C#" is pronounced as "C Sharp" in English today. C# is a Microsoft programming product, which I believe is designed by Microsoft to compete with Java. It absorbs a lot of smart features from Java language/platform, which leads to great similarity between these 2 languages. By the way, .NET framework is also similar to J2EE platform.
There's a nice article describing the commonality and difference between C# and Java, which is available in http://msdn.microsoft.com/vstudio/java/gettingstarted/csharpforjava/ .
I always like the capability of nice, convenient GUI design offered by Microsoft Visual Studio products, and I have the feeling that programs written in VB, VC++ are more efficient in run-time than those written in Java on windows platform. So maybe if it's time for me to migrate from Java to C# if C# is really like Microsoft Visual Java :)

Wednesday, June 28, 2006

Issues with Java ArrayList handling streaming data

In my master project called "index processing for complex event detection", I create several data structure to temporarily store and processing streaming data.
I only used data structure implementations provided by JDK 1.4.2 once, but that's where problems occurred. There's a module in my prototype system where a time window is defined and simple events in this window are stored in memory for later processing. The data structure for storing such information is very simple, so I just used ArrayList (It is commented by Java as the fastest list implementation).

During prototype system evaluation, "ArrayIndexOutOfBound" exception frequently appeared in run-time. I checked my code and made sure that everything was OK. According to Java API Specification, ArrayList is supposed to grow automatically. But why is there such an error?
I decreased the generation rate of streaming data, the exception disappeared. After I gradually increased the rate, the exception appeared again, which indicates that there must be something wrong with the automated growth of ArrayList.

Finally, I found out that the implemenation of ArrayList is not synchronized. But I'm still not sure if it will lead to the aforementioned problem during size growth.
There are 2 solutions to this situation: one is to use ensureCapacity() method to declare the minimal capacity for this ArrayList to avoid automatic resize operations when handling large amount of streaming data. The other is to wrapping the unsafe ArrayList.
For simplicity, I just tried the first alternative by giving a suitable initial capacity. The problem never occurs even when handling a more intensive data stream.

Try Google Map API

I think Google Map API is a very cool tool that could flash your personal web site.
I just found it, and I'm starting to read the documentation now.
Hope I can embed a map into this site.

Link of Google Map API : http://www.google.com/apis/maps/

Actually I found it very hard to check out the latitude and longitude of the address outside USA. The following website could provide a little help by querying latlongitude via post code (for European countries): http://www.geonames.org/export/free-geocoding.html . But it's still not very accurate...

Friday, June 23, 2006

Start

Welcome to my blog.
I've been thinking about building such a blog for almost 2 years. But I'm too lazy and too busy to do it until now.
The main purpose of this blog is to keep the list of my favorite web sites, and record some issues related to IT.