Saturday, May 10, 2008

Flex 2 : Intro

One might think why blog about Flex2 .. when Flex 3 is out ?
well even i dont know why ? all i can say is ... I like flex so thought need to start somewhere !

FLEX

Flex application take advantage of Flash player 9 runtime environment ( which inturn supports 3 ) Flex application can also interact with Javascript.

Flex 2 SDK consists of
  1. the Flex framework (class library)
  2. the Flex compilers,
  3. the debugger,
  4. the MXML
  5. ActionScript programming languages
  6. other utilities.

Flex applications consist of MXML ( set up the UI ) and ActionScript source files
  1. MXML is used for
    1. Visual components - set up UI
    2. Non visual components - acess network based components ( like webservice ) server based resorces ( flex data service ) bindings ( between UI & data )
  2. ActionScript - provides flowcontrol and object manupilation features
Flex applications do not directly connect to a database, you use other tools and services
such as PHP, ASP, JSP, Web services, Flex Data Services, ColdFusion Flash Remoting Services,
and so on.

The Flex platform supports a range of deployment models:
  1. Client-side only Applications run on the client and use no server resources.
  2. Access server data through simple RPC services - Applications interact with remote data through web services and HTTP services. The Flex HTTPService and WebService tags let you retrieve data from a remote server For security, by default Flash Player does not allow an application to access a remote data source from a domain other than the domain from which the application was served. Therefore, a remote server must either be in the same domain as the server hosting your application, or the remote server must define a crossdomain.xml file.
  3. Flex Data Services - used to provide advanced features, such as data synchronization, security, and messaging.

Flex supports two versions of Flash Player
  1. standard flash player
  2. debug flash player

Some questions i couldnt answer
doesnt Flex use Http request / response at low level ? if yes then how ?
if yes why is it so fast than JSF etc ? may be because of Flash player ??

May be when i read more will be able to do so

Interesting Link
http://coenraets.org/testdrive/flex4j/index.htm

Thursday, May 8, 2008

Object.hashcode VS System.identityHashCode

basically System.identityHashCode (Object) always returns different values for different objects
Its declared as public static int identityHashCode(Object x)

The hash code for the null reference is zero.

// FileName Test.java
class HashCodeTest
{
int number;
public HashCodeTest(int i)
{ this.number=i;}

public int hashCode() { return this.number; }
}

public class Test
{
public static void main(String[] args)
{
String s1="Namaste"; String s2 = s1;
System.out.println("s1 "+ s1 + " s2 "+ s2);
System.out.println("System.identityHashCode(s1)" + System.identityHashCode(s1));
System.out.println("System.identityHashCode(s2) "+ System.identityHashCode(s2));
s1=s1 + " World";
System.out.println(" s1 "+ s1 + " s2 " + s2);
System.out.println("System.identityHashCode(s1) " + System.identityHashCode(s1));
System.out.println("System.identityHashCode(s2) "+ System.identityHashCode(s2));

HashCodeTest o1 = new HashCodeTest(5);
HashCodeTest o2 = new HashCodeTest(5);

System.out.println("");
System.out.println("First Object : " + o1);
System.out.println("Second Object : " + o2);
System.out.println("System.identityHashCode(First_Object) : " + System.identityHashCode(o1));
System.out.println("System.identityHashCode(Second_Object) : " + System.identityHashCode(o2));
}
}


the output was something like


s1 Namaste s2 Namaste
System.identityHashCode(s1)1671711
System.identityHashCode(s2) 1671711
s1 Namaste World s2 Namaste
System.identityHashCode(s1) 11394033
System.identityHashCode(s2) 1671711

First Object : HashCodeTest@5
Second Object : HashCodeTest@5
System.identityHashCode(First_Object) : 4384790
System.identityHashCode(Second_Object) : 9634993

String.split( ) VS stringTokenizer

tring.split() was introduced in jdk 1.4'

For one thing, StringTokenizer returns one substring at a time whereas the split method returns an array of substrings.
Best to use String's split(...) method. The StringTokenizer class is a legacy class (NOT deprecated)

By default, StringTokenizer uses whitespace to delimit tokens, and if there are two or more delimiter characters
in a row, it collapses them into one delimiter. So the split() equivalent would be

String[] result = "this is a test".split("\\s+");


But if you really want to treat each character as a separate delimiter, you just remove the plus sign again.
This is, in my opinion, the best reason to choose split(); while there is a way to do single-character delimiters in StringTokenizer, it's kludgy and awkward.

And yes, StringTokenizer is inherently faster,

StringTokenizer can split strings based on single characters, split() takes regular expressions.
I imagine that might make split() a bit slower when splitting for single characters (can't be arsed to test it
though), but usually it's more handy.

Monday, May 5, 2008

Business WebService VS Normal WebService

Business WebService demands the following extra features
  • Security - Obvious
  • Context - when one webservice needs to call another can pass security context need to pass extra context parameter.
  • Transactions - Business Transaction Protocol can help us manage it
  • Process Flow - Controlling the process Flow

Thursday, May 1, 2008

SOAP

SOAP - Stands for Simple Object Access Protocol
  • Simple - transport accross HTTP using Http (mime) and SMTP headers using XML. So is XML simple ? It can be as simple or complex u want it to be. Is MIME simple? It is simple only because it uses a data formatting convention that is already in widespread use, is familiar to most IT people, and is conceptually understood by less technical people.
  • Object - has to do with ways of accessing object
  • Access - A key feature of SOAP and web services is their accessibility. Possible to create SOAP binding for almost any protocol.
  • Protocol - Its a rule.
SOAP messages are fundamentally one-way transmissions from a SOAP sender to a SOAP receiver; however, SOAP messages are often combined to implement patterns such as request/response.

whats the use and difference beween xsi and xsd namespace used in SOAP ??
for example xsi:type , and from xsd you get xsd:string or xsd:boolean

Http has a SOAPAction header.

Its easy to write a program that reads XML document and and wraps it inside SOAP message and send to URL. The class org.apache.soap.Envelope ( extends Lang.Object ) has a default constructor. Similarly org.apache.soap.Body has default constructor and method setBodyEntries(java.util.Vector).

// Build the Message.
org.apache.soap.messaging.Message msg = new org.apache.soap.messaging.Message( );
msg.send (new java.net.URL(m_hostURL), URI, envelope);
org.apache.soap.transport.SOAPTransport st = msg.getSOAPTransport ( );
BufferedReader br = st.receive ( );

JAXB

How do you access and use an XML document through the Java ? Two ways
  1. JAXP - Use Java API for XML Processing (JAXP) ( ie either SAX or DOM )
  2. JAXB - Java Architecture for XML Binding (JAXB)

we can modify the second way as two step process

  1. Bind the Schema for XML document into a set of Java classes ( and interface ) that represents the schema. All JAXB implementations provide a tool called a binding compiler to bind a schema (the way the binding compiler is invoked can be implementation-specific).
  2. Unmarshal the document into Java content objects. Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document. The content tree is not a DOM-based tree. In fact, content trees produced through JAXB can be more efficient in terms of memory use than DOM-based trees.

The binding framework comprises three packages. The primary package, javax.xml.bind, contains classes and interfaces for performing operations such as unmarshalling, marshalling, and validation (marshalling and validation will be covered later). A second package, javax.xml.bind.util, contains a number of utility classes. The third package, javax.xml.bind.helper, is designed for JAXB implementation providers.

(1) Get Classes from XML
To unmarshal an XML document, you:

Create a JAXBContext object. This object provides the entry point to the JAXB API. When you create the object, you need to specify a context path. This is a list of one or more package names that contain interfaces generated by the binding compiler. By allowing multiple package names in the context path, JAXB allows you to unmarshal a combination of XML data elements that correspond to different schemas.

import javax.xml.bind.JAXBContext;
JAXBContext jc = JAXBContext.newInstance("test.jaxb");



Create an Unmarshaller object.

import javax.xml.bind.Unmarshaller;
Unmarshaller unmarshaller = jc.createUnmarshaller();


Call the unmarshal method

unmarshaller.setValidating(true);// validate source data against schema
Collection collection= (Collection)unmarshaller.unmarshal(new File( "books.xml"));

Here the Collection is the base xml element not java.util.Collection

Use the get methods in the schema-derived classes to access the XML data.


you can unmarshal XML data from other input sources such as an InputStream object, a URL, or a DOM node. You can even unmarshal transformed XML data.

JAXB also allows you to access XML data without having to unmarshal it. One of the classes generated from a schema, ObjectFactory, contains methods to generate objects for each of the schema-derived interfaces and classes. For example, ObjectFactory class that has methods such as createElement_Name to create a object corresponding to Node in the XML document. You can use these methods to create a tree of content objects without doing any unmarshalling. All your program needs is access to the ObjectFactory class that's in the package for the pertinent schema. Then you can use the appropriate methods in the ObjectFactory class to create the objects you need. After you create the objects, you need to provide their content. To do that, you use the set methods in the objects.

(2) Write XML from Classes
you would:

* Bind the schema for the XML document (if it isn't already bound - same as above)
* Create the content tree.

ObjectFactory objFactory = new ObjectFactory();
Collection collection=(Collection) objFactory.createCollection(); // collection is element name
Collection.BooksType booksType = objFactory.createCollectionTypeBooksType(); // books is element name ( acts as a conatiner )
BookType book = objFactory.createBookType(); // its a element name

Then use set methods in the created objects to specify data values. For example:

book.setItemId("307");
book.setName("JAXB today and beyond");


* Marshal the content tree into the XML document.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
// step 1 - create context
JAXBContext jaxbContext = JAXBContext.newInstance("test.jaxb");
// step 2 - create marshaller
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,new Boolean(true));
// step 3 - call marshal
marshaller.marshal(collection,new FileOutputStream("jaxbOutput.xml"));


Notice that validation is not performed as part of the marshalling operation. In other words, unlike the case for unmarshalling, there is no setValidating method for marshalling. Instead, when marshalling data, you use the Validator class that is a part of the binding framework to validate a content tree against a schema. For example:


import javax.xml.bind.Validator;

Validator validator = jaxbContext.createValidator();
validator.validate(collection))


(3) Updating XML document



References
http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
http://wiki.java.net/bin/view/Javawsxml/JaxbArticles

Java Annotations

Annotations in General
Basically it is the Metadata a syntactitcal way of adding metadata to source doe
Differences in computer languages have given rise to a variety of words for programmer-added metadata, including annotation (Java, Python), attribute (C#), pragma (C), and metadata (HTML).

Java Annotations
Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.Annotations are modifiers you can add to your code and apply to package declarations, type declarations, constructors, methods, fields, parameters, and variables.

JSR 175, A Metadata Facility for the Java Programming Language, gives more details on it.

The Interface java.lang.annotation.Annotation is extended by all annotation types. Note that an interface that manually extends this one does not define an annotation type. Also note that this interface does not itself define an annotation type.

J2SE 5 comes with following prebuilt annotations...

(1) java.lang.Overrides - If a method is decorated with this annotation type but does not override a superclass method, then the compiler will generate an error message... may be if one gives incorrect argument type then function will be overloaded so its better use this.
Should be used only on methods (not on classes, package declarations, or other constructs)

@Override
interface Closable {
void close();
}

class File implements Closable {
@Override
public void close() {
//... close this file...
}
}

The compiler generates an error complaining that File.close doesn't override any method from its superclass. This is because it is not overriding Closable.close, it is implementing it!

While it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.


(2) java.lang.annotation.Documented
This annotation indicates that the annotation to which this is applied is to be documented by javadoc and similar tools. Note that this annotation is just a hint, and a tool can ignore the annotation if it desires to do so.

(3) java.lang.Deprecated
This annotation provides a hint to the Java compiler to warn users if they use the class, method, or field annotated with this annotation.
When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example. The use of the "@" symbol in both Javadoc comments and in annotations is not coincidental—they are related conceptually. Also, note that the Javadoc tag starts with a lowercase "d" and the annotation starts with an uppercase "D".

// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
}


(4) java.lang.annotation.Inherited
Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.


(5) java.lang.annotation.Retention
The Retention annotation takes a single parameter that determines the decorated annotation's availability. The values of this parameter are:

  1. RetentionPolicy.SOURCE: The decorated annotation is available at the source code level only
  2. RetentionPolicy.CLASS: The decorated annotation is available in the source code and compiled class file, but is not loaded into the JVM at runtime
  3. RetentionPolicy.RUNTIME: The decorated annotation is available in the source code, the compiled class file, and is also loaded into the JVM at runtime

By default, all annotations are available at the source level only.

(6) java.lang.annotation.Target
This annotation is used to indicate the type of program element (such as a class, method, or field) to which the declared annotation is applicable. If the Target annotation is not present on an annotation type declaration, then the declared type may be used on any program element. If the Target annotation is present, then the compiler will enforce the specified usage restriction. Legal values for the Target annotation are contained in the java.lang.annotation.ElementType enumeration.

(7) SuppressWarnings

@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
objectOne.deprecatedMethod(); //deprecation warning - suppressed
}

Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked." The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled "Generics"). To suppress more than one category of warnings, use the following syntax:

@SuppressWarnings({"unchecked", "deprecation"})

consider the following code:

int a;
Object object = new Object();

I declared two variables and did nto use them in my code. When I compiled the code, I got 2 warnings:

The local variable a is never read
The local variable object is never read

To remove the warnings, I will use the @SuppressWarnings annotation.

@SuppressWarnings("unused")
Object object = new Object();
@SuppressWarnings("unused")
int a;

// Suppress warnings about missing serialVersionUID
@SuppressWarnings( "serial" ) public void myMethod1()

Note that @SuppressWarnings(value={"unchecked"}) is same as @SuppressWarnings("unchecked")

Assertion Exception

AnnotationTypeMismatchException
Its a runtime exception. Thrown to indicate that a program has attempted to access an element of an annotation whose type has changed after the annotation was compiled (or serialized).

IncompleteAnnotationException
Its a RuntimeException
Thrown to indicate that a program has attempted to access an element of an annotation type that was added to the annotation type definition after the annotation was compiled (or serialized). This exception will not be thrown if the new element has a default value.

Custom Annotation
From Structure point of view Annotations fall into three basic categories:

* Marker annotations have no variables. The annotation simply appears, identified by name, with no additional data supplied. For example, @MarkerAnnotation is a marker annotation. It includes no data, just the annotation name.
An annotation type with no elements is termed a marker annotation type, for example:

/**
* Indicates that the specification of the annotated API element
* is preliminary and subject to change.
*/
public @interface Preliminary { }

It is permissible to omit the parentheses in marker annotations, as shown below:

@Preliminary public class TimeTravel { ... }


* Single-value annotations are similar to markers, but provide a single piece of data. Because only a single bit of data is supplied, you can use a shortcut syntax (assuming the annotation type is defined to accept this syntax): @SingleValueAnnotation("my data"). This should look a lot like a normal Java method call, aside from the @ sign.

In annotations with a single element, the element should be named value, as shown below:

/**
* Associates a copyright notice with the annotated API element.
*/
public @interface Copyright {
String value();
}

It is permissible to omit the element name and equals sign (=) in a single-element annotation whose element name is value, as shown below:

@Copyright("2002 Yoyodyne Propulsion Systems")
public class OscillationOverthruster { ... }


* Full annotations have multiple data members. As a result, you must use a fuller syntax (and the annotation doesn't look quite so much like a normal Java method anymore):
 @FullAnnotation(var1="data value 1", var2="data value 2", var3="data value 3"). <


@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
String[] reviewers(); // Note use of array
}


The annotation type definition looks somewhat like an interface definition where the keyword interface is preceded by the @ character (@ = "AT" as in Annotation Type). The body of the annotation definition above contains annotation type element declarations, which look a lot like methods. Note that they may define optional default values.

// this code lives in a file called Copyright.java
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* embedded copyright
*/
@Documented
@Retention(RetentionPolicy.RUNTIME) @interface Copyright
{
// ------------------------------ FIELDS ------------------------------

/**
* copyright to embed via annotations so it is embedded in all classes that reference it
*/
String defaultCopyright =
"copyright (c) 1998-2008 Roedy Green, "
+ "Canadian Mind Products, http://mindprod.com";
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* get value of this annotation
*
* @return the copyright string
*/
String value() default defaultCopyright;
}


Annotation Uses
In general, annotations benefits falls into three categories: documentation, compiler checking, and code analysis.
  • Documentation : is probably the least relevant reason for adding metadata to the Java language. Javadoc is very powerful.
  • Code Analysis :
  • Compiler Checking : like Override
Annotations allow programmers to decorate Java code with their own attributes. These attributes can be used for code documentation, code generation, and, even during runtime, for providing special services such as enhanced business-level security or special business logic.

Metadata provides a helpful way to indicate if methods are dependent on other methods, if they are incomplete, if a certain class must reference another class, and so on. This is indeed useful, but documentation is probably the least relevant reason for adding metadata to the Java language. Javadoc already provides a fairly easy-to-understand and robust way to document code. Besides, who wants to write a documentation tool when one already exists and works fine for the most part?

Many APIs require a fair amount of boilerplate code. For example, in order to write a JAX-RPC web service, you must provide a paired interface and implementation. This boilerplate could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible.

Other APIs require “side files” to be maintained in parallel with programs. For example JavaBeans requires a BeanInfo class to be maintained in parallel with a bean, and Enterprise JavaBeans (EJB) requires a deployment descriptor. It would be more convenient and less error-prone if the information in these side files were maintained as annotations in the program itself.

used to create documentation
used to track down dependencies in code
used to perform rudimentary compile-time checking.

The more advanced uses of annotations include writing an annotation processor that can read a Java program and take actions based on its annotations.






and should be run by the testing tool:

import java.lang.annotation.*;

/**
* Indicates that the annotated method is a test method.
* This annotation should be used only on parameterless static methods.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test { }

Note that the annotation type declaration is itself annotated. Such annotations are called meta-annotations. The first (@Retention(RetentionPolicy.RUNTIME)) indicates that annotations with this type are to be retained by the VM so they can be read reflectively at run-time. The second (@Target(ElementType.METHOD)) indicates that this annotation type can be used to annotate only method declarations.

Here is the testing tool:

import java.lang.reflect.*;

public class RunTests {
public static void main(String[] args) throws Exception {
int passed = 0, failed = 0;
for (Method m : Class.forName(args[0]).getMethods()) {
if (m.isAnnotationPresent(Test.class)) {
try {
m.invoke(null);
passed++;
} catch (Throwable ex) {
System.out.printf("Test %s failed: %s %n", m, ex.getCause());
failed++;
}
}
}
System.out.printf("Passed: %d, Failed %d%n", passed, failed);
}
}


References *
http://mindprod.com/jgloss/annotations.html
http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html
http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-tiger3.html
http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
http://www.ibm.com/developerworks/java/library/j-annotate1/index.html