View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.sap.prd.mobile.ios.mios;
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.LinkedHashMap;
25  import java.util.Map;
26  
27  final class Settings implements ISettings
28  {
29  
30    private final static String XCODE_OUTPUT_DIRECTORY = "build";
31  
32    enum ManagedSetting
33    {
34      CODE_SIGN_IDENTITY(false, null), CODE_SIGNING_REQUIRED(false, null), PROVISIONING_PROFILE(false, null),
35            DSTROOT(true, XCODE_OUTPUT_DIRECTORY), SYMROOT(false, XCODE_OUTPUT_DIRECTORY), SHARED_PRECOMPS_DIR(true,
36            XCODE_OUTPUT_DIRECTORY), OBJROOT(true, XCODE_OUTPUT_DIRECTORY);
37  
38      private final boolean required;
39      private String defaultValue;
40  
41      static ManagedSetting forName(String name)
42      {
43        for (ManagedSetting setting : values()) {
44          if (setting.name().equals(name)) {
45            return setting;
46          }
47        }
48  
49        return null;
50      }
51  
52      ManagedSetting(boolean required, String defaultValue)
53      {
54        this.required = required;
55        this.defaultValue = defaultValue;
56      }
57  
58      boolean isRequired()
59      {
60        return required;
61      }
62  
63      String getDefaultValue()
64      {
65        return defaultValue;
66      }
67    };
68  
69    private final static Map<String, String> REQUIRED = new LinkedHashMap<String, String>(7);
70  
71    static {
72      // Output directories should be specified (recommended by Apple - http://developer.apple.com/devcenter/download.action?path=/wwdc_2012/wwdc_2012_session_pdfs/session_404__building_from_the_command_line_with_xcode.pdf)
73      for (ManagedSetting setting : ManagedSetting.values()) {
74        if (setting.isRequired()) {
75          REQUIRED.put(setting.name(), setting.getDefaultValue());
76        }
77      }
78    }
79  
80    private final Map<String, String> userSettings, managedSettings;
81  
82    Settings(Map<String, String> userSettings, Map<String, String> managedSettings)
83    {
84  
85      if (userSettings == null) {
86        this.userSettings = Collections.emptyMap();
87      }
88      else {
89        this.userSettings = Collections.unmodifiableMap(new HashMap<String, String>(userSettings));
90      }
91  
92      validateUserSettings(this.userSettings);
93  
94      if (managedSettings == null) {
95        this.managedSettings = Collections.unmodifiableMap(new HashMap<String, String>(REQUIRED));
96      }
97      else {
98  
99        Map<String, String> _managedSettings = new HashMap<String, String>();
100 
101       for (Map.Entry<String, String> e : managedSettings.entrySet()) {
102 
103         if (e.getKey() == null || e.getKey().trim().isEmpty())
104           throw new IllegalArgumentException("Empty key found in settings. Value was: '" + e.getValue() + "'.");
105 
106         if (ManagedSetting.forName(e.getKey().trim()) == null)
107           throw new IllegalArgumentException("Setting with key '" + e.getKey() + "' and value '" + e.getValue()
108                 + "' was provided. This setting is managed by the plugin" +
109                 "and must not be provided as managed setting.");
110 
111         if (e.getValue() == null) {
112 
113           if (e.getKey().equals(ManagedSetting.CODE_SIGN_IDENTITY.name())) {
114             throw new IllegalArgumentException("CodesignIdentity was empty: '" + e.getValue()
115                   + "'. If you want to use the code"
116                   + " sign identity defined in the xCode project configuration just do"
117                   + " not provide the 'codeSignIdentity' in your Maven settings.");
118           }
119 
120           throw new IllegalArgumentException("No value provided for key '" + e.getKey() + "'.");
121         }
122 
123         _managedSettings.put(e.getKey(), e.getValue());
124       }
125       _managedSettings.putAll(REQUIRED);
126       this.managedSettings = Collections.unmodifiableMap(new HashMap<String, String>(_managedSettings));
127     }
128   }
129 
130   public final Map<String, String> getUserSettings() {
131     return Collections.unmodifiableMap(this.userSettings);
132   }
133   
134   public final Map<String, String> getManagedSettings() {
135     return Collections.unmodifiableMap(this.managedSettings);
136   }
137   
138   /* (non-Javadoc)
139    * @see com.sap.prd.mobile.ios.mios.ISettings#getAllSettings()
140    */
141   @Override
142   public final Map<String, String> getAllSettings()
143   {
144     Map<String, String> result = new HashMap<String, String>(this.userSettings.size() + this.managedSettings.size());
145     result.putAll(this.userSettings);
146     result.putAll(this.managedSettings);
147     return Collections.unmodifiableMap(result);
148   }
149 
150   /**
151    * @param userSettings
152    *          to be validated.
153    * @return the passed in userSettings if validation passed without exception
154    * @throws IllegalArgumentException
155    *           if the userSettings contain a key of an XCode setting that is managed by the plugin.
156    */
157   private final static Map<String, String> validateUserSettings(Map<String, String> userSettings)
158   {
159 
160     for (String key : userSettings.keySet()) {
161       if (ManagedSetting.forName(key.trim()) != null) {
162         throw new IllegalArgumentException(
163               "Setting '"
164                     + key
165                     + "' contained in user settings. This settings is managed by the plugin and must not be provided from outside.");
166       }
167     }
168     return userSettings;
169   }
170 
171   @Override
172   public String toString()
173   {
174     final String ls = System.getProperty("line.separator");
175     StringBuffer buffer = new StringBuffer();
176     for (Map.Entry<String, String> entry : getAllSettings().entrySet()) {
177       buffer.append(" ").append(entry.getKey()).append("=").append(entry.getValue()).append(ls);
178     }
179     return buffer.toString();
180   }
181 
182   @Override
183   public int hashCode()
184   {
185     final int prime = 31;
186     int result = 1;
187     result = prime * result + ((managedSettings == null) ? 0 : managedSettings.hashCode());
188     result = prime * result + ((userSettings == null) ? 0 : userSettings.hashCode());
189     return result;
190   }
191 
192   @Override
193   public boolean equals(Object obj)
194   {
195     if (this == obj) return true;
196     if (obj == null) return false;
197     if (getClass() != obj.getClass()) return false;
198     Settings other = (Settings) obj;
199     if (managedSettings == null) {
200       if (other.managedSettings != null) return false;
201     }
202     else if (!managedSettings.equals(other.managedSettings)) return false;
203     if (userSettings == null) {
204       if (other.userSettings != null) return false;
205     }
206     else if (!userSettings.equals(other.userSettings)) return false;
207     return true;
208   }
209 }