Archive Page 2

Tired of Struts, try Stripes


Ever felt the pain of creating struts-config.xml and pouring all the information linking your ActionBeans, Action classes and your jsp pages in your Struts application? See how Struts has been cheating you to write too much of information. Try out Stripes.

-rwsr-xr-x Unix file permission

Everybody handling a Unix operating system would very well know what chmod 777 means. That the owner, group and the user of the file is given all permissions (Read, Write and Execute on a particular file). This could otherwise be written as “chmod ugo+rwx “. Meaning that you are giving User, Group and Owner of the file, the rights to Read, Write and Execute the file.

Here comes the rws scenario. Best example that is available for this rws is /usr/bin/passwd command (just issue a “ls -l /usr/bin/passwd”) .

Normally, any user is allowed change HIS password. Meaning he can make an entry or change HIS entry in the /etc/passwd file. But he can never be given ‘WRITE’ permissions on the file because he might end up disturbing other person’s password too. Only a ROOT user is allowed permissions on the /etc/passwd file.

This is where the “rws” comes to picture. When we give “rws” permission to the /usr/bin/passwd command, Unix would assume that the command is executed by the ROOT user. (the user doesnt have permissions on the /etc/passwd file but the root user has). Root user (RWS) permissions could be given on a file as chmod 4700 .

arun@arun-desktop:~/Desktop$ chmod 4700 hi.txt
arun@arun-desktop:~/Desktop$ ls -l hi.txt
-rws—— 1 arun arun 0 2007-01-17 06:48 hi.txt

If you need to act as a group user of a file and not a normal user when executing a particular command (as against the root user) then user “chmod 2700 ”

arun@arun-desktop:~/Desktop$ chmod 2700 hi.txt
arun@arun-desktop:~/Desktop$ ls -l hi.txt
-rwx–S— 1 arun arun 0 2007-01-17 06:48 hi.txt

The 4 and 2 in the front of the chmod commands are called as SUID and SGID bits.

What if we put a 1 instead of 4 and 2 (chmod 1700 ).

arun@arun-desktop:~/Desktop$ chmod 1700 hi.txt
arun@arun-desktop:~/Desktop$ ls -l hi.txt
-rwx—–T 1 arun arun 0 2007-01-17 06:48 hi.txt

It shows a “T” in the place of “x” for a normal user. This “T” bit is called as the Sticky bit.

“When the sticky bit is turned on for a directory users can have read and/or write permissions for that directory, but they can only remove or rename files that they own. The sticky bit on a file tells the operating system that the file will be executed frequently. Files like this are kept in swap space even when they aren’t being executed. Although this takes up swap space it greatly reduces the time it takes to execute the program. Some programs such as vi have the sticky bit turned on by default on some Unixes.”

source : http://www.uwsg.iu.edu

Classloading in action

Here is a sample code for custom classloaders and the hierarchical relationship that i wrote for my personal learning. Thought it would be useful to you.In the example, I have two classes (Class1 and Class2) whose implementation is this

package com.beanpicks.classes;

public class Class1 {

}

:-) Class2 is also the same.

I am trying to load these two classes with the help of two different ClassLoaders (FirstClassLoader and SecondClassLoader). Both have the same implementation. I just created two ClassLoaders just to see how the hierarchical classloading works.

And finally, i have a Client class which does the actual plumbing job. Here are its usecases

1) Instantiates the two classloaders
2) Loads Class1 using FirstClassLoader
3) Loads Class2 using SecondClassLoader
4) Lists all the classes loaded under both ClassLoaders (using the method
listAllClasses(ClassLoader whatClassLoaderYouNeedToList))


So, the main method looks like this.


//Instantiate the FirstClassLoader
FirstClassLoader firstClassLoader = new FirstClassLoader(classesFolder);
//Load Class1 using FirstClassLoader
firstClassLoader.loadClass(“com.beanpicks.classes.Class1″);
//List all the list of classes that are loaded in FirstClassLoader
listLoadedClasses(firstClassLoader);

The only complicated thing in the client class is the implementation of the listAllClasses (i use a lot of reflection in there). Here is the gist of what i am doing

1) Get the Class TYPE of the ClassLoader using the ClassLoader.getClass();
2) Get the special Field called “classes” from the java.lang.ClassLoader class using the
class.getDeclaredField().

ie. i am getting it from the parent of all custom class loaders.
This MASTER Field has all the classes loaded by all its
sub-classloaders.

3) In order to get the classes loaded by FirstClassLoader, we need to pass the
FirstClassLoader into the get method
of the Field.
4) the get method returns a List. Iterate it.

Now, we can just check out the implementation of the FirstClassLoader

I have overridden loadClass and the findClass methods of the classloader. What i am doing in the loadClass is that i am simply called the findClass method. I know its the dumbest of the implementation. But then, i need to load the System classes too. (otherwise the compiler will complain for the missing java.lang.Object).

So my implementation of the loadClass method is

public Class loadClass(String className) throws ClassNotFoundException{

if (className.startsWith(“java.”)) {
return findSystemClass(className);
}
return findClass(className);
}

then the findClass method

public Class findClass(String name)throws ClassNotFoundException{

byte[] classBytes = findClassBytes(name);
if (classBytes==null){
throw new ClassNotFoundException();
}
else{
return defineClass(name, classBytes, 0, classBytes.length);
}
}

As you can see, i am just getting the bytes of the class file the findClassBytes method. Then calling the defineClass magic method.

finally, the findClassBytes

public byte[] findClassBytes(String className){

try{
String pathName = classesFolder +
File.separatorChar + className.replace(‘.’, File.separatorChar)+ “.class”;
FileInputStream inFile = new FileInputStream(pathName);
byte[] classBytes = new byte[inFile.available()];
inFile.read(classBytes);
return classBytes;
}
catch (java.io.IOException ioEx){
return null;
}
}

I am getting the fully qualified name of the class, replace the “.” with a “/” (File.separatorChar) and load the bytes. Done.

Here is the output of the program

**********************************************************
ClassLoader : com.beanpicks.classloaders.FirstClassLoader@130c19b
**********************************************************
class com.beanpicks.classes.Class1
class com.beanpicks.client.Client
class com.beanpicks.classloaders.FirstClassLoader
class com.beanpicks.classloaders.SecondClassLoader

**********************************************************
ClassLoader : com.beanpicks.classloaders.SecondClassLoader@35ce36
**********************************************************
class com.beanpicks.classes.Class2
class com.beanpicks.client.Client
class com.beanpicks.classloaders.FirstClassLoader
class com.beanpicks.classloaders.SecondClassLoader

As you can see, the firstclassloader loads Class1. Its parent, the ApplicationClassLoader loads the Client, FirstClassLoader and the SecondClassLoader. The Classes loaded by the ApplicationClassLoader is also listed because i am looping for the entire classloading hierarchy in the main method.

while (currentClassLoader != null) {

listAllClasses(currentClassLoader);
//switching to a parent classloader
currentClassLoader = currentClassLoader.getParent();
}

As you understand, currentClassLoader will be null when it meets the bootstrap loader.

Classloading — who carries my class???

Class loading is always an interesting discussion for any Java developer. Here are a few quintessential concepts.

We know that every class that we write and work on runs on the JVM. And we know that before doing the actual functionality that the class is written for (ie running on the JVM for the functionality), it gets loaded. As you already know that class is loaded only once. ie. one per TYPE and not one per object. So, if you have 20 thousand String objects in your heap, you just have one java.lang.String TYPE per classloader. And this TYPE is represented as java.lang.Class.You have a java.lang.Class representation for every single thing that exists in java. For objects you can get access to this type using the getClass() method (available in the java.lang.Object). For primitives you have a special variable called “class” in it. (eg. int.class)

JVM loads these classes using various ClassLoaders. You can just see how many classes are loaded before your actual class is run by just giving a java -verbose .

There are basically three class loaders that are needed before even your actual class gets started.

Bootstrap loader which loads the core Java classes (starts from java.lang.Object, if you look a the java -verbose)

Extension class loader loads all the jar files that are present in your jre/lib/ext folder

System class loader which loads all the jars and classes you specify in the CLASSPATH variable in your machine

and finally, your own custom application class loader.

The best example for a custom class loading that could be quoted is the J2EE servers. They have their own class loaders for dynamically loading classes into itself. And thats the reason why changes that we make in the code is reflected without even restarting the server (for hot deployments).

class hierarchy

© ibm.com

As you could see from the picture, the class loaders are hierarchical. Meaning that before your custom class loader loads a class, it consults with the system class loader which in turn calls its parent till it reaches the Bootstrap loader. For the first time, when a class is loaded, the ClassLoader would read all the bytes and deploy it into the JVM. Second time, when the same TYPE comes up, it uses the cached version, so that it need not traverse through the hierarchy again.

Again, the same class could never be deployed by two classloaders in the same hierarchy. Classes that are loaded in the topmost hierarchy prevails if there is a duplication.

But the same class could be loaded in two different hierarchies. WHAT???

identity crisis
© ibm.com

The picture clearly shows that two classes and interfaces are loaded in different hierarchies.

Lets consider a situation where class Class1 loaded by ClassLoader1 tries to access Class2 loaded by ClassLoader2. And that’s the job of the ClassNotFoundException.

MHT Archives in Mozilla

If you were an Internet Explorer user and recently migrated to Mozilla, just because you finally decided to ditch that buggy OS and embrace the lovely Linux, you would have missed a few good (accidental) features of IE.

One, the creation of MHT archives is not possible in the “Vanilla” Mozilla. Personally, i hate creating a folder and a html page for every single page i save from the internet. I know you too are. We need it in one single pack. Like the MHT archive in IE. Here comes the solution for Mozilla.

Find the plugin here.

http://atrey.karlin.mff.cuni.cz/~sanda/maf/

Just open the file with Firefox and you are done with the installation process. It works fine with my Firefox 2.0.

Two, the Image Toolbar in IE that comes up when we move our mouse over an image. Mozilla does provide the same feature but as an add-on

Click Tools –> Add ons –> Get extensions

image toolbar

Search for Image Toolbar in the “Find More Addons” (our stuff comes up in the second page)

There are a lot, lot and lots of add-ons available there. You could just browse and find yourself lost. There was also some plugin for Orkut !!!

Here is my list of addons.

toobar list

Mozilla is ruling the internet. Same is the effect on me.

Strange UPDATE clause in Oracle

master table

staging_table.png

I had this problem at work a few weeks back and thought i could share with you. Most of you must have known this. But yet, I thought this post could be useful so that i could come back here and refresh myself.

I have a staging table, a few colums of which are populated from direct flat file loads (Oracle SQL Loader utility). I need to populate a few other columns of this table looking up from a master table. Finally, after all columns has been populated, i move the staging table records to a “fair copy table”.

Let me give you an example. Our staging table will be staging_table. Four columns of it (key_to_all_locks, flat_file_load_colm_1, flat_file_load_colm_2 and flat_file_load_colm_3) are flat file loads. (please look the screenshot for full description)

I have a master table master_table which has many columns. Out of which i need to lookup three columns (lookup_colm1, lookup_colm2 and lookup_colm3) using the key_to_all_locks column.

And i will do a lookup only when the lookup_or_not_flag is ‘Y’. And i should not unnecessarily lookup for the ‘already populated records’ from the master_table.

One other information, a single flat file load will populate a minimum of 10K records into the staging_table.

Simple problem. But if i had to do this the Java way, i need to execute a select query for the records whose values are not yet populated from the master_table and then loop through the ResultSet. Meaning, i should avoid lookups for those records for which the flat_file_load_colm_1, flat_file_load_colm_2 and flat_file_load_colm_3) are not null and loop through the rest of the records who needs lookup (lookup_or_not_flag is ‘Y’).

While looping through the ResultSet, i should fetch the key_to_all_locks column and then execute a select on the master_table to fetch all the columns which needs to be populated in the staging_table. And finally, execute an update query on the staging_table with the newly fetched values.

Looks like a lot of job for the Garbage collector.

Doing it the PL/SQL way would also mean the same thing. Only advantage over the Java way is that we could escape with lesser number of Java-DB bridge hits).

I was planned to use a MERGE statement for this scenario. For the benefit of those, who just like me, forgot what MERGE statement really does, here is an explanation from the Oracle Handbook.

Merge statements
1) provides the ability to conditionally update or insert data into a table
2) performs an UPDATE if the row exists and an insert if its a new row.

Syntax

MERGE INTO table_name AS table_alias
USING (table|view|sub_query) AS alias
ON (join_condition)
WHEN MATCHED THEN
UPDATE SET
col1=col_val1,
col2=col2_val
WHEN NOT MATCHED THEN
INSERT (column_list)
VALUES (column_values);

and this is what i did. Since i just want an UPDATE and not an INSERT, this is the query i derived.

MERGE INTO staging_table AS st
USING master_table as mt
ON st.key_to_all_locks=mt.key_to_all_locks
WHEN MATCHED THEN
UPDATE SET
st.lookup_colm_from_master_table1=lookup_colm1,
st.lookup_colm_from_master_table2=lookup_colm2,
st.lookup_colm_from_master_table3=lookup_colm3
WHEN NOT MATCHED THEN
INSERT VALUES (NULL,NULL,NULL)

(WHEN MATCHED AND WHEN NOT MATCHED are optional clauses only from Oracle 10g. Unfortunately i was working on 9i)

Only later did i find that i was in deep trouble when i came to know that in a MERGE statement, the source and the target tables should have the same structure. But i had some 11 columns in the master_table and 8 columns in staging_table. And the MERGE INTO clause cannot follow a VIEW. A Table name is needed. Only a USING clause can follow a VIEW.

So, the oly alternative i had is to use the UPDATE clause. I can use one subquery per column like

UPDATE staging_table SET
st.lookup_colm_from_master_table1=(SELECT lookup_colm1 FROM master_table where key_to_all_locks=?),
st.lookup_colm_from_master_table2=(SELECT lookup_colm2 FROM master_table where key_to_all_locks=?),
st.lookup_colm_from_master_table3=(SELECT lookup_colm3 FROM master_table where key_to_all_locks=?)

Meaning there is one table level SEARCH for master_table per column. master_table actually is a huggeeeeeeeee table.

Then i came across this particular UPDATE clause which i guess you might find helpful. I dont really know whether this is ANSI standard and could be used for other RDBMS. But this is really cool. And best of it, it solved by problem.

UPDATE staging_table SET
(lookup_colm_from_master_table1,
lookup_colm_from_master_table2,
lookup_colm_from_master_table3)=

(SELECT lookup_colm1,
lookup_colm2,
lookup_colm3
from master_table where key_to_all_locks=?)

WHERE lookup_colm_from_master_table1 IS NOT NULL
AND lookup_or_not_flag=’Y’
AND key_to_all_locks=?


I know i am setting the same value of key_to_all_locks twice. Of course they are value inputs for two different tables. Just let me know if its possible to set key_to_all_locks only once.

Google ads on hoardings…

 google

In a patent filing Google has revealed that it is looking into entering the physical advertising industry. The patent filing itself alludes to placing adverts on billboards, with the primary innovation being that they’re interactive and connected to the internet — what, you didn’t really believe that Google would go in for static ads did you?
read more…

Gravity might pull Apple to ground…

Falling under the category of “I hate Microsoft because its Microsoft”, nothing could enthuse me other than any announcements from the Linux or the Mac circles. And for the last entire week, my only pass time was just to muse at the photographs of the iPhone.

The devil is in the details, they say. And the same devil woke me up from my dreams.

Though iPhone comes with a sexy key-less interface, a full featured powerful Unix based OS, Safari web browser and a couple of widgets, it was lacking on one major thing. Third party software support. Meaning, no real players, word processors, no ringtones other than built-in or from a iTunes purchase. And my worst nightmare is that there is no support for Java (at least yet). With almost all the smartphones supporting Java based software and games, i feel, Apple is wrong in claiming that it is “five years ahead of any other mobile phone”

Here is the list of why iPhone might fail.

1) No 3G connectivity support. 3G is the hottest of the broadband solutions to mobile technology. It now has been exploited to support speeds up to 7 Mbps (14 Mbps in near future). All the smartphones now support this. iPhone has a meek EDGE technology with data rates of 230 Kbps.

2) Third party software support. All competitive phone models either run on Symbian OS, Windows Mobile or a variant of Linux — all of which has support for third party software.

3) Even though camera is not one of the much advertised feature for the iPhone, it has a humble 2 megapixel inside it. On the contrary Nokia and Samsung has a 5 MP and 10 MP in the offing.

4) 1% market share in the US is not a realistic goal holding hands with a single operator Cingular. Verizon has the largest coverage network and there are some areas where Cingular does not literally exist. Sprint and T-Mobile has its own share of followers who would not trade sides just for a handset.

All said, there is simply a place for a iPhone in our heart and thats the touch screen capability and the very look and feel of this feature-anemic handset. In that case you should simply check out the LG KE 850 that is due release very soon.

iphone vs ke

iphone vs lg

Looks like a clone isnt it? Thats right. But the phone on the left side is the clone and the ‘original’ is from LG.

More on creepy iPhone

http://communities-dominate.blogs.com/brands/2007/01/creepy_iphone_i.html

Steve Jobs, CEO of Apple, quotes “You don’t want your phone to be like a PC. The last thing you want is to have loaded three apps on your phone and then you go to make a call and it doesn’t work anymore. These (iPhones) are more like iPods than they are like computers.”

Thanks. I am happy with one iPod.

 I knew most people know of this feature in Oracle but yet i just wanted to refresh your mind on this little yet powerful statement.

I just got reminded of this today because frankly this is the first time i am using it in work. We had a table something resembling the following structure.

All records

CHILDID is the unique id of a person with name NAME. His manager or his parent is denoted by the value in the PARENTID. NULL values in the PARENTID indicate that he is the “BIGGEST PARENT”.

We had this requirement. Pick up a childid somewhere in the middle of the hierarchy, say 103 and find his “root” parent. Meaning the parent of the parent of the parent (or the parent where the hierarchy ends).

Of course we can put a series of subqueries if we know the depth of the hierarchy. But Oracle provides this wonderful pseudocolumn called LEVEL and a few notable clauses.

Here is the syntax of the statement (Never mind. Syntaxes are always headaches).

Syntax:

SELECT…
[START WITH initial_condition]
CONNECT BY PRIOR recurse_condition

Key:

START WITH : The row(s) to be used as the root of the hierarchy

CONNECT BY : Condition that identifies the relationship between
parent and child rows of the hierarchy

The PRIOR keyword can be on either side of the = operator.

Done with Syntax.

Here we go,

CONNECT BY PRIOR childid=parentid will return a TOP-DOWN hierarchical results wherein the resultset starts with your child being the top most person and displays all the children of your childid.

i.e.

SELECT LEVEL, NAME, CHILDID FROM RELATIONSHIP START WITH CHILDID=103 CONNECT BY PRIOR CHILDID=PARENTID;

will return

child id parent id

CONNECT BY PRIOR parentid=childid will give BOTTOM-UP results. So, this the query we wanted …

SELECT LEVEL, NAME, CHILDID FROM RELATIONSHIP START WITH CHILDID=103 CONNECT BY PRIOR PARENTID=CHILDID;

parent id child id

The LEVEL pseducolumn returns a number indicating the level in the heirarchy: 1 for a root row, 2 for a child of a root, and so on. As we already know what we put after the START WITH statement becomes the root of the hierarchy.

Now, that we want the root parent, i just put a small hack around (I really dont know whether the following query is the most optimal one but yet gave what i really needed)

SELECT * FROM
(SELECT LEVEL, NAME, CHILDID FROM RELATIONSHIP START WITH CHILDID=103 CONNECT BY PRIOR PARENTID=CHILDID
ORDER BY LEVEL DESC)
WHERE ROWNUM<2>

(I am just using our original derived query as a view and extracting the top most record — forgot to mention the ORDER BY clause). Here comes what was needed. 

derived

Here is the complete SQL i used

–Table creation
CREATE TABLE RELATIONSHIP (CHILDID NUMBER, NAME VARCHAR2(50), PARENTID NUMBER)

–Population
INSERT INTO RELATIONSHIP VALUES (100, ‘ARUN’, NULL);
INSERT INTO RELATIONSHIP VALUES (101, ‘CHILD OF ARUN’, 100);
INSERT INTO RELATIONSHIP VALUES (102, ‘GRAND CHILD OF ARUN’, 101);
INSERT INTO RELATIONSHIP VALUES (103, ‘GREAT GRAND CHILD OF ARUN’, 102);
INSERT INTO RELATIONSHIP VALUES (104, ‘GREAT GREAT GRAND CHILD OF ARUN’, 103);
INSERT INTO RELATIONSHIP VALUES (100123, ‘NEVER KNEW WHO THIS IS’, 143143);

– Verification
SELECT * FROM RELATIONSHIP

–Top down results
SELECT LEVEL, NAME, CHILDID FROM RELATIONSHIP START WITH CHILDID=103 CONNECT BY PRIOR CHILDID=PARENTID

–Bottom up results
SELECT LEVEL, NAME, CHILDID FROM RELATIONSHIP START WITH CHILDID=103 CONNECT BY PRIOR PARENTID=CHILDID

–Derived query

SELECT * FROM (SELECT LEVEL, NAME, CHILDID FROM RELATIONSHIP START WITH CHILDID=103 CONNECT BY PRIOR PARENTID=CHILDID ORDER BY LEVEL DESC)
WHERE ROWNUM<2

Oracle 10g XE goes free

If you are Oracle user at home, you should have really felt the pain of the database server eating most of your system resources. Added to it, in all possibilities, we are using an illegal copy of the Personal Edition or the Enterprise Edition.

You would never imagine running Java applications against Oracle just because of the simple reason we can never run our application servers and database server and, of course, our IDE in a single machine. So, we just move in for MySQL, Postgre or other open-source alternatives.

You would have already known that DB2, just a few months back, came up with a free edition of its Database. And Microsoft is also coming up with its free edition of SQL Server 2005 as ‘Express Edition’

Oracle now joins the bandwagon. And, as always, Oracle is our favourite database.

(Please go through the document (at least the faq) and the pdf attachment for more details). The twp_general_10gdb_product

_family.pdf has a wonderful “feature comparison” table at the end of it.

Please dont get alarmed when they say Java development is not possible in Oracle 10g XE. They just mean that we cannot write Oracle PL/SQL stored procedures in Java. And i’ve still not seen anybody writing

“CREATE PROCEDURE DEPT_PROCEDURE AS LANGUAGE JAVA”

That because Oracle XE doesnt come with an inbuilt JVM. (You know Oracle 9i came up with JRE 1.4 bundled inside). Regular SQL, (ALL) PL/SQL, JDBC calls will just work fine.

Best part, Oracle 10g XE comes just as a 250MB iso image.

Linux users, who were denied of the privilege of using Oracle, just because we dont get a pirated version of the full database, can now download their RPM installers direct from the oracle site. Debian users (Ubuntu, Kubuntu, MEPIS, Mandriva, CentOS) can download their .deb installers. All installers are around the same size.

Good luck.

« Previous PageNext Page »


 

December 2009
M T W T F S S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Archives

a