File Source: IdentityManager.java
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.config;
24
25 import com.dmdirc.Main;
26 import com.dmdirc.Precondition;
27 import com.dmdirc.logger.ErrorLevel;
28 import com.dmdirc.logger.Logger;
29 import com.dmdirc.updater.Version;
30 import com.dmdirc.util.ConfigFile;
31 import com.dmdirc.util.InvalidConfigFileException;
32 import com.dmdirc.util.WeakList;
33 import com.dmdirc.util.resourcemanager.ResourceManager;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44 * The identity manager manages all known identities, providing easy methods
45 * to access them.
46 *
47 * @author chris
48 */
49 public final class IdentityManager {
50
51 /** The identities that have been loaded into this manager. */
/*
P/P * Method: com.dmdirc.config.IdentityManager__static_init
*
* Postconditions:
* identities == &new ArrayList(IdentityManager__static_init#1)
* managers == &new WeakList(IdentityManager__static_init#2)
* new ArrayList(IdentityManager__static_init#1) num objects == 1
* new WeakList(IdentityManager__static_init#2) num objects == 1
*/
52 private final static List<Identity> identities = new ArrayList<Identity>();
53
54 /** The config managers that have registered with this manager. */
55 private final static List<ConfigManager> managers = new WeakList<ConfigManager>();
56
57 /** The identity file used for the global config. */
58 private static Identity config;
59
60 /** The identity file used for addon defaults. */
61 private static Identity addonConfig;
62
63 /** The config manager used for global settings. */
64 private static ConfigManager globalconfig;
65
66 /** Creates a new instance of IdentityManager. */
/*
P/P * Method: void com.dmdirc.config.IdentityManager()
*/
67 private IdentityManager() {
68 }
69
70 /** Loads all identity files. */
71 public static void load() {
/*
P/P * Method: void load()
*
* Preconditions:
* init'ed(globalconfig)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.FATAL)
*
* Postconditions:
* addonConfig == &new Identity(load#4)
* globalconfig == One-of{old globalconfig, &new ConfigManager(getGlobalConfig#1)}
* globalconfig != null
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects == 0
* new ConfigManager(setOption#2) num objects == 0
* new MapList(ConfigManager#1) num objects == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects == new ArrayList(getSources#1) num objects
* new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
* ...
*
* Test Vectors:
* globalconfig: Addr_Set{null}, Inverse{null}
* java.util.List:size(...)@85: {-231..-1, 1..232-1}, {0}
*/
72 identities.clear();
73 managers.clear();
74
75 if (globalconfig != null) {
76 // May have been created earlier
77 managers.add(globalconfig);
78 }
79
80 loadVersion();
81 loadDefaults();
82 loadUser();
83 loadConfig();
84
85 if (getProfiles().size() == 0) {
86 try {
87 Identity.buildProfile("Default Profile");
88 } catch (IOException ex) {
89 Logger.userError(ErrorLevel.FATAL, "Unable to write default profile", ex);
90 }
91 }
92
93 // Set up the identity used for the addons defaults
94 final ConfigTarget target = new ConfigTarget();
95 target.setGlobalDefault();
96 target.setOrder(500000);
97
98 final ConfigFile addonConfigFile = new ConfigFile((File) null);
99 final Map<String, String> addonSettings = new HashMap<String, String>();
100 addonSettings.put("name", "Addon defaults");
101 addonConfigFile.addDomain("identity", addonSettings);
102
103 addonConfig = new Identity(addonConfigFile, target);
104 IdentityManager.addIdentity(addonConfig);
105
106 if (!getGlobalConfig().hasOptionString("identity", "defaultsversion")) {
107 Logger.userError(ErrorLevel.FATAL, "Default settings "
108 + "could not be loaded");
109 }
110 }
111
112 /** Loads the default (built in) identities. */
113 private static void loadDefaults() {
/*
P/P * Method: void loadDefaults()
*
* Preconditions:
* init'ed(globalconfig)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* getGlobalConfig(...).sources != null
* java.io.File:listFiles(...)@119 != null
*
* Postconditions:
* globalconfig != null
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects == new ArrayList(getSources#1) num objects
* new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
* new ArrayList(getSources#1) num objects == 0
* new ConfigManager(getGlobalConfig#1) num objects == 0
* new ConfigManager(getGlobalConfig#1).channel == &java.lang.StringBuilder:toString(...)
* init'ed(new ConfigManager(getGlobalConfig#1).channel)
* possibly_updated(new ConfigManager(getGlobalConfig#1).channel)
* ...
*
* Test Vectors:
* com.dmdirc.updater.Version:compareTo(...)@136: {-231..0}, {1..232-1}
* java.io.File:exists(...)@119: {0}, {1}
* java.io.File:listFiles(...).length@119: {1..+Inf}, {0}
* java.io.File:listFiles(...)@119: Addr_Set{null}, Inverse{null}
*/
114 final String[] targets = {"default", "modealiases"};
115 final String dir = getDirectory();
116
117 for (String target : targets) {
118 final File file = new File(dir + target);
119 if (!file.exists() || file.listFiles() == null || file.listFiles().length == 0) {
120 file.mkdirs();
121 extractIdentities(target);
122 }
123
124 loadUser(file);
125 }
126
127 // If the bundled defaults are newer than the ones the user is
128 // currently using, extract them.
129 if (getGlobalConfig().hasOptionString("identity", "defaultsversion")
130 && getGlobalConfig().hasOptionString("updater", "bundleddefaultsversion")) {
131 final Version installedVersion = new Version(getGlobalConfig()
132 .getOption("identity", "defaultsversion"));
133 final Version bundledVersion = new Version(getGlobalConfig()
134 .getOption("updater", "bundleddefaultsversion"));
135
136 if (bundledVersion.compareTo(installedVersion) > 0) {
137 extractIdentities("default");
138 loadUser(new File(dir, "default"));
139 }
140 }
141 }
142
143 /**
144 * Extracts the specific set of default identities to the user's identity
145 * folder.
146 *
147 * @param target The target to be extracted
148 */
149 private static void extractIdentities(final String target) {
150 try {
/*
P/P * Method: void extractIdentities(String)
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* com.dmdirc.util.resourcemanager.ResourceManager:getResourceManager(...)@151 != null
*/
151 ResourceManager.getResourceManager().extractResources(
152 "com/dmdirc/config/defaults/" + target,
153 getDirectory() + target, false);
154 } catch (IOException ex) {
155 Logger.userError(ErrorLevel.MEDIUM, "Unable to extract default "
156 + "identities: " + ex.getMessage());
157 }
158 }
159
160 /**
161 * Retrieves the directory used to store identities in.
162 *
163 * @return The identity directory path
164 */
165 public static String getDirectory() {
/*
P/P * Method: String getDirectory()
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &java.lang.StringBuilder:toString(...)
*/
166 return Main.getConfigDir() + "identities" + System.getProperty("file.separator");
167 }
168
169 /** Loads user-defined identity files. */
170 public static void loadUser() {
/*
P/P * Method: void loadUser()
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
*
* Test Vectors:
* java.io.File:exists(...)@173: {1}, {0}
*/
171 final File dir = new File(getDirectory());
172
173 if (!dir.exists()) {
174 try {
175 dir.mkdirs();
176 dir.createNewFile();
177 } catch (IOException ex) {
178 Logger.userError(ErrorLevel.MEDIUM, "Unable to create identity dir");
179 }
180 }
181
182 loadUser(dir);
183 }
184
185 /**
186 * Recursively loads files from the specified directory.
187 *
188 * @param dir The directory to be loaded
189 */
190 @Precondition({
191 "The specified File is not null",
192 "The specified File is a directory"
193 })
194 private static void loadUser(final File dir) {
/*
P/P * Method: void loadUser(File)
*
* Preconditions:
* dir != null
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* arr$.length@203 <= 232-1
* arr$[i$]@203 != null
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* java.io.File:listFiles(...)@203 != null
*
* Test Vectors:
* java.io.File:isDirectory(...)@204: {0}, {1}
* java.io.File:listFiles(...)@198: Inverse{null}, Addr_Set{null}
*/
195 Logger.assertTrue(dir != null);
196 Logger.assertTrue(dir.isDirectory());
197
198 if (dir.listFiles() == null) {
199 Logger.userError(ErrorLevel.MEDIUM,
200 "Unable to load user identity files from "
201 + dir.getAbsolutePath());
202 } else {
203 for (File file : dir.listFiles()) {
204 if (file.isDirectory()) {
205 loadUser(file);
206 } else {
207 loadIdentity(file);
208 }
209 }
210 }
211 }
212
213 /**
214 * Loads an identity from the specified file. If the identity already
215 * exists, it is told to reload instead.
216 *
217 * @param file The file to load the identity from.
218 */
219 @SuppressWarnings("deprecation")
220 private static void loadIdentity(final File file) {
/*
P/P * Method: void loadIdentity(File)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) file != null
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* identity.file@222 != null
* java.util.Iterator:next(...)@222 != null
*
* Test Vectors:
* java.io.File:equals(...)@223: {0}, {1}
* java.util.Iterator:hasNext(...)@222: {1}, {0}
*/
221 synchronized (identities) {
222 for (Identity identity : identities) {
223 if (file.equals(identity.getFile().getFile())) {
224 try {
225 identity.reload();
226 } catch (IOException ex) {
227 Logger.userError(ErrorLevel.MEDIUM,
228 "I/O error when reloading identity file: "
229 + file.getAbsolutePath() + " (" + ex.getMessage() + ")");
230 } catch (InvalidConfigFileException ex) {
231 // Do nothing
232 }
233
234 return;
235 }
236 }
237 }
238
239 try {
240 addIdentity(new Identity(file, false));
241 } catch (InvalidIdentityFileException ex) {
242 Logger.userError(ErrorLevel.MEDIUM,
243 "Invalid identity file: " + file.getAbsolutePath()
244 + " (" + ex.getMessage() + ")");
245 } catch (IOException ex) {
246 Logger.userError(ErrorLevel.MEDIUM,
247 "I/O error when reading identity file: "
248 + file.getAbsolutePath());
249 }
250 }
251
252 /** Loads the version information. */
253 public static void loadVersion() {
254 try {
/*
P/P * Method: void loadVersion()
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.FATAL)
*/
255 addIdentity(new Identity(Main.class.getResourceAsStream("version.config"), false));
256 } catch (IOException ex) {
257 Logger.appError(ErrorLevel.FATAL, "Unable to load version information", ex);
258 } catch (InvalidIdentityFileException ex) {
259 Logger.appError(ErrorLevel.FATAL, "Unable to load version information", ex);
260 }
261 }
262
263 /** Loads the config identity. */
264 private static void loadConfig() {
265 try {
/*
P/P * Method: void loadConfig()
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.HIGH)
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
*
* Postconditions:
* config == One-of{&new Identity(loadConfig#3), old config}
* init'ed(config.globalConfig)
* init'ed(config.needSave)
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigFile(Identity#2) num objects <= 1
* new ConfigManager(setOption#2) num objects == 0
* new ConfigManager(setOption#2) num objects <= 1
* init'ed(new ConfigManager(setOption#2).channel)
* ...
*
* Test Vectors:
* java.io.File:exists(...)@268: {1}, {0}
*/
266 final File file = new File(Main.getConfigDir() + "dmdirc.config");
267
268 if (!file.exists()) {
269 file.createNewFile();
270 }
271
272 config = new Identity(file, true);
273 config.setOption("identity", "name", "Global config");
274 addIdentity(config);
275 } catch (InvalidIdentityFileException ex) {
276 // This shouldn't happen as we're forcing it to global
277 Logger.appError(ErrorLevel.HIGH, "Unable to load global config", ex);
278 } catch (IOException ex) {
279 Logger.userError(ErrorLevel.MEDIUM, "I/O error when loading file: "
280 + ex.getMessage());
281 }
282 }
283
284 /**
285 * Retrieves the identity used for the global config.
286 *
287 * @return The global config identity
288 */
289 public static Identity getConfigIdentity() {
/*
P/P * Method: Identity getConfigIdentity()
*
* Preconditions:
* init'ed(config)
*
* Postconditions:
* return_value == config
* init'ed(return_value)
*/
290 return config;
291 }
292
293 /**
294 * Retrieves the identity used for addons defaults.
295 *
296 * @return The addons defaults identity
297 */
298 public static Identity getAddonIdentity() {
/*
P/P * Method: Identity getAddonIdentity()
*
* Preconditions:
* init'ed(addonConfig)
*
* Postconditions:
* return_value == addonConfig
* init'ed(return_value)
*/
299 return addonConfig;
300 }
301
302 /**
303 * Saves all modified identity files to disk.
304 */
305 public static void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* identity.file@307 != null
* identity.listeners@307 != null
* java.util.Iterator:next(...)@307 != null
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(new ArrayList(getSources#1) num objects)
* init'ed(new ConfigManager(save#3*) num objects)
* init'ed(new ConfigManager(save#3*).channel)
* init'ed(new ConfigManager(save#3*).ircd)
* init'ed(new ConfigManager(save#3*).listeners)
* init'ed(new ConfigManager(save#3*).network)
* init'ed(new ConfigManager(save#3*).server)
* init'ed(new ConfigManager(save#3*).sources)
* init'ed(new MapList(ConfigManager#1) num objects)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@307: {1}, {0}
*/
306 synchronized (identities) {
307 for (Identity identity : identities) {
308 identity.save();
309 }
310 }
311 }
312
313 /**
314 * Adds the specific identity to this manager.
315 * @param identity The identity to be added
316 */
317 @Precondition("The specified Identity is not null")
318 public static void addIdentity(final Identity identity) {
/*
P/P * Method: void addIdentity(Identity)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) identity != null
* (soft) identity.file != null
* (soft) identity.listeners != null
* (soft) identity.myTarget != null
* (soft) init'ed(identity.myTarget.data)
* (soft) identity.myTarget.type != null
*
* Presumptions:
* java.util.Iterator:next(...)@330 != null
* manager.listeners@330 != null
* manager.sources@330 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@330: {1}, {0}
* java.util.List:contains(...)@321: {0}, {1}
*/
319 Logger.assertTrue(identity != null);
320
321 if (identities.contains(identity)) {
322 removeIdentity(identity);
323 }
324
325 synchronized (identities) {
326 identities.add(identity);
327 }
328
329 synchronized (managers) {
330 for (ConfigManager manager : managers) {
331 manager.checkIdentity(identity);
332 }
333 }
334 }
335
336 /**
337 * Removes an identity from this manager.
338 * @param identity The identity to be removed
339 */
340 @Precondition({
341 "The specified Identity is not null",
342 "The specified Identity has previously been added and not removed"
343 })
344 public static void removeIdentity(final Identity identity) {
/*
P/P * Method: void removeIdentity(Identity)
*
* Preconditions:
* (soft) identity != null
* (soft) identity.file != null
* (soft) identity.listeners != null
*
* Presumptions:
* java.util.Iterator:next(...)@353 != null
* manager.listeners@353 != null
* manager.sources@353 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@353: {1}, {0}
*/
345 Logger.assertTrue(identity != null);
346 Logger.assertTrue(identities.contains(identity));
347
348 synchronized (identities) {
349 identities.remove(identity);
350 }
351
352 synchronized (managers) {
353 for (ConfigManager manager : managers) {
354 manager.removeIdentity(identity);
355 }
356 }
357 }
358
359 /**
360 * Adds a config manager to this manager.
361 * @param manager The ConfigManager to add
362 */
363 @Precondition("The specified ConfigManager is not null")
364 public static void addConfigManager(final ConfigManager manager) {
/*
P/P * Method: void addConfigManager(ConfigManager)
*/
365 Logger.assertTrue(manager != null);
366
367 synchronized (managers) {
368 managers.add(manager);
369 }
370 }
371
372 /**
373 * Retrieves a list of identities that serve as profiles.
374 * @return A list of profiles
375 */
376 public static List<Identity> getProfiles() {
/*
P/P * Method: List getProfiles()
*
* Presumptions:
* identity.file@380 != null
* java.util.Iterator:next(...)@380 != null
*
* Postconditions:
* return_value == &new ArrayList(getProfiles#1)
* new ArrayList(getProfiles#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@380: {1}, {0}
*/
377 final List<Identity> profiles = new ArrayList<Identity>();
378
379 synchronized (identities) {
380 for (Identity identity : identities) {
381 if (identity.isProfile()) {
382 profiles.add(identity);
383 }
384 }
385 }
386
387 return profiles;
388 }
389
390 /**
391 * Retrieves a list of all config sources that should be applied to the
392 * specified config manager.
393 *
394 * @param manager The manager requesting sources
395 * @return A list of all matching config sources
396 */
397 public static List<Identity> getSources(final ConfigManager manager) {
398
/*
P/P * Method: List getSources(ConfigManager)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) manager != null
* (soft) init'ed(manager.channel)
* (soft) init'ed(manager.ircd)
* (soft) init'ed(manager.network)
* (soft) init'ed(manager.server)
*
* Presumptions:
* identity.myTarget.type@402 != null
* identity.myTarget@402 != null
* java.util.Iterator:next(...)@402 != null
*
* Postconditions:
* return_value == &new ArrayList(getSources#1)
* new ArrayList(getSources#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@402: {1}, {0}
*/
399 final List<Identity> sources = new ArrayList<Identity>();
400
401 synchronized (identities) {
402 for (Identity identity : identities) {
403 if (manager.identityApplies(identity)) {
404 sources.add(identity);
405 }
406 }
407 }
408
409 Collections.sort(sources);
410
411 return sources;
412 }
413
414 /**
415 * Retrieves the global config manager.
416 *
417 * @return The global config manager
418 */
419 public static synchronized ConfigManager getGlobalConfig() {
/*
P/P * Method: ConfigManager getGlobalConfig()
*
* Preconditions:
* init'ed(globalconfig)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Postconditions:
* globalconfig == One-of{old globalconfig, &new ConfigManager(getGlobalConfig#1)}
* globalconfig != null
* return_value == globalconfig
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1).channel == &java.lang.StringBuilder:toString(...)
* new ConfigManager(getGlobalConfig#1).ircd == &""
* new ConfigManager(getGlobalConfig#1).listeners == &new MapList(ConfigManager#1)
* new ConfigManager(getGlobalConfig#1).network == &""
* ...
*
* Test Vectors:
* globalconfig: Inverse{null}, Addr_Set{null}
*/
420 if (globalconfig == null) {
421 globalconfig = new ConfigManager("", "", "");
422 }
423
424 return globalconfig;
425 }
426
427 /**
428 * Retrieves the config for the specified channel@network. The config is
429 * created if it doesn't exist.
430 *
431 * @param network The name of the network
432 * @param channel The name of the channel
433 * @return A config source for the channel
434 */
435 @Precondition({
436 "The specified network is non-null and not empty",
437 "The specified channel is non-null and not empty"
438 })
439 public static Identity getChannelConfig(final String network, final String channel) {
/*
P/P * Method: Identity getChannelConfig(String, String)
*
* Preconditions:
* channel != null
* network != null
* (soft) init'ed(com.dmdirc.config.ConfigTarget$1__static_init.new int[](ConfigTarget$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.HIGH)
* getTarget(...).data@453 != null
* identity.myTarget@453 != null
* java.lang.String:isEmpty(...)@440 == 0
* java.lang.String:isEmpty(...)@445 == 0
* ...
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(return_value)
* new ConfigFile(Identity#2) num objects == 0, if init'ed
* new ConfigManager(setOption#2) num objects == 0, if init'ed
* new ConfigManager(setOption#2).channel == null
* new ConfigManager(setOption#2).ircd == null
* new ConfigManager(setOption#2).listeners == null
* new ConfigManager(setOption#2).network == null
* new ConfigManager(setOption#2).server == null
* new ConfigManager(setOption#2).sources == null
* ...
*
* Test Vectors:
* getTarget(...).type@453: Inverse{&com.dmdirc.config.ConfigTarget$TYPE__static_init.new ConfigTarget$TYPE(ConfigTarget$TYPE__static_init#8)}, Addr_Set{&com.dmdirc.config.ConfigTarget$TYPE__static_init.new ConfigTarget$TYPE(ConfigTarget$TYPE__static_init#8)}
* java.lang.String:equalsIgnoreCase(...)@454: {0}, {1}
* java.util.Iterator:hasNext(...)@453: {1}, {0}
*/
440 if (network == null || network.isEmpty()) {
441 throw new IllegalArgumentException("getChannelConfig called "
442 + "with null or empty network\n\nNetwork: " + network);
443 }
444
445 if (channel == null || channel.isEmpty()) {
446 throw new IllegalArgumentException("getChannelConfig called "
447 + "with null or empty channel\n\nChannel: " + channel);
448 }
449
450 final String myTarget = (channel + "@" + network).toLowerCase();
451
452 synchronized (identities) {
453 for (Identity identity : identities) {
454 if (identity.getTarget().getType() == ConfigTarget.TYPE.CHANNEL
455 && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
456 return identity;
457 }
458 }
459 }
460
461 // We need to create one
462 final ConfigTarget target = new ConfigTarget();
463 target.setChannel(myTarget);
464
465 try {
466 return Identity.buildIdentity(target);
467 } catch (IOException ex) {
468 Logger.userError(ErrorLevel.HIGH, "Unable to create channel identity", ex);
469 return null;
470 }
471 }
472
473 /**
474 * Retrieves the config for the specified network. The config is
475 * created if it doesn't exist.
476 *
477 * @param network The name of the network
478 * @return A config source for the network
479 */
480 @Precondition("The specified network is non-null and not empty")
481 public static Identity getNetworkConfig(final String network) {
/*
P/P * Method: Identity getNetworkConfig(String)
*
* Preconditions:
* network != null
* (soft) init'ed(com.dmdirc.config.ConfigTarget$1__static_init.new int[](ConfigTarget$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.HIGH)
* getTarget(...).data@490 != null
* identity.myTarget@490 != null
* java.lang.String:isEmpty(...)@482 == 0
* java.util.Iterator:next(...)@490 != null
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(return_value)
* new ConfigFile(Identity#2) num objects == 0, if init'ed
* new ConfigManager(setOption#2) num objects == 0, if init'ed
* new ConfigManager(setOption#2).channel == null
* new ConfigManager(setOption#2).ircd == null
* new ConfigManager(setOption#2).listeners == null
* new ConfigManager(setOption#2).network == null
* new ConfigManager(setOption#2).server == null
* new ConfigManager(setOption#2).sources == null
* ...
*
* Test Vectors:
* getTarget(...).type@490: Inverse{&com.dmdirc.config.ConfigTarget$TYPE__static_init.new ConfigTarget$TYPE(ConfigTarget$TYPE__static_init#6)}, Addr_Set{&com.dmdirc.config.ConfigTarget$TYPE__static_init.new ConfigTarget$TYPE(ConfigTarget$TYPE__static_init#6)}
* java.lang.String:equalsIgnoreCase(...)@491: {0}, {1}
* java.util.Iterator:hasNext(...)@490: {1}, {0}
*/
482 if (network == null || network.isEmpty()) {
483 throw new IllegalArgumentException("getNetworkConfig called "
484 + "with null or empty network\n\nNetwork:" + network);
485 }
486
487 final String myTarget = network.toLowerCase();
488
489 synchronized (identities) {
490 for (Identity identity : identities) {
491 if (identity.getTarget().getType() == ConfigTarget.TYPE.NETWORK
492 && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
493 return identity;
494 }
495 }
496 }
497
498 // We need to create one
499 final ConfigTarget target = new ConfigTarget();
500 target.setNetwork(myTarget);
501
502 try {
503 return Identity.buildIdentity(target);
504 } catch (IOException ex) {
505 Logger.userError(ErrorLevel.HIGH, "Unable to create network identity", ex);
506 return null;
507 }
508 }
509
510 /**
511 * Retrieves the config for the specified server. The config is
512 * created if it doesn't exist.
513 *
514 * @param server The name of the server
515 * @return A config source for the server
516 */
517 @Precondition("The specified server is non-null and not empty")
518 public static Identity getServerConfig(final String server) {
/*
P/P * Method: Identity getServerConfig(String)
*
* Preconditions:
* server != null
* (soft) init'ed(com.dmdirc.config.ConfigTarget$1__static_init.new int[](ConfigTarget$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.HIGH)
* getTarget(...).data@527 != null
* identity.myTarget@527 != null
* java.lang.String:isEmpty(...)@519 == 0
* java.util.Iterator:next(...)@527 != null
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(return_value)
* new ConfigFile(Identity#2) num objects == 0, if init'ed
* new ConfigManager(setOption#2) num objects == 0, if init'ed
* new ConfigManager(setOption#2).channel == null
* new ConfigManager(setOption#2).ircd == null
* new ConfigManager(setOption#2).listeners == null
* new ConfigManager(setOption#2).network == null
* new ConfigManager(setOption#2).server == null
* new ConfigManager(setOption#2).sources == null
* ...
*
* Test Vectors:
* getTarget(...).type@527: Inverse{&com.dmdirc.config.ConfigTarget$TYPE__static_init.new ConfigTarget$TYPE(ConfigTarget$TYPE__static_init#7)}, Addr_Set{&com.dmdirc.config.ConfigTarget$TYPE__static_init.new ConfigTarget$TYPE(ConfigTarget$TYPE__static_init#7)}
* java.lang.String:equalsIgnoreCase(...)@528: {0}, {1}
* java.util.Iterator:hasNext(...)@527: {1}, {0}
*/
519 if (server == null || server.isEmpty()) {
520 throw new IllegalArgumentException("getServerConfig called "
521 + "with null or empty server\n\nServer: " + server);
522 }
523
524 final String myTarget = server.toLowerCase();
525
526 synchronized (identities) {
527 for (Identity identity : identities) {
528 if (identity.getTarget().getType() == ConfigTarget.TYPE.SERVER
529 && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
530 return identity;
531 }
532 }
533 }
534
535 // We need to create one
536 final ConfigTarget target = new ConfigTarget();
537 target.setServer(myTarget);
538
539 try {
540 return Identity.buildIdentity(target);
541 } catch (IOException ex) {
542 Logger.userError(ErrorLevel.HIGH, "Unable to create network identity", ex);
543 return null;
544 }
545 }
546
547 }
SofCheck Inspector Build Version : 2.17854
| IdentityManager.java |
2009-Jun-25 01:54:24 |
| IdentityManager.class |
2009-Sep-02 17:04:16 |