001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.core;
016:
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.Map;
020: import org.apache.commons.lang.ObjectUtils;
021: import org.apache.commons.lang.StringUtils;
022: import org.araneaframework.Environment;
023:
024: /**
025: * A simple {@link org.araneaframework.Environment} implementation.
026: *
027: * @author "Toomas Römer" <toomas@webmedia.ee>
028: */
029: public class StandardEnvironment extends BaseEnvironment {
030: private Map entries;
031: private Environment parentEnv;
032:
033: /**
034: * Constructs an object with the env parent Environment and with the entries data.
035: * @param env the parent environment
036: * @param entries a map of the entries in the Environment
037: */
038: public StandardEnvironment(Environment env, Map entries) {
039: Assert.notNullParam(entries, "entries");
040:
041: this .entries = entries;
042: parentEnv = env;
043: }
044:
045: /**
046: * Constructs an object with the env parent Environment and entries data containing <key, value>.
047: * @param env the parent environment
048: * @param key a key of the value in the map of the Environment entries.
049: * @param value a value corresponding to given key in the map of the Environment entries.
050: */
051: public StandardEnvironment(Environment env, Object key, Object value) {
052: Assert.notNullParam(key, "key");
053:
054: entries = new HashMap(1);
055: entries.put(key, value);
056: parentEnv = env;
057: }
058:
059: /**
060: * Returns the map with the entries in this Environment. An entry is a key value pair.
061: * @return a map with the entries.
062: */
063: public Map getEntryMap() {
064: return entries;
065: }
066:
067: /**
068: * Returns the corresponding value of this Envrionment's entries. If none is
069: * found from the entries then the entry is returned from the parent environment.
070: * If a value to the key does not exist, AraneaNoSuchEnvironmentEntryException is thrown.
071: * @param key the key of the entry
072: * @return the Object under the key provided
073: * @throws AraneaNoSuchEnvironmentEntryException
074: */
075: public Object getEntry(Object key) {
076: if (entries.containsKey(key)) {
077: return entries.get(key);
078: }
079:
080: if (parentEnv == null) {
081: return null;
082: }
083:
084: return parentEnv.getEntry(key);
085: }
086:
087: public String toString() {
088: return toString(0);
089: }
090:
091: private static final String space = " ";
092: private static final String lf = "\n";
093:
094: private String toString(int pad) {
095: String padding = StringUtils.leftPad("", pad, space);
096: StringBuffer result = new StringBuffer();
097:
098: if (entries != null) {
099: for (Iterator i = entries.entrySet().iterator(); i
100: .hasNext();) {
101: Map.Entry e = (Map.Entry) i.next();
102: result.append(padding + e.getKey() + "="
103: + ObjectUtils.identityToString(e.getValue())
104: + lf);
105: }
106: }
107:
108: if (parentEnv instanceof StandardEnvironment) {
109: result.append(((StandardEnvironment) parentEnv)
110: .toString(pad + (space.length() * 2)));
111: }
112:
113: result.append("\n");
114: return result.toString();
115: }
116: }
|