001 /*
002 $Id: Expando.java,v 1.4 2005/06/13 06:43:38 glaforge Exp $
003
004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005
006 Redistribution and use of this software and associated documentation
007 ("Software"), with or without modification, are permitted provided
008 that the following conditions are met:
009
010 1. Redistributions of source code must retain copyright
011 statements and notices. Redistributions must also contain a
012 copy of this document.
013
014 2. Redistributions in binary form must reproduce the
015 above copyright notice, this list of conditions and the
016 following disclaimer in the documentation and/or other
017 materials provided with the distribution.
018
019 3. The name "groovy" must not be used to endorse or promote
020 products derived from this Software without prior written
021 permission of The Codehaus. For written permission,
022 please contact info@codehaus.org.
023
024 4. Products derived from this Software may not be called "groovy"
025 nor may "groovy" appear in their names without prior written
026 permission of The Codehaus. "groovy" is a registered
027 trademark of The Codehaus.
028
029 5. Due credit should be given to The Codehaus -
030 http://groovy.codehaus.org/
031
032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043 OF THE POSSIBILITY OF SUCH DAMAGE.
044
045 */
046 package groovy.util;
047
048 import groovy.lang.Closure;
049 import groovy.lang.GroovyObjectSupport;
050 import groovy.lang.GroovyRuntimeException;
051 import groovy.lang.MetaExpandoProperty;
052
053 import java.util.HashMap;
054 import java.util.Map;
055 import java.util.Map.Entry;
056 import java.util.List;
057 import java.util.ArrayList;
058 import java.util.Iterator;
059
060
061 /**
062 * Represents a dynamically expandable bean.
063 *
064 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
065 * @version $Revision: 1.4 $
066 */
067 public class Expando extends GroovyObjectSupport {
068
069 private Map expandoProperties;
070
071 public Expando() {
072 }
073
074 public Expando(Map expandoProperties) {
075 this.expandoProperties = expandoProperties;
076 }
077
078 /**
079 * @return the dynamically expanded properties
080 */
081 public Map getExpandoProperties() {
082 if (expandoProperties == null) {
083 expandoProperties = createMap();
084 }
085 return expandoProperties;
086 }
087
088 public List getProperties() {
089 // run through all our current properties and create MetaProperty objects
090 List ret = new ArrayList();
091 Iterator itr = getExpandoProperties().entrySet().iterator();
092 while(itr.hasNext()) {
093 Entry entry = (Entry) itr.next();
094 ret.add(new MetaExpandoProperty(entry));
095 }
096
097 return ret;
098 }
099
100 public Object getProperty(String property) {
101 try {
102 return super.getProperty(property);
103 }
104 catch (GroovyRuntimeException e) {
105 return getExpandoProperties().get(property);
106 }
107 }
108
109 public void setProperty(String property, Object newValue) {
110 try {
111 super.setProperty(property, newValue);
112 }
113 catch (GroovyRuntimeException e) {
114 getExpandoProperties().put(property, newValue);
115 }
116 }
117
118 public Object invokeMethod(String name, Object args) {
119 try {
120 return super.invokeMethod(name, args);
121 }
122 catch (GroovyRuntimeException e) {
123 // br should get a "native" property match first. getProperty includes such fall-back logic
124 Object value = this.getProperty(name);
125 if (value instanceof Closure) {
126 Closure closure = (Closure) value;
127 closure.setDelegate(this);
128 return closure.call(args);
129 }
130 else {
131 throw e;
132 }
133 }
134
135 }
136
137 public String toString() {
138 return expandoProperties.toString();
139 }
140
141 /**
142 * Factory method to create a new Map used to store the expando properties map
143 * @return a newly created Map implementation
144 */
145 protected Map createMap() {
146 return new HashMap();
147 }
148
149 }