001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (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 General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany;
019
020import java.io.InputStream;
021import java.text.DateFormat;
022import java.text.SimpleDateFormat;
023import java.util.Date;
024import java.util.Locale;
025import java.util.Properties;
026
027import org.syncany.config.UserConfig;
028import org.syncany.operations.Operation;
029import org.syncany.operations.OperationOptions;
030import org.syncany.operations.OperationResult;
031
032/**
033 * The client class is a convenience class to call the application's {@link Operation}s
034 * using a central entry point. The class offers wrapper methods around the operations.
035 * 
036 * <p>The methods typically take an {@link OperationOptions} instance as an argument, 
037 * and return an instance of the {@link OperationResult} class.
038 *  
039 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
040 */
041public class Client {
042        protected static final String APPLICATION_PROPERTIES_RESOURCE = "/application.properties"; // TODO [low] Move this!
043        protected static final String APPLICATION_PROPERTIES_TEST_RESOURCE = "/org/syncany/application.test.properties";
044        protected static final String APPLICATION_PROPERTIES_RELEASE_KEY = "applicationRelease";
045        protected static final String APPLICATION_PROPERTIES_VERSION_KEY = "applicationVersion";
046        protected static final String APPLICATION_PROPERTIES_VERSION_FULL_KEY = "applicationVersionFull";
047        protected static final String APPLICATION_PROPERTIES_REVISION_KEY = "applicationRevision";
048        protected static final String APPLICATION_PROPERTIES_DATE_KEY = "applicationDate";
049        protected static final String APPLICATION_PROPERTIES_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
050        
051        protected static Properties applicationProperties;
052        
053        static {
054                initUserConfig();
055                initApplicationProperties();
056                initTestApplicationProperties();
057        }
058        
059        public static Properties getApplicationProperties() {
060                return applicationProperties;
061        }       
062
063        public static boolean isApplicationRelease() {
064                return Boolean.parseBoolean(applicationProperties.getProperty(APPLICATION_PROPERTIES_RELEASE_KEY));
065        }
066
067        public static String getApplicationVersion() {
068                return applicationProperties.getProperty(APPLICATION_PROPERTIES_VERSION_KEY);
069        }
070
071        public static String getApplicationVersionFull() {
072                return applicationProperties.getProperty(APPLICATION_PROPERTIES_VERSION_FULL_KEY);
073        }
074
075        public static String getApplicationRevision() {
076                return applicationProperties.getProperty(APPLICATION_PROPERTIES_REVISION_KEY);
077        }
078        
079        public static Date getApplicationDate() {
080                try {
081                        DateFormat dateFormat = new SimpleDateFormat(APPLICATION_PROPERTIES_DATE_FORMAT, Locale.ENGLISH);
082                    Date applicationDate = dateFormat.parse(applicationProperties.getProperty(APPLICATION_PROPERTIES_DATE_KEY)); 
083                    
084                        return applicationDate;
085                }
086                catch (Exception e) {
087                        return null;
088                }
089        }
090        
091        private static void initUserConfig() {
092                UserConfig.init();
093        }
094
095        private static void initApplicationProperties() {
096                InputStream globalPropertiesInputStream = Client.class.getResourceAsStream(APPLICATION_PROPERTIES_RESOURCE);
097
098                try {
099                        applicationProperties = new Properties();
100                        applicationProperties.load(globalPropertiesInputStream);
101                }  
102                catch (Exception e) {
103                        throw new RuntimeException("Cannot load application properties.", e);
104                }
105        }
106
107        private static void initTestApplicationProperties() {
108                InputStream testApplicationProperties = Client.class.getResourceAsStream(APPLICATION_PROPERTIES_TEST_RESOURCE);
109                boolean isTestEnvironment = testApplicationProperties != null;
110                
111                if (isTestEnvironment) {
112                        try {
113                                applicationProperties.clear();
114                                applicationProperties.load(testApplicationProperties);
115                        }
116                        catch (Exception e) {
117                                throw new RuntimeException("Cannot load TEST-ONLY application properties.", e);
118                        }
119                }
120        }
121}