Total Pageviews

Friday, October 19, 2012

Java Threads via Extending Thread class, implementing Runnable interface and Callable interface


If anybody asks - What are the two methods of using Threads in Java? The answer is on our tip of tongue- TWO, but further if it is asked what is the difference between threads implemented via extending Thread class and implementing runnable interface? we feel dificulty in explaining. Here some of my thoughts are presented to draw a line among them.
  1.  As we all know Java doesn't support multiple inheritance. If you want to extend the Thread class then it will make your class unable to extend other classes as java is having single inheritance feature whereas If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems.
  2. In OOP, we extend a class for adding some new behaviour. If we are not making any modification on Thread than its a good OOP design to use Runnable interface.
  3. When we extend a thread class, all the Properties and Behaviours of Thread class and inherited by the subclass, its kind of overhead if we are not going to use it and the same can be achieved on ease with Runnable.
  4. Runnable interface represent Task and the same gives flexibility to get them executed by either Executors or Thread or any other means. So it draws a line between Task as Runnable than Thread as good design decision. However Executors accept Runnable as Task and they have worker thread which executes those tasks, but that's a totally different point. 
  5. While we are differentiating task as Runnable exhibits it can be reused and we are also provided with liberty to execute it from different means. As we know, a thread cannot be restarted once it's complete so again Runnable seems more promising. 


A Short Note on Callable and Runnable:
Both the interfaces are implemented by those classes who want to execute a thread of execution, but few notable differences are as follows:
  1. A Thread implemented via Callable interface may return a value, whereas its not possible if we are using a Runnable interface.
  2. A Thread implemented via Callable interface may throw checked exceptions, whereas its not possible if we are using a Runnable interface.


What I feel, the developers of Java felt a need of enhancing the capabilities of the Runnable interface, without changing the usage of the Runnable interface and that is why probably they've developed Callable interface.

I hope this will clear some of the doubt clouds about threads.

Thursday, October 18, 2012

Liferay: Reading cookies in Theme Template

We can read the cookies in theme template using the following code:
Hope this will help.

Sunday, October 14, 2012

Overriding Events: Working with PreActions and PostActions in Liferay using EXT

Liferay provides us facility to execute some code before and after of some specific events, namely Login, Logout and Service. Login and Logout are self-explanatory, Service is the event that takes place before or after every Action. For instance, If we have CustomServicePreAction and CustomLOginPreAction, in that case CustomServicePreAction will be called first then CustomLOginPreAction and then the LoginAction class will be invoked.
To achieve this, we need to make some entry in the portal-ext.properties, however some values are already present in portal.properties file, we have to update those properties, as following:
Here the our custom classes are: 

  1. com.liferay.portal.events.CustomServicePreAction, 
  2. com.liferay.portal.events.CustomServicePostAction, 
  3. com.liferay.portal.events.CustomLoginPreAction, 
  4. com.liferay.portal.events.CustomLoginPostAction,
  5. com.liferay.portal.events.CustomLogoutPreAction, and 
  6. com.liferay.portal.events.CustomLogoutPostAction.


Now we have to add the following classes in Ext:

1. CustomServicePreAction


2. CustomServicePostAction

3. CustomLoginPreAction

4. CustomLoginPostAction

5. CustomLogoutPreAction

6. CustomLogoutPostAction


And now, run your code. It will work.

Saturday, October 13, 2012

Creating Account Creation URL in Theme Velocity Template in LIferay


For creating URL for new account creation in theme velocity template, the following code can be used:

Friday, September 28, 2012

LIferay - Using Custom Portlet Services in EXT

Using custom portlet services in theme velocity templates is very easy and ServiceLocator class provides an overloaded method for the same. (See this for example), but for using them in ext is difficult because this class cannot be used in ext. I searched in forum but found only one solution which was suggesting to move the portlet service jar to global class-loader. This solution was working for me as well, but I was thinking not to move jar, instead trying to achieve it by some other way.

So, I tried a few things and the following code snippet worked for me:
For the above mentioned code, consider "example-portlet" is servlet context name, and we are invoking exampleFunction(long userid) method from com.apoorva.portlet.service.ExampleLocalService class, with the following imports:
Hope this will help you some day.

Sunday, August 26, 2012

Automated Undeployment of Liferay EXT

Those who work with Light Weight EXT environment very well know that its redeployment is a bit tricky because it needs its undeployment correctly. I've already written an article for its manual undeployment (see article), but during development it takes good time and efforts, so I thought to get some automated way to get it done and the solution I got is using ANT. There the three variation with change only in invocation method. Lets see it:

NOTE: In all examples, name of ext is considered as : example-ext, and following scripts are to added in build.xml file inside the ext folder of sdk. We need to shutdown the server, run this target, start the server and deploy ext again.

1. EXT context name is hardcoded inside ANT script.


Now this can be invoked, as the following:

2. Secondly, we all make the "build.${user.name}.properties" (eg. build.apoorva.prakash.properties") in which we generally set app.server.dir property with path of our web server path. We can create a new property in the same file with the name of ext instead of hard coding the name of ext in ant script, so that it will become a bit of configurable. Advantage of this approach is - as we are allowed to have only one ext in a project and while we create the custom build properties file, we can put the name of the ext in that file along with the name custom app.server.name property. So the custom build file will look as the following:
Now we need to make some minor changes in ANT script, which will look as following:
We can call the target simply as following:

3. Lastly, we can pass name of the ext on the command prompt to make the approach more flexible. If we invoke the ANT script written above as follows, it will supply the name from the command line:
The point to remember is that, don't make the property in the custom build property file.

Hope this will help.

Saturday, August 11, 2012

Using Custom portlet services in Theme Template

Service builders and velocity template are two very important parts of Liferay, and I personally feel joy working with velocity. Today, I am going to write one more article on Velocity templates, which is about the usage of custom portlet services in theme velocity templates. I've already written an article about usage of Liferay services in theme templates (see article). Calling custom portlet services which we build using the service builder is a bit different, however we need to call the same findService method which is overloaded in ServiceLocator class. All difference is we need to pass the servlet context name along with the service class name, as under
The servlet context name is the name of folder of your portlet inside the web-app folder, and if you have doubt, you can use following code to get servlet context name.
One caution, you should have the portlet deployed in the server, whose service you are going to use in theme, otherwise this will give you "Bean Locator is null" exception on server logs.

Hope this will help.

Saturday, June 16, 2012

Adding portlets on page using JavaScript

This is gonna be a very small post, but trust me these two lines are amazing.
This code add portlet on the current page.

You just have to use it inside a javascript function, like:

All now neede is to envoke this function on click on a button with propert portletId, like:


and yes its done. Hope this will help.

Saturday, June 2, 2012

Creating User Page, Applying theme and Adding portlet to them

Creating User Page, Applying theme and Adding portlet to them

The following post tells you how to create a user page(layout), add portlets to them and apply some theme.

First of all you have to add the following properties in portal-ext.properties file, (I'm just filling example data, you can fill as per your requirements:) Now call the following function as per the implementation logic. Code is kinda self explanatory,a nd I've already put comments between them:

Hope this will help.

Tuesday, May 22, 2012

Getting User Image


Following are two code snippets which can be used for getting user image:

and
Hope this will help.

Monday, May 21, 2012

Liferay Cache - ehcache


Liferay internally uses EhCache for implementing cache for the database. However it is possible to implement ehcache in our custom code.
You can see the additional knowledge about it here.
But this how the liferay caches the database it is using. Apart form this, we can also use ehcache for our custom implementation. Lets see how this can be done:
Here we have two function setting and getting values in cache. We can import the class and use in our code.
Hope this will help.

Sunday, May 13, 2012

Parsing Web Content in Java


Template based web content is one of the most promising feature of Liferay and I personally found it very helpful for various requirements.
Creating Structure based template is quite easy, see this link.


Once the web content is created, there are various ways to display it. The easiest way is to put a web content display portlet on the page and select the web content to display in it. But there are cases in which we need to get field's data inside Java code. This makes web contents more usable in live scanerios like you have to render selected content of all template based web contents.


The following code will help you about this.
Once a web content is created, a content is created, but once it is modified another one is vreated, so need to get the latest versions of all the articles, following code will help you. 
Please help yourself with proper imports. Hope this will help you.

Sunday, April 1, 2012

Using Velocity Templates in Portlets


In the very beginning when I was introduced to Velocity Templates, it was very confusing. But with the passing time, I came to know the importance and now I love to work with them. 
We've used them very often in themes, in fact its an important part of theme.
So story seems quite straight, now lets get a twist. 

What If we want to use them in portlet ???
And in answer, you'll ask me a question why the hell you want do it, if we have JSPs to serve the purpose ?

hmmm !!! You are right... Yea, we have JSPs and they work very well, no doubt... But the thing I'm talking about is related with heavy data rendering and the advantage is - we can process them in action class itself instead of sending them to JSP in response for further processing  for display. We get faster processing, as full rendered HTML is sent to JSP in response. So, in that condition we just need to put that response for output in JSP. Ambigious, huh... trust me, it will be very transparent after you finish this post...

Lets start, do create a portlet, depoy it and add on a page to see default output. Now complex climax of the story begins.
By default, the portlet we create is MVC portlet and use default MVCPortlet class. Now do the following:
1. At First create a custom class and it should inherit com.liferay.util.bridges.mvc.MVCPortlet class.
2. We have to override the doView method where we will write our code for Velocity Engine initialization. This is main task. Lets do it. 
In addition i've written one more function, just for modularization, in which I'm merging velocity template, as under:
3. Now you are done with java. Create a folders inside docroot for keeping templates and JSPs with relevent names. I've created with name VelocityTemplate and HTML respectively. Now create and template in side the VelocityTemplate folder and write some HTML stuff and create your JSP with help of following:
4. Now lets start the writing the climex of the story. We have to edit the portlet.xml and liferay-plugin-package.properties file. We have to change the <portlet-class>, and <init-params> for getting template's and JSP's path, like following:
liferay-plugin-package.properties
And trust me, its done... Just try it one or get my attaced war file for referance. Cheers !!!

Tuesday, March 27, 2012

Liferay Cache: a key to improve performance


The main concern of this article is to present performance enhancing information using Liferay cache.
Liferay has out of box Cache MultiVMPool, which we can implement in our portlet's code for caching of results. It becomes necessary if we are using heavy web services.
MultiVMPool Cache is quite easy to implement even over the clustered environment specially if you're frank with handing objects saved in request and session, its quite similar. When I needed it for implementation, I didn't find anything relevant and that is why I need to contribute this article.


Let's know MultiVMPool Cache !!!
MultiVMPool Cache is an Out of Box Cache provided by Liferay and so such kind of complex server and bean configuration is not needed to change for its implementation :  No additional infrastructure configuration  needed. It uses the Liferay's EHChache for wrapper services. It can be implemented in clustered environment easily and used cross WARs also (Because its a JVM level Cache).


Implementation
Putting some value in Cache
There are several other overriden methods also, but most likable is to use the following:
MultiVMPoolUtil.put("Cache-name", "key" ,"value");
NOTE: New cache is automatically created if doesn't exits.


Retriving Value
String value = (String) MultiVMPoolUtil.get("Cache-name", "key");
Returns the value of "key" form "cache-name" cache.


Removing one key
MultiVMPoolUtil.remove("Cache-name", "key");
Removing one key form a cache.


Clearing Cache
MultiVMPoolUtil.clear("Cache-name");
Clear the cache named "cache-name".


Caching over Clustered environment
Uncomment the following in a clustered environment form portal-ext.properties.
    ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-clustered.xml

Don't forget to serialize objects so that caching can be synchronized for other network connections also.


Hope this will help you.


Ref. docs.liferay.com

Monday, March 12, 2012

Organizations Vs. Communities



Liferay portal is known to have great multi-tenancy support. We know that we can have many independent sites within a single installation of Liferay. This is known "multi-tenancy support". There are two ways to achieve it - Organizations and Communities, and generally people are confused among these two. Lets draw a line among these two:

Community
  1. The users of the portal do not fit in any hierarchy. They are like an ad-hoc group of people and join or leave the group at any point of time.
  2. There is some function or area of interest which cuts across the entire structure of the portal.
 Organization
  1. The users of the portal fit in some sort of hierarchy like the Company-Department-Team.
  2. When a user and the group has very tight coupling, for example, the user cannot join or leave the group at his/her will very easily.
  3. When you have some kind of delegation chain in the hierarchy.
  4. When you want the admin of the group to manage the member's profile.


So, when we've got the line between Organization and Communities, it becomes easier to decide which one to use. Just analyze the scenario.

If we just have to create a website with pages, content and some applications, with no specific access rules and which can be accessed by group of users of the site, then no doubt communities is the best solution.

In addition, if you want to organize users, we should use Organizations, in case.

To Conclude, we can say:
Communities: Communities are basically a way to have groups of pages that can display contents and applications.

Organizations: In Organizations, user can be organized in hierarchy of organizations.

Thursday, February 23, 2012

Redeployment of EXT

Redeployment of EXT is a bit hectic in Liferay 6.x. So we have to take care of a few steps while redeploying EXT.

Lets see how it can be achieved. I assume that you have WAR file ready for deployment and name of ext is abc-ext.war.

  1. First of all, shutdown your tomcat if its already running and delete your work and temp folder. Basically /temp/liferay/com/liferay/portal/deploy/dependencies/ext-abc-ext-util-bridges.jar, /temp/liferay/com/liferay/portal/deploy/dependencies/ext-abc-ext-util-taglib.jar/temp/liferay/com/liferay/portal/deploy/dependencies/ext-abc-ext-util-java.jar needed to be deleted but there is no worth in wasting time in finding these files.
  2. Now go to \lib\ext folder of tomcat, and remove ext-abc-ext-service.jar.
  3. Go inside webapps, and remove the ext.
  4. Go inside webapps\ROOT\WEB-INF and remove liferay-display-ext.xml, ext-abc-ext.xml, liferay-portlet-ext.xml, portlet-ext.xml, and tiles-defs.xml files.
  5. Go inside  webapps\ROOT\WEB-INF\lib and delete ext-abc-ext-impl.jar, ext-abc-ext-util-bridges.jar, ext-abc-ext-util-java.jar and ext-abc-ext-util-taglib.jar.
  6. Start the tomcat.
  7. Put WAR file inside hot deploy folder and wait till it expends and give message to reboot your server.
  8. Restart tomcat and its done.
See, there may be some variations in JBOSS, like as if services are not overridden then ext-service JAR may not get deployed in JBOSS's ext folder of lib. These things depend. If you dont have any changes in any XML file then removing them is also not necessary except ext-abc-ext.xml. You must remove it for redeployment. However this is a stranded procedure, this may vary

This is a complex procedure but sorry I can't help it. It only works in that way. If you have something which is shorter and works, please share. :)

Tuesday, February 14, 2012

Reading portal-ext.properties file in VM


portal-ext.properties file can be read from VM also. Its very useful in live scenario:

portal-ext.properties

VM

Hope this will help you out someday.

Wednesday, February 8, 2012

Using Web Contents on our portlets's JSP page

Web contents is really a very great feature of Liferay which allows us to create any kind of valid HTML with the help of very good HTML editor and use it on Liferay pages. But the way Liferay provide to use these web contents on the page is using the web content display portlet.

Generally, text on the JSP pages is loaded form properties file. Now what I think if there is some big content, we can load that from web content. That will be very helpful in externalizing. Another advantage is we can load different contents using the same name for different groups. Let's see how is that possible -

Enter the name of the Article in portal-ex.properties-

Now use the following code on JSP-
Hope this will help. Cheers !!!

Thursday, January 26, 2012

Getting URL Parameter Values

In straight way, our portlet does not have access to the URL parameter. The reason behind it is - requests are forwarded to portlets which are namespaced and portlets only have access to their namespaced parameters.

Things can be achived by hook or crook ;)

In JSP:
In a Controller class:
render() method
processAction() method
Hopefully will help you out someday. :)

Friday, January 13, 2012

Knowing Liferay's URL Parameters

The sole reason for the evolution of DNS was making easy to remember URL, and it served its purpose very well. But sometimes we see a lot of things appended with that URL which is called Parameters. The URL and Parameters are separated by '?', multiple parameters are separated by '&' and parameters exists in key value pair distinguished by '='. 


The point that drags my attention is - if we can use Hidden parameters in request object and it serves purpose very well, then why it is needed to use Visible parameters? The answer is - when request is not directly coming back to page.


Following is the very general URL form of Liferay, lets understand it.


http://localhost:8080/web/guest/home?p_p_id=56_INSTANCE_5ViU&p_p_lifecycle=1&p_p_state=maximized&p_p_col_id=column-2&p_p_col_pos=2&p_p_col_count=4


I hope no need to explain the part before '?'


p_p_id
This is the portlet ID of the portlet. This portlet ID is unique and that identifies portlet. It sometimes contains _INSTANCE_<some alphanumeric value> that tells that portlet instance can be made.

p_p_lifecycle
This parameter identifies the action of the portlet, means it identifies which method will be invoked - render() or processAction. 0 depicts render whereas 1 depicts processAction. Previously It was known as p_p_action in LR 5 and before. We have also seen struts_action parameter that tells the struts action path which was just served.


p_p_state
This parameter explains the window state of the portlet, which can be maximixed, minimized and exclusive.


p_p_col_id
Tells id of the page layout on which that instance of portlet is added.


p_p_col_pos
Tells the column number in which the portlet is added.


p_p_col_count
Tells the total number of portlets in the current layout (page).


These values are handled by FriendlyURLMapper class which includes only those parameters which have some values, others are removed form the URL.