001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.application;
030:
031: import javax.faces.application.*;
032: import javax.faces.context.*;
033:
034: import java.util.*;
035: import java.util.logging.*;
036:
037: import com.caucho.jsf.cfg.*;
038:
039: public class NavigationHandlerImpl extends NavigationHandler {
040: private static final Logger log = Logger
041: .getLogger(NavigationHandlerImpl.class.getName());
042:
043: private ArrayList<NavigationRule> _ruleList = new ArrayList<NavigationRule>();
044:
045: private HashMap<String, NavigationRule[]> _ruleMap = new HashMap<String, NavigationRule[]>();
046:
047: public void addRule(NavigationRule rule) {
048: _ruleList.add(rule);
049: }
050:
051: public void handleNavigation(FacesContext context,
052: String fromAction, String outcome) {
053: if (outcome == null) {
054: if (log.isLoggable(Level.FINE))
055: log.fine("Jsf[" + context.getViewRoot().getViewId()
056: + "] action " + fromAction + " has no outcome");
057:
058: return;
059: }
060:
061: NavigationRule[] ruleList;
062:
063: synchronized (_ruleMap) {
064: String viewId = context.getViewRoot().getViewId();
065:
066: ruleList = _ruleMap.get(viewId);
067:
068: if (ruleList == null) {
069: ruleList = findRuleList(viewId);
070: }
071:
072: _ruleMap.put(viewId, ruleList);
073: }
074:
075: for (int i = 0; i < ruleList.length; i++) {
076: if (ruleList[i].handleNavigation(context, fromAction,
077: outcome))
078: return;
079: }
080:
081: if (log.isLoggable(Level.FINE)) {
082: log.fine("Jsf[" + context.getViewRoot().getViewId()
083: + "] action:" + fromAction + " outcome:" + outcome
084: + " has no matching navigation rule");
085: }
086: }
087:
088: private NavigationRule[] findRuleList(String viewId) {
089: ArrayList<NavigationRule> ruleList = new ArrayList<NavigationRule>();
090:
091: for (int i = 0; i < _ruleList.size(); i++) {
092: NavigationRule rule = _ruleList.get(i);
093:
094: if (rule.isMatch(viewId))
095: ruleList.add(rule);
096: }
097:
098: NavigationRule[] rules = new NavigationRule[ruleList.size()];
099: ruleList.toArray(rules);
100:
101: Arrays.sort(rules);
102:
103: return rules;
104: }
105:
106: public String toString() {
107: return "NavigationHandlerImpl[]";
108: }
109: }
|