Sunday, 23 October 2011

Sample Code for PortletSiteAnalyzerLoggingService in Websphere Portal Server

WebSphere Portal Express implements a logging function for your usage data. The portal writes usage records to a dedicated log file if site analysis logging is enabled. Multiple types of site analyzer loggers allow portal administrators to collect statistical data in various areas. The portal server manages the collection of data on its own, but from a business point of view you can also log custom details of business events.

The portal configuration service, SiteAnalyzerLogService, determines the type of site analysis data that the portal logs at run time. Depending on the service configuration, the portal logs the following events:
  • Page management, such as creating, reading, updating, deleting pages.
  • Requests of pages by users.
  • Requests of portlets by users.
  • Session activities, such as login, logout, time out, login failed.
  • User management actions, such as creating, reading, updating, and deleting users and groups.

SiteAnalyzerLogService is not a replacement for other logs such as audit, performance, or system event logs. The audiences for those kinds of logs are different:
  • Audit logging is usually used in security-sensitive environments where changes made to the portal's run time configuration are recorded. Auditing is primarily part of the administrative function in WebSphere Portal. Portal analytics, on the other hand, focuses on that part of the portal that end users see as well as how they use the portal.
  • Performance logs are used to find out how well a particular part of the whole portal performs in the real production environment. It enables the operator to determine to what extent the portal consumes valuable resources (such as CPU or memory) while it adheres to the defined Service Level Agreements (SLAs).

  • System event logs help the system administrator to understand what issues occur while running the portal. Typically, records in the system event log contain Java™ exception traces that enable an administrator to take appropriate action

    Operation of SiteAnalyzerLoggingService

    Figure  shows an overview of the conceptual event-processing flow for server-side analytics, described as follows:

    1.      User requests are sent from the browser to the WebSphere Portal Server.

    2.      WebSphere Portal decomposes the browser request into the details of the user request andanalyzes the semantics of the request.

    3.      Based on the analysis of the request, WebSphere Portal writes event records to the siteanalytics log file (SA.log) on the WebSphere Portal Server's file system of the portal nodes.

    4.      These events reflect requested resources (for example, portal pages and portlets) as well asuser interactions (customization, log in, log out…) with corresponding event types.

    5.      These event types are written to the log file in a standard compliant syntax that follows theNational Center for Supercomputing Application (NCSA) Combined log format.

    Configuration of SiteAnalyzerLoggingService

    Once the server is restarted try accessing few pages and then look at the sa.log file you should see events being added to it.

    Since sa.log is NASA Format,we need some reporting  tools to view .I used Awstats.

    Click here to know  how to install Awstats and  intergate  Awstats with websphere portal.

    In this example,i have logged some messages for view and processaction method.

    Sample Code:

    package com.ourwebsphereportal.portletsiteanalyzerloggingservice;
    import java.util.*;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.portlet.*;


     * A sample portlet based on GenericPortlet
    public class PortletSiteAnalyzerLoggingServicePortlet extends GenericPortlet {

        public static final String JSP_FOLDER    = "/_PortletSiteAnalyzerLoggingService/jsp/";    // JSP folder name

        public static final String VIEW_JSP      = "PortletSiteAnalyzerLoggingServicePortletView";         // JSP file name to be rendered on the view mode
        public static final String SESSION_BEAN  = "PortletSiteAnalyzerLoggingServicePortletSessionBean";  // Bean name for the portlet session
        public static final String FORM_SUBMIT   = "PortletSiteAnalyzerLoggingServicePortletFormSubmit";   // Action name for submit form
        public static final String FORM_TEXT     = "PortletSiteAnalyzerLoggingServicePortletFormText";     // Parameter name for the text input
        PortletServiceHome psh;
        PortletSiteAnalyzerLoggingServiceHome saLogServiceHome=null;
        PortletSiteAnalyzerLogger logger;

         * @see javax.portlet.Portlet#init()
        public void init() throws PortletException{
            Context ctx =null;
            try {
                ctx = new InitialContext();
                psh = (PortletServiceHome) ctx.lookup(PortletSiteAnalyzerLoggingServiceHome.JNDI_NAME);
                 // obtain the service object
                saLogServiceHome = (PortletSiteAnalyzerLoggingServiceHome) psh
            } catch (NamingException e) {
                // TODO Auto-generated catch block


         * Serve up the <code>view</code> mode.
         * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
        public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
            // Set the MIME type for the render response

            // Check if portlet session exists
            PortletSiteAnalyzerLoggingServicePortletSessionBean sessionBean = getSessionBean(request);
            if( sessionBean==null ) {
                response.getWriter().println("<b>NO PORTLET SESSION YET</b>");
            logger=saLogServiceHome.getLogger(request, response);
            logger.log("Entering into Render");
            // Invoke the JSP to render
            PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));

         * Process an action request.
         * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
        public void processAction(ActionRequest request, ActionResponse response) throws PortletException, {
            if( request.getParameter(FORM_SUBMIT) != null ) {
                // Set form text in the session bean
                PortletSiteAnalyzerLoggingServicePortletSessionBean sessionBean = getSessionBean(request);
                if( sessionBean != null )
            logger=saLogServiceHome.getLogger(request, response);
            logger.log("Entering into processAction");

         * Get SessionBean.
         * @param request PortletRequest
         * @return PortletSiteAnalyzerLoggingServicePortletSessionBean
        private static PortletSiteAnalyzerLoggingServicePortletSessionBean getSessionBean(PortletRequest request) {
            PortletSession session = request.getPortletSession();
            if( session == null )
                return null;
            PortletSiteAnalyzerLoggingServicePortletSessionBean sessionBean = (PortletSiteAnalyzerLoggingServicePortletSessionBean)session.getAttribute(SESSION_BEAN);
            if( sessionBean == null ) {
                sessionBean = new PortletSiteAnalyzerLoggingServicePortletSessionBean();
            return sessionBean;

         * Returns JSP file path.
         * @param request Render request
         * @param jspFile JSP file name
         * @return JSP file path
        private static String getJspFilePath(RenderRequest request, String jspFile) {
            String markup = request.getProperty("wps.markup");
            if( markup == null )
                markup = getMarkup(request.getResponseContentType());
            return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);

         * Convert MIME type to markup name.
         * @param contentType MIME type
         * @return Markup name
        private static String getMarkup(String contentType) {
            if( "text/vnd.wap.wml".equals(contentType) )
                return "wml";
                return "html";

         * Returns the file extension for the JSP file
         * @param markupName Markup name
         * @return JSP extension
        private static String getJspExtension(String markupName) {
            return "jsp";



    Portlet result:

    Awstat results for view and processaction method

    Click here to download the source code

No comments:

Post a Comment