jsfdeveloperaid.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 756 行 · 第 1/2 页
JAVA
756 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Alex Rojkov */package com.caucho.jsf.dev;import com.caucho.util.L10N;import com.caucho.jsf.webapp.FacesServletImpl;import com.caucho.server.webapp.WebApp;import javax.el.ValueExpression;import javax.faces.component.EditableValueHolder;import javax.faces.component.UIComponent;import javax.faces.component.UIComponentBase;import javax.faces.component.UIViewRoot;import javax.faces.component.ValueHolder;import javax.faces.context.ExternalContext;import javax.faces.context.FacesContext;import javax.faces.event.PhaseEvent;import javax.faces.event.PhaseId;import javax.faces.event.PhaseListener;import java.io.Serializable;import java.lang.reflect.Method;import java.lang.reflect.Field;import java.lang.reflect.Array;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;public class JsfDeveloperAid implements PhaseListener{ public static final String URL_PATTERN = "caucho.jsf.developer.aid"; private static final Logger log = Logger.getLogger(FacesServletImpl.class.getName()); private static final L10N L = new L10N(JsfDeveloperAid.class); private String _developerAidLinkStyle; public JsfDeveloperAid() { WebApp webApp = WebApp.getCurrent(); _developerAidLinkStyle = webApp.getJsf().getDeveloperAidLinkStyle(); } public void afterPhase(PhaseEvent event) { final FacesContext context = event.getFacesContext(); final ExternalContext exContext = context.getExternalContext(); final Map<String, Object> sessionMap = exContext.getSessionMap(); Map<String, JsfRequestSnapshot> aidMap = (Map<String, JsfRequestSnapshot>) sessionMap.get( "caucho.jsf.developer.aid"); if (aidMap == null) { aidMap = new HashMap<String, JsfRequestSnapshot>(); sessionMap.put("caucho.jsf.developer.aid", aidMap); } try { final UIViewRoot uiViewRoot = context.getViewRoot(); if (uiViewRoot != null) { final String viewId = uiViewRoot.getViewId(); final String phaseId = event.getPhaseId().toString(); final ViewRoot viewRoot = (ViewRoot) reflect(context, uiViewRoot); viewRoot.setPhase(phaseId); //request attributes Map<String, Object> requestMap = exContext.getRequestMap(); Map<String, Bean> requestSnapshot = new HashMap<String, Bean>(); for (String key : requestMap.keySet()) { if (key.startsWith("caucho.") || key.startsWith("com.caucho.") || key.startsWith("javax.")) continue; Bean bean = reflect(requestMap.get(key)); requestSnapshot.put(key, bean); } viewRoot.setRequestMap(requestSnapshot); //session attributes Map<String, Bean> sessionSnapshot = new HashMap<String, Bean>(); for (String key : sessionMap.keySet()) { if (key.startsWith("caucho.") || key.startsWith("com.caucho.") || key.startsWith("javax.")) continue; Bean bean = reflect(sessionMap.get(key)); sessionSnapshot.put(key, bean); } viewRoot.setSessionMap(sessionSnapshot); //application attributes Map<String, Object> applicationMap = exContext.getApplicationMap(); Map<String, Bean> applicationSnapshot = new HashMap<String, Bean>(); for (String key : applicationMap.keySet()) { if (key.startsWith("caucho.") || key.startsWith("com.caucho.") || key.startsWith("javax.")) continue; Bean bean = reflect(applicationMap.get(key)); applicationSnapshot.put(key, bean); } viewRoot.setApplicationMap(applicationSnapshot); JsfRequestSnapshot snapshot; if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId())) { snapshot = new JsfRequestSnapshot(); //headers Map<String, String> map = exContext.getRequestHeaderMap(); snapshot.setHeaderMap(new HashMap<String, String>(map)); //parameters map = exContext.getRequestParameterMap(); snapshot.setParameterMap(new HashMap<String, String>(map)); aidMap.put(viewId, snapshot); } else { snapshot = aidMap.get(viewId); } snapshot.addViewRoot(viewRoot); } } catch (IllegalStateException e) { log.log(Level.FINER, e.getMessage(), e); } catch (Throwable t) { log.log(Level.FINER, t.getMessage(), t); } } public void beforePhase(PhaseEvent event) { if (!PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) return; UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); if (viewRoot == null) return; JsfDeveloperAidLink link = new JsfDeveloperAidLink(); link.setStyle(_developerAidLinkStyle); viewRoot.getChildren().add(link); } public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } public Component reflect(FacesContext facesContext, UIComponent uiComponent) { final Component result; if (uiComponent instanceof UIViewRoot) { UIViewRoot uiViewRoot = (UIViewRoot) uiComponent; result = new ViewRoot(); ViewRoot viewRoot = (ViewRoot) result; viewRoot.setLocale(uiViewRoot.getLocale()); viewRoot.setRenderKitId(uiViewRoot.getRenderKitId()); } else result = new Component(); result._uiComponentClass = uiComponent.getClass().getSimpleName(); result._clientId = uiComponent.getClientId(facesContext); result._family = uiComponent.getFamily(); final int childCount = uiComponent.getChildCount(); if (childCount > 0) { List<UIComponent> children = uiComponent.getChildren(); result._children = new ArrayList<Component>(children.size()); for (int i = 0; i < childCount; i++) { UIComponent child = children.get(i); if (!(child instanceof JsfDeveloperAidLink)) result._children.add(reflect(facesContext, children.get(i))); } } final int facetCount = uiComponent.getFacetCount(); if (facetCount > 0) { Map<String, UIComponent> facets = uiComponent.getFacets(); result._facets = new HashMap<String, Component>(facets.size()); Set<String> names = facets.keySet(); for (String name : names) { UIComponent child = facets.get(name); result._facets.put(name, reflect(facesContext, child)); } } if (uiComponent instanceof ValueHolder) { result._isValueHolder = true; Object value; try { value = ((ValueHolder) uiComponent).getValue(); } catch (Throwable t) { value = "Failed due to: " + t.getMessage(); } result._value = String.valueOf(value); Object localValue; try { localValue = ((ValueHolder) uiComponent).getLocalValue(); } catch (Throwable t) { localValue = "Failed due to: " + t.getMessage(); } result._localValue = String.valueOf(localValue); } if (uiComponent instanceof EditableValueHolder) { result._isEditableValueHolder = true; Object submittedValue; try { submittedValue = ((EditableValueHolder) uiComponent).getSubmittedValue(); } catch (Throwable t) { submittedValue = "Failed due to: " + t.getMessage(); } if (submittedValue instanceof Object[]) { StringBuilder sb = new StringBuilder('['); Object []values = (Object[]) submittedValue; for (int i = 0; i < values.length; i++) { Object value = values[i]; sb.append(String.valueOf(value)); if ((i + 1) < values.length) sb.append(','); } sb.append(']'); result._submittedValue = sb.toString(); } else { result._submittedValue = String.valueOf(submittedValue); } } for (Method method : uiComponent.getClass().getMethods()) { if (!method.getName().startsWith("get") && !method.getName().startsWith("is")) continue; else if (method.getParameterTypes().length != 0) continue; String name; if (method.getName().startsWith("get")) name = method.getName().substring(3); else if (method.getName().startsWith("is")) name = method.getName().substring(2); else continue; name = Character.toLowerCase(name.charAt(0)) + name.substring(1); ValueExpression expr = uiComponent.getValueExpression(name); Class type = method.getReturnType(); if (expr != null) { result.setAttribute("expr:" + name, expr.getExpressionString()); } else if (method.getDeclaringClass().equals(UIComponent.class) || method.getDeclaringClass().equals(UIComponentBase.class)) { } else if (name.equals("family") || name.equals("value") || name.equals("localValue") || name.equals("submittedValue")) { } else if (String.class.equals(type)) { try { Object value = method.invoke(uiComponent); if (value != null) result.setAttribute(name, String.valueOf(value)); } catch (Exception e) { } } } return result; } public Bean reflect(Object obj) { if (obj == null) return null; final Bean result; if (obj instanceof String || obj instanceof Boolean || obj instanceof Character || obj instanceof Number
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?