Total Pageviews

Tuesday, December 20, 2011

NullPointerException - Exception of Exceptions

Once or more, but every java programmer faced NULL POINTER EXCEPTION and we generally ignore its cause. But a slight caution can harbinger in minimizing the chances of NPE. Lets understand it in deep.

When we declare a reference variable (i.e. an object, or sa instance of a class) we are really creating a pointer to an object. Consider the following code where we declare a variable of primitive type int:

In this example the variable x is an integer and Java will initialize it to 0. When we assign it to 10 in the second line our value 10 is written into the memory location pointed to by x.

But, when we try to declare a reference type something different happens. Take the following code:
The first line declare a variable named num, but, it does not contain a primitive value. Instead it contains a pointer (because the type is Integer which is a reference type). Since we did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing".

In the second line the new keyword is used to instantiate (or create) an object of type Integer and the pointer variable num is assigned this object. We can now reference the object using the dereferencing operator . (a dot).

NPE occurs when we declare a variable but did not create an object. If we attempt to dereference num BEFORE creating the object we get a NullPointerException. In the most trivial cases the compiler will catch the problem and let us know that "num may not have been initialized" but sometime we write code that does not directly create the object.

For instance we may have a method as follows:
in which case we are not creating the object num, rather assuming that is was created before the doSomething method was called. Unfortunately it is possible to call the method like this:
In which case num is null. The best way to avoid this type of exception is to always check for null when you did not create the object yourself. So doSomething should be re-written as:
To conclude, we can say NPE occur when we try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NPE.

Saturday, October 1, 2011

Available implicit objects Liferay JSP page

On a normal JSP page, some objects are implicitly available. In addition, we can get several others in Liferay using the taglibs. But we don't know all. So, lets become a technical James Bond and investigate about it. :D

Lets see about normal JSP first:
These objects are created by the container automatically and the container makes them available to us. Since these objects are created automatically by the container and are accessed using standard variables; and that is why, they are called implicit objects. They are parsed by the container. They are available only within the jspService method and not in any declaration.

These are 9 objects.
Now, lets ebter the liferay context:

The following statements  will give 14 default objects :
And the following statements will give 18 default objects :
and the type of each object as follows :
These default objects can be acquired using pageContext which is an implicit object of JSP. Use the following code for getting default objects.

So, here are the objects that I found. Hope this will help.

Thursday, September 22, 2011

Extracting HttpServletRequest object from RenderRequest object

In daily coding scenario, we use some minor problem that keeps you stuck on something else for long time. The thing I'm writing today is one amongst them.
This is how we can extract the object of HttpServletRequest form RendeRequest object.
HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)); This will work for you in when simple getParamater() method doesn't respond well.
Surely, it'll help you someday.

Saturday, August 27, 2011

A story of "Portlet" and "Servlet"

There are so many things which are very clear and understood but if someone ask, it become difficult to explain them in deep. The topic I am going to write today, is something like that. Hope, you'll enjoy reading it, as I am going to write this time full on text, no practical implementation.

The contents of servlets provide generally takes up the whole page in a browser (if you are not framing them), and portlets content are wrapped in a window and we can place multiple portlets on same page with one another and each can provide different content and functionality. A portlet behaves like a full fledged application. The portal can provide some house keeping functionality and secured single point of entries to all of the portlets on a page. 

lets see first some differences among portlets and servlets
  1. Servlets can provide complete web pages, whereas portlets only provide fragments. These fragments are then aggregated to form a complete web page by the portal.
  2. Portlets aren't allowed to generated HTML code that contains tags such as base, body, frame, frameset, head, html, or title. The iframe tag can be used with caution.
  3. The user cannot access a portlet directly using a URL in the way that a servlet is accessed. Instead, the URL points to the page containing all of the portlets on one page.
  4. Communication between the web client and the portlets is performed through the portal.
  5. Portlets can be provided with buttons or controls to manipulate the portlet's window states or portlet modes. 
  6. Multiple instances of a single portlet can be placed onto the same page.
  7. Portlets support persistent configuration and customization.
  8. Portlets also support user profile information.
  9. Portlets support two scopes within the session; application scope and portlet scope .
Here are some similarities
  1. Servlets and portlets are web based components that utilize Java for their implementation.
  2. Portlets are managed by a portlet container similar to a servlet container.
  3. Both of these components generate content, which can be static or dynamic.
  4. Both portlets and servlets have a lifecycle that is controlled by the container.
  5. The client/server model is used for both servlets and portlets.
  6. The packaging and deployment are essentially the same.
  7. The manner in which the classes are loaded and the class loaders that perform the work are also the same.
  8. Lifecycle management is similar.
  9. The Request and Response semantics are also similar.
Advantage of Servlets over portlets-
  1. Portlet aren't allowed to set the character set encoding of the response.
  2. Portlet also aren't allowed to set the HTTP headers on the response.
  3. Portlet cannot manipulate the URL of the client request to the portal.
Advantage of Portlets over Servlets-

  1. Portlet Modes :  Each portlet has a current mode, which indicates the function the portlet is performing.
  2. All JSR 168 compliant portals should support the View, Edit and Help modes.
  3. Window States :Window states indicate the amount of portal page space that will be assigned to a portlet.
  4. Portlet Preferences :Portlets can be configured to provide a custom view or behavior for different users.

Saturday, August 20, 2011

Creating a new category in Control Panel and Adding portlet in it

While development of any big portal, we need to customize control panel also. In such a case, its generally needed to make a new category in control panel and adding portlet in it. So, here are the steps, hope you'll find it helpful.

1. Override the class PortletCategoryKeys (in ext-impl\src\com\liferay\portal\util\
Put the category in ALL array in the order you wish to appear your category.

2. Now add portlet in this category using the following entry in liferay-portlet-ext.xml file
here  portlet_id is the id of the portlet that you wish to add in our custom category. Specify the name of the category in <control-panel-entry-category> entry and sequence of portlet appearance in determined through <control-panel-entry-weight>. The higher the weight, upper will be the portlet(weight can also be in floating number).

3. Now an important point, we must enter the category name entry in file, so that our category can be recognized, otherwise you'll get category name as "category.mycategory"
That's all I did, hope will work for you too.

Saturday, August 13, 2011

Writing Parameterized properties

Hi all,
Its quite a small thing, but very useful in live scenario.
Just two points-
write a property in file as follows-
and now use it as follows.
for using more then one replacement, do as follows.
and use it as:
One key point is- 'x' is nothing but only a token placeholder for programmer understanding where the value will be inserted. The actual thing is {1} like tokens.
Hope this will help. Cheers !!!

Saturday, August 6, 2011

Using Liferay Services in VM using ServiceLocator

When developing custom themes, we need to use Liferay services in VM. Its quite easy but tricky. So, putting here a simple example-
Write following in init-custom.vm

Now you can use this service as follows:
where $myPlace is a group.

Hope this will help.

Tuesday, July 26, 2011

MVC portlet in Liferay 6

The Liferay 6 plugins SDK is fully re-architectured. The ext environment comes now as plugin, and therefore adding new fields(customization) the core liferay portlets of Liferay is now a bit difficult as it was quite easy in LR5. The normal way most of us are accustomed to creating a portlet was through the StrutsPortlet class which is Liferay Struts bridge. But we cannot extend this class in the plugins SDK as the StrutsPortlet class is packaged within the portal-impl.jar file, which is not accessible in the plugins SDK.

There is a new addition - MVCPortlet, which is Liferay MVC pattern class. When we create a portlet in the Plugins SDK,  we get an MVCPortlet. You can chek it by viewing the <portlet-class> attribute of portlet.xml file. Some of its major features are - page management, automatic display of success messages, automatically calling the appropriate action methods an so on. This class extends the LiferayPortlet class.  

To do some custom task by this MVC Portlet, we need to create a new class that must extend the MVCPortlet. So, the question is whay to extend MVCPortlet class? My answer is quite small - Extending MVCPortlet class will help us various ways, broadly we can say - we'll get about all benefits of MVCPortlet class and will add our own functionality in the class we are writing. 

Try creating with me by following the steps mentioned below - 

1. Create a new portlet by entering the command (must be in portlet folder of plugin)
create abc "abc"

NOTE: I suggest you to go and examine the xml files which are created by default.

2. Now go to '\portlets\abc-portlet\docroot\WEB-INF' and open 'portlet.xml' file. Modify the <portlet-class> attribute. The default valuve will be <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>. Give here the package of your class. we are giving ''.

3. In the <init-param> attribute, give the relative path of the jsp that you wish to open by default, when the portlet gets loded. For, instance we are giving <value>/html/com/xyz/view.jsp</value>.

4. Now create the class in the respective package. We writing the following ABCAction class in '\portlets\abc-portlet\docroot\WEB-INF\src\com\xyz\action'

5. Now just one more thing, create the jsp files in the respective path. here, we will create in '\portlets\abc-portlet\docroot\html\com\xyz' (don't bother to create the folder and files if u need, ypu'll need here to create html folder in docroot). Now the point to coin in mind that we must have the same name of the <portlet:actionURL> corresponding to which function we wish to invoke for our custom class. We here have the name of the actionURL and  function is 'customFunction'


This is all I've did. Hope, you'll find this article helpful.

Thursday, July 21, 2011

Tabs in Portlets

Hi all,
Making tabs in Liferay portlet is quite easy. We can make it with very limited code.
I'm giving an example of tabs in a struts portlet. (I hope you can make one, and if not then please refer to my old posts for the same)

1. Write the following code in the jsp of portlet, you wish to make tabs. I'm making it in view.jsp
2. Now make three jsp files with the names 'about.jsp', 'siteMap.jsp' and 'contactUs.jsp'. These are the jsp files that get included in case of the respective tabs.

3. Make the following entry in struts-config.xml
4. finally, add following in tiles-defs.xml
This is all for making tabs in portlet. Hope this will help.

Tuesday, March 1, 2011

Calling remote service method using Javascript/JSON

Liferay enables us to call the methods using Javascript. There are various advantages of it, but the most useful is - we can call a method without refreshing a the portlet (without rendering), and we can display contents as per logic.

Step 1. Firstly, generate your service for your entity keeping remote-service="true"  in service.xml

Step 2. Write a method in ServiceImpl that you wish to call from Javascript.

Step 3. Build service using ant, from ext-impl.

Step 4. Deploy the whole project.

Step 5. Method will be available in ext-web/docroot/html/js/liferay/ext_service.js.

Step 6. Include js in the jsp file you need to call the method, as follows:

<script type="text/javascript" src="/html/js/liferay/ext_service.js"></script>
Step 7. Call method as follows in javascript:

This is it... All the best...

Wednesday, February 2, 2011

Liferay Scheduler

Sometimes it is needed to create a task that is being executed periodically. We can use scheduler (based on Quartz API ) that is integrated with Liferay. There are 3 steps to do this.

1. Write a class the implements the com.liferay.portal.job.Scheduler interface

2. Write a class that contains actual task that needs to be executed. This class must implements com.liferay.portal.job.IntervalJob interface

3. Register your scheduler in liferay-portlet-ext.xml ( if you use ext environment )

Now let’s go to the details.

1. Write a class that implements Scheduler interface.

2. Write the class that contains actual task. On this example, our actual task would be only to write ‘THIS IS THE ACTUAL TASK!” to console. This task would be executed every minute.

If you want to change the interval, you can just change this line :

Change Time.MINUTE to Time.HOUR or Time.DAY.

3. Register in liferay-portlet-ext.xml.