001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.el;
030:
031: import javax.el.ELContext;
032: import javax.el.ELResolver;
033:
034: import java.util.*;
035:
036: /**
037: * Variable resolver using the system properties (@link System#getProperty()}
038: * and the environment {@link System@getenv()}.
039: *
040: */
041: public class SystemPropertiesResolver extends AbstractVariableResolver {
042: /**
043: * Creates the resolver
044: */
045: public SystemPropertiesResolver() {
046: }
047:
048: /**
049: * Creates the resolver
050: */
051: public SystemPropertiesResolver(ELResolver next) {
052: super (next);
053: }
054:
055: /**
056: * Returns the named variable value.
057: */
058: @Override
059: public Object getValue(ELContext env, Object base, Object property) {
060: String var;
061:
062: if (base == null && property instanceof String)
063: var = (String) property;
064: else if (base == this && property instanceof String)
065: var = (String) property;
066: else
067: return null;
068:
069: Object value = System.getProperty(var);
070:
071: if (value != null) {
072: env.setPropertyResolved(true);
073: return value;
074: }
075:
076: value = System.getenv(var);
077:
078: if (value != null) {
079: env.setPropertyResolved(true);
080: return value;
081: }
082:
083: if ("Var".equals(var)) {
084: // server/10m5
085: env.setPropertyResolved(true);
086: return new PropertyMap(env);
087: }
088:
089: return null;
090: }
091:
092: /**
093: * Returns the system property resolver.
094: */
095: public String toString() {
096: return "SystemPropertiesResolver[]";
097: }
098:
099: static class PropertyMap extends AbstractMap {
100: private ELContext _env;
101:
102: PropertyMap(ELContext env) {
103: _env = env;
104: }
105:
106: public Object get(Object key) {
107: return _env.getELResolver().getValue(_env, null, key);
108: }
109:
110: public Set entrySet() {
111: throw new UnsupportedOperationException();
112: }
113: }
114: }
|