Saturday, April 29, 2006
Cool Calendar - Javascript Web Based Calendar Application
Last term for a user interface design class, I made the a web-based calendar. The calendar was about 3000 lines of Javascript. It emulates Outlook in that you can drag and drop select and so forth. The calendar however was just built for the user interface and has no backend. Therefore any changes you make are only held in the client machine's memory. When you reload they are reset. Also its only Firefox 1.0* + compatible. To see pre-populated events , go back to Dec. 2005.Try it out here. -Pawan
Monday, April 17, 2006
Phew
I have been coding in JSP for a while now and worked on the Struts framework as well. The servlet API's allow adding of instances to various levels of scope within the webapp. Like Application scope for the entire webapp, Session scope for the user session, Request scope for a particular request. Request scope is useful as the particular request response can be generated in parts by chaining servlets together. Now these methods make life pretty easy. Something which will used throughout the webapp can be kept at application scope and similarly at varying degrees of granularity at other scope. Like on correct credentials for a login form, a String can be stored in session scope.
This would always disturb me. Simply because client specific data was being stored in-memory. Upto how many users can be supported in such an architecture? even if 10 bytes are stored per client, that can count to a lot of memory for say 10k users. Then the overhead of caching, maintaining state of live data and associating it with the client.
Initially storing stuff in scope would make me think of the size of the objects I used to store. Then I realised frameworks like Struts store huge amounts of data at various scope. Like the action form etc in the request scope. So that made me feel that this is not such an issue. If such relatively heavy objects can be stored then that must be fine. And now with JSF etc these object are just getting larger.
Then I read on the Php Share-Nothing architecture. So the Share-Nothing architecture advocates... simply sharing nothing about the client on the server. As simple as that. Instead of storing stuff in memory, store details in a DB and make DB calls always. Now I am not actually sure how performant this is. But DB's have been around for a long while and have been well tuned. Plus DB's can be easily made to work in parallel redundant mode and have very good features.
Again I know I am being premature to be so anti-in-memory stuff. Many large systems have been built, especially in the enterprise. But it just does not feel right. In the end it seems easier to scale horizontally with the Share-Nothing approach. The in-memory approach seems to force towards vertical scaling.
Some examples... Flickr and Yahoo are two php based webapps. And they dont get bigger than them. Ebay is the biggest java based webapp i know of. But Google for ebay architecture and they too have a custom Share-Nothing like system in place. Gmail too uses Java, but have some super optimizations in place.
Rails is one very hot webapp framework right now. This is one of the more intelligent discussions I have read. Coupled with this blog post Rails seems like something I would like to learn soon. These guys seem to be aware of both php and Java webapp dev. Any idea on their principles on this issue?
What dyu guys think? Any of you really stick to the Share-Nothing principle?
This would always disturb me. Simply because client specific data was being stored in-memory. Upto how many users can be supported in such an architecture? even if 10 bytes are stored per client, that can count to a lot of memory for say 10k users. Then the overhead of caching, maintaining state of live data and associating it with the client.
Initially storing stuff in scope would make me think of the size of the objects I used to store. Then I realised frameworks like Struts store huge amounts of data at various scope. Like the action form etc in the request scope. So that made me feel that this is not such an issue. If such relatively heavy objects can be stored then that must be fine. And now with JSF etc these object are just getting larger.
Then I read on the Php Share-Nothing architecture. So the Share-Nothing architecture advocates... simply sharing nothing about the client on the server. As simple as that. Instead of storing stuff in memory, store details in a DB and make DB calls always. Now I am not actually sure how performant this is. But DB's have been around for a long while and have been well tuned. Plus DB's can be easily made to work in parallel redundant mode and have very good features.
Again I know I am being premature to be so anti-in-memory stuff. Many large systems have been built, especially in the enterprise. But it just does not feel right. In the end it seems easier to scale horizontally with the Share-Nothing approach. The in-memory approach seems to force towards vertical scaling.
Some examples... Flickr and Yahoo are two php based webapps. And they dont get bigger than them. Ebay is the biggest java based webapp i know of. But Google for ebay architecture and they too have a custom Share-Nothing like system in place. Gmail too uses Java, but have some super optimizations in place.
Rails is one very hot webapp framework right now. This is one of the more intelligent discussions I have read. Coupled with this blog post Rails seems like something I would like to learn soon. These guys seem to be aware of both php and Java webapp dev. Any idea on their principles on this issue?
What dyu guys think? Any of you really stick to the Share-Nothing principle?
Friday, April 07, 2006
C# futures
Its been interesting to see the path static languages like C# and Java have taken over the past year with their 2.0 and 5.0 releases respectively. There has been a drive towards more 'staticness' with generics i.e. more type specification at compile time. This has generally been seen as a good thing... more descriptive, more type safe and more performant (in the case of C# ;-) code. But then you have dynamic languages like Python and Ruby which are essentially the complete opposite with no static type specification at all. All the variables only have dynamic types with everything being inferred.
What's even more interesting is that inspite of this core difference C# has borrowed features for its 2.0 release from dynamic languages with more on the way for 3.0.
Iterators are a well known design pattern for traversing collections. They are a good way to loosley couple collections from the actual iterating process. So one can have multiple iterators, iterating collections in different ways. Iterators are sprinkled all throughout the Java Collection Framework and similarly C# has Enumerators having pretty much the same interface. Creating these iterators/enumerators invovles creating classes which keep track of state. Thats pretty much all they do... some logic to know where you are and to provide the next element. C# 2.0 introduced the yield keyword which generates iterators dynamically that manage the state automatically. Ruby and Python both have it. Here's a simple ex...
class MyCollection {
private int[] myElements;
public IEnumerator GetEnumerator() {
foreach ( int i in this.myElements ) {
yield i;
}
}
}
Thats it. No creating a class which implements IEnumerator. It's all generated automatically dynamically. Huge productivity booster. Complements the foreach functionality nicely.
C# 3.0 which is a ways off from being released has a lot more in store. Probably the biggest annoucement was about LINQ (Language Integrated Query) which introduces new syntax within the language to work with datasets - collections, relational databases (DLINQ) and xml (XLINQ). This major feature brings with it many smaller ones which again seem to borrow a lot from dynamic languages...
Probably, the most surprising one is Implicit Typing. You can do things like
var i = 1;
var s = "string";
var d = 1.11;
var numbers = new int[] { 0, 1, 2 };
Surprising since it goes against what C#/Java type languages have been known for. But this is a feature needed to make LINQ work since you don't know the final type which will be the result of queries.
Another interesting one is Extension Methods. Ruby has this feature. You can add new methods to existing types without being part of any type hierarchy. You can even add methods to sealed classes like String. As an example, think about a method that checks if a string is a palindrome. Normally, one would create something like a Palindrome class which has a static isPalindrome method which takes a string... Palindrome.IsPalindrome( text ). Pretty inelegant. With extension methods, you can define a method like this
static boolean IsPalindrome( this string text ) { ... }
And call it like this
string text = "civic";
bool palindrome = text.IsPalindrome();
It's a pretty cool feature which, again, is needed to add some LINQ funtionality. But I think there is potential for abuse here and without proper documentation it might cause some confusion.
Lambda Expressions have been available in many languages for a while. C# is finally getting this feature in 3.0. These expressions are popular when filtering datasets and as you can imagine would be an integral part of LINQ. Here's a simple example...
List<int> numbers = new List<int>;
numbers.add( 0 );
numbers.add( 1 );
numbers.add( 2 );
numbers.add( 3 );
numbers.add( 4 );
List<int> evenNumbers = numbers.FindAll( i => ( i % 2 ) == 0 );
So FindAll() will filter the list based on the lambda expression. Syntax seems a bit strange.
A final interesting feature was Anonymous Types. Languages like Python and Ruby have this concept of a tuple which can hold multiple values. So you can have methods returning multiple values. In C# or Java this isn't possible. What many end up doing is to return an array with 2 or more values. Pretty inelegant. Or you have to actually define a type which just holds those values and return that. It's a hassle. With anonymous types you can again dynamically create types without (as the name would suggest) giving it a name...
var person = new { Name = "C Sharp", Age = 4 };
Console.WriteLine( "Name: {0}, Age: {1}", person.Name, person.Age );
Again, as you can guess this is another needed feature for LINQ.
I haven't mentioned much about LINQ itself since I've only read a little about it and seen a video by the man. So dunno a lot of details myself. What would be interesting is to see all the IL that is generated to make all these abstractions work.
Anyway, it's something to look out for. Maybe Java will also be including some data related features for their Dolphin release.
What's even more interesting is that inspite of this core difference C# has borrowed features for its 2.0 release from dynamic languages with more on the way for 3.0.
Iterators are a well known design pattern for traversing collections. They are a good way to loosley couple collections from the actual iterating process. So one can have multiple iterators, iterating collections in different ways. Iterators are sprinkled all throughout the Java Collection Framework and similarly C# has Enumerators having pretty much the same interface. Creating these iterators/enumerators invovles creating classes which keep track of state. Thats pretty much all they do... some logic to know where you are and to provide the next element. C# 2.0 introduced the yield keyword which generates iterators dynamically that manage the state automatically. Ruby and Python both have it. Here's a simple ex...
class MyCollection {
private int[] myElements;
public IEnumerator GetEnumerator() {
foreach ( int i in this.myElements ) {
yield i;
}
}
}
Thats it. No creating a class which implements IEnumerator. It's all generated automatically dynamically. Huge productivity booster. Complements the foreach functionality nicely.
C# 3.0 which is a ways off from being released has a lot more in store. Probably the biggest annoucement was about LINQ (Language Integrated Query) which introduces new syntax within the language to work with datasets - collections, relational databases (DLINQ) and xml (XLINQ). This major feature brings with it many smaller ones which again seem to borrow a lot from dynamic languages...
Probably, the most surprising one is Implicit Typing. You can do things like
var i = 1;
var s = "string";
var d = 1.11;
var numbers = new int[] { 0, 1, 2 };
Surprising since it goes against what C#/Java type languages have been known for. But this is a feature needed to make LINQ work since you don't know the final type which will be the result of queries.
Another interesting one is Extension Methods. Ruby has this feature. You can add new methods to existing types without being part of any type hierarchy. You can even add methods to sealed classes like String. As an example, think about a method that checks if a string is a palindrome. Normally, one would create something like a Palindrome class which has a static isPalindrome method which takes a string... Palindrome.IsPalindrome( text ). Pretty inelegant. With extension methods, you can define a method like this
static boolean IsPalindrome( this string text ) { ... }
And call it like this
string text = "civic";
bool palindrome = text.IsPalindrome();
It's a pretty cool feature which, again, is needed to add some LINQ funtionality. But I think there is potential for abuse here and without proper documentation it might cause some confusion.
Lambda Expressions have been available in many languages for a while. C# is finally getting this feature in 3.0. These expressions are popular when filtering datasets and as you can imagine would be an integral part of LINQ. Here's a simple example...
List<int> numbers = new List<int>;
numbers.add( 0 );
numbers.add( 1 );
numbers.add( 2 );
numbers.add( 3 );
numbers.add( 4 );
List<int> evenNumbers = numbers.FindAll( i => ( i % 2 ) == 0 );
So FindAll() will filter the list based on the lambda expression. Syntax seems a bit strange.
A final interesting feature was Anonymous Types. Languages like Python and Ruby have this concept of a tuple which can hold multiple values. So you can have methods returning multiple values. In C# or Java this isn't possible. What many end up doing is to return an array with 2 or more values. Pretty inelegant. Or you have to actually define a type which just holds those values and return that. It's a hassle. With anonymous types you can again dynamically create types without (as the name would suggest) giving it a name...
var person = new { Name = "C Sharp", Age = 4 };
Console.WriteLine( "Name: {0}, Age: {1}", person.Name, person.Age );
Again, as you can guess this is another needed feature for LINQ.
I haven't mentioned much about LINQ itself since I've only read a little about it and seen a video by the man. So dunno a lot of details myself. What would be interesting is to see all the IL that is generated to make all these abstractions work.
Anyway, it's something to look out for. Maybe Java will also be including some data related features for their Dolphin release.
Monday, March 27, 2006
Bombay
A podcast on the city I call home - Bombay, by Suketu Mehta Author, "Maximum City".
http://www.itconversations.com/shows/detail769.html
http://www.itconversations.com/shows/detail769.html
Wednesday, March 15, 2006
Java tip - Get the method call hierarchy
So here's a small tip I learnt recently which I find useful at times. Quite often it is a pain to debug an application. You just want to have a trace from where a particular method was called.
Simply use this
<code>
MyClass() {
myMethod() {
new RuntimeException().printStackTrace();
}
}
</code>
Now whenever myMethod is called; a stack trace will be printed. So you can easily get the hierarchy of the calls made to reach that execution point. Notice that the Exception was not thrown; hence no handling is needed.
Simply use this
<code>
MyClass() {
myMethod() {
new RuntimeException().printStackTrace();
}
}
</code>
Now whenever myMethod is called; a stack trace will be printed. So you can easily get the hierarchy of the calls made to reach that execution point. Notice that the Exception was not thrown; hence no handling is needed.
Monday, March 06, 2006
JUnit Revelation
I stumbled upon this post by Martin Fowler via another blog. It's a bit dated (2004), but interesting. It reveals that JUnit creates a new instance of TestCase for each test method defined within it. The primary reason for doing this is so that tests are isolated from each other. That is tests don't share the state of objects. So they can be run in any order needed.
JUnit has two special methods setUp and tearDown (these I guess can be called anything in the newer version using Annotations) that are automatically run (if defined) before each test method. I wondered how they did this. Now it makes sense.
JUnit has two special methods setUp and tearDown (these I guess can be called anything in the newer version using Annotations) that are automatically run (if defined) before each test method. I wondered how they did this. Now it makes sense.
Monday, February 27, 2006
Re: JSTL - What's the point?
Chris,
But I think the JSTL was meant to be a base framework, allowing developers to extend and write their own tags. They're called "custom tags", and they can be pretty useful.
For example, I can write a tag that handles the logic for displaying a set of page number links. Then the web designer just needs to know how to place [foo:pagination style="xyz"/] on the page, and voila, the pagination comes out. Or, you could log an advertisement impression with a simple tag like "[ads:logImpression position="${pos}"/]. Then the code behind this tag can do whatever it needs to do -- in this case, log an impression to the database. The former example results in HTML output, and the latter doesn't.
The examples you give make perfect sense. Placing custom tags which encapsulate all the display logic within them is fine. The designer and the presentation layer don't see any of that logic. This is precisely the idea behind ASP.NET web controls like <asp:DataGrid> or <asp:Calendar>. I only took issue with having tags like <c:if> and <c:foreach> ingrained within the html. I didn't see them as being any improvement over scriptlets within html.
(I had to use [ and ] above instead of less-than and greater-than - blogger wouldn't let me enter less-than and greater-than!)
Yeah < and > are special characters. The browser interprets it as being an html tag and tries to parse it. To actually display it you have to use < and >.
There are some frameworks built on top of servlets/JSP/JSTL that provide higher-level custom tags, such as Struts and JSF, as you mentioned. Components such as "DataGrid" as nice for rapid development, but if you need to customize that component, the built-in tags may not work well. (I don't know much about DataGrid in particular, as I'm not familiar with .NET)
I dunno how custom tags like <ads:logImpression> are developed (I assume using JSTL/EL?), but in ASP.NET these web controls are basically classes. So the <asp:Calendar> control is actually implemented in System.Web.UI.WebControls.Calendar within the .NET framework. Its 'real' code - as in implemented using C#. So all the rules of OOP apply here. All web controls directly or indirectly inherit from the base System.Web.UI.Control class which provides some common functionality. And it's really easy to customize any functionality you would want. Just create your own custom controls that inherit from one of these base controls and override away. These controls also allow you to hook in callbacks for certain events which is another way to customize the controls. Check out this article which provides a nice explanation of both processes.
Could you post or link a simple ASP.NET example showing this separation of UI/model?
One of the areas where the Servlet/JSP model wins is in the clear separation between the control code and the presentation. There are two separate components. Requests (generally) hit Servlets after which they are forwarded to independent JSPs. In ASP.NET, there is only one component - the Page. Although this page is separated between into a .aspx file (UI) and a .cs/.vb file (code) using the Code Behind Model, these are compiled down to one component. So basically all requests go to a Page and then could potentially be forwarded elsewhere. It's sort of "backwards" to the MVC model. So the Servlet/JSP API model is richer in that sense. Having said this, the Code Behind model does have its advantages. Probably the biggest one being all the UI components are represented as objects which can be manipulated in the code behind page. Here is a simple explanation of the concept.
But I think the JSTL was meant to be a base framework, allowing developers to extend and write their own tags. They're called "custom tags", and they can be pretty useful.
For example, I can write a tag that handles the logic for displaying a set of page number links. Then the web designer just needs to know how to place [foo:pagination style="xyz"/] on the page, and voila, the pagination comes out. Or, you could log an advertisement impression with a simple tag like "[ads:logImpression position="${pos}"/]. Then the code behind this tag can do whatever it needs to do -- in this case, log an impression to the database. The former example results in HTML output, and the latter doesn't.
The examples you give make perfect sense. Placing custom tags which encapsulate all the display logic within them is fine. The designer and the presentation layer don't see any of that logic. This is precisely the idea behind ASP.NET web controls like <asp:DataGrid> or <asp:Calendar>. I only took issue with having tags like <c:if> and <c:foreach> ingrained within the html. I didn't see them as being any improvement over scriptlets within html.
(I had to use [ and ] above instead of less-than and greater-than - blogger wouldn't let me enter less-than and greater-than!)
Yeah < and > are special characters. The browser interprets it as being an html tag and tries to parse it. To actually display it you have to use < and >.
There are some frameworks built on top of servlets/JSP/JSTL that provide higher-level custom tags, such as Struts and JSF, as you mentioned. Components such as "DataGrid" as nice for rapid development, but if you need to customize that component, the built-in tags may not work well. (I don't know much about DataGrid in particular, as I'm not familiar with .NET)
I dunno how custom tags like <ads:logImpression> are developed (I assume using JSTL/EL?), but in ASP.NET these web controls are basically classes. So the <asp:Calendar> control is actually implemented in System.Web.UI.WebControls.Calendar within the .NET framework. Its 'real' code - as in implemented using C#. So all the rules of OOP apply here. All web controls directly or indirectly inherit from the base System.Web.UI.Control class which provides some common functionality. And it's really easy to customize any functionality you would want. Just create your own custom controls that inherit from one of these base controls and override away. These controls also allow you to hook in callbacks for certain events which is another way to customize the controls. Check out this article which provides a nice explanation of both processes.
Could you post or link a simple ASP.NET example showing this separation of UI/model?
One of the areas where the Servlet/JSP model wins is in the clear separation between the control code and the presentation. There are two separate components. Requests (generally) hit Servlets after which they are forwarded to independent JSPs. In ASP.NET, there is only one component - the Page. Although this page is separated between into a .aspx file (UI) and a .cs/.vb file (code) using the Code Behind Model, these are compiled down to one component. So basically all requests go to a Page and then could potentially be forwarded elsewhere. It's sort of "backwards" to the MVC model. So the Servlet/JSP API model is richer in that sense. Having said this, the Code Behind model does have its advantages. Probably the biggest one being all the UI components are represented as objects which can be manipulated in the code behind page. Here is a simple explanation of the concept.
Re: JSTL - What's the point?
What is the logic behind separating presentation from code? Apart from the "MVC pattern"/"loosely coupled principle", it is also to accommodate designers and coders....The "logic" is still ingrained within the presentation. It is still code, just with tags, instead of java. How is this any better for designers or code maintenance? Somehow this made no sense to me.
What you mentioned is very right. See this post section Using the SQL Actions
<excerpt>
The JSTL includes a number of actions that provide a mechanism for interacting with databases. The previous sentence should, at a very minimum, send up a red flag in your architectural visions. One might ask, "Do I really want to be able to perform SQL actions such as queries, updates, and transactions from my JSP? Isn't that business logic that belongs in the model?" The answer is yes. Yes, yes, yes. To follow a Model-View-Controller (MVC) architecture, which is the predominant design pattern used in building web applications today, you definitely want to keep your model information in your business logic. This means that you don't want it in your JSPs. Why then are these actions even provided in the JSTL? Good question and one that I've discussed with various members of the JSR-53 expert group. The reason is the "C" or community in the Java Community Process (JCP). The community has asked for it, the community has gotten it.
</excerpt>
So thats that. Even the tags Mohnish mentioned are not that great. But... its fine.
Adding to the comment ; JSTL was a another step. Jsp did something better than Servlets. Jstl added something. Then Jstl EL was added which was purposefully given a more JavaScript like syntax. Now we have JSF which gives a more component based web dev feel. I don't know much on JSF though. Also there are actually competing web frameworks in the Java world like Struts, Tapestry... So again loads of choice :)
ASP.NET has the right solution for separation with web controls.
Could you post or link a simple ASP.NET example showing this separation of UI/model?
How is webapp dev in php? Do they too generally look out for such MVC stuff?
What you mentioned is very right. See this post section Using the SQL Actions
<excerpt>
The JSTL includes a number of actions that provide a mechanism for interacting with databases. The previous sentence should, at a very minimum, send up a red flag in your architectural visions. One might ask, "Do I really want to be able to perform SQL actions such as queries, updates, and transactions from my JSP? Isn't that business logic that belongs in the model?" The answer is yes. Yes, yes, yes. To follow a Model-View-Controller (MVC) architecture, which is the predominant design pattern used in building web applications today, you definitely want to keep your model information in your business logic. This means that you don't want it in your JSPs. Why then are these actions even provided in the JSTL? Good question and one that I've discussed with various members of the JSR-53 expert group. The reason is the "C" or community in the Java Community Process (JCP). The community has asked for it, the community has gotten it.
</excerpt>
So thats that. Even the tags Mohnish mentioned are not that great. But... its fine.
Adding to the comment ; JSTL was a another step. Jsp did something better than Servlets. Jstl added something. Then Jstl EL was added which was purposefully given a more JavaScript like syntax. Now we have JSF which gives a more component based web dev feel. I don't know much on JSF though. Also there are actually competing web frameworks in the Java world like Struts, Tapestry... So again loads of choice :)
ASP.NET has the right solution for separation with web controls.
Could you post or link a simple ASP.NET example showing this separation of UI/model?
How is webapp dev in php? Do they too generally look out for such MVC stuff?
Friday, February 24, 2006
JSTL - What's the point?
I've been taking a look at Web related stuff in Java lately. Nothing too complex, just your basic Servlets/JSP. The general concepts behind these web frameworks (ASP[.NET], PHP, Ruby etc...) are all similar. The book I'm reading (Head First Servlets and JSP) has a nice way of explaining the components of the Java system. It starts with the simplest way to accomplish your goal, then shows what's wrong with it and finally how to improve upon it.
First you got your basic requests hitting a Servlet that does some processing and spits out HTML to the client. Nice and simple, but writing all that HTML code within the Servlet is horrible. Enter JSP. JSPs can contain the presentation (HTML). So now your requests hit a Servlet that does the processing after which it redirects to your JSP which has all the HTML code. Now there's a nice separation between code (Servlet) and presentation (JSP). But the JSP is pretty static. What if the presentation needs to be dynamic and depends on the processing done in the Servlet. Enter Scriptlets. These are code segments within JSP that can make the page dynamic. Great... you get nice dynamic pages now, but your presentation is cluttered with code. Enter JSTL (Java Server Tag Library). This is a tag library which is to replace those scriplets. So instead of code you have 'html like' tags.
This is pretty new to me, so I was trying to understand the rational behind it all. The progression made sense from Servlets to JSP to Scriptlets. But somehow the point of JSTL was completely lost on me.
What is the logic behind separating presentation from code? Apart from the "MVC pattern"/"loosely coupled principle", it is also to accommodate designers and coders. Designers can work on the presentation and not have to deal with code. But when you look at JSTL, its core tags are <c:if>, <c:choose>, <c:forEach>, <c:set>, <c:remove> etc... It's just replacing code statements with tags. The "logic" is still ingrained within the presentation. It is still code, just with tags, instead of java. How is this any better for designers or code maintenance? Somehow this made no sense to me.
ASP.NET has the right solution for separation with web controls. You can place these components within a page. These are plain tags like <asp:DataGrid>, <asp:Textbox>, <asp:Labels> etc... They are just responsible for rendering plain html. The logic to decide WHAT they render is placed in a 'code behind' page. Complete separation of code from presentation. Designers don't need to see any logic disguised as tags. I've heard of Java Server Faces which is something that's similar to this thats come up recently. Rahul can expand on it. But I can't believe JSTL was considered a solution at some stage.
First you got your basic requests hitting a Servlet that does some processing and spits out HTML to the client. Nice and simple, but writing all that HTML code within the Servlet is horrible. Enter JSP. JSPs can contain the presentation (HTML). So now your requests hit a Servlet that does the processing after which it redirects to your JSP which has all the HTML code. Now there's a nice separation between code (Servlet) and presentation (JSP). But the JSP is pretty static. What if the presentation needs to be dynamic and depends on the processing done in the Servlet. Enter Scriptlets. These are code segments within JSP that can make the page dynamic. Great... you get nice dynamic pages now, but your presentation is cluttered with code. Enter JSTL (Java Server Tag Library). This is a tag library which is to replace those scriplets. So instead of code you have 'html like' tags.
This is pretty new to me, so I was trying to understand the rational behind it all. The progression made sense from Servlets to JSP to Scriptlets. But somehow the point of JSTL was completely lost on me.
What is the logic behind separating presentation from code? Apart from the "MVC pattern"/"loosely coupled principle", it is also to accommodate designers and coders. Designers can work on the presentation and not have to deal with code. But when you look at JSTL, its core tags are <c:if>, <c:choose>, <c:forEach>, <c:set>, <c:remove> etc... It's just replacing code statements with tags. The "logic" is still ingrained within the presentation. It is still code, just with tags, instead of java. How is this any better for designers or code maintenance? Somehow this made no sense to me.
ASP.NET has the right solution for separation with web controls. You can place these components within a page. These are plain tags like <asp:DataGrid>, <asp:Textbox>, <asp:Labels> etc... They are just responsible for rendering plain html. The logic to decide WHAT they render is placed in a 'code behind' page. Complete separation of code from presentation. Designers don't need to see any logic disguised as tags. I've heard of Java Server Faces which is something that's similar to this thats come up recently. Rahul can expand on it. But I can't believe JSTL was considered a solution at some stage.
Wednesday, February 22, 2006
Outsourcing and Globalization
Seems like stories about outsourcing have cooled a bit. Atleast sources I check haven't been making too much noise about it lately. One of the podcasts I listen to had an interview with a guy who has a small co in NY and outsources to two cos in India - Pune and Delhi. I thought his story was pretty good. Check it out here.
Sunday, February 19, 2006
Broadband as a utility
Really blows my mind... 100 megabits for $25 per month.
Rahul had recently sent me a link to an interview with Josh Bloch/Neal Gafter (java gods) over at javapolis. One of the questions asked was about future directions about the language. Neal Gafter talked about doing more on the client side. He mentioned how the gmail experience would be much better if it could be used as a client app with better offline support. You really have to wonder if this is going to be an issue moving forward with services like what City Telecom is offering - always on connectivity at huge speeds.
Rahul had recently sent me a link to an interview with Josh Bloch/Neal Gafter (java gods) over at javapolis. One of the questions asked was about future directions about the language. Neal Gafter talked about doing more on the client side. He mentioned how the gmail experience would be much better if it could be used as a client app with better offline support. You really have to wonder if this is going to be an issue moving forward with services like what City Telecom is offering - always on connectivity at huge speeds.
Thursday, February 09, 2006
Re: One Thread to rule them all
Since we're having a discussion on threads, this article seemed topical - Threads Without the Pain
Re: One Thread to rule them all
I've used threads in C and Java. In C, I used the pthreads (POSIX threads) library. Of course, it's not as peaceful as threads in Java.
Synchronization is done using mutexes and condition variables. Mutexes allow you to avoid race conditions. Condition variables allow you to wait until any specified condition is satisfied. There's a bunch of functions that are used to do this - pthread_mutex_init/lock/unlock/trylock etc. and pthread_cond_init/wait/broadcast etc.
Of course, thread operations are completely procedural in nature - pthread library functions that take as arguments function pointers/thread variables (pthread_t) and such other stuff.
򪪪򪪪򪪪򪪪򪪪Here's a decent tutorial for pthreads.
Sometimes, it's better to just fork() processes and have them communicate using pipes, semaphores in shared memory etc.
Just a personal opinion - while a lot of things are much easier to do in Java, I would still recommend trying them out in C (or C++) atleast once. You just get a slightly 'inside' view of things... not just threads, even stuff like socket programming. However, for day to day use, Java's a better bet.
Have never used threads in Lisp but I do know that there's a package for threads. While Lisp is projected as an AI language, it has support for a whole lot of things from threads, sockets, interfacing with the OS etc. (OT - While the 'biggest deal' about lisp is its natural use to do functional programming, it also supports procedural and object oriented programming)
Synchronization is done using mutexes and condition variables. Mutexes allow you to avoid race conditions. Condition variables allow you to wait until any specified condition is satisfied. There's a bunch of functions that are used to do this - pthread_mutex_init/lock/unlock/trylock etc. and pthread_cond_init/wait/broadcast etc.
Of course, thread operations are completely procedural in nature - pthread library functions that take as arguments function pointers/thread variables (pthread_t) and such other stuff.
򪪪򪪪򪪪򪪪򪪪Here's a decent tutorial for pthreads.
Sometimes, it's better to just fork() processes and have them communicate using pipes, semaphores in shared memory etc.
Just a personal opinion - while a lot of things are much easier to do in Java, I would still recommend trying them out in C (or C++) atleast once. You just get a slightly 'inside' view of things... not just threads, even stuff like socket programming. However, for day to day use, Java's a better bet.
Have never used threads in Lisp but I do know that there's a package for threads. While Lisp is projected as an AI language, it has support for a whole lot of things from threads, sockets, interfacing with the OS etc. (OT - While the 'biggest deal' about lisp is its natural use to do functional programming, it also supports procedural and object oriented programming)
Monday, February 06, 2006
Re: One Thread to rule them all
The generic question now. Have you guys come across any similar stuff in other languages? Java has had good support for threading since the early days and now in Java 5 this has been greatly enhanced. What about other languages? C++, C#. And anyone have info about dynamic languages like Lisp, Python etc?
I haven't seen the pattern (WorkerThread) that you wrote about... having one worker thread manage multiple tasks. I don't think this pattern is there in .NET. From what I understand, .NET has a different framework. There is probably a one to one similarity with the Threading package (atleast pre Java 5.0). However, .NET has an asynchronous framework built into all delegates. You can invoke delegates with myDelegate.BeginInvoke() and it will run asynchronously. That is, control will be immediately returned. I believe it picks a thread from a ThreadPool and executes it on that in the background. It's pretty nice in that all your own custom delegates get this feature for free.
When talking about UIs and threads there's another important aspect. Any updates to UI controls should only be made by the thread that created it. So if you have a background thread doing some work and you want to display a message in the UI when it's done, you can't simply access the control and update it directly. You need to marshal any updates through the 'owner' thread. Here's an example in C#...
// UI
public class ClientUI : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblStatus;
...
private void UpdateStatus()
{
if ( this.lblStatus.InvokeRequired )
{
this.lblStatus.Invoke( new MethodInvoker( this.UpdateStatus ) );
}
else
{
this.lblStatus.Text = "Done!";
}
}
}
All UpdateStatus() is doing is setting a property on a Label. UpdateStatus() would likely be registered as a callback. So when the background thread is done with its processing, it would raise an event and UpdateStatus() would get called. When it does, it can't just update the control since it is not the 'owner' thread. So it needs to marshal the call. This is done with this.lblStatus.Invoke(). MethodInvoker() is just a delegate which takes methods that don't have any arguments and returns nothing. Invoke() takes care of the marshalling.
What's the if/then statement for? InvokeRequired is a property on every UI control. It will tell you if the call was made from the thread that owns the control or not. If it does own it, it's just a direct update, if not, it needs to be marshalled.
All UI elements inherit from the Control class and all of them in turn have the InvokeRequired property and Invoke() method.
I haven't seen the pattern (WorkerThread) that you wrote about... having one worker thread manage multiple tasks. I don't think this pattern is there in .NET. From what I understand, .NET has a different framework. There is probably a one to one similarity with the Threading package (atleast pre Java 5.0). However, .NET has an asynchronous framework built into all delegates. You can invoke delegates with myDelegate.BeginInvoke() and it will run asynchronously. That is, control will be immediately returned. I believe it picks a thread from a ThreadPool and executes it on that in the background. It's pretty nice in that all your own custom delegates get this feature for free.
When talking about UIs and threads there's another important aspect. Any updates to UI controls should only be made by the thread that created it. So if you have a background thread doing some work and you want to display a message in the UI when it's done, you can't simply access the control and update it directly. You need to marshal any updates through the 'owner' thread. Here's an example in C#...
// UI
public class ClientUI : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblStatus;
...
private void UpdateStatus()
{
if ( this.lblStatus.InvokeRequired )
{
this.lblStatus.Invoke( new MethodInvoker( this.UpdateStatus ) );
}
else
{
this.lblStatus.Text = "Done!";
}
}
}
All UpdateStatus() is doing is setting a property on a Label. UpdateStatus() would likely be registered as a callback. So when the background thread is done with its processing, it would raise an event and UpdateStatus() would get called. When it does, it can't just update the control since it is not the 'owner' thread. So it needs to marshal the call. This is done with this.lblStatus.Invoke(). MethodInvoker() is just a delegate which takes methods that don't have any arguments and returns nothing. Invoke() takes care of the marshalling.
What's the if/then statement for? InvokeRequired is a property on every UI control. It will tell you if the call was made from the thread that owns the control or not. If it does own it, it's just a direct update, if not, it needs to be marshalled.
All UI elements inherit from the Control class and all of them in turn have the InvokeRequired property and Invoke() method.
One Thread to rule them all
In UI Applications it is a requirement to run some tasks in different threads so that the user will not notice a lag while performing some operations. Also often these threads are either of not very long duration or need to be run one after the other. So how do we solve this?
A small diversion to Threads in Java
Now in Java the Runnable interface is used to create threads. Runnable contains a single method run() which needs to be implemented.
<code>
public class MyThread implements Runnable {
public void run() {
//do some task
}
}
</code>
To execute the above as a separate thread you need to call the start method of the Thread class. The Thread class can accept a Runnable instance
<code>
new Thread(new MyThread()).start()
</code>
start() internally performs some housekeeping to actually create the new thread. After performing the necessary operations, Runnable.run() is called.
Back to our UI Application.
What is done is a simple event queue is built for handling all non-ui tasks. Which internally contains a Queue and a single thread.
<code>
public class Worker implements Runnable {
private Queue queue;
private boolean running;
private Worker INSTANCE = new Worker();
private Worker() {
new Thread(this).start();
}
public static Worker getInstance() {
return INSTANCE;
}
public void run() {
if(!running) {
running = true;
while(true) {
if( queue.peek() ) {
Runnable runnable = queue.pop();
runnable.run();
}
}
}
public void addRunnable(Runnable runnable) {
queue.push(runnable);
}
}
</code>
I have just given a basic skeleton here and left out the most important part of synchronizing the class. The running boolean was added to prevent new Thread(Worker.getInstance()).start(). You guys see any more errors? Threads are one of my primary weaknesses which I hope to rectify by learning about the new Java 5 Concurrency features.
For small tasks it is more efficient to use a Worker like this which internally doesn't create a new Thread for each task and re-uses a Thread or even a thread pool. This is because the start() method does quite a bit internally.
In Swing it's suggested to use a SwingWorker class which does something similar. Also Java 5 (Tiger) has added a new SwingWorker class which is additionally Generic.
The generic question now. Have you guys come across any similar stuff in other languages? Java has had good support for threading since the early days and now in Java 5 this has been greatly enhanced. What about other languages? C++, C#. And anyone have info about dynamic languages like Lisp, Python etc?
A small diversion to Threads in Java
Now in Java the Runnable interface is used to create threads. Runnable contains a single method run() which needs to be implemented.
<code>
public class MyThread implements Runnable {
public void run() {
//do some task
}
}
</code>
To execute the above as a separate thread you need to call the start method of the Thread class. The Thread class can accept a Runnable instance
<code>
new Thread(new MyThread()).start()
</code>
start() internally performs some housekeeping to actually create the new thread. After performing the necessary operations, Runnable.run() is called.
Back to our UI Application.
What is done is a simple event queue is built for handling all non-ui tasks. Which internally contains a Queue and a single thread.
<code>
public class Worker implements Runnable {
private Queue queue;
private boolean running;
private Worker INSTANCE = new Worker();
private Worker() {
new Thread(this).start();
}
public static Worker getInstance() {
return INSTANCE;
}
public void run() {
if(!running) {
running = true;
while(true) {
if( queue.peek() ) {
Runnable runnable = queue.pop();
runnable.run();
}
}
}
public void addRunnable(Runnable runnable) {
queue.push(runnable);
}
}
</code>
I have just given a basic skeleton here and left out the most important part of synchronizing the class. The running boolean was added to prevent new Thread(Worker.getInstance()).start(). You guys see any more errors? Threads are one of my primary weaknesses which I hope to rectify by learning about the new Java 5 Concurrency features.
For small tasks it is more efficient to use a Worker like this which internally doesn't create a new Thread for each task and re-uses a Thread or even a thread pool. This is because the start() method does quite a bit internally.
In Swing it's suggested to use a SwingWorker class which does something similar. Also Java 5 (Tiger) has added a new SwingWorker class which is additionally Generic.
The generic question now. Have you guys come across any similar stuff in other languages? Java has had good support for threading since the early days and now in Java 5 this has been greatly enhanced. What about other languages? C++, C#. And anyone have info about dynamic languages like Lisp, Python etc?
Saturday, February 04, 2006
Refactoring enhanced
Probably the single most common refactoring one does when writing code is renaming variables. Saw this cool feature in Visual Studio 2005...
Monday, January 30, 2006
Re: Google China
I think Diana Hsieh puts it very eloquently here.
I wish Google had taken a stand and said no to the Chinese Government, but ultimately its their company and they have a right to do what they wish with their search results.
Dinesh.
I wish Google had taken a stand and said no to the Chinese Government, but ultimately its their company and they have a right to do what they wish with their search results.
Dinesh.
Friday, January 27, 2006
Google China
Found this article (on yahoo ;-) - Google's Action Makes A Mockery Of Its Values.
You must have heard about Google sensoring their search results to meet China's demands. What dyou think? Once a company goes public, it takes on many more responsibilities. Although an admirable motto - Do No Evil - it was always going to be hard to stick to that when you have to satisfy shareholders. Growth in revenues and profits becomes your number one goal. That is automatically seen as 'evil'. Which is another interesting point... you think a super successful company can ever be seen in a favorable light?
You must have heard about Google sensoring their search results to meet China's demands. What dyou think? Once a company goes public, it takes on many more responsibilities. Although an admirable motto - Do No Evil - it was always going to be hard to stick to that when you have to satisfy shareholders. Growth in revenues and profits becomes your number one goal. That is automatically seen as 'evil'. Which is another interesting point... you think a super successful company can ever be seen in a favorable light?
Thursday, January 26, 2006
Re: Another greasemonkey script....
A few minor changes... Instead of an alert, a message is displayed at the top left of the screen for 5 seconds. In the code, the literal "constants" are moved outside of the for loop. The check for presence of both reference and code is moved inside the loop.
// ==UserScript==
// @name Google Analytics Detector
// @namespace http://codeword.blogspot.com
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==
var URL = "http://www.google-analytics.com/urchin.js";
var TRACKER = "urchinTracker()";
var scripts = document.getElementsByTagName( "script" );
var refPresent = false, codePresent = false, log = false;
for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];
// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;
if ( ref != null ) {
refPresent = ( ref.search( URL ) != -1 );
if( log )
GM_log( "Tested ref: " + ref + " Result: " + refPresent );
}
}
// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;
if ( code != null ) {
codePresent = ( code.search( TRACKER ) != -1 );
if( log )
GM_log( "Tested code: " + code + " Result: " + codePresent );
}
}
if ( refPresent && codePresent ) {
var logo = document.createElement("div");
logo.id = "logo";
logo.innerHTML = '<div style="position: absolute; left: 0px; top: 0px;' +
'border-bottom: 1px solid #000000; margin-bottom: 5px; ' +
'font-size: small; background-color: #000000; z-index: 100;' +
'color: #ffffff; width:200px; opacity: .75;"><p style="margin: 2px 0 1px 0;"> ' +
'<b>Google Analytics enabled</b>' +
'</p></div>';
document.body.insertBefore( logo, document.body.firstChild );
window.setTimeout(
function() {
var logo = document.getElementById( "logo" );
if ( logo ) {
logo.parentNode.removeChild( logo );
}
}
, 5000 );
return;
}
}
// ==UserScript==
// @name Google Analytics Detector
// @namespace http://codeword.blogspot.com
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==
var URL = "http://www.google-analytics.com/urchin.js";
var TRACKER = "urchinTracker()";
var scripts = document.getElementsByTagName( "script" );
var refPresent = false, codePresent = false, log = false;
for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];
// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;
if ( ref != null ) {
refPresent = ( ref.search( URL ) != -1 );
if( log )
GM_log( "Tested ref: " + ref + " Result: " + refPresent );
}
}
// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;
if ( code != null ) {
codePresent = ( code.search( TRACKER ) != -1 );
if( log )
GM_log( "Tested code: " + code + " Result: " + codePresent );
}
}
if ( refPresent && codePresent ) {
var logo = document.createElement("div");
logo.id = "logo";
logo.innerHTML = '<div style="position: absolute; left: 0px; top: 0px;' +
'border-bottom: 1px solid #000000; margin-bottom: 5px; ' +
'font-size: small; background-color: #000000; z-index: 100;' +
'color: #ffffff; width:200px; opacity: .75;"><p style="margin: 2px 0 1px 0;"> ' +
'<b>Google Analytics enabled</b>' +
'</p></div>';
document.body.insertBefore( logo, document.body.firstChild );
window.setTimeout(
function() {
var logo = document.getElementById( "logo" );
if ( logo ) {
logo.parentNode.removeChild( logo );
}
}
, 5000 );
return;
}
}
Re: Another greasemonkey script....
Here's the first attempt...
// ==UserScript==
// @name Google Analytics Detector
// @namespace http://codeword.blogspot.com
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==
var scripts = document.getElementsByTagName( "script" );
var refPresent = false, codePresent = false;
for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];
// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;
if ( ref != null ) {
var URL = "http://www.google-analytics.com/urchin.js";
refPresent = ( ref.search( URL ) != -1 );
GM_log( "Tested ref: " + ref + " Result: " + refPresent );
}
}
// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;
if ( code != null ) {
var ACCT = "_uacct";
var TRACKER = "urchinTracker()";
codePresent = ( ( code.search( ACCT ) != -1 ) && ( code.search( TRACKER ) != -1 ) );
GM_log( "Tested code: " + code + " Result: " + codePresent );
}
}
}
if ( refPresent && codePresent ) {
alert( "This smart bastard is using Google Analytics" );
}
Save as analyticsDetector.user.js, open in Firefox and then "Install This User Script..."
Couple things... Besides the code snippet Rahul posted, there is also an external js file that is referenced (http://www.google-analytics.com/urchin.js). I check that both code as well as reference are present. Also in the code, I think "_udn" is optional. It's there on slashdot, but not on codeword, so I don't check for that. Didn't test this very much. Just on codeword, slashdot (detected) and yahoo, microsoft (undetected).
Any improvements?
// ==UserScript==
// @name Google Analytics Detector
// @namespace http://codeword.blogspot.com
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==
var scripts = document.getElementsByTagName( "script" );
var refPresent = false, codePresent = false;
for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];
// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;
if ( ref != null ) {
var URL = "http://www.google-analytics.com/urchin.js";
refPresent = ( ref.search( URL ) != -1 );
GM_log( "Tested ref: " + ref + " Result: " + refPresent );
}
}
// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;
if ( code != null ) {
var ACCT = "_uacct";
var TRACKER = "urchinTracker()";
codePresent = ( ( code.search( ACCT ) != -1 ) && ( code.search( TRACKER ) != -1 ) );
GM_log( "Tested code: " + code + " Result: " + codePresent );
}
}
}
if ( refPresent && codePresent ) {
alert( "This smart bastard is using Google Analytics" );
}
Save as analyticsDetector.user.js, open in Firefox and then "Install This User Script..."
Couple things... Besides the code snippet Rahul posted, there is also an external js file that is referenced (http://www.google-analytics.com/urchin.js). I check that both code as well as reference are present. Also in the code, I think "_udn" is optional. It's there on slashdot, but not on codeword, so I don't check for that. Didn't test this very much. Just on codeword, slashdot (detected) and yahoo, microsoft (undetected).
Any improvements?
Subscribe to:
Posts (Atom)