001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.configuration;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    import org.granite.config.GraniteConfig;
029    import org.granite.config.flex.ServicesConfig;
030    
031    /**
032     * @author Franck WOLFF
033     */
034    public class SimpleConfiguration implements Configuration {
035    
036            private String graniteStdConfigPath = null;
037            private String graniteConfigPath = null;
038            private List<Configurator> configurators = new ArrayList<Configurator>(); 
039            
040            private GraniteConfig graniteConfig = null;
041            private ServicesConfig servicesConfig = null;
042    
043            
044            public SimpleConfiguration() {          
045            }
046            
047            public SimpleConfiguration(String graniteStdConfigPath, String graniteConfigPath) {
048                    this.graniteStdConfigPath = graniteStdConfigPath;
049                    this.graniteConfigPath = graniteConfigPath;
050            }
051            
052            public void setGraniteStdConfigPath(String graniteConfigPath) {
053                    this.graniteStdConfigPath = graniteConfigPath;
054            }
055            
056            public void setGraniteConfigPath(String graniteConfigPath) {
057                    this.graniteConfigPath = graniteConfigPath;
058            }
059            
060            public void addConfigurator(Configurator configurator) {
061                    this.configurators.add(configurator);
062            }
063            
064            public void load() {
065                    InputStream is = null;
066                    try {
067                            is = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/granite/client/configuration/granite-config.xml");
068                            if (graniteConfigPath != null)
069                                    is = Thread.currentThread().getContextClassLoader().getResourceAsStream(graniteConfigPath);
070                            graniteConfig = new GraniteConfig(graniteStdConfigPath, is, null, null);
071                            for (Configurator configurator : configurators)
072                                    configurator.configure(graniteConfig);
073                            servicesConfig = new ServicesConfig(null, null, false);
074                    }
075                    catch (Exception e) {
076                            graniteConfig = null;
077                            servicesConfig = null;
078                            throw new RuntimeException("Cannot load configuration", e);
079                    }
080                    finally {
081                            if (is != null) try {
082                                    is.close();
083                            }
084                            catch (IOException e) {
085                            }
086                    }
087            }
088            
089            public GraniteConfig getGraniteConfig() {
090                    return graniteConfig;
091            }
092            
093            public ServicesConfig getServicesConfig() {
094                    return servicesConfig;
095            }
096    
097    }