Coverage Report - javax.faces.internal.AttachedObjectStateWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
AttachedObjectStateWrapper
80%
28/35
75%
12/16
8
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.internal;
 17  
 
 18  
 import java.io.Serializable;
 19  
 
 20  
 import javax.faces.component.StateHolder;
 21  
 import javax.faces.context.FacesContext;
 22  
 
 23  
 import org.seasar.framework.log.Logger;
 24  
 
 25  
 /**
 26  
  * @author shot
 27  
  * @author manhole
 28  
  *
 29  
  * A Helper class for UIComponentBase.saveAttachedState
 30  
  *
 31  
  * This class might be changed without notice. Please do not use it
 32  
  * excluding the JSF specification part.
 33  
  */
 34  
 public class AttachedObjectStateWrapper implements Serializable {
 35  
 
 36  1
     private static final Logger logger = Logger
 37  1
             .getLogger(AttachedObjectStateWrapper.class);
 38  
 
 39  
     private static final long serialVersionUID = 3256726169255885111L;
 40  
 
 41  153
     private Object savedState = null;
 42  
 
 43  153
     private String className = null;
 44  
 
 45  153
     private boolean isSavedStateHolder = false;
 46  
 
 47  153
     public AttachedObjectStateWrapper(FacesContext context, Object obj) {
 48  153
         if (obj == null) {
 49  0
             throw new IllegalArgumentException();
 50  
         }
 51  153
         className = obj.getClass().getName();
 52  153
         if (obj instanceof StateHolder) {
 53  99
             StateHolder stateHolder = (StateHolder) obj;
 54  99
             if (!stateHolder.isTransient()) {
 55  99
                 savedState = stateHolder.saveState(context);
 56  99
                 isSavedStateHolder = true;
 57  
             }
 58  
         } else {
 59  54
             if (!(obj instanceof Serializable)) {
 60  44
                 logger.debug("class : " + className
 61  
                         + " should be serializable.");
 62  
             }
 63  54
             savedState = obj;
 64  
         }
 65  153
     }
 66  
 
 67  
     public Object restore(FacesContext context) throws IllegalStateException {
 68  153
         Object result = null;
 69  153
         if (!isSavedStateHolder) {
 70  54
             result = savedState;
 71  99
         } else if (isSavedStateHolder) {
 72  
             try {
 73  99
                 ClassLoader loader = ClassLoaderUtil.getClassLoader(context);
 74  99
                 Class clazz = ClassLoaderUtil.loadClass(loader, className);
 75  99
                 result = clazz.newInstance();
 76  0
             } catch (ClassNotFoundException e) {
 77  0
                 throw new IllegalStateException(e.getMessage());
 78  0
             } catch (InstantiationException e) {
 79  0
                 throw new IllegalStateException(e.getMessage());
 80  0
             } catch (IllegalAccessException e) {
 81  0
                 throw new IllegalStateException(e.getMessage());
 82  99
             }
 83  99
             if (savedState != null && result instanceof StateHolder) {
 84  98
                 ((StateHolder) result).restoreState(context, savedState);
 85  
             }
 86  
         }
 87  153
         return result;
 88  
     }
 89  
 
 90  
 }