cn.teamlab.wg.framework.struts2.breadcrumb.TopBreadCrumbInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for cn.teamlab.wg.framework.struts2.breadcrumb.TopBreadCrumbInterceptor.java

Source

/*
 *  Copyright 2011 - Giovanni Tosto
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package cn.teamlab.wg.framework.struts2.breadcrumb;

/**
    
 */
import java.util.Comparator;
import java.util.Stack;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionInvocation;

/**
 * @author Giovanni Tosto
 * @version $Id: BreadCrumbInterceptor.java 128 2011-03-21 17:54:01Z
 *          giovanni.tosto $
 */
public class TopBreadCrumbInterceptor extends BreadCrumbInterceptor {

    private static final long serialVersionUID = 1L;

    private static final Log LOG = LogFactory.getLog(TopBreadCrumbInterceptor.class);

    protected void beforeInvocation(ActionInvocation invocation) {

        BreadCrumb annotation = processAnnotation(invocation);

        /*
         * overrides rewind mode of this invocation if needed
         */

        if (annotation != null) {

            Crumb current = makeCrumb(invocation, annotation.name(), annotation.key());

            // get the bread crumbs trail stored in session(CRUMB_KEY)
            BreadCrumbTrail trail = getBreadCrumbTrail(invocation);

            // set default configuration
            RewindMode mode = trail.rewindMode;
            int maxCrumbs = trail.maxCrumbs;
            Comparator<Crumb> comparator = trail.comparator;

            // TODO override configuration (if needed)
            if (annotation.rewind() != RewindMode.DEFAULT)
                mode = annotation.rewind();

            if (annotation.comparator() != BreadCrumb.NULL.class) {
                comparator = createComparator(annotation.comparator());
            }

            // The comparator to use

            // then set initial condition the crumbs
            Stack<Crumb> crumbs = trail.getCrumbs();

            /*
             * synchronized region is needed to prevent
             * ConcurrentModificationException(s) for concurrent
             * request (operating on the same session) that would
             * modify the bread crumbs trail.
             */
            synchronized (crumbs) {
                LOG.debug("aquired lock on crumbs trail");

                Crumb last = (crumbs.size() == 0) ? null : crumbs.lastElement();

                /*
                 * compare current and last crumbs
                 */
                /*
                 * if (comparator.compare(current, last) != 0) {
                 * int dupIdx = trail.indexOf(current,
                 * comparator); if (mode == RewindMode.AUTO &&
                 * dupIdx != -1) { trail.rewindAt(dupIdx - 1); }
                 * crumbs.push(current); if (crumbs.size() >
                 * maxCrumbs) crumbs.remove(0);
                 * 
                 * } else {
                 * 
                 * if (crumbs.size() > 0) {
                 * crumbs.remove(crumbs.size() - 1);
                 * crumbs.push(current); } }
                 */

                if (crumbs.size() > 0) {
                    // group checking (excluding the first one)
                    if (last.getAction().startsWith(current.getAction().split("/")[0]) == false) {
                        // not the same group
                        trail.rewindAt(0);
                    }

                    // the same group, sort them
                    int dupIdx = trail.indexOf(current, comparator);
                    if (mode == RewindMode.AUTO && dupIdx != -1) {
                        trail.rewindAt(dupIdx - 1);
                    }
                    crumbs.push(current);
                    if (crumbs.size() > maxCrumbs) {
                        crumbs.remove(0);
                    }
                    /*
                     * if (crumbs.size() > 2) { // sort by
                     * name trail.sort(comparator); }
                     */
                } else {
                    // add directly (after the first one)
                    crumbs.push(current);
                }

                LOG.debug("releasing lock on crumbs trail");
            } // synchronized
        }

    }

}