Pages

Tuesday 14 January 2014

What is Null interface or Marker Interface in Java?

What is Null interface or Marker Interface in Java?

Marker Interfaces are those which doesn't contain any methods, but which got some purpose or capability.

It gives a message to the java compiler that it can add some special behaviour towards the class which implementing it.

Some of the Marker Interfaces are:
  • java.lang.Cloneable
  • java.io.Serializable
  • java.util.EventListener

Let's take an example:

1) java.io.Serializable 

java.io.Serializable is a marker interface, it doesn't contain any method declarations.

When a java class is to be serialized , i.e when we want to store the state of an object or pass through the network, we should intimate the java compiler in some way that there is a possibility of serializing this java class.

The java class which needs to be serialized has to implement the java.io.Serializable marker interface and by doing this we are informing the java compiler to add that capability.

2) java.lang.Cloneable

All classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). 

The Java compiler makes sure that if the clone() method has been called on that class which implements Cloneable Interface and also the class is implementing the Cloneable interface.

For example, consider the following call to the clone() method on an object cloneableClass:

CloneableClass cloneableClass = new CloneableClass ();
CloneableClass cl = (CloneableClass )(cloneableClass.clone());

In the above, the CloneableClass should implement the interface Cloneable, if not then the compiler will give an error. This is because the clone() method may only be called by objects of type "Cloneable."

So even if, Marker Interfaces are empty one's, it holds some special purpose.

Wednesday 22 May 2013

Load Testing with JMeter

Load Testing with JMeter
Apache JMeter is a 100% pure Java desktop application which is used for load/traffic testing of Web Applications. 

  • JMeter is mainly used for load testing. It will throw many concurrent requests at your site for an extended period of time as you wish.While our load test is executing, we have the opportunity to monitor our entire server stack and see what happens over time as your application handles the load.
  • Initially JMeter needs to be configured to sit between the browser and the target server and then it records all the HTTP requests sent by the browser to the server. If the target GUI accepts only HTTPS requests, then at this phase the browser should send HTTP requests (and Not HTTPS) to the JMeter, JMeter records those requests and encrypt and send those to the server. Reverse thing happens for HTTPS response. JMeter receives the responses from the server, decrypts those records and sends the HTTP responses back to the browser. The logical entity that is responsible for this HTTP message recording is called Http Proxy manager.
  • Once the recording is done, JMeter can be configured to start a large number of simultaneous threads. Each thread represents one user sending/receiving the same set of requests/responses recorded. Now onwards, it’s between the JMeter and the server; browser is no longer used. The component responsible for this is called thread group.

Now, To Configure JMeter follow the bellow steps.

  1. Download Jmeter from Here
  2. Go to JMETER_HOME/bin and start JMeter with jmeter.bat on windows. New window will open once you run jmeter.bat file
  3. Select "Test Plan" on the tree.
  4. Enter the details in User Defined Variables if you wish. It's not mandatory.
  5. Right click on the "Test Plan" and add a new thread group: Add > Threads (Users) >Thread Group.
  6. Select the Thread Group.
  7. Right click on Add -> Config Element -> HTTP Request Defaults.
  8. In new HTTP Request Defaults element: Server name or IP - Enter “localhost” (if you are testing it on your local machine otherwise enter relevant URL or IP address).
  9. Path – leave blank.
  10. Right click on the "Thread Group" and add a recording controller: Add > Logic Controller > Recording Controller.
  11. Next, select WorkBench. 
  12. Right click on WorkBench and add the Http proxy: Add -> Non-Test Elements -> HTTP Proxy Server.
  13. On HTTP Proxy Server, Under Global Settings section entered port number (e.g:9090)
  14. On HTTP Proxy Server, Select the target Controller (created in above point number 11).
  15. On HTTP Proxy Server, click the “Add” button in “URL Patterns to Include”. This will create a blank entry.
  16. Enter ".*\.html"
  17. On HTTP Proxy Server, click the “Add suggested Excludes” button in “URL Patterns to Exclude”.
  18. Right click on "HTTP Proxy Server" and add a listener: Add -> Listener -> View Results Tree.
  
Now, Configure your browser to use the JMeter HTTP Proxy.
  1. In Firefox :    
    1. Click on Tools >  Options > Select Advanced > Select Network Tab > Click on Settings.
    2. Select Radio Button "Manual Proxy Configuration".
    3. Add Http Proxy: localhost and Port (Entered the same port number as entered in above point number 14)
    4. Checked check box of "Use this proxy server for all protocols".
    5. Press Ok.
  2.  In Chrome:
    1. Click on Settings.
    2. Type Change proxy settings in search box.
    3. Click on Change proxy settings button.It will open new window of Internet Properties.
    4. Click on LAN settings.
    5. Checked the check box of "Use a proxy server for your LAN".
    6. Add Address: localhost and Port (Entered the same port number as entered in above point number 14)
    7. Click on Advanced button and Checked check box of "Use the same proxy server for all protocols". 
    8. Click Ok.
  3.  In IE :
    1. Click on Tools > Internet Options > Select Connections.
    2. Click on LAN settings.
    3. Checked the check box of "Use a proxy server for your LAN".
    4. Add Address: localhost and Port (Entered the same port number as entered in above point number 14)
    5. Click on Advanced button and Checked check box of "Use the same proxy server for all protocols". 
    6. Click Ok.

Record Your Test Plan:
  1. Return to HTTP Proxy Server, and click the "Start" button at the bottom
  2. In your browser, In the Address bar enter "localhost:8080" (localhost: web server port number) and hit the enter key.
  3. Click on a few links of your application pages.
  4. Return to HTTP Proxy Server, and click the "Stop" button at the bottom.
Now,Expand the thread group and there should be several samplers.

 Run the Test plan with JMeter:
At this point, we are ready to run our test plan. You can run the test plan with below two ways:
    1. Run --> Start
    2. Ctrl + R 
While the test plan is running, in the upper right-hand corner, there should be a green square. When the test is done, the box should be grey.

Sunday 11 November 2012

Send an email from liferay portal

Create a Scheduler in Liferay

If you want to send an email from your liferay portal then its bit easy because liferay provide an Utility class called MailEngine.So we don't have to play with direct JavaMail API.

In this article, we’ll use Gmail as SMTP

Below are the steps to send an email in Liferay.
  •  Configure your SMTP provider, outgoing port, user id and password.
    • Login as super user in your liferay portal
    •   Go to Control Panel
    •  Click on Server Administration from server section
    • Click on Mail Tab
    • Fill out the SMTP fields here,
      1. Set Outgoing SMTP Server (E.g. smtp.gmail.com)
      2. Set Outgoing Port (E.g. 465)
      3. Check the box of Use a Secure Network Connection if you want.
      4. Set User Name and Password.
      5. Press “Save” Button 
      6. You are done with SMTP configuration of outgoing mail sever.

  • In your MVCPortlet or related class add the below peace of code to send an email.


MailMessage mailMessage = new MailMessage();
                   mailMessage.setHTMLFormat(true);
                   mailMessage.setBody("set body here");
                   mailMessage.setFrom(new InternetAddress("fromAddress","fromName"));
                   mailMessage.setSubject("set mail subject here");
                   mailMessage.setTo(new InternetAddress("set receiver email id here"));
                   mailEngine.send(mailMessage);
  • If you want to add attachment to your email or you want to add Cc you can use below send method of MailEngine class,

        send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, String subject, String body, boolean html format, InternetAddress[] replyTo, String messageId, String inReplyTo, File[] attachments);

I hope this tutorial will help you.. :)

Friday 9 November 2012

Generate .xsd Schema file from .xml file

Create a Scheduler in Liferay
An easy way to generate your .xsd file from .xml is with trang command line tool.

Below are the steps to generate .xsd file from .xml file.

  1.     Download tarng.jar file and save it at your local drive.  
  2.       Generate .xsd file using below command
  • java -jar <<your local machine path of trang.jar>>trang.jar addToCart.xml addToCart.xsd

E.g suppose your .xml file is as below then It will generate .xsd

addToCart.xml

<?xml version="1.0" encoding="UTF-8"?>
<addToCart> 
         <cartItem>   
             <cart_item_id><![CDATA[156]]></cart_item_id> 
             <cart_id><![CDATA[9]]></cart_id>
             <nid><![CDATA[167]]></nid>   
             <qty><![CDATA[1]]></qty>   
             <title><![CDATA[Born To Die (Album)]]></title>
             <price><![CDATA[99]]></price>
             <discountedPrice><![CDATA[89.1]]></discountedPrice>
         </cartItem>  
 </addToCart>

addToCart.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="addToCart">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cartItem"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="cartItem">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cart_item_id"/>
        <xs:element ref="cart_id"/>
        <xs:element ref="nid"/>
        <xs:element ref="qty"/>
        <xs:element ref="title"/>
        <xs:element ref="price"/>
        <xs:element ref="discountedPrice"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="cart_item_id" type="xs:integer"/>
  <xs:element name="cart_id" type="xs:integer"/>
  <xs:element name="nid" type="xs:integer"/>
  <xs:element name="qty" type="xs:integer"/>
  <xs:element name="title" type="xs:string"/>
  <xs:element name="price" type="xs:integer"/>
  <xs:element name="discountedPrice" type="xs:decimal"/>
</xs:schema>

 



Tuesday 6 November 2012

Liferay remove docbar for general user

Liferay remove docbar for general user If you want to remove dockbar for general users form liferay page.
Add the below code in your portal_normal.vm file.


#if ( $is_signed_in )
#set ($rService = $serviceLocator.findService(“com.liferay.portal.service.RoleService”))
#set ($usrRoles = $rService.getUserRoles( $user_id ))
#set ($hasPermission = false)
#foreach( $usrRole in $usrRoles )
#if ( $usrRole.getName() == “Administrator” || $usrRole.getName() == “Content-Admin” || $usrRole.getName() == “Content-Editor”)
#set ($hasPermission = true)
#end
#end
#end
#if($hasPermission == true)
#dockbar()
#end

Sunday 28 October 2012

Create a Scheduler in Liferay

Create a Scheduler in Liferay
In this small blog we will give an understanding of how can we write a Scheduler to run a cron job for performing a background job on the portal. 

Step 1 : Create a scheduler class in your portlet

import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;

public class NewsLetterScheduler implements MessageListener {

  public void receive(Message arg0) throws MessageListenerException {
       System.out.println("newsletter received!!!");
        /* Add your business logic here*/
  }
}

Step 2 : Specify the path in the liferay-portlet.xml.

The class name specified in the liferay-portlet.xml must match with scheduler class.

<portlet>
<portlet-name>newsletter-portlet</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
    <scheduler-description>
       scheduler will run on every 15 minutes
    </scheduler-description>
    <scheduler-event-listener-class>
      com.sa.newsletter.trigger.NewsLetterScheduler</scheduler-event-listener-class>
 
   <trigger>
       <simple>
          <simple-trigger-value>15</simple-trigger-value>
          <time-unit>minute</time-unit>
       </simple>
  
        <!--<cron>
            <cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
        </cron>
-->

  </trigger>
</scheduler-entry>
</portlet>

This (<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>) indicates that the scheduler will run every night at 00 hrs.

<cron>
    <cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
</cron>



I Hope this small blog helps you a lot!!!

Sunday 14 October 2012

Add a portlet to the Liferay Control Panel

Add a portlet to the Liferay Control Panel
If you want your portlet to be available in the liferay control panel, just add the following line in your liferay-portlet.xml:


<control-panel-entry-category>content</control-panel-entry-category>
<control-panel-entry-weight>15</control-panel-entry-weight>



  • control-panel-entry-category: The category where your plugin portlet will appear.
    • There are total 4 values you can use as per your requirements for category element.
      1. my
      2. content
      3. portal
      4. server
  • control-panel-entry-category: Determine the relative position in your relevant category portlet.The higher the number, the lower in the list your portlet will appear within that category. 
  • control-panel-entry-class: This is optional and is used to define rights (Who can see the portlet in the control panel). 


Make sure your portlet is not instanciable if you want your portlet in control panel.

add below line to your liferay-portlet.xml (for make portlet not instanciable).

<instanciable>false</instanciable>

I hope it helps you guys.... :)


Liferay Configuration in windows with Tomcat

Liferay Configuration in windows with Tomcat

Solution Analysts


In this brief tutorial, we will discuss how to setup Liferay with Apache tomcat Web Server.


Requirements before getting started #

  • JAVA (JDK & JRE)
  • Eclipse IDE
  • Liferay Tomcat bundle (Community Edition or Enterprise Edition)
  • Liferay Plug-in SDK
  • Apache Ant (with ecj.jar if not having)


Step -1: Download & Installation

1) To setup machine for Liferay development first of all download JDK from below link and install.

2) Download eclipse from below link.

3) Download Liferay plug-in SDK and Tomcat bundle from below link.

4) Download Apache ant version 1.8.1 from below link.

That’s all about downloading and installation bundles for development.


Step -2: Setting UP Environment Variables

Set JAVA_HOME:



Set LIFERAY_HOME:



Set ANT_HOME:




Step - 3:  Eclipse Configuration for Liferay Development

 First open eclipse and follow the instruction below,
1) Go to Help menu --> Eclipse Marketplace
2) Find here “Liferay” and just install Liferay IDE.



Install Liferay IDE and restart eclipse.


Step - 4:  Liferay Plug-ins SDK Setup

1) Open eclipse with Liferay IDE installed.
2) Open Preference page for Liferay (Go to window ->preferences -> Liferay -> Installed plugin SDK).



3) Here press the add button.
4) Browse to the location of your Plug-ins SDK installation.
5) Select OK and you should see your SDK in the list of installed SDKs.
6) Here, at the bottom of dialog screen, you will have option to update build. <username>.properties file. Select here prompt.

Congratulations! Your system is ready with development environment for Liferay.


Note: multiple SDKs can be added to the preferences but you will need to select at least one SDK to be the default.

Step – 5: Liferay Portal: Apache Tomcat Runtime/Server Setup #

          1) From eclipse go to Window àPreferences à Server àRuntime environments.


        
        2) Now, Click on Add to add a new Liferay runtime and find Liferay V6.1 tomcat under the     Liferay, Inc. and press next button.
           
            

      3) Now, set the Liferay tomcat directory here, also set the runtime JRE (Liferay v6.1 CE(tomcat 7) JRE) and press next button.


          
          4) Set here Liferay tomcat bundle zip file. And press Finish.




Step – 6: Set build.username.properties file

Set here tomcat directory path if not configured properly.

app.server.type=tomcat
          app.server.dir=${project.dir}/../bundles/tomcat-7.0.23
          app.server.deploy.dir=${app.server.dir}/webapps
          app.server.lib.global.dir=${app.server.dir}/lib/ext
          app.server.portal.dir=${app.server.dir}/webapps/ROOT

Step – 7: Create new Liferay Plug-in Project

1)   Now, a SDK and portal server have been configured. You can create a new Liferay plug-in project.

Go to file àNew à Liferay project.

2)     In the Liferay Plug-in project page, First add the project name and display name. after that select the SDK and Liferay runtime and then select the plug-in  type (Portal is default) and you can create a new plug-in project by clicking finish.





3)     If it worked fine than you should see a new plug-in project in the package explorer, so you are to begin Plug-in development.



Step – 8: Deploy Portlet

1)   Right click on portlet plug-in project àselect LiferayàSDKàdeploy


2) If deployment is successful than you got the message in console"BUILD SUCCESSFUL"


Step – 8: Start Apache tomcat server

1)    Run startup.bat from your Liferay tomcat bundle bin directory.
     
    wait till server start up.


    


3)    if server start successful, browser should open to the portal home at http://localhost:8080