001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 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    package org.granite.client.configuration;
021    
022    import java.io.IOException;
023    import java.io.InputStream;
024    
025    import org.granite.client.messaging.codec.MessagingCodec.ClientType;
026    import org.granite.config.GraniteConfig;
027    import org.granite.config.flex.ServicesConfig;
028    
029    /**
030     * @author Franck WOLFF
031     */
032    public class SimpleConfiguration implements Configuration {
033            
034            private String graniteStdConfigPath = SimpleConfiguration.class.getPackage().getName().replace('.', '/') + "/granite-config.xml";
035            private String graniteConfigPath = null;
036            
037            private GraniteConfig graniteConfig = null;
038            private ServicesConfig servicesConfig = null;
039    
040            private ClientType clientType = ClientType.AS3;
041            
042            public SimpleConfiguration() {          
043            }
044            
045            public SimpleConfiguration(String graniteStdConfigPath, String graniteConfigPath) {
046                    this.graniteStdConfigPath = graniteStdConfigPath;
047                    this.graniteConfigPath = graniteConfigPath;
048            }
049            
050            public ClientType getClientType() {
051                    return clientType;
052            }
053    
054            public void setClientType(ClientType clientType) {
055                    this.clientType = clientType;
056            }
057            
058            public void setGraniteStdConfigPath(String graniteConfigPath) {
059                    this.graniteStdConfigPath = graniteConfigPath;
060            }
061            
062            public void setGraniteConfigPath(String graniteConfigPath) {
063                    this.graniteConfigPath = graniteConfigPath;
064            }
065            
066            public void load() {
067                    InputStream is = null;
068                    try {
069                            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(graniteStdConfigPath);
070                            if (graniteConfigPath != null)
071                                    is = Thread.currentThread().getContextClassLoader().getResourceAsStream(graniteConfigPath);
072                            graniteConfig = new GraniteConfig(graniteStdConfigPath, is, null, null);
073                            postLoad(graniteConfig);
074                            servicesConfig = new ServicesConfig(null, null, false);
075                    }
076                    catch (Exception e) {
077                            graniteConfig = null;
078                            servicesConfig = null;
079                            throw new RuntimeException("Cannot load configuration", e);
080                    }
081                    finally {
082                            if (is != null) try {
083                                    is.close();
084                            }
085                            catch (IOException e) {
086                            }
087                    }
088            }
089            
090            protected void postLoad(GraniteConfig graniteConfig) {          
091            }
092            
093            public GraniteConfig getGraniteConfig() {
094                    return graniteConfig;
095            }
096            
097            public ServicesConfig getServicesConfig() {
098                    return servicesConfig;
099            }
100    
101    }