Search This Blog

Friday, November 8, 2013

Configure Cron Job for Schedulers


Cron

"Cron" is a UNIX tool which works as time-based job scheduler . The cron jobs can be configured by writing cron expressions which , the scheduler can understand. In this article we will show sample examples of cron expressions to understand it's configuration. 'CronTrigger' class uses the scheduling capabilities of cron to trigger the scheduler functions.

The Cron Expression

The cron expressions are  strings of texts made up of six or seven values separated by white space. The values are actually field positions which refer to a particular unit of time. The values are restricted to a specific syntax and numbers along with some special characters.

Position/TimeUnit Required Restricted Values  Restricted Special Characters
  1  Seconds    Yes        0-59           , - * /
  2  Minutes    Yes        0-59           , - * /
  3  Hours    Yes        0-23           , - * /
  4  Day of month    Yes        1-31           , - * ? / L W
  5  Month    Yes    1-12 or JAN-DEC           , - * /
  6  Day of week    Yes    1-7 or SUN-SAT           , - * ? / L #
  7  Year    No    empty, 1970-2099           , - * /


  • '*' (all values):-wildcard used to mean all values in possible in the time unit.

  • '?' (any specific value, only for day-of-month and day-of-week ):- like * but limited to the mentioned fields only
  • '-'  (ranges of values):- like 1-2 would mean 1-2 units of time.
  • ',' (separated specific values):- 1,5 would mean cron will run only on 1st and 5th unit of time.
  • '/' (incremental value):- "0/10" would mean cron will run only on every 10th unit of time.
  • 'L' (last unit of time) :- "L in Day of month" would mean cron will run only on the Last day of the month. Similarly last day of year, week.. etc .
  • 'W' (nearest weekday) :- "10W" at the day-of-month field, would mean : "the nearest weekday to the 10th of the month"..
 note:- 'LW' at day-of-month field would mean last weekday of the month.
  • '#'(specific day of month) -  "1#2" in the day-of-week field would mean : "the second Monday of the month".(i.e.daynumber#occurance in the month)

The Cron Expression Examples
 
Cron Expression Time Meaning
0 0  13 * * ? Run at 13pm daily.
0 20 9 ? * * Run at 9:20am daily.
0 25 9 * * ? Run at 9:25am daily.
0 25 6 * * ? * Run at 6:25am daily.
0 5 11 * * ? 2013 Run at 11:5am daily, throughout the year 2013
0 * 15 * * ? Run every minute, starting at 3pm and ending at 3:59pm, daily.
0 0/10 17 * * ? Run every 10 minutes starting at 5pm and ending at 5:55pm, daily.
0 0/10 17,18 * * ? Run every 10 minutes starting at 5pm and ending at 5:55pm, AND Run every 10 minutes starting at 6pm and ending at 6:55pm, daily.
0 0-10 10 * * ? Run every 1 minute starting at 10am and ending in 10 minutes, daily.
0 10,20 10 ? 4 Fri Run at 10:10am and at 10:20am every Friday in the month of April.
0 25 20 ? * MON-FRI Run at 8:25pm every weekday.
0 20 9 10 * ? Run at 9:20am on the 10th day of every month
0 20 9 L * ? Run at 9:20am on the last day of every month
0 20 9 L-3 * ? Run at 9:20am on the 3rd-to-last last day of every month
0 20 9 ? * 6L Run at 9:20am on the last Friday of every month
0 20 9 ? * 6L Run at 9:20am on the last Friday of every month
0 20 9 ? * 6L 2002-2005 Run at 9:20am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 20 9 ? * 6#3 Run at 9:20am on the third Friday of every month
0 0 13 1/15 * ? Run at 13pm every 15 days every month, starting on the first day of the month.
0 12 12 12 12 ? Run every December 12th at 12:12am.


Happy coding :)

How to select DISTINCT on a CLOB field.

Lets consider a table 'PREFERENCES' with two values 'ID' and 'PREFERENCES'. Where id is a Number and preferences is a CLOB field.

      ID   PREFERENCES
---------- ---------------------------------------------
         1 Oracle 
         2 Oracle 
         6 MSSQL
         7 MySQL
         8 Oracle

 


Now  if you want to find out how many distinct values are there in the Preference column.You might try to use;



This will generate the following  SQL error:



If you then use



This will also generate error :-


as per the Error Description If you use



Still, an SQL Error will be thrown , which is

There are lot of ways to solve this problem.One of the simplest way is to increase the Buffer in the same sql . As displayed below.



This runs without error and gets you all the unique values of preferences column.

Happy Coding!!




Friday, October 18, 2013

To make JSP compatible with IE8 or IE 9

Add the following code to the javascript block.

#if ($browserSniffer.getMajorVersion($request) < 9)
  <meta http-equiv="X-UA-Compatible" content="IE=8" />
#else
  <meta http-equiv="X-UA-Compatible" content="IE=9" />
#end

this will make the jsp compatible with IE 8 or IE 9

Happy Coding !!

 

Monday, September 30, 2013

Session timeout redirection

The session timeout can be handled using few properties in the portal-ext.properties file. .

# Extending session automatically
session.timeout.auto.extend=false



#This property can be used to redirect to home page when the session expires.
session.timeout.redirect.on.expire=true

#Set the login url
auth.login.url=/web/guest/home


note :
for session.timeout.warning=30,
#it is redirects to http://localhost:8080/web/guest/home

for session.timeout.warning=0,
#it is redirects to
#http://localhost:8080/web/guest/home?p_p_id=58&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&saveLastPath=0&_58_struts_action=%2flogin%2flogin&_58_redirect=%2fweb%2f10311%2fwelcome

Set JAVA_HOME variable in Windows



 Find Install Directory

Look for the install path for the Java or Software Development Kit(SDK), to find the installation path do the following:
  1. JDK (Java Development Kit) during default installation, are kept in a directory under C:\Program Files\Java. If it is not there we need to find it on the system.
  2. At the path, there will be a sub-directory like jdk1.6.0_03.

    So, the installation path is  C:\Program Files\Java\jdk1.6.0_08.

 Apply Setting

Once you have found the JDK (Java Development Kit) install path:
  1. Right click on the "My Computer" icon on your desktop and select "Properties".
  2. Click the "Advanced" Tab
  3. Click the "Environment Variables" button
  4. In the  "System Variable" tab, click "New"
  5. Enter the variable name as "JAVA_HOME"
  6. Enter the value as the installation path for the Development Kit
  7. Click "OK" button.
  8. Click "Apply" Changes 

  Check JAVA installation

  1. Click on the "Start" button.
  2. Type in "cmd" in the search bar. Press "Enter" button on the keyboard.
  3. The command prompt must open up. Enter the command "java-version" in it.
  4. It must come up with the java details installed on the system.

Translate Liferay in Your Language.(Pootle)



Now, it is your opportunity to translate Liferay in your own language. Liferay has started a forum for the ardent Liferay lovers to collaborate and translate the entire portal in their own language. This helps the community to customize and present their products with their own liking in their own language. The translate project also shows the percentage of completion of the entire project for each language. Here are two snapshots of the project updated to 30th Sept 2013.




 Liferay Translate or Pootle Here is the link to the project:- http://translate.liferay.com/pootle/ The project can also be accessed through the menu on our home page. Happy Translating Liferay!!

Wednesday, September 11, 2013

Add "HTTPOnly" and "Secure" attribute to Cookie for Liferay

Here are the ways
---------------------------
Secure Attribute
Writing a servlet filter to overwrite the session cookie:

private void rewriteCookieToHeader(HttpServletRequest request, HttpServletResponse response) {
if (response.containsHeader("SET-COOKIE")) {
String sessionid = request.getSession().getId();
String contextPath = request.getContextPath();
String secure = "";
if (request.isSecure()) {
secure = "; Secure";
}
response.setHeader("SET-COOKIE", "JSESSIONID=" + sessionid
+ "; Path=" + contextPath + "; HttpOnly" + secure);
}
}
------------------------

HTTPOnly Attribute

Go to Tomcat conf/context.xml file ,add

restart tomcat.

Unable to download documents from Document Library

This is one of the showstopper issue which people face when they use Liferay 6.1.20 EE GA2 with SSH. and IE 8,7 as the browser. While it works on other browsers like Firefox.The documents from the document library are not available.

Reason:

When the server uses SSL (https os Secure Socket Layer). The apache adds the following to the http headers.

Pragma: no-cache
Cache-control: no-cache,max-age=0,must-revalidate


The IE Browser is  not allowed to store the document in the local system and thus it fails to download the object.

refer:
response.setHeader( HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_PUBLIC_VALUE);

present in the classes ServletResponseUtil, PortletResponseUtil.

Here is a link to the microsoft support , which explains the browser issue .
http://support.microsoft.com/default.aspx?scid=kb;en-us;316431

Solution:

Solution 1. Override the code of the classes ServletResponseUtil, PortletResponseUtil. in the setHeaders(request, response, fileName, contentType) method.

                           (or)

solution 2. Add the following line to the apache config file.


        Header unset Pragma

This will remove the pragma entry from the header and the issue would be solved. 






Tuesday, July 30, 2013

SAP Error Codes

The SAP native error codes during its integration with other technologies are always difficult to understand.. Here is a possible list of standard SAP Error Codes

/**********************************************************************/

/* Standard error codes.                                              */

/**********************************************************************/


ECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECodeECode



Wednesday, July 24, 2013

Form re submission of/on Page Reload Errror

The property 'action-url-redirect' must be added to the liferay-portlet.xml file. This will not allow the form to be re submitted once the page is refreshed after a form submission is already done.




By default the property is false and thus the form resubmission occurs

Display error and success messages in liferay

Error Display Many times we need to display the errors messages or the success messages on the jsp pages.The way it is done is the following. Add the following the your implementation of processAction.
 //for Errors 
    com.liferay.portal.kernel.servlet.SessionErrors.add(portletRequest, "unique-error-key"); 
//for Messages 
    com.liferay.portal.kernel.servlet.SessionMessages.add(portletRequest, "unique-message-key"); 
or In Language_xx.properties add your custom message in this file like this
unique-message-key2="Success"

 TO display them in your jsp use this:

Friday, July 19, 2013

Running liferay script on control Panel Script console

Liferay has a script console at the Control Panel. It is accessible to the Liferay administrator. In the Script console Liferay API code , and other various code can be run seamlessly.

The script console is located at

ContolPanel --> Server --> Server Administration. 






 Here are few examples of codes run in the console and screenshot of the output. Most of the  codes were run here by selecting the Language as "Groovy".

Example 1.  Get list of users

Language : Groovy
code:

import com.liferay.portal.service.UserLocalServiceUtil;

userCount = UserLocalServiceUtil.getUsersCount();
users = UserLocalServiceUtil.getUsers(0, userCount);
count=0;
for (user in users) {
    count++;
    println(count+". User: " + user.getFullName());

}





































Example 2.  Get list of roles supported be the portal.


Language : Groovy
code:

import com.liferay.portal.service.RoleLocalServiceUtil;

roleCount =RoleLocalServiceUtil.getRolesCount();
roles=RoleLocalServiceUtil.getRoles(0,roleCount );

for(role in roles){
    println("Role Name: " + role.getName());
}




Example 3.  Get number of users in the portal . (using Javascript as language default script)
Language : Javascript
code:

number = Packages.com.liferay.portal.service.UserLocalServiceUtil.getUsersCount();

out.println(number);



How to Remove default Success Message your-request-completed-successfully



There are several ways to remove the message from displaying.


To remove the display use any of the following mentioned approach:

1. Remove entirely from a Portlet :
 Add this init param to your portlet.xml:


2. Remove  from specific  Portlet actions:
Add this to your implementation of processAction method.
SessionMessages.add(actionRequest, 
(LiferayPortletConfig)portletConfig.getPortletName()+ 
SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
  

3.Using Hook for HOOK for html\common\themes\ portlet_messages.jspf

a) remove the message display from all the portlets.



Comment out the lines , then the message will not be displayed for any of the portlets in Liferay at all.

b) remove the message display from specific portlets.

  create a hook and edit the portlet_messages.jspf file. Use the portletId to show/hide the message display. As shown

 

4. Masking with Custom Message in Portlet : Change that message key in your portlet's language.properties file with your custom text. Like, in the language_en.properties file for your custom portlet. add the custom message to the key:

 your-request-processed-successfully=Success! Process completed with no Errors. 

How to remove the message you-have-entered-invalid-data

There are several ways to remove the message from displaying. This message is present in the portal at the following path :-

   html\taglib\ui\error\end.jsp   


To remove the display use any of the following mentioned approach:

1. Using HOOK for all portlets :create a hook and edit the end.jsp file. In which you can comment out the lines , then the message will not be displayed for any of the portlets in  Liferay.

2. Using HOOK for specific portlet: create a hook and edit the end.jsp file. Use the portletId to show/hide the message display.


 


3. Masking with Custom Message in Portlet :
Change that message key in your portlet's language.properties file with your custom text.
Like, in the language_en.properties file for your custom portlet. add the custom message to the key:

   you-have-entered-invalid-data=Server Error: Please try again. 


4. Using CSS property:
Hook the file end.jsp (at html\taglib\ui\error\end.jsp ) with a surrounding CSS.


in the custom CSS of your portlet add

   .portlet-msg-error-invalid-data-show-hide{
     display : none;
   }

Wednesday, July 17, 2013

You are not allowed to use this program (crontab)

The crontab command used in unix sometimes shows the following error.

crontab -e
You (UnixUser) are not allowed to use this program (crontab)
See crontab(1) for more information
The system checks cron.allow file for allowing the user to use the crontab command. So, just add the user to the crontab.allow using the following command.
[UnixUser~]$ su
Password:
[root UnixUser]# echo UnixUser> /etc/cron.allow
 
Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule.

XML Validation in Liferay

The property 'xml.validation.enabled' must be added to the Portal-ext.properties file.
If set false,

xml.validation.enabled=false

The xml files won't be validated inside the liferay. In some cases,where the xml,DTD ..etc files are kept external. This property can be used to avoid the validation errors.

Tuesday, July 16, 2013

Enable Transactional cache in Liferay

RSS Portlet Connection Timeout

The property 'rss.connection.timeout' must be added to the Portal-ext.properties file.
Set the timeout in milliseconds like,
rss.connection.timeout=2000
This makes the RSS reades wait untill 2 seconds for reading the RSS feeds before timeout.

Show / Hide Lookand Feel Icon on a Liferay portlet

The property 'portlet.css.enabled' must be added to the Portal-ext.properties file.

If made true,

portlet.css.enabled=true

# shows the 'Look and Feel' icon on the portlet titlebar. Enabling the user to edit it.This is default.

If made false,

portlet.css.enabled=false

# hides the 'Look and Feel' icon on the portlet titlebar . Disabling it can speed up the portal performance.

How to get the current logged in user in liferay

There are more than one way to do this. Let's look at some of the options available.
  1. User user = (User) request.getAttribute(WebKeys.USER);
    The User class is the Liferay's model User class.

  2. ThemeDisplay td  = (ThemeDisplay) request.getAttribute( WebKeys.THEME_DISPLAY );
    User user = td.getUser();


    This ' ThemeDisplay 'class can be used to get various information about the portal page,user and theme. Classes ThemeDisplay, User and WebKeys are available in portal-service.jar

  3. User currentUser = PortalUtil.getUser( request );

    The 'PortalUtil' has various utility functions , which come handy during Liferay Programming. This is also a part of the Portal-service.jar.

  4. LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
    User currentUser=liferayFacesContext.getUser();


    The class 'PortalFacesContext' has been renamed to 'LiferayFacesContext'.This class exists in order to assist with migrating old JSF 1.2 based projects from the legacy PortletFaces-Tools project to the new LiferayFaces project.

    Hope It helps you in your portal programming. Happy Coding :)


My Blog List