You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IdentityManager.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Copyright (c) 2006-2011 DMDirc Developers
  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. package com.dmdirc.config;
  23. import com.dmdirc.interfaces.IdentityFactory;
  24. import com.dmdirc.interfaces.IdentityController;
  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.io.ConfigFile;
  31. import com.dmdirc.util.io.InvalidConfigFileException;
  32. import com.dmdirc.util.collections.MapList;
  33. import com.dmdirc.util.collections.WeakMapList;
  34. import com.dmdirc.util.resourcemanager.ResourceManager;
  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.LinkedHashSet;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Set;
  44. import java.util.logging.Level;
  45. /**
  46. * The identity manager manages all known identities, providing easy methods
  47. * to access them.
  48. */
  49. public class IdentityManager implements IdentityFactory, IdentityController {
  50. /** A logger for this class. */
  51. private static final java.util.logging.Logger LOGGER = java.util.logging
  52. .Logger.getLogger(IdentityManager.class.getName());
  53. /** A singleton instance of IdentityManager. */
  54. private static final IdentityManager INSTANCE = new IdentityManager();
  55. /**
  56. * The identities that have been loaded into this manager.
  57. *
  58. * Standard identities are inserted with a <code>null</code> key, custom
  59. * identities use their custom type as the key.
  60. */
  61. private final MapList<String, Identity> identities
  62. = new MapList<String, Identity>();
  63. /**
  64. * The {@link IdentityListener}s that have registered with this manager.
  65. *
  66. * Listeners for standard identities are inserted with a <code>null</code>
  67. * key, listeners for a specific custom type use their type as the key.
  68. */
  69. private final MapList<String, IdentityListener> listeners
  70. = new WeakMapList<String, IdentityListener>();
  71. /** The identity file used for the global config. */
  72. private Identity config;
  73. /** The identity file used for addon defaults. */
  74. private Identity addonConfig;
  75. /** The identity file bundled with the client containing version info. */
  76. private Identity versionConfig;
  77. /** The config manager used for global settings. */
  78. private ConfigManager globalconfig;
  79. /** Creates a new instance of IdentityManager. */
  80. private IdentityManager() {
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public void initialise() throws InvalidIdentityFileException {
  85. identities.clear();
  86. loadVersionIdentity();
  87. loadDefaults();
  88. loadUserIdentities();
  89. loadConfig();
  90. if (getIdentitiesByType("profile").isEmpty()) {
  91. try {
  92. Identity.buildProfile("Default Profile");
  93. } catch (IOException ex) {
  94. Logger.userError(ErrorLevel.FATAL, "Unable to write default profile", ex);
  95. }
  96. }
  97. // Set up the identity used for the addons defaults
  98. final ConfigTarget target = new ConfigTarget();
  99. target.setGlobalDefault();
  100. target.setOrder(500000);
  101. final ConfigFile addonConfigFile = new ConfigFile((File) null);
  102. final Map<String, String> addonSettings = new HashMap<String, String>();
  103. addonSettings.put("name", "Addon defaults");
  104. addonConfigFile.addDomain("identity", addonSettings);
  105. addonConfig = new Identity(addonConfigFile, target);
  106. registerIdentity(addonConfig);
  107. if (!getGlobalConfiguration().hasOptionString("identity", "defaultsversion")) {
  108. Logger.userError(ErrorLevel.FATAL, "Default settings "
  109. + "could not be loaded");
  110. }
  111. }
  112. /**
  113. * Loads all identity files.
  114. *
  115. * @throws InvalidIdentityFileException If there is an error with the config
  116. * file.
  117. * @deprecated Use non-static methods instead
  118. */
  119. @Deprecated
  120. public static void load() throws InvalidIdentityFileException {
  121. INSTANCE.initialise();
  122. }
  123. /** Loads the default (built in) identities. */
  124. private void loadDefaults() {
  125. final String[] targets = {"default", "modealiases"};
  126. final String dir = getDirectory();
  127. for (String target : targets) {
  128. final File file = new File(dir + target);
  129. if (file.exists() && !file.isDirectory()) {
  130. boolean success = false;
  131. for (int i = 0; i < 10 && !success; i++) {
  132. final String suffix = ".old" + (i > 0 ? "-" + i : "");
  133. success = file.renameTo(new File(file.getParentFile(), target + suffix));
  134. }
  135. if (!success) {
  136. Logger.userError(ErrorLevel.HIGH, "Unable to create directory for "
  137. + "default settings folder (" + target + ")", "A file "
  138. + "with that name already exists, and couldn't be renamed."
  139. + " Rename or delete " + file.getAbsolutePath());
  140. continue;
  141. }
  142. }
  143. if (!file.exists() || file.listFiles() == null || file.listFiles().length == 0) {
  144. file.mkdirs();
  145. extractIdentities(target);
  146. }
  147. loadUser(file);
  148. }
  149. extractFormatters();
  150. // If the bundled defaults are newer than the ones the user is
  151. // currently using, extract them.
  152. if (getGlobalConfiguration().hasOptionString("identity", "defaultsversion")
  153. && getGlobalConfiguration().hasOptionString("updater", "bundleddefaultsversion")) {
  154. final Version installedVersion = new Version(getGlobalConfiguration()
  155. .getOption("identity", "defaultsversion"));
  156. final Version bundledVersion = new Version(getGlobalConfiguration()
  157. .getOption("updater", "bundleddefaultsversion"));
  158. if (bundledVersion.compareTo(installedVersion) > 0) {
  159. extractIdentities("default");
  160. loadUser(new File(dir, "default"));
  161. }
  162. }
  163. }
  164. /**
  165. * Extracts the bundled formatters to the user's identity folder.
  166. */
  167. private void extractFormatters() {
  168. try {
  169. ResourceManager.getResourceManager().extractResource(
  170. "com/dmdirc/config/defaults/default/formatter",
  171. getIdentityDirectory() + "default/", false);
  172. } catch (IOException ex) {
  173. Logger.userError(ErrorLevel.MEDIUM, "Unable to extract default "
  174. + "formatters: " + ex.getMessage());
  175. }
  176. }
  177. /**
  178. * Extracts the specific set of default identities to the user's identity
  179. * folder.
  180. *
  181. * @param target The target to be extracted
  182. */
  183. private void extractIdentities(final String target) {
  184. try {
  185. ResourceManager.getResourceManager().extractResources(
  186. "com/dmdirc/config/defaults/" + target,
  187. getIdentityDirectory() + target, false);
  188. } catch (IOException ex) {
  189. Logger.userError(ErrorLevel.MEDIUM, "Unable to extract default "
  190. + "identities: " + ex.getMessage());
  191. }
  192. }
  193. /** {@inheritDoc} */
  194. @Override
  195. public String getIdentityDirectory() {
  196. return Main.getConfigDir() + "identities" + System.getProperty("file.separator");
  197. }
  198. /**
  199. * Retrieves the directory used to store identities in.
  200. *
  201. * @return The identity directory path
  202. * @deprecated Use non-static methods instead
  203. */
  204. @Deprecated
  205. public static String getDirectory() {
  206. return INSTANCE.getIdentityDirectory();
  207. }
  208. /** {@inheritDoc} */
  209. @Override
  210. public void loadUserIdentities() {
  211. final File dir = new File(getIdentityDirectory());
  212. if (!dir.exists()) {
  213. try {
  214. dir.mkdirs();
  215. dir.createNewFile();
  216. } catch (IOException ex) {
  217. Logger.userError(ErrorLevel.MEDIUM, "Unable to create identity dir");
  218. }
  219. }
  220. loadUser(dir);
  221. }
  222. /**
  223. * Loads user-defined identity files.
  224. * @deprecated Use non-static methods instead
  225. */
  226. @Deprecated
  227. public static void loadUser() {
  228. INSTANCE.loadUserIdentities();
  229. }
  230. /**
  231. * Recursively loads files from the specified directory.
  232. *
  233. * @param dir The directory to be loaded
  234. */
  235. @Precondition({
  236. "The specified File is not null",
  237. "The specified File is a directory"
  238. })
  239. private void loadUser(final File dir) {
  240. Logger.assertTrue(dir != null);
  241. Logger.assertTrue(dir.isDirectory());
  242. if (dir.listFiles() == null) {
  243. Logger.userError(ErrorLevel.MEDIUM,
  244. "Unable to load user identity files from "
  245. + dir.getAbsolutePath());
  246. } else {
  247. for (File file : dir.listFiles()) {
  248. if (file.isDirectory()) {
  249. loadUser(file);
  250. } else {
  251. loadIdentity(file);
  252. }
  253. }
  254. }
  255. }
  256. /**
  257. * Loads an identity from the specified file. If the identity already
  258. * exists, it is told to reload instead.
  259. *
  260. * @param file The file to load the identity from.
  261. */
  262. private void loadIdentity(final File file) {
  263. synchronized (identities) {
  264. for (Identity identity : getAllIdentities()) {
  265. if (identity.isFile(file)) {
  266. try {
  267. identity.reload();
  268. } catch (IOException ex) {
  269. Logger.userError(ErrorLevel.MEDIUM,
  270. "I/O error when reloading identity file: "
  271. + file.getAbsolutePath() + " (" + ex.getMessage() + ")");
  272. } catch (InvalidConfigFileException ex) {
  273. // Do nothing
  274. }
  275. return;
  276. }
  277. }
  278. }
  279. try {
  280. registerIdentity(new Identity(file, false));
  281. } catch (InvalidIdentityFileException ex) {
  282. Logger.userError(ErrorLevel.MEDIUM,
  283. "Invalid identity file: " + file.getAbsolutePath()
  284. + " (" + ex.getMessage() + ")");
  285. } catch (IOException ex) {
  286. Logger.userError(ErrorLevel.MEDIUM,
  287. "I/O error when reading identity file: "
  288. + file.getAbsolutePath());
  289. }
  290. }
  291. /**
  292. * Retrieves all known identities.
  293. *
  294. * @return A set of all known identities
  295. * @since 0.6.4
  296. */
  297. private Set<Identity> getAllIdentities() {
  298. final Set<Identity> res = new LinkedHashSet<Identity>();
  299. for (Map.Entry<String, List<Identity>> entry : identities.entrySet()) {
  300. res.addAll(entry.getValue());
  301. }
  302. return res;
  303. }
  304. /**
  305. * Returns the "group" to which the specified identity belongs. For custom
  306. * identities this is the custom identity type, otherwise this is
  307. * <code>null</code>.
  308. *
  309. * @param identity The identity whose group is being retrieved
  310. * @return The group of the specified identity
  311. * @since 0.6.4
  312. */
  313. private String getGroup(final Identity identity) {
  314. return identity.getTarget().getType() == ConfigTarget.TYPE.CUSTOM
  315. ? identity.getTarget().getData() : null;
  316. }
  317. /** {@inheritDoc} */
  318. @Override
  319. public void loadVersionIdentity() {
  320. try {
  321. versionConfig = new Identity(Main.class.getResourceAsStream("version.config"), false);
  322. registerIdentity(versionConfig);
  323. } catch (IOException ex) {
  324. Logger.appError(ErrorLevel.FATAL, "Unable to load version information", ex);
  325. } catch (InvalidIdentityFileException ex) {
  326. Logger.appError(ErrorLevel.FATAL, "Unable to load version information", ex);
  327. }
  328. }
  329. /**
  330. * Loads the version information.
  331. *
  332. * @deprecated Use non-static methods instead
  333. */
  334. @Deprecated
  335. public static void loadVersion() {
  336. INSTANCE.loadVersionIdentity();
  337. }
  338. /**
  339. * Loads the config identity.
  340. *
  341. * @throws InvalidIdentityFileException if there is a problem with the
  342. * config file.
  343. */
  344. private void loadConfig() throws InvalidIdentityFileException {
  345. try {
  346. final File file = new File(Main.getConfigDir() + "dmdirc.config");
  347. if (!file.exists()) {
  348. file.createNewFile();
  349. }
  350. config = new Identity(file, true);
  351. config.setOption("identity", "name", "Global config");
  352. registerIdentity(config);
  353. } catch (IOException ex) {
  354. Logger.userError(ErrorLevel.FATAL, "I/O error when loading global config: "
  355. + ex.getMessage(), ex);
  356. }
  357. }
  358. /** {@inheritDoc} */
  359. @Override
  360. public Identity getGlobalConfigIdentity() {
  361. return config;
  362. }
  363. /**
  364. * Retrieves the identity used for the global config.
  365. *
  366. * @return The global config identity
  367. * @deprecated Use non-static methods instead
  368. */
  369. @Deprecated
  370. public static Identity getConfigIdentity() {
  371. return INSTANCE.getGlobalConfigIdentity();
  372. }
  373. /** {@inheritDoc} */
  374. @Override
  375. public Identity getGlobalAddonIdentity() {
  376. return addonConfig;
  377. }
  378. /**
  379. * Retrieves the identity used for addons defaults.
  380. *
  381. * @return The addons defaults identity
  382. * @deprecated Use non-static methods instead
  383. */
  384. @Deprecated
  385. public static Identity getAddonIdentity() {
  386. return INSTANCE.getGlobalAddonIdentity();
  387. }
  388. /** {@inheritDoc} */
  389. @Override
  390. public Identity getGlobalVersionIdentity() {
  391. return versionConfig;
  392. }
  393. /**
  394. * Retrieves the identity bundled with the DMDirc client containing
  395. * version information.
  396. *
  397. * @return The version identity
  398. * @since 0.6.3m2
  399. * @deprecated Use non-static methods instead
  400. */
  401. @Deprecated
  402. public static Identity getVersionIdentity() {
  403. return INSTANCE.getGlobalVersionIdentity();
  404. }
  405. /** {@inheritDoc} */
  406. @Override
  407. public void saveAll() {
  408. synchronized (identities) {
  409. for (Identity identity : getAllIdentities()) {
  410. identity.save();
  411. }
  412. }
  413. }
  414. /**
  415. * Saves all modified identity files to disk.
  416. * @deprecated Use non-static methods instead
  417. */
  418. @Deprecated
  419. public static void save() {
  420. INSTANCE.saveAll();
  421. }
  422. /** {@inheritDoc} */
  423. @Override
  424. public void registerIdentity(final Identity identity) {
  425. Logger.assertTrue(identity != null);
  426. final String target = getGroup(identity);
  427. if (identities.containsValue(target, identity)) {
  428. unregisterIdentity(identity);
  429. }
  430. synchronized (identities) {
  431. identities.add(target, identity);
  432. }
  433. LOGGER.log(Level.FINER, "Adding identity: {0} (group: {1})",
  434. new Object[]{identity, target});
  435. synchronized (listeners) {
  436. for (IdentityListener listener : listeners.safeGet(target)) {
  437. listener.identityAdded(identity);
  438. }
  439. }
  440. }
  441. /**
  442. * Adds the specific identity to this manager.
  443. *
  444. * @param identity The identity to be added
  445. * @deprecated Use non-static methods instead
  446. */
  447. @Deprecated
  448. @Precondition("The specified Identity is not null")
  449. public static void addIdentity(final Identity identity) {
  450. INSTANCE.registerIdentity(identity);
  451. }
  452. /** {@inheritDoc} */
  453. @Override
  454. public void unregisterIdentity(final Identity identity) {
  455. Logger.assertTrue(identity != null);
  456. final String group = getGroup(identity);
  457. Logger.assertTrue(identities.containsValue(group, identity));
  458. synchronized (identities) {
  459. identities.remove(group, identity);
  460. }
  461. synchronized (listeners) {
  462. for (IdentityListener listener : listeners.safeGet(group)) {
  463. listener.identityRemoved(identity);
  464. }
  465. }
  466. }
  467. /**
  468. * Removes an identity from this manager.
  469. * @param identity The identity to be removed
  470. * @deprecated Use non-static methods instead
  471. */
  472. @Deprecated
  473. @Precondition({
  474. "The specified Identity is not null",
  475. "The specified Identity has previously been added and not removed"
  476. })
  477. public static void removeIdentity(final Identity identity) {
  478. INSTANCE.unregisterIdentity(identity);
  479. }
  480. /** {@inheritDoc} */
  481. @Override
  482. public void registerIdentityListener(final IdentityListener listener) {
  483. registerIdentityListener(null, listener);
  484. }
  485. /**
  486. * Adds a new identity listener which will be informed of all settings
  487. * identities which are added to this manager.
  488. *
  489. * @param listener The listener to be added
  490. * @since 0.6.4
  491. * @deprecated Use non-static methods instead
  492. */
  493. @Deprecated
  494. @Precondition("The specified listener is not null")
  495. public static void addIdentityListener(final IdentityListener listener) {
  496. INSTANCE.registerIdentityListener(listener);
  497. }
  498. /** {@inheritDoc} */
  499. @Override
  500. public void registerIdentityListener(final String type, final IdentityListener listener) {
  501. Logger.assertTrue(listener != null);
  502. synchronized (listeners) {
  503. listeners.add(type, listener);
  504. }
  505. }
  506. /**
  507. * Adds a new identity listener which will be informed of all identities
  508. * of the specified custom type which are added to this manager.
  509. *
  510. * @param type The type of identities to listen for
  511. * @param listener The listener to be added
  512. * @since 0.6.4
  513. * @deprecated Use non-static methods instead
  514. */
  515. @Deprecated
  516. @Precondition("The specified listener is not null")
  517. public static void addIdentityListener(final String type, final IdentityListener listener) {
  518. INSTANCE.registerIdentityListener(type, listener);
  519. }
  520. /** {@inheritDoc} */
  521. @Override
  522. public List<Identity> getIdentitiesByType(final String type) {
  523. return Collections.unmodifiableList(identities.safeGet(type));
  524. }
  525. /**
  526. * Retrieves a list of identities that belong to the specified custom type.
  527. *
  528. * @param type The type of identity to search for
  529. * @return A list of matching identities
  530. * @since 0.6.4
  531. * @deprecated Use non-static methods instead
  532. */
  533. @Deprecated
  534. public static List<Identity> getCustomIdentities(final String type) {
  535. return INSTANCE.getIdentitiesByType(type);
  536. }
  537. /** {@inheritDoc} */
  538. @Override
  539. public List<Identity> getIdentitiesForManager(final ConfigManager manager) {
  540. final List<Identity> sources = new ArrayList<Identity>();
  541. synchronized (identities) {
  542. for (Identity identity : identities.safeGet(null)) {
  543. if (manager.identityApplies(identity)) {
  544. sources.add(identity);
  545. }
  546. }
  547. }
  548. Collections.sort(sources);
  549. return sources;
  550. }
  551. /**
  552. * Retrieves a list of all config sources that should be applied to the
  553. * specified config manager.
  554. *
  555. * @param manager The manager requesting sources
  556. * @return A list of all matching config sources
  557. * @deprecated Use non-static methods instead
  558. */
  559. @Deprecated
  560. public static List<Identity> getSources(final ConfigManager manager) {
  561. return INSTANCE.getIdentitiesForManager(manager);
  562. }
  563. /** {@inheritDoc} */
  564. @Override
  565. public synchronized ConfigManager getGlobalConfiguration() {
  566. if (globalconfig == null) {
  567. globalconfig = new ConfigManager("", "", "", "");
  568. }
  569. return globalconfig;
  570. }
  571. /**
  572. * Retrieves the global config manager.
  573. *
  574. * @return The global config manager
  575. * @deprecated Use non-static methods instead
  576. */
  577. @Deprecated
  578. public static ConfigManager getGlobalConfig() {
  579. return INSTANCE.getGlobalConfiguration();
  580. }
  581. /** {@inheritDoc} */
  582. @Override
  583. public Identity createChannelConfig(final String network, final String channel) {
  584. if (network == null || network.isEmpty()) {
  585. throw new IllegalArgumentException("getChannelConfig called "
  586. + "with null or empty network\n\nNetwork: " + network);
  587. }
  588. if (channel == null || channel.isEmpty()) {
  589. throw new IllegalArgumentException("getChannelConfig called "
  590. + "with null or empty channel\n\nChannel: " + channel);
  591. }
  592. final String myTarget = (channel + "@" + network).toLowerCase();
  593. synchronized (identities) {
  594. for (Identity identity : identities.safeGet(null)) {
  595. if (identity.getTarget().getType() == ConfigTarget.TYPE.CHANNEL
  596. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  597. return identity;
  598. }
  599. }
  600. }
  601. // We need to create one
  602. final ConfigTarget target = new ConfigTarget();
  603. target.setChannel(myTarget);
  604. try {
  605. return Identity.buildIdentity(target);
  606. } catch (IOException ex) {
  607. Logger.userError(ErrorLevel.HIGH, "Unable to create channel identity", ex);
  608. return null;
  609. }
  610. }
  611. /**
  612. * Retrieves the config for the specified channel@network. The config is
  613. * created if it doesn't exist.
  614. *
  615. * @param network The name of the network
  616. * @param channel The name of the channel
  617. * @return A config source for the channel
  618. * @deprecated Use non-static methods instead
  619. */
  620. @Deprecated
  621. @Precondition({
  622. "The specified network is non-null and not empty",
  623. "The specified channel is non-null and not empty"
  624. })
  625. public static Identity getChannelConfig(final String network, final String channel) {
  626. return INSTANCE.createChannelConfig(network, channel);
  627. }
  628. /** {@inheritDoc} */
  629. @Override
  630. public Identity createNetworkConfig(final String network) {
  631. if (network == null || network.isEmpty()) {
  632. throw new IllegalArgumentException("getNetworkConfig called "
  633. + "with null or empty network\n\nNetwork:" + network);
  634. }
  635. final String myTarget = network.toLowerCase();
  636. synchronized (identities) {
  637. for (Identity identity : identities.safeGet(null)) {
  638. if (identity.getTarget().getType() == ConfigTarget.TYPE.NETWORK
  639. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  640. return identity;
  641. }
  642. }
  643. }
  644. // We need to create one
  645. final ConfigTarget target = new ConfigTarget();
  646. target.setNetwork(myTarget);
  647. try {
  648. return Identity.buildIdentity(target);
  649. } catch (IOException ex) {
  650. Logger.userError(ErrorLevel.HIGH, "Unable to create network identity", ex);
  651. return null;
  652. }
  653. }
  654. /**
  655. * Retrieves the config for the specified network. The config is
  656. * created if it doesn't exist.
  657. *
  658. * @param network The name of the network
  659. * @return A config source for the network
  660. * @deprecated Use non-static methods instead
  661. */
  662. @Deprecated
  663. @Precondition("The specified network is non-null and not empty")
  664. public static Identity getNetworkConfig(final String network) {
  665. return INSTANCE.createNetworkConfig(network);
  666. }
  667. /** {@inheritDoc} */
  668. @Override
  669. public Identity createServerConfig(final String server) {
  670. if (server == null || server.isEmpty()) {
  671. throw new IllegalArgumentException("getServerConfig called "
  672. + "with null or empty server\n\nServer: " + server);
  673. }
  674. final String myTarget = server.toLowerCase();
  675. synchronized (identities) {
  676. for (Identity identity : identities.safeGet(null)) {
  677. if (identity.getTarget().getType() == ConfigTarget.TYPE.SERVER
  678. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  679. return identity;
  680. }
  681. }
  682. }
  683. // We need to create one
  684. final ConfigTarget target = new ConfigTarget();
  685. target.setServer(myTarget);
  686. try {
  687. return Identity.buildIdentity(target);
  688. } catch (IOException ex) {
  689. Logger.userError(ErrorLevel.HIGH, "Unable to create network identity", ex);
  690. return null;
  691. }
  692. }
  693. /**
  694. * Retrieves the config for the specified server. The config is
  695. * created if it doesn't exist.
  696. *
  697. * @param server The name of the server
  698. * @return A config source for the server
  699. * @deprecated Use non-static methods instead
  700. */
  701. @Deprecated
  702. @Precondition("The specified server is non-null and not empty")
  703. public static Identity getServerConfig(final String server) {
  704. return INSTANCE.createServerConfig(server);
  705. }
  706. /**
  707. * Gets a singleton instance of the Identity Manager.
  708. *
  709. * @return A singleton instance of the IdentityManager.
  710. */
  711. public static IdentityManager getIdentityManager() {
  712. return INSTANCE;
  713. }
  714. }