com.pureinfo.ark.lifecycle.LifecyleHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.ark.lifecycle.LifecyleHelper.java

Source

/**
 * PureInfo Ark
 * @(#)LifecyleHelper.java   1.0 2005-9-13
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.ark.lifecycle;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.pureinfo.ark.ArkExceptionTypes;
import com.pureinfo.ark.cache.ContentCacheServer;
import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.ark.content.model.ArkContent;
import com.pureinfo.ark.content.model.ContentType;
import com.pureinfo.ark.lifecycle.model.ILifecycleListener;
import com.pureinfo.ark.lifecycle.model.LifecycleEvent;
import com.pureinfo.force.PureFactory;
import com.pureinfo.force.exception.PureException;

/**
 * <P>
 * Created on 2005-9-13 11:40:48 <BR>
 * Last modified on 2005-9-13
 * </P>
 * LifecyleHelper: lifecycle management helper.
 * 
 * @author Why
 * @version 1.0, 2005-9-13
 * @since Ark 1.0
 */
public class LifecyleHelper {
    //logger
    private final static Logger logger = Logger.getLogger(LifecyleHelper.class.getName());

    /**
     * constructor: forbid to call
     */
    private LifecyleHelper() {

    }

    /**
     * Handles lifecycle event for Ark content.
     * 
     * @param _nType
     *            event type
     * @param _source
     *            event source
     * @return the total number of listeners who have handled this event.
     * @throws PureException
     *             if failed to find the listeners or to handle the event.
     */
    public static int handleEvent(int _nType, ArkContent _source) throws PureException {
        ContentType contentType = ArkContentHelper.lookupTypeByClass(_source.getClass().getName());
        LifecycleEvent event = new LifecycleEvent(_nType, _source);
        int nTotalHandled = 0;

        //1. to check if the content is cached
        if (contentType.isCached()) {
            ContentCacheServer.handleEvent(event);
            nTotalHandled++;
        }

        //2. to notify other listeners
        String[] arrListenerIds = contentType.getListenerIds();
        if (arrListenerIds == null || arrListenerIds.length == 0) {
            return nTotalHandled;
        }

        //else
        try {
            ILifecycleListener listener;
            for (int i = 0; i < arrListenerIds.length; i++) {
                listener = (ILifecycleListener) PureFactory.getBean(arrListenerIds[i]);
                if (listener.handleEvent(event)) {
                    nTotalHandled++;
                }
            }

            if (logger.isDebugEnabled()) {
                logger.debug(nTotalHandled + " listeners have handled the event: " + event.toString());
            }
            return nTotalHandled;
        } catch (Exception ex) {
            logger.error("event " + event.toString() + "; listeners=" + StringUtils.join(arrListenerIds, ","), ex);
            throw new PureException(ArkExceptionTypes.LIFECYCLE_EVENT_HANDLE, event.toString(), ex);
        }
    }

}