Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.myfaces.trinidad.util;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
/**
* A utility to store a reference to an UIComponent. Application developers
* should use this tool if they need to have a reference to an instance of the
* UIComponent class in managed beans that are longer than requested scoped
* --for example Session and Application Scoped. The reference will return the UIComponent, if any, with
* the same scoped id as the Component used to create the reference, in the current UIViewRoot.
*
* Use newUIComponentReference() to create a ComponentReference and
* use the getComponent() to look up the referenced UIComponent.
*
* For example, a current weather application might have have a session scoped weatehrBean
* containing the current list of locations to report the weather on and support using a
* selectMany component to remove the locations:
*
*
*
*
*
*
*
*
* The weatherBean might looks like this:
*
* public class WeatherBean implements Serializable
* {
* public void setLocationsToRemove(UIXSelectMany locationsToRemove)
* {
* _locationsToRemove = locationsToRemove;
* }
*
* public UIXSelectMany getLocationsToRemove()
* {
* return _locationsToRemove;
* }
*
* public void removeLocationListener(ActionEvent actionEvent)
* {
* ... code calling getLocationsToRemove() to get the UIXSelectMany ...
* }
*
* private UIXSelectMany _locationsToRemove
* }
*
* This code has several problems:
*
*
Since UIComponents aren't Serializable, the class will fail serialization during fail-over
* when default Serialization attempts to serialize _locationsToRemove.
*
If the user opens two windows on this page, only the last window rendered have the
* correct UIXSelectMany instance. If the remove locations button is pressed on the first
* window after rendering the second window, the wrong UIXSelectMany instance will be used.
*
Since UIComponents aren't thread-safe, the above case could also result in bizare
* behavior if requests from both windows were being processed by the application server at the
* same time.
*
If the Trinidad view state token cache isn't used, or if the user navigates to this page
* using the backbutton, a new UIXSelectMany instance will be created, which also won't match
* the instance we are holding onto.
*
If we don't clear the UIXSelectMany instance when we navigate off of this page, we will
* continue to pin the page's UIComponent tree in memory for the lifetime of the Session.
*
*
* Rewritten using ComponentReference, the weatherBean might looks like this:
*
* public class WeatherBean implements Serializable
* {
* public void setLocationsToRemove(UIXSelectMany locationsToRemove)
* {
* _locationsToRemoveRef = UIComponentReference.newUIComponentReference(locationsToRemove);
* }
*
* public UIXSelectMany getLocationsToRemove()
* {
* return _locationsToRemoveRef.getComponent();
* }
*
* public void removeLocationListener(ActionEvent actionEvent)
* {
* ... code calling getLocationsToRemove() to get the UIXSelectMany ...
* }
*
* private UIComponentReference _locationsToRemoveRef
* }
*
* The above code saves a reference to the component passed to the managed bean and then
* retrieves the correct instance given the current UIViewRoot for this request whenever
* getLocationsToRemove() is called.
*
Please note:
*
*
This class is not completely thread-safe, since it depends on UIComponent
* APIs, however the class is safe to use as long as either of the following is true
*
*
The component passed to newUIComponentReference has an id and is in the
* component hierarchy when newUIComponentReference is called and all subsequent calls to
* getComponent are made from Threads with a valid FacesContext
*
*
The first call to getComponent is on the same Thread that
* newUIComponentReference was called on
*
*
*
The passed in UIComponent is required to have an ID
*
The reference will break if the UIComponent is moved between
* NamingContainers or
* if any of the ancestor NamingContainers have their IDs changed.
*
The reference is persistable. HoweverUIComponents are not
* Serializable and therefore can not be used at any scope longer than request.
*
*
* @see ComponentReference#newUIComponentReference(UIComponent)
* @see ComponentReference#getComponent()
*/
public abstract class ComponentReference implements Serializable
{
// don't allow other subclasses
private ComponentReference(List