package com.nesterovskyBros; import java.io.IOException; import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; import javax.el.ValueExpression; import javax.faces.component.UINamingContainer; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; /** * A scope component. * Injects a component into attribute scope on each phase. */ public class ScopeComponent extends UINamingContainer { /** * Gets a client-side identifier for this component. * @return a client-side identifier for this component. */ public String getClientId() { return getClientId(getFacesContext()); } /** * Gets component's unique id. * Unique id is used to render component exactly once. * @return component's unique id. */ public String getUniqueId() { return uniqueId; } /** * Sets component's unique id. * @param value optional component unique id. */ public void setUniqueId(String value) { uniqueId = value; } /** * Gets the binding map. * @return the binding map. */ public Map getBindings() { if (bindings == null) { bindings = new Map() { public void clear() { throw new UnsupportedOperationException(); } public boolean containsKey(Object key) { return (key != null) && (getValueExpression(key.toString()) != null); } public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } public Set> entrySet() { throw new UnsupportedOperationException(); } public ValueExpression get(Object key) { return getValueExpression(key != null ? key.toString() : null); } public boolean isEmpty() { throw new UnsupportedOperationException(); } public Set keySet() { throw new UnsupportedOperationException(); } public ValueExpression put(String key, ValueExpression value) { ValueExpression previous = get(key); setValueExpression(key, value); return previous; } public void putAll(Map map) { if (map != null) { for(Map.Entry entry: map.entrySet()) { put(entry.getKey(), entry.getValue()); } } } public ValueExpression remove(Object key) { return put(key != null ? key.toString() : null, null); } public int size() { throw new UnsupportedOperationException(); } public Collection values() { throw new UnsupportedOperationException(); } }; } return bindings; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void encodeAll(FacesContext context) throws IOException { Map attributes = context.getExternalContext().getRequestMap(); String uniqueId = getUniqueId(); if (uniqueId != null) { String key = ScopeComponent.class.getName(); Map> viewRootIds = (Map>)attributes.get(key); if (viewRootIds == null) { viewRootIds = new WeakHashMap>(); attributes.put(key, viewRootIds); } UIViewRoot viewRoot = context.getViewRoot(); Set ids = viewRootIds.get(viewRoot); if (ids == null) { ids = new HashSet(); viewRootIds.put(viewRoot, ids); } if (!ids.add(uniqueId)) { return; } } Object previous = null; String id = getId(); if (id != null) { previous = attributes.put(id, this); } try { super.encodeAll(context); } finally { if (id != null) { attributes.put(id, previous); } } } /** * {@inheritDoc} */ @Override public void processDecodes(FacesContext context) { Map attributes = context.getExternalContext().getRequestMap(); Object previous = null; String id = getId(); if (id != null) { previous = attributes.put(id, this); } try { super.processDecodes(context); } finally { if (id != null) { attributes.put(id, previous); } } } /** * {@inheritDoc} */ @Override public Object processSaveState(FacesContext context) { Map attributes = context.getExternalContext().getRequestMap(); Object previous = null; String id = getId(); if (id != null) { previous = attributes.put(id, this); } try { return super.processSaveState(context); } finally { if (id != null) { attributes.put(id, previous); } } } /** * {@inheritDoc} */ @Override public void processRestoreState(FacesContext context, Object state) { Map attributes = context.getExternalContext().getRequestMap(); Object previous = null; String id = getId(); if (id != null) { previous = attributes.put(id, this); } try { super.processRestoreState(context, state); } finally { if (id != null) { attributes.put(id, previous); } } } /** * {@inheritDoc} */ @Override public void processUpdates(FacesContext context) { Map attributes = context.getExternalContext().getRequestMap(); Object previous = null; String id = getId(); if (id != null) { previous = attributes.put(id, this); } try { super.processUpdates(context); } finally { if (id != null) { attributes.put(id, previous); } } } /** * {@inheritDoc} */ @Override public void processValidators(FacesContext context) { Map attributes = context.getExternalContext().getRequestMap(); Object previous = null; String id = getId(); if (id != null) { previous = attributes.put(id, this); } try { super.processValidators(context); } finally { if (id != null) { attributes.put(id, previous); } } } /** * A unique component id. */ private String uniqueId; /** * A bindings map. */ private Map bindings; }