001: /*
002: * Copyright 2004-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.myfaces.config;
018:
019: import java.io.File;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.faces.context.ExternalContext;
027:
028: import org.apache.myfaces.config.element.ManagedBean;
029: import org.apache.myfaces.config.element.NavigationCase;
030: import org.apache.myfaces.config.element.NavigationRule;
031: import org.apache.myfaces.shared_impl.util.ClassUtils;
032:
033: public class FacesConfigValidator {
034:
035: public static final String VALIDATE_CONTEXT_PARAM = "org.apache.myfaces.VALIDATE";
036:
037: private FacesConfigValidator() {
038: // hidden
039: }
040:
041: public static List<String> validate(ExternalContext ctx,
042: String ctxPath) {
043:
044: RuntimeConfig runtimeConfig = RuntimeConfig
045: .getCurrentInstance(ctx);
046:
047: Map managedBeansMap = runtimeConfig.getManagedBeans();
048:
049: Iterator managedBeans = managedBeansMap == null ? null
050: : managedBeansMap.values() == null ? null
051: : managedBeansMap.values().iterator();
052:
053: Iterator<NavigationRule> navRules = runtimeConfig
054: .getNavigationRules() == null ? null : runtimeConfig
055: .getNavigationRules().iterator();
056:
057: return validate(managedBeans, navRules, ctxPath);
058:
059: }
060:
061: public static List<String> validate(Iterator managedBeans,
062: Iterator<NavigationRule> navRules, String ctxPath) {
063:
064: List<String> list = new ArrayList<String>();
065:
066: if (navRules != null)
067: validateNavRules(navRules, list, ctxPath);
068:
069: if (managedBeans != null)
070: validateManagedBeans(managedBeans, list);
071:
072: return list;
073: }
074:
075: private static void validateNavRules(
076: Iterator<NavigationRule> navRules, List<String> list,
077: String ctxPath) {
078:
079: while (navRules.hasNext()) {
080:
081: NavigationRule navRule = navRules.next();
082:
083: validateNavRule(navRule, list, ctxPath);
084:
085: }
086:
087: }
088:
089: private static void validateNavRule(NavigationRule navRule,
090: List<String> list, String ctxPath) {
091:
092: String fromId = navRule.getFromViewId();
093: String filePath = ctxPath + fromId;
094:
095: if (fromId != null && !"*".equals(fromId)
096: && !new File(filePath).exists())
097: list.add("File for navigation 'from id' does not exist "
098: + filePath);
099:
100: Collection cases = navRule.getNavigationCases();
101:
102: Iterator iterator = cases.iterator();
103:
104: while (iterator.hasNext()) {
105:
106: NavigationCase caze = (NavigationCase) iterator.next();
107:
108: String toViewPath = ctxPath + caze.getToViewId();
109:
110: if (!new File(toViewPath).exists())
111: list.add("File for navigation 'to id' does not exist "
112: + toViewPath);
113:
114: }
115:
116: }
117:
118: private static void validateManagedBeans(Iterator managedBeans,
119: List<String> list) {
120:
121: while (managedBeans.hasNext()) {
122:
123: ManagedBean managedBean = (ManagedBean) managedBeans.next();
124:
125: validateManagedBean(managedBean, list);
126:
127: }
128:
129: }
130:
131: private static void validateManagedBean(ManagedBean managedBean,
132: List<String> list) {
133:
134: String className = managedBean.getManagedBeanClassName();
135:
136: try {
137: ClassUtils.classForName(className);
138: } catch (ClassNotFoundException e) {
139:
140: String msg = "Could not locate class " + className
141: + " for managed bean '"
142: + managedBean.getManagedBeanName() + "'";
143:
144: list.add(msg);
145:
146: }
147:
148: }
149:
150: }
|