001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.xbean.blueprint.cm;
022    
023    import java.util.Collection;
024    import java.util.Map;
025    import java.util.Set;
026    
027    import org.apache.aries.blueprint.compendium.cm.CmPropertyPlaceholder;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    /**
032     * @version $Rev: 907189 $ $Date: 2010-02-06 03:01:43 -0500 (Sat, 06 Feb 2010) $
033     */
034    public class JexlPropertyPlaceholder extends CmPropertyPlaceholder {
035    
036        private static final Logger LOGGER = LoggerFactory.getLogger(JexlPropertyPlaceholder.class);
037    
038        private transient JexlExpressionParser parser;
039    
040        @Override
041        protected String processString(String str) {
042            LOGGER.debug("Processing {} from configuration with pid {}", str, getPersistentId());
043            JexlExpressionParser parser = getParser();
044            try {
045                return parser.evaluate(str).toString();
046            } catch (Exception e) {
047                LOGGER.info("Could not evaluate expressions {}  for {}", str, getPersistentId());
048                LOGGER.info("Exception:", e);
049            }
050            return str;
051        }
052    
053        protected synchronized JexlExpressionParser getParser() {
054            if (parser == null) {
055    //            try {
056                    parser = new JexlExpressionParser(toMap());
057    //            } catch (IOException e) {
058                    // ignore
059    //            }
060            }
061            return parser;
062        }
063    
064        private Map<String, Object> toMap() {
065            return new ConfigMap();
066    //        Map<String, Object> map = new HashMap<String, Object>();
067    //        if (config != null) {
068    //            Dictionary<String, Object> properties = config.getProperties();
069    //            for (Enumeration<String> e = properties.keys(); e.hasMoreElements(); ) {
070    //                String key = e.nextElement();
071    //                Object value = properties.get(key);
072    //                map.put(key, value);
073    //            }
074    //        }
075    //        return map;
076        }
077    
078        private class ConfigMap implements Map<String, Object> {
079    
080            @Override
081            public int size() {
082                return 0;
083            }
084    
085            @Override
086            public boolean isEmpty() {
087                return false;
088            }
089    
090            @Override
091            public boolean containsKey(Object o) {
092                return getProperty((String) o) != null;
093            }
094    
095            @Override
096            public boolean containsValue(Object o) {
097                return false;
098            }
099    
100            @Override
101            public Object get(Object o) {
102                return getProperty((String) o);
103            }
104    
105            @Override
106            public Object put(String s, Object o) {
107                return null;
108            }
109    
110            @Override
111            public Object remove(Object o) {
112                return null;
113            }
114    
115            @Override
116            public void putAll(Map<? extends String, ? extends Object> map) {
117            }
118    
119            @Override
120            public void clear() {
121            }
122    
123            @Override
124            public Set<String> keySet() {
125                return null;
126            }
127    
128            @Override
129            public Collection<Object> values() {
130                return null;
131            }
132    
133            @Override
134            public Set<Entry<String, Object>> entrySet() {
135                return null;
136            }
137        }
138    }