View Javadoc
1 /* 2 * SetBeanCfg.java 3 * Created on September 20, 2003 4 * 5 * The Blues Framework - A lightweight application framework 6 * Copyright (C) 2003 Lonnie Pryor 7 * http://blues.lonniepryor.com 8 * 9 * This library is free software; you can redistribute it and/or modify it under the 10 * terms of the GNU Lesser General Public License as published by the Free Software 11 * Foundation; either version 2.1 of the License, or (at your option) any later 12 * version. 13 * 14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 16 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License along 19 * with this library; if not, write to: 20 * 21 * The Free Software Foundation, Inc. 22 * 59 Temple Place, Suite 330 23 * Boston, MA 02111-1307 USA 24 * 25 */ 26 package com.lonniepryor.blues.cfg; 27 28 import com.lonniepryor.blues.util.BeanHelper; 29 import com.lonniepryor.blues.util.Types; 30 31 /*** 32 * Child element for setting java beans properties to a basic value. 33 * 34 * @author Lonnie Pryor 35 * @version $Revision: 1.1 $ 36 */ 37 public class SetBeanCfg extends AbstractBeanCfg implements SetCfg, Verifiable { 38 /*** The name of the property to set. */ 39 private String property; 40 /*** The type of the bean to set. */ 41 private Class beanClass; 42 43 /*** 44 * Creates a new SetBeanCfg object. 45 */ 46 public SetBeanCfg () { 47 } 48 49 /*** 50 * Sets the name of the property to set. 51 * 52 * @param property The name of the property to set. 53 */ 54 public void setProperty (String property) { 55 this.property = property; 56 } 57 58 /*** 59 * Sets the type of the bean to set. 60 * 61 * @param beanClass The type of the bean to set. 62 */ 63 public void setClass (Class beanClass) { 64 this.beanClass = beanClass; 65 } 66 67 /* (non-Javadoc) 68 * @see com.lonniepryor.blues.cfg.Verifiable#verify() 69 */ 70 public void verify () { 71 if (property == null) 72 throw new IllegalStateException("No property name specified"); 73 if (property.length() == 0) 74 throw new IllegalStateException("Illegal empty property name"); 75 if (beanClass == null) 76 throw new IllegalStateException("No property bean class specified"); 77 if (!Types.javaBeans().isSatisfiedBy(beanClass)) 78 throw new IllegalStateException( 79 "Invalid java bean class: " + beanClass.getName()); 80 } 81 82 /* (non-Javadoc) 83 * @see com.lonniepryor.blues.cfg.SetCfg#configure( 84 * com.lonniepryor.blues.util.BeanHelper, java.lang.Object) 85 */ 86 public void configure ( 87 BeanHelper helper, Object beanInstance, Directory serviceDirectory) { 88 if (helper == null) 89 throw new NullPointerException("Bean helper cannot be null"); 90 if (beanInstance == null) 91 throw new NullPointerException("Bean instance cannot be null"); 92 BeanHelper myHelper = BeanHelper.introspect(beanClass); 93 Object myBeanInstance = myHelper.newBeanInstance(); 94 configureBean(myHelper, myBeanInstance, serviceDirectory); 95 if (!helper.setProperty(beanInstance, property, myBeanInstance)) 96 throw new IllegalStateException( 97 "Cannot set property '" + property + "' on " + beanInstance.getClass() 98 + " to bean '" + myBeanInstance + "'"); 99 } 100 }

This page was automatically generated by Maven