Sunday, September 18, 2005

Handling the real hot .Net Framework 2.0

.Net framework 2.0 is still in beta but just shows where Microsoft is going and the world may be going.

I am very much excited about the new version of the framework. And so about C# 2.0 and ASP.Net 2.0.

WebPart is one of the beautiful introductions. Inspired from the Java Portlets, they provide the simplicity.

Right now, I am going through Java Server Faces. I just read the first chapter and was totally frustrated about the complexity and configurations invovled. Why the hell do we need to mention the bean scope as session, context or application in the configuration file. That's once for all. Damn!!!!

As I go through the book, I would be commenting on the JSF and comparing it with ASP.Net. I wish, there was a more powerful initiative as JSPX!!

Monday, June 20, 2005

Multi-Mode Authentication in ASP.Net - I

I had been facing a lot of issues with my new authentication system that I have been trying to implement. The problem with current authentication system is that it works with only one of the types - Forms, Windows or Passport.

I want to have multi-mode authentication with custom authentication systems built in. To explain what I mean, here's a real-life scenario that I faced working with one of my clients:

  • The company has two variations of employees - permanent and temp.
  • Permanent employees have a domain-ID while temp staff works with non-domain ID.
  • Permanent employees have more access than temp staff which normally works through web.
  • How do I integrate the authentication for the two scenarios?

To explain the problem technically, permanent employees would login through Windows authentication mode - handled through a domain controller internally where as the temp staff needs Forms authentication since they don't have a domain-ID.

In the current scenario it is kind of impossible. I posted this problem on newsgroups some time back (around 6 months) to which Paul was courteous enough to send his then unpublished article. The article is now available here

Look at the solution. You get an estimate of the pains? Still not.. try ahead to implement it.

I thought of making a custom authentication mode whereby users can add their own custom authentication modes. It would also be possible to allow more than one mode to work simulaneously.

Well, at the face of it, the authentication modules - WindowsAuthenticationModule, FormsAuthenticationModule and PassportAuthenticationModule - are added to <httpModules> section in machine.config but the modules come into action based on the "Mode" value given in <authentication> section.

See my next posting on Unified Authentication

Monday, February 21, 2005

The New Java (JDK 1.5) and Annotations

I am not sure if annotations is an idea originally from Sun Micrososystems or an offshoot of the Attributes in .Net from Microsoft. I am biased for the latter reasoning.

In anycase, annotations are a good thing to have but, may be, not as good and powerful as what attributes are in .Net. Annotations can reduce your code to a large extend, remove the complexities (and hence, probability of errors) in your code.

One beautiful example of annotations with JAXB-2.0 to persist the classes in XML file (read: XML-serialization) is here.

And you know, it prompts to write about the new features in Java, sspecially on Generics, auto-boxing and unboxing, type-unification, annotations, the MessageFormat class with "{0}" things.

One thing for sure... Java has finally grown up a little bit. May be after 10yrs, but some enhancements in language and at byte-code level is much appreciated. But it's still a little too far from where .Net (and C#) is.

Friday, January 28, 2005

Java Portlets - Inter-Portlet Communication II

Continuing from my previous article, this article describes the alternate method to share data across multiple portlets, by making use of PortletContext.

One can directly make use of setAttribute(String, Object) method to share data across portlets.

In this method, there's a different catch in this method. A porlet that sets attribute is shared by the portlet, no matter how many times it is used in a page or anywhere. It is even shared by all users. Use this method with caution!

I would suggest using this method when a common notification needs to be shown to others. An example of this would be showing unavailability of a service used by a portlet. Real-life example would be a portlet getting latest news from Yahoo should show an available note when the service becomes unavailable.

Think once again before using inter-portlet communication. Is it really required? Cannot you do without it?

Thursday, January 27, 2005

Java Portlets - Inter-Portlet Communication I

Inspired from Portlet Tip 1. Thanks for the comment for the original URL.

There are many articles on this topic that you can search over the net. However, I don't really understand the reason for having so. Well, you can put whole of things in session or portlet-context and that's really the way to do so. Interestingly, when I searched on Google, the top searches were the threads complaining about unable to work on inter-portlet communication.

Here's how you can achieve it using PortletSession.

By default setAttribute(String, Object) method sets the attribute in the scope of the portlet.

Make use of the overloaded method setAttribute(String, Object, int) method with the scope being set as APPLICATION so that it can be shared across portlets. Note, however, that the application scope here limits to the session and is not same as the APPLICATION scope of the servlet.

As such, you can set the attribute in one portlet and get using another. However, there is a catch.

JSR-168 does not specify the order in which the rendering methods - doView, doEdit or doHelp etc - for the various portlets on a page should be called. It is, as such, possible that the order in which you expected a method to be called may not turn out to be so.

But there's a ray of hope. processAction is called before any rendering method. Therefore, it is advisable to use this method to set all attributes that need to be shared across portlets and then be retrieved in rendering methods.

Wednesday, January 19, 2005

Swarming-something

I don't remember the exact word that Narendra told me, but with what he told me, it's going to be the next killer-app. Well, don't look at his profile - he himself is a big killer-app, at least from the photo that he's put up.

What is it all about? The application would act similar to what Napster, KaZaA, BitTorrent are but with something more. One can not only choose who can be the clients and hosts to which the data can be shared but also put restrictions of access rights and also encrypt the data. The best part is - it can be done with a very minimal bandwidth.

I googled for the keywords (that I don't recall right now), but could not find any search results. However, I can say that Narendra - working with a leading national daily - cannot be totally wrong. Whenever there's smoke, there must be fire. And from the reputation of Skype, this can be expected - easily.

Just hoping that this app is out in market soon... coz I need it, IMMEDIATELY!

Tuesday, January 18, 2005

Running Apache Tomcat over SSL

The document provided on the Apache website is quite OK. But at the same time, it may be somewhare confusing for new-comers. This article lists down the steps necessary to import an already available certificate (PFX) to the Java keystore and use it for Tomcat.

1. Dowload OpenSSL tools from www.openssl.org. Windows users may download binaries here.
2. Execute

openssl pkcs12
             -in certificate.pfx
             -out myPEMfile.pem

3. Execute
openssl pkcs12
             -export
             -in myPEMfile.pem
             -out storeFile.p12
             -name "Some Name"

4. Add the storeFile.p12 to the store by executing
keytool -import
             -alias alias-like-tomcat
             -keystore storeFile.p12

5. Update the server.xml configuration file to something like this:
 
<Connector port="8443" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" disableUploadTimeout="true" acceptCount="100" debug="0" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS">
    <Factory className="org.apache.coyote.tomcat5.CoyoteServerSocketFactory" clientAuth="false" protocol="TLS" keystoreType="PKCS12" keystoreFile="mykeystore.p12" keystorePass=yourKeystorePass
    />
</Connector>

Monday, January 17, 2005

Type-Unification, Boxing and Un-boxing

 

This article describes what type-unification is. It also discusses about boxing and un-boxing, and compares what is done in Java (1.4) where not everything is an object.

 

Those working in Java would know about what are known as primitive and composite data-types. For those who've never worked with Java, here's a brief overview of the same and related.

 

Object oriented programming has three pillars - encapsulation, inheritance and polymorphism. C++ has them all. They are implemented by what are known as classes.

 

In C++, you have classes, you can have methods and fields within classes (allowing encapsulation and inheritance) and you can override the methods (polymorphism). And you can have global methods and variables.

 

In Java, you have classes with almost all the features as in C++ but you do not have anything global. Everything has to be within a class. All classes ultimately derive from what is known as "Object".

 

But not everything is an object. Data-types like char, long, short etc are not classes but what are known as primitive data-types, which are always passed by value. All classes are known as composite data-types and are always passed by reference.

 

Primitive data-types are excellent performance-wise. Creating an object using "new" is not very efficient as all objects are placed on heap. Heap management is costlier than stack management, more so for simple objects (like long, char etc use minimal memory). Primitive types are used to create automatic variables and put them on stack (in-line).

 

These data-types are not derived from "Object" and hence, unusable by collections like ArrayList, Hashtable etc. However, they do have "wrapper" classes (in java.lang package) as given below:

 

Primitive Type

Wrapper Class

Primitive Type

Wrapper Class

byte

Byte

char

Character

short

Short

int

Integer

long

Long

float

Float

double

Double

void

Void

 

As such, if you need to use a "long" in a collection, you need to wrap it:

 

[Java]

long l = 10;

Long longObject = new Long(long); // Wrap

 

Long longValue = longObject.longValue(); // Un-wrap


In C# (and Java 1.5), everything is an "Object", be they primitive-type or composite-type.

 

In C#, the so-called primitive types of Java are implemented as what are known as value-types, meaning that they would be passed by value and not be reference.

 

In addition to classes (which are always reference-types), C# has structures and enumerations which are always passed by values. The primitive-types of Java have been implemented as structures in .Net, which are always created on-heap/in-line. These data-types are available directly through simple keywords in C#.

 

 

Java-Type

C# Keyword

.Net-Type (Structures)

byte

byte

System.Byte

short

short

System.Int16

int

int

System.Int32

long

long

System.Int64

float

float

System.Single

double

double

System.Double

char

char

System.Char

boolean

bool

System.Boolean

unsigned short

ushort

UInt16

unsigned int

uint

UInt32

unsigned long

ulong

UInt64

 

 

At the byte-code level, these types are classes derived from special class System.ValueType. You cannot explicitly derive your class from this class.
A structure is directly derived from this abstract class while enumerations are derived from another special (abstract) class System.Enum which in-turn derives from System.ValueType.

 

The screenshots on the next page demonstrate the same.

 


Actual code:

 

 

Disassembled view of the assembly:

 

 

Disassembled view of the System.Enum class:

 

 

Disassembled view of the System.ValueType class:

 

 

 


This brings us to what is known as Type-Unification: each data-type is derived from “Object”, be it value-type or reference-type.

 

The process of conversion of a value-type to a reference-type is known as boxing. The reverse of this is known as un-boxing. Boxing is an automatic process while un-boxing requires explicit type-casting.

 

[C#]

int x = 10;

object o = x; //Auto-Boxing

int y = (int)o; //Un-boxing, explicit type-casting

 

It has a strong advantage that one can pass a "short" to any method that accepts "Object" as the parameter without a need for wrapping.

 

It is important to note that both boxing and un-boxing, like wrapping and unwrapping, are costly processes.

 

During boxing, memory is allocated on heap. The value is copied onto the heap and the reference is provided. During un-boxing, using the reference, the value is extracted from the heap and copied to the variable on stack.

 

Note that Java 1.5 has taken the path of .Net and introduced type-unification, boxing and un-boxing.