001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.core;
020
021import java.util.HashSet;
022import java.util.Set;
023
024/**
025 * SubConfiguration objects lets childs objects access attributes
026 * in its parent object with a given prefix, for example "child.".
027 * Child objects can access properties with their prefix removed.
028 *
029 * @author <a href="mailto:alcarraz@iie.edu.uy">Andr?s Alcarraz</a>
030 * @version $Id$
031 */
032public class SubConfiguration implements Configuration {
033    /** Parent Configuration */
034    protected Configuration cfg;
035    /** prefix identifying the child parameters */
036    protected String prefix;
037    /** Creates a new empty SubConfiguration object */
038    public SubConfiguration() {
039        super();
040    }
041    /**
042     * Creates a new SubConfiguration from its parent's Configuration 
043     * and the a given <code>prefix</code>.
044     * @param cfg Parent's Configuration 
045     * @param prefix prefix identifying child parameters.
046     */
047    public SubConfiguration(Configuration cfg, String prefix) {
048        super ();
049        this.cfg = cfg;
050        this.prefix = prefix;
051    }
052    /**
053     * Sets the container configuration.
054     * @param newCfg New container configuration.
055     */
056    public void setConfiguration(Configuration newCfg){
057        cfg=newCfg;
058    }
059    /**
060     * Sets the prefix that identifies the parameters of the child object
061     * inside the parent configuration.
062     * @param newPrefix New prefix
063     */
064    public void setPrefix(String newPrefix){
065        prefix = newPrefix;
066    }
067    public String get(String propertyName){
068        return cfg.get(prefix + propertyName);
069    }
070    public String[] getAll(String propertyName){
071        return cfg.getAll(prefix + propertyName);
072    }
073    public int[] getInts (String propertyName) {
074        return cfg.getInts (prefix + propertyName);
075    }
076    public long[] getLongs (String propertyName) {
077        return cfg.getLongs (prefix + propertyName);
078    }
079    public double[] getDoubles (String propertyName) {
080        return cfg.getDoubles (prefix + propertyName);
081    }
082    public boolean[] getBooleans (String propertyName) {
083        return cfg.getBooleans(prefix + propertyName);
084    }
085    public String get(String propertyName, String defaultValue){
086        return cfg.get(prefix + propertyName, defaultValue);
087    }
088    public boolean getBoolean(String propertyName){
089        return cfg.getBoolean(prefix + propertyName);
090    }
091    public boolean getBoolean(String propertyName, boolean defaultValue){
092        return cfg.getBoolean(prefix + propertyName, defaultValue);
093    }
094    public double getDouble(String propertyName){
095        return cfg.getDouble(prefix + propertyName);
096    }
097    public double getDouble(String propertyName, double defaultValue){
098        return cfg.getDouble(prefix + propertyName, defaultValue);
099    }
100    public long getLong(String propertyName){
101        return cfg.getLong(prefix + propertyName);
102    }
103    public long getLong(String propertyName, long defaultValue){
104        return cfg.getLong(prefix + propertyName, defaultValue);
105    }
106    public int getInt(String propertyName){
107        return cfg.getInt(prefix + propertyName);
108    }
109    public int getInt(String propertyName, int defaultValue){
110        return cfg.getInt(prefix + propertyName, defaultValue);
111    }
112    public void put (String name, Object value) {
113        cfg.put(prefix + name, value);
114    }
115    /**
116     * Creates a new object, it takes the class from the value of the property
117     * <code>propertyName</code>
118     * @param propertyName Property whose value is the class name of
119     * the object being created.
120     * @return the instantiated (and optionally configured) object
121     * @throws ConfigurationException if the class cannot be resolved or instantiated
122     */
123    public Object getObject (String propertyName) throws ConfigurationException{
124        try{
125            Object ret = 
126                Class.forName (get (propertyName)).newInstance();
127            if(ret instanceof Configurable) 
128                ((Configurable)ret).setConfiguration(this);
129            return ret;
130        } catch (Exception e){
131            throw new ConfigurationException ("Error trying to create an " 
132                                             + "object from property " 
133                                             + prefix + propertyName, e
134                                             );
135        }
136    }
137    @Override
138    public Set<String> keySet() {
139        Set<String> keys = new HashSet<String>();
140        for (String k : cfg.keySet())
141            if (k.startsWith(prefix))
142                keys.add(k);
143
144        return keys;
145    }
146}