Home » Mojarra-2.0.1 » javax » faces » component » [javadoc | source]

    1   /*
    2    * $Id: StateHolderSaver.java,v 1.16 2007/04/27 22:00:03 ofung Exp $
    3    */
    4   
    5   /*
    6    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    7    * 
    8    * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
    9    * 
   10    * The contents of this file are subject to the terms of either the GNU
   11    * General Public License Version 2 only ("GPL") or the Common Development
   12    * and Distribution License("CDDL") (collectively, the "License").  You
   13    * may not use this file except in compliance with the License. You can obtain
   14    * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
   15    * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
   16    * language governing permissions and limitations under the License.
   17    * 
   18    * When distributing the software, include this License Header Notice in each
   19    * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
   20    * Sun designates this particular file as subject to the "Classpath" exception
   21    * as provided by Sun in the GPL Version 2 section of the License file that
   22    * accompanied this code.  If applicable, add the following below the License
   23    * Header, with the fields enclosed by brackets [] replaced by your own
   24    * identifying information: "Portions Copyrighted [year]
   25    * [name of copyright owner]"
   26    * 
   27    * Contributor(s):
   28    * 
   29    * If you wish your version of this file to be governed by only the CDDL or
   30    * only the GPL Version 2, indicate your decision by adding "[Contributor]
   31    * elects to include this software in this distribution under the [CDDL or GPL
   32    * Version 2] license."  If you don't indicate a single choice of license, a
   33    * recipient has the option to distribute your version of this file under
   34    * either the CDDL, the GPL Version 2 or to extend the choice of license to
   35    * its licensees as provided above.  However, if you add GPL Version 2 code
   36    * and therefore, elected the GPL Version 2 license, then the option applies
   37    * only if the new code is made subject to such option by the copyright
   38    * holder.
   39    */
   40   
   41   package javax.faces.component;
   42   
   43   import javax.faces.context.FacesContext;
   44   import javax.faces.component.StateHolder;
   45   
   46   import java.io.Serializable;
   47   
   48   /**
   49    * <p>Helper class for saving and restoring attached objects.</p>
   50    */
   51   class StateHolderSaver implements Serializable {
   52   
   53       private static final long serialVersionUID = 6470180891722042701L;
   54   
   55       private String className = null;
   56       private Serializable savedState = null;
   57   
   58       public StateHolderSaver(FacesContext context, Object toSave) {
   59           className = toSave.getClass().getName();
   60   
   61           if (toSave instanceof StateHolder) {
   62               // do not save an attached object that is marked transient.
   63               if (!((StateHolder) toSave).isTransient()) {
   64                   savedState =
   65                         (Serializable) ((StateHolder) toSave).saveState(context);
   66               } else {
   67                   className = null;
   68               }
   69           } else if (toSave instanceof Serializable) {
   70               savedState = (Serializable) toSave;
   71               className = null;
   72           }
   73       }
   74   
   75       /**
   76        *
   77        * @return the restored {@link StateHolder} instance.
   78        */
   79   
   80       public Object restore(FacesContext context) throws IllegalStateException {
   81           Object result = null;
   82           Class toRestoreClass;
   83   
   84           // if the Object to save implemented Serializable but not
   85           // StateHolder
   86           if (null == className && null != savedState) {
   87               return savedState;
   88           }
   89   
   90           // if the Object to save did not implement Serializable or
   91           // StateHolder
   92           if (className == null) {
   93               return null;
   94           }
   95   
   96           // else the object to save did implement StateHolder
   97   
   98           try {
   99               toRestoreClass = loadClass(className, this);
  100           }
  101           catch (ClassNotFoundException e) {
  102               throw new IllegalStateException(e);
  103           }
  104   
  105           if (null != toRestoreClass) {
  106               try {
  107                   result = toRestoreClass.newInstance();
  108               }
  109               catch (InstantiationException e) {
  110                   throw new IllegalStateException(e);
  111               }
  112               catch (IllegalAccessException a) {
  113                   throw new IllegalStateException(a);
  114               }
  115           }
  116   
  117           if (null != result && null != savedState &&
  118               result instanceof StateHolder) {
  119               // don't need to check transient, since that was done on
  120               // the saving side.
  121               ((StateHolder) result).restoreState(context, savedState);
  122           }
  123           return result;
  124       }
  125   
  126   
  127       private static Class loadClass(String name, 
  128               Object fallbackClass) throws ClassNotFoundException {
  129           ClassLoader loader =
  130               Thread.currentThread().getContextClassLoader();
  131           if (loader == null) {
  132               loader = fallbackClass.getClass().getClassLoader();
  133           }
  134           return Class.forName(name, false, loader);
  135       }
  136   }

Home » Mojarra-2.0.1 » javax » faces » component » [javadoc | source]