Coverage Report - org.seasar.cubby.plugins.s2.el.S2BeanELResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
S2BeanELResolver
0%
0/91
0%
0/48
7.25
 
 1  
 /*
 2  
  * Copyright 2004-2010 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  
 
 17  
 package org.seasar.cubby.plugins.s2.el;
 18  
 
 19  
 import static org.seasar.framework.message.MessageFormatter.getMessage;
 20  
 
 21  
 import java.beans.FeatureDescriptor;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 import javax.el.ELContext;
 27  
 import javax.el.ELException;
 28  
 import javax.el.ELResolver;
 29  
 import javax.el.PropertyNotFoundException;
 30  
 import javax.el.PropertyNotWritableException;
 31  
 
 32  
 import org.seasar.cubby.spi.beans.Attribute;
 33  
 import org.seasar.cubby.spi.beans.BeanDesc;
 34  
 import org.seasar.cubby.spi.beans.BeanDescFactory;
 35  
 
 36  
 /**
 37  
  * S2Container の public フィールドをプロパティとして認識させるための {@link ELResolver} です。
 38  
  * 
 39  
  * @author baba
 40  
  */
 41  
 public class S2BeanELResolver extends ELResolver {
 42  
 
 43  
         /** 読み込み専用。 */
 44  
         private final boolean readOnly;
 45  
 
 46  
         /**
 47  
          * インスタンスを生成します。
 48  
          */
 49  
         public S2BeanELResolver() {
 50  0
                 this(false);
 51  0
         }
 52  
 
 53  
         /**
 54  
          * インスタンスを生成します。
 55  
          * 
 56  
          * @param readOnly
 57  
          *            読み込み専用とする場合は <code>true</code>、そうでない場合は <code>false</code>
 58  
          */
 59  
         public S2BeanELResolver(final boolean readOnly) {
 60  0
                 super();
 61  0
                 this.readOnly = readOnly;
 62  0
         }
 63  
 
 64  
         /**
 65  
          * {@inheritDoc}
 66  
          */
 67  
         @Override
 68  
         public Object getValue(final ELContext context, final Object base,
 69  
                         final Object property) throws NullPointerException,
 70  
                         PropertyNotFoundException, ELException {
 71  0
                 if (context == null) {
 72  0
                         throw new NullPointerException();
 73  
                 }
 74  0
                 if (base == null || property == null) {
 75  0
                         return null;
 76  
                 }
 77  
 
 78  0
                 final String propertyName = property.toString();
 79  0
                 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 80  0
                 if (!beanDesc.hasPropertyAttribute(propertyName)) {
 81  0
                         return null;
 82  
                 }
 83  
 
 84  
                 try {
 85  0
                         final Attribute attributeDesc = beanDesc
 86  
                                         .getPropertyAttribute(propertyName);
 87  0
                         final Object value = attributeDesc.getValue(base);
 88  0
                         context.setPropertyResolved(true);
 89  0
                         return value;
 90  0
                 } catch (final Exception e) {
 91  0
                         return null;
 92  
                 }
 93  
         }
 94  
 
 95  
         /**
 96  
          * {@inheritDoc}
 97  
          */
 98  
         @Override
 99  
         public Class<?> getType(final ELContext context, final Object base,
 100  
                         final Object property) {
 101  0
                 if (context == null) {
 102  0
                         throw new NullPointerException();
 103  
                 }
 104  
 
 105  0
                 if (base == null || property == null) {
 106  0
                         return null;
 107  
                 }
 108  
 
 109  0
                 final String propertyName = property.toString();
 110  0
                 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 111  0
                 if (!beanDesc.hasPropertyAttribute(propertyName)) {
 112  0
                         return null;
 113  
                 }
 114  
 
 115  
                 try {
 116  0
                         final Attribute attributeDesc = beanDesc
 117  
                                         .getPropertyAttribute(propertyName);
 118  0
                         final Class<?> propertyType = attributeDesc.getType();
 119  0
                         context.setPropertyResolved(true);
 120  0
                         return propertyType;
 121  0
                 } catch (final Exception e) {
 122  0
                         return null;
 123  
                 }
 124  
         }
 125  
 
 126  
         /**
 127  
          * {@inheritDoc}
 128  
          */
 129  
         @Override
 130  
         public void setValue(final ELContext context, final Object base,
 131  
                         final Object property, final Object value) {
 132  0
                 if (context == null) {
 133  0
                         throw new NullPointerException();
 134  
                 }
 135  0
                 if (base == null || property == null) {
 136  0
                         return;
 137  
                 }
 138  
 
 139  0
                 final String propertyName = property.toString();
 140  0
                 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 141  0
                 if (!beanDesc.hasPropertyAttribute(propertyName)) {
 142  0
                         return;
 143  
                 }
 144  
 
 145  0
                 if (this.readOnly) {
 146  0
                         throw new PropertyNotWritableException(getMessage("ECUB0001",
 147  
                                         new Object[] { base.getClass().getName() }));
 148  
                 }
 149  
 
 150  
                 try {
 151  0
                         final Attribute attributeDesc = beanDesc
 152  
                                         .getPropertyAttribute(propertyName);
 153  0
                         attributeDesc.setValue(base, value);
 154  0
                         context.setPropertyResolved(true);
 155  0
                 } catch (final Exception e) {
 156  0
                 }
 157  0
         }
 158  
 
 159  
         /**
 160  
          * {@inheritDoc}
 161  
          */
 162  
         @Override
 163  
         public boolean isReadOnly(final ELContext context, final Object base,
 164  
                         final Object property) {
 165  0
                 if (context == null) {
 166  0
                         throw new NullPointerException();
 167  
                 }
 168  0
                 if (base == null || property == null) {
 169  0
                         return false;
 170  
                 }
 171  
 
 172  0
                 final String propertyName = property.toString();
 173  0
                 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 174  0
                 if (!beanDesc.hasPropertyAttribute(propertyName)) {
 175  0
                         return true;
 176  
                 }
 177  
 
 178  0
                 if (this.readOnly) {
 179  0
                         return true;
 180  
                 }
 181  
 
 182  
                 try {
 183  0
                         final Attribute attributeDesc = beanDesc
 184  
                                         .getPropertyAttribute(propertyName);
 185  0
                         final boolean readOnly = !attributeDesc.isWritable();
 186  0
                         context.setPropertyResolved(true);
 187  0
                         return readOnly;
 188  0
                 } catch (final Exception e) {
 189  0
                         return false;
 190  
                 }
 191  
         }
 192  
 
 193  
         /**
 194  
          * {@inheritDoc}
 195  
          */
 196  
         @Override
 197  
         public Iterator<FeatureDescriptor> getFeatureDescriptors(
 198  
                         final ELContext context, final Object base) {
 199  0
                 if (context == null) {
 200  0
                         throw new NullPointerException();
 201  
                 }
 202  
 
 203  0
                 if (base == null) {
 204  0
                         return null;
 205  
                 }
 206  
 
 207  0
                 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 208  
                 try {
 209  0
                         final List<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>();
 210  0
                         for (final Attribute attributeDesc : beanDesc
 211  
                                         .findtPropertyAttributes()) {
 212  0
                                 final String propertyName = attributeDesc.getName();
 213  0
                                 final FeatureDescriptor descriptor = new FeatureDescriptor();
 214  0
                                 descriptor.setDisplayName(propertyName);
 215  0
                                 descriptor.setExpert(false);
 216  0
                                 descriptor.setHidden(false);
 217  0
                                 descriptor.setName(propertyName);
 218  0
                                 descriptor.setPreferred(true);
 219  0
                                 descriptor.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
 220  0
                                 descriptor.setValue(TYPE, attributeDesc.getType());
 221  0
                                 descriptors.add(descriptor);
 222  0
                         }
 223  
 
 224  0
                         return descriptors.iterator();
 225  0
                 } catch (final Exception e) {
 226  
                         // do nothing
 227  
                 }
 228  0
                 return null;
 229  
         }
 230  
 
 231  
         /**
 232  
          * {@inheritDoc}
 233  
          */
 234  
         @Override
 235  
         public Class<?> getCommonPropertyType(final ELContext context,
 236  
                         final Object base) {
 237  0
                 if (context == null) {
 238  0
                         throw new NullPointerException();
 239  
                 }
 240  
 
 241  0
                 if (base != null) {
 242  0
                         return Object.class;
 243  
                 }
 244  
 
 245  0
                 return null;
 246  
         }
 247  
 
 248  
 }