summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting.core/src/com/motorolamobility/preflighting/core/validation/ValidationManagerConfiguration.java
blob: 76d519f554695b5d9af19eec48ef08fdc0446082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorolamobility.preflighting.core.validation;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;

/**
 * Singleton that encapsulates the data in a file that configures App Validator execution.
 */
public class ValidationManagerConfiguration
{

    private final static String APP_VALIDATOR_CONFIG_FILE_NAME = "appvalidator.cfg"; //$NON-NLS-1$

    private static ValidationManagerConfiguration instance;

    /**
     * @return singleton instance of {@link ValidationManagerConfiguration}
     */
    public static ValidationManagerConfiguration getInstance()
    {
        if (instance == null)
        {
            instance = new ValidationManagerConfiguration();
        }
        return instance;
    }

    /**
     * Properties that are available into {@link ValidationManagerConfiguration#APP_VALIDATOR_CONFIG_FILE_NAME}. 
     */
    public static enum ConfigProperties
    {
        BASE_URL_PROPERTY("base_url"), //$NON-NLS-1$

        URL_QUERY_PROPERTY("info_url_query"), //$NON-NLS-1$

        APK_EXTRACTION_MODE("apk_extraction_mode"); //$NON-NLS-1$

        private String name;

        private ConfigProperties(String name)
        {
            this.name = name;
        }

        /**
         * @return string with the name of the configuration property
         */
        public String getName()
        {
            return name;
        }
    }

    /**
     * Property that exposes the modes to analyze APK.
     */
    public static enum ExtractionModes
    {
        APKTOOL_MODE("apktool"), //$NON-NLS-1$

        AAPT_MODE("aapt"); //$NON-NLS-1$

        private String mode;

        private ExtractionModes(String mode)
        {
            this.mode = mode;
        }

        /**
         * @return mode the chosen tool (mode to analyze APK)  
         */
        public String getMode()
        {
            return mode;
        }

        /**
         * Checks if a mode is not contained in the list of {@link ExtractionModes} available
         * @param mode the string containing the mode (tool to analyze APK)
         * @return <code>true</code> if found, <code>false</code> otherwise
         */
        public static boolean contains(String mode)
        {
            boolean contains = false;
            if (mode != null)
            {
                for (ExtractionModes input : ExtractionModes.values())
                {
                    if (input.getMode().equals(mode))
                    {
                        contains = true;
                        break;
                    }
                }
            }

            return contains;
        }
    }

    private final static String DEFAULT_URL = "http://developer.motorola.com/"; //$NON-NLS-1$

    private final Properties p = new Properties();

    /**
     * Initializes map with app validator startup configuration
     */
    private ValidationManagerConfiguration()
    {
        String path = Platform.getInstallLocation().getURL().getPath();
        if (!path.endsWith(File.separator))
        {
            path += File.separator;
        }
        path += APP_VALIDATOR_CONFIG_FILE_NAME;

        File f = new File(path);
        FileInputStream fis = null;
        try
        {
            if (f.exists() && f.isFile())
            {
                fis = new FileInputStream(f);
                p.load(fis);
            }
            if (getProperty(ConfigProperties.BASE_URL_PROPERTY.getName()) == null)
            {
                p.put(ConfigProperties.BASE_URL_PROPERTY.getName(), DEFAULT_URL);
            }
            if (getProperty(ConfigProperties.URL_QUERY_PROPERTY.getName()) == null)
            {
                p.put(ConfigProperties.URL_QUERY_PROPERTY.getName(), "");
            }
            if (!ExtractionModes.contains(getProperty(ConfigProperties.APK_EXTRACTION_MODE
                    .getName())))
            {
                p.put(ConfigProperties.APK_EXTRACTION_MODE.getName(),
                        ExtractionModes.APKTOOL_MODE.getMode());
            }
        }
        catch (IOException e)
        {
            //populate with default values
            p.put(ConfigProperties.BASE_URL_PROPERTY.getName(), DEFAULT_URL);
            p.put(ConfigProperties.URL_QUERY_PROPERTY.getName(), "");
            p.put(ConfigProperties.APK_EXTRACTION_MODE.getName(),
                    ExtractionModes.APKTOOL_MODE.getMode());
        }
        finally
        {
            if (fis != null)
            {
                try
                {
                    fis.close();
                }
                catch (IOException e)
                {
                    // do nothing
                }
            }
        }
    }

    /**
     * Get the selected property.
     * @param property string with one of the items available in {@link ConfigProperties}
     * @return the selected property or null if its not found.
     */
    public String getProperty(String property)
    {
        return p.getProperty(property);
    }
}