Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

IdentityManager.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Copyright (c) 2006-2015 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.Precondition;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.ConfigProvider;
  26. import com.dmdirc.interfaces.config.ConfigProviderListener;
  27. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  28. import com.dmdirc.interfaces.config.IdentityController;
  29. import com.dmdirc.interfaces.config.IdentityFactory;
  30. import com.dmdirc.util.ClientInfo;
  31. import com.dmdirc.util.io.ConfigFile;
  32. import com.dmdirc.util.io.FileUtils;
  33. import com.dmdirc.util.io.InvalidConfigFileException;
  34. import com.google.common.collect.ArrayListMultimap;
  35. import com.google.common.collect.Multimap;
  36. import java.io.IOException;
  37. import java.lang.ref.WeakReference;
  38. import java.net.URISyntaxException;
  39. import java.nio.file.DirectoryStream;
  40. import java.nio.file.Files;
  41. import java.nio.file.Path;
  42. import java.util.ArrayList;
  43. import java.util.Collection;
  44. import java.util.Collections;
  45. import java.util.HashMap;
  46. import java.util.List;
  47. import java.util.Map;
  48. import java.util.Objects;
  49. import java.util.concurrent.ConcurrentHashMap;
  50. import java.util.stream.Collectors;
  51. import org.slf4j.Logger;
  52. import org.slf4j.LoggerFactory;
  53. import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
  54. import static com.dmdirc.util.LogUtils.FATAL_USER_ERROR;
  55. import static com.dmdirc.util.LogUtils.USER_ERROR;
  56. import static com.google.common.base.Preconditions.checkArgument;
  57. import static com.google.common.base.Preconditions.checkNotNull;
  58. public class IdentityManager implements IdentityFactory, IdentityController {
  59. private static final Logger LOG = LoggerFactory.getLogger(IdentityManager.class);
  60. /** A regular expression that will match all characters illegal in file names. */
  61. private static final String ILLEGAL_CHARS = "[\\\\\"/:\\*\\?\"<>\\|]";
  62. /** The domain used for identity settings. */
  63. private static final String IDENTITY_DOMAIN = "identity";
  64. /** The domain used for profile settings. */
  65. private static final String PROFILE_DOMAIN = "profile";
  66. /** Base configuration directory where the main configuration file will be located. */
  67. private final Path configDirectory;
  68. /** Directory to save and load identities in. */
  69. private final Path identitiesDirectory;
  70. /**
  71. * The identities that have been loaded into this manager.
  72. *
  73. * Standard identities are inserted with a {@code null} key, custom identities use their
  74. * custom type as the key.
  75. */
  76. private final Multimap<String, ConfigFileBackedConfigProvider> identities = ArrayListMultimap.create();
  77. /** Map of paths to corresponding config providers, to facilitate reloading. */
  78. private final Map<Path, ConfigFileBackedConfigProvider> configProvidersByPath = new ConcurrentHashMap<>();
  79. /**
  80. * The {@link ConfigProviderListener}s that have registered with this manager.
  81. *
  82. * Listeners for standard identities are inserted with a {@code null} key, listeners for a
  83. * specific custom type use their type as the key.
  84. */
  85. private final Multimap<String, WeakReference<ConfigProviderListener>> listeners =
  86. ArrayListMultimap.create();
  87. /** Client info objecty. */
  88. private final ClientInfo clientInfo;
  89. /** The identity file used for the global config. */
  90. private ConfigFileBackedConfigProvider config;
  91. /** The identity file used for addon defaults. */
  92. private ConfigFileBackedConfigProvider addonConfig;
  93. /** The identity file bundled with the client containing version info. */
  94. private ConfigFileBackedConfigProvider versionConfig;
  95. /** The config manager used for global settings. */
  96. private AggregateConfigProvider globalconfig;
  97. /**
  98. * Creates a new instance of IdentityManager.
  99. *
  100. * @param baseDirectory The BASE config directory.
  101. * @param identitiesDirectory The directory to store identities in.
  102. */
  103. public IdentityManager(final Path baseDirectory, final Path identitiesDirectory,
  104. final ClientInfo clientInfo) {
  105. this.configDirectory = baseDirectory;
  106. this.identitiesDirectory = identitiesDirectory;
  107. this.clientInfo = clientInfo;
  108. }
  109. /**
  110. * Loads all identity files.
  111. *
  112. * @throws InvalidIdentityFileException If there is an error with the config file.
  113. */
  114. public void initialise() throws InvalidIdentityFileException {
  115. identities.clear();
  116. loadVersionIdentity();
  117. loadDefaults();
  118. loadUserIdentities();
  119. loadConfig();
  120. // Set up the identity used for the addons defaults
  121. final ConfigTarget target = new ConfigTarget();
  122. target.setGlobalDefault();
  123. target.setOrder(500000);
  124. final ConfigFile addonConfigFile = new ConfigFile((Path) null);
  125. final Map<String, String> addonSettings = new HashMap<>();
  126. addonSettings.put("name", "Addon defaults");
  127. addonConfigFile.addDomain("identity", addonSettings);
  128. addonConfig = new ConfigFileBackedConfigProvider(this, addonConfigFile, target);
  129. addConfigProvider(addonConfig);
  130. }
  131. /** Loads the default (built in) identities. */
  132. private void loadDefaults() {
  133. try {
  134. loadIdentity(FileUtils.getPathForResource(getClass().getResource(
  135. "defaults/default/defaults")));
  136. loadIdentity(FileUtils.getPathForResource(getClass().getResource(
  137. "defaults/default/formatter")));
  138. } catch (URISyntaxException ex) {
  139. LOG.error(FATAL_APP_ERROR, "Unable to load settings", ex);
  140. }
  141. final Path file = identitiesDirectory.resolve("modealiases");
  142. if (!Files.exists(file)) {
  143. try {
  144. Files.createDirectories(file);
  145. } catch (IOException ex) {
  146. LOG.info(USER_ERROR, "Unable to create modealiases directory", file, ex);
  147. }
  148. }
  149. try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(file)) {
  150. if (!directoryStream.iterator().hasNext()) {
  151. extractIdentities("modealiases");
  152. }
  153. } catch (IOException ex) {
  154. LOG.error(FATAL_USER_ERROR, "Unable to iterate required directory '{}'. Please check"
  155. + "file permissions or specify a different configuration directory.", file, ex);
  156. return;
  157. }
  158. loadUser(file);
  159. }
  160. /**
  161. * Extracts the specific set of default identities to the user's identity folder.
  162. *
  163. * @param target The target to be extracted
  164. */
  165. private void extractIdentities(final String target) {
  166. try {
  167. FileUtils.copyResources(getClass().getResource("defaults/" + target),
  168. identitiesDirectory);
  169. } catch (IOException ex) {
  170. LOG.warn(USER_ERROR, "Unable to extract default identities: {}", ex.getMessage(), ex);
  171. }
  172. }
  173. @Override
  174. public void loadUserIdentities() {
  175. if (!Files.exists(identitiesDirectory)) {
  176. try {
  177. Files.createDirectories(identitiesDirectory);
  178. } catch (IOException ex) {
  179. LOG.warn(USER_ERROR, "Unable to create identity dir", ex);
  180. }
  181. }
  182. loadUser(identitiesDirectory);
  183. }
  184. /**
  185. * Recursively loads files from the specified directory.
  186. *
  187. * @param dir The directory to be loaded
  188. */
  189. @Precondition({
  190. "The specified File is not null",
  191. "The specified File is a directory"
  192. })
  193. private void loadUser(final Path dir) {
  194. checkNotNull(dir);
  195. checkArgument(Files.isDirectory(dir));
  196. try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir)) {
  197. for (Path child : directoryStream) {
  198. if (Files.isDirectory(child)) {
  199. loadUser(child);
  200. } else {
  201. loadIdentity(child);
  202. }
  203. }
  204. } catch (IOException ex) {
  205. LOG.warn(USER_ERROR, "Unable to load user identity files from: {}", dir, ex);
  206. }
  207. }
  208. /**
  209. * Loads an identity from the specified file. If the identity already exists, it is told to
  210. * reload instead.
  211. *
  212. * @param file The file to load the identity from.
  213. */
  214. private void loadIdentity(final Path file) {
  215. synchronized (identities) {
  216. if (configProvidersByPath.containsKey(file)) {
  217. try {
  218. configProvidersByPath.get(file).reload();
  219. } catch (IOException ex) {
  220. LOG.warn(USER_ERROR, "I/O error when reloading identity file: {} ({})",
  221. file, ex.getMessage(), ex);
  222. } catch (InvalidConfigFileException ex) {
  223. // Do nothing
  224. }
  225. }
  226. }
  227. try {
  228. final ConfigFileBackedConfigProvider provider = new ConfigFileBackedConfigProvider(this, file, false);
  229. addConfigProvider(provider);
  230. configProvidersByPath.put(file, provider);
  231. } catch (InvalidIdentityFileException ex) {
  232. LOG.warn(USER_ERROR, "Invalid identity file: {} ({})", file, ex.getMessage(), ex);
  233. } catch (IOException ex) {
  234. LOG.warn(USER_ERROR, "I/O error when reading identity file: {}", file, ex);
  235. }
  236. }
  237. /**
  238. * Retrieves all known identities.
  239. *
  240. * @return A set of all known identities
  241. *
  242. * @since 0.6.4
  243. */
  244. private Iterable<ConfigFileBackedConfigProvider> getAllIdentities() {
  245. return identities.values();
  246. }
  247. /**
  248. * Returns the "group" to which the specified identity belongs. For custom identities this is
  249. * the custom identity type, otherwise this is <code>null</code>.
  250. *
  251. * @param identity The identity whose group is being retrieved
  252. *
  253. * @return The group of the specified identity
  254. *
  255. * @since 0.6.4
  256. */
  257. private String getGroup(final ConfigFileBackedConfigProvider identity) {
  258. return identity.getTarget().getType() == ConfigTarget.TYPE.CUSTOM
  259. ? identity.getTarget().getData() : null;
  260. }
  261. @Override
  262. public void loadVersionIdentity() {
  263. try {
  264. versionConfig = new ConfigFileBackedConfigProvider(IdentityManager.class.
  265. getResourceAsStream("/com/dmdirc/version.config"), false);
  266. addConfigProvider(versionConfig);
  267. } catch (IOException | InvalidIdentityFileException ex) {
  268. LOG.warn(USER_ERROR, "Unable to load version information.", ex);
  269. }
  270. }
  271. /**
  272. * Loads the config identity.
  273. *
  274. * @throws InvalidIdentityFileException if there is a problem with the config file.
  275. */
  276. private void loadConfig() throws InvalidIdentityFileException {
  277. try {
  278. final Path file = configDirectory.resolve("dmdirc.config");
  279. if (!Files.exists(file)) {
  280. Files.createFile(file);
  281. }
  282. config = new ConfigFileBackedConfigProvider(this, file, true);
  283. config.setOption("identity", "name", "Global config");
  284. configProvidersByPath.put(file, config);
  285. addConfigProvider(config);
  286. } catch (IOException ex) {
  287. LOG.warn(USER_ERROR, "I/O error when loading global config: {}", ex.getMessage(), ex);
  288. }
  289. }
  290. @Override
  291. public ConfigProvider getUserSettings() {
  292. return config;
  293. }
  294. @Override
  295. public ConfigProvider getAddonSettings() {
  296. return addonConfig;
  297. }
  298. @Override
  299. public ConfigProvider getVersionSettings() {
  300. return versionConfig;
  301. }
  302. @Override
  303. public void saveAll() {
  304. synchronized (identities) {
  305. for (ConfigProvider identity : getAllIdentities()) {
  306. identity.save();
  307. }
  308. }
  309. }
  310. @Override
  311. public void addConfigProvider(final ConfigFileBackedConfigProvider identity) {
  312. checkNotNull(identity);
  313. final String target = getGroup(identity);
  314. if (identities.containsEntry(target, identity)) {
  315. removeConfigProvider(identity);
  316. }
  317. synchronized (identities) {
  318. identities.put(target, identity);
  319. }
  320. LOG.debug("Adding identity: {} (group: {})", new Object[]{identity, target});
  321. synchronized (listeners) {
  322. listeners.get(target).stream()
  323. .map(WeakReference::get)
  324. .filter(Objects::nonNull)
  325. .forEach(l -> l.configProviderAdded(identity));
  326. }
  327. }
  328. @Override
  329. public void removeConfigProvider(final ConfigFileBackedConfigProvider identity) {
  330. checkNotNull(identity);
  331. final String group = getGroup(identity);
  332. checkArgument(identities.containsEntry(group, identity));
  333. Path path = null;
  334. for (Map.Entry<Path, ConfigFileBackedConfigProvider> entry : configProvidersByPath.entrySet()) {
  335. if (entry.getValue() == identity) {
  336. path = entry.getKey();
  337. }
  338. }
  339. if (path != null) {
  340. configProvidersByPath.remove(path);
  341. }
  342. synchronized (identities) {
  343. identities.remove(group, identity);
  344. }
  345. synchronized (listeners) {
  346. listeners.get(group).stream()
  347. .map(WeakReference::get)
  348. .filter(Objects::nonNull)
  349. .forEach(l -> l.configProviderRemoved(identity));
  350. }
  351. }
  352. @Override
  353. public void registerIdentityListener(final ConfigProviderListener listener) {
  354. registerIdentityListener(null, listener);
  355. }
  356. @Override
  357. public void unregisterIdentityListener(final ConfigProviderListener listener) {
  358. synchronized (listeners) {
  359. listeners.entries().stream().filter(e -> {
  360. final ConfigProviderListener value = e.getValue().get();
  361. return value == null || value.equals(listener);
  362. }).forEach(e -> listeners.remove(e.getKey(), e.getValue()));
  363. }
  364. }
  365. @Override
  366. public void registerIdentityListener(final String type, final ConfigProviderListener listener) {
  367. checkNotNull(listener);
  368. synchronized (listeners) {
  369. listeners.put(type, new WeakReference<>(listener));
  370. }
  371. }
  372. @Override
  373. public Collection<ConfigProvider> getProvidersByType(final String type) {
  374. return Collections.unmodifiableCollection(identities.get(type));
  375. }
  376. /**
  377. * Retrieves a list of all config sources that should be applied to the specified config
  378. * manager.
  379. *
  380. * @param manager The manager requesting sources
  381. *
  382. * @return A list of all matching config sources
  383. */
  384. List<ConfigFileBackedConfigProvider> getIdentitiesForManager(final ConfigManager manager) {
  385. final List<ConfigFileBackedConfigProvider> sources = new ArrayList<>();
  386. synchronized (identities) {
  387. sources.addAll(identities.get(null).stream()
  388. .filter(manager::identityApplies)
  389. .collect(Collectors.toList()));
  390. }
  391. Collections.sort(sources, new ConfigProviderTargetComparator());
  392. LOG.debug("Found {} source(s) for {}", sources.size(), manager);
  393. return sources;
  394. }
  395. @Override
  396. public synchronized AggregateConfigProvider getGlobalConfiguration() {
  397. if (globalconfig == null) {
  398. globalconfig = createAggregateConfig("", "", "", "");
  399. }
  400. return globalconfig;
  401. }
  402. @Override
  403. public ConfigProvider createChannelConfig(final String network, final String channel) {
  404. if (network == null || network.isEmpty()) {
  405. throw new IllegalArgumentException("getChannelConfig called "
  406. + "with null or empty network\n\nNetwork: " + network);
  407. }
  408. if (channel == null || channel.isEmpty()) {
  409. throw new IllegalArgumentException("getChannelConfig called "
  410. + "with null or empty channel\n\nChannel: " + channel);
  411. }
  412. final String myTarget = (channel + '@' + network).toLowerCase();
  413. synchronized (identities) {
  414. for (ConfigFileBackedConfigProvider identity : identities.get(null)) {
  415. if (identity.getTarget().getType() == ConfigTarget.TYPE.CHANNEL
  416. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  417. return identity;
  418. }
  419. }
  420. }
  421. // We need to create one
  422. final ConfigTarget target = new ConfigTarget();
  423. target.setChannel(myTarget);
  424. return createConfig(target);
  425. }
  426. @Override
  427. public ConfigProvider createNetworkConfig(final String network) {
  428. if (network == null || network.isEmpty()) {
  429. throw new IllegalArgumentException("getNetworkConfig called "
  430. + "with null or empty network\n\nNetwork:" + network);
  431. }
  432. final String myTarget = network.toLowerCase();
  433. synchronized (identities) {
  434. for (ConfigFileBackedConfigProvider identity : identities.get(null)) {
  435. if (identity.getTarget().getType() == ConfigTarget.TYPE.NETWORK
  436. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  437. return identity;
  438. }
  439. }
  440. }
  441. // We need to create one
  442. final ConfigTarget target = new ConfigTarget();
  443. target.setNetwork(myTarget);
  444. return createConfig(target);
  445. }
  446. @Override
  447. public ConfigProvider createServerConfig(final String server) {
  448. if (server == null || server.isEmpty()) {
  449. throw new IllegalArgumentException("getServerConfig called "
  450. + "with null or empty server\n\nServer: " + server);
  451. }
  452. final String myTarget = server.toLowerCase();
  453. synchronized (identities) {
  454. for (ConfigFileBackedConfigProvider identity : identities.get(null)) {
  455. if (identity.getTarget().getType() == ConfigTarget.TYPE.SERVER
  456. && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
  457. return identity;
  458. }
  459. }
  460. }
  461. // We need to create one
  462. final ConfigTarget target = new ConfigTarget();
  463. target.setServer(myTarget);
  464. return createConfig(target);
  465. }
  466. @Override
  467. public ConfigProvider createCustomConfig(final String name, final String type) {
  468. final Map<String, Map<String, String>> settings = new HashMap<>();
  469. settings.put(IDENTITY_DOMAIN, new HashMap<>(2));
  470. settings.get(IDENTITY_DOMAIN).put("name", name);
  471. settings.get(IDENTITY_DOMAIN).put("type", type);
  472. try {
  473. return createIdentity(settings);
  474. } catch (InvalidIdentityFileException | IOException ex) {
  475. LOG.warn(USER_ERROR, "Unable to create identity", ex);
  476. return null;
  477. }
  478. }
  479. @Override
  480. public ConfigProvider createProfileConfig(final String name) {
  481. final Map<String, Map<String, String>> settings = new HashMap<>();
  482. settings.put(IDENTITY_DOMAIN, new HashMap<>(1));
  483. settings.put(PROFILE_DOMAIN, new HashMap<>(2));
  484. final String nick = System.getProperty("user.name").replace(' ', '_');
  485. settings.get(IDENTITY_DOMAIN).put("name", name);
  486. settings.get(PROFILE_DOMAIN).put("nicknames", nick);
  487. settings.get(PROFILE_DOMAIN).put("realname", nick);
  488. try {
  489. return createIdentity(settings);
  490. } catch (InvalidIdentityFileException | IOException ex) {
  491. LOG.warn(USER_ERROR, "Unable to create identity", ex);
  492. return null;
  493. }
  494. }
  495. @Override
  496. public ConfigProvider createConfig(final ConfigTarget target) {
  497. final Map<String, Map<String, String>> settings = new HashMap<>();
  498. settings.put(IDENTITY_DOMAIN, new HashMap<>(2));
  499. settings.get(IDENTITY_DOMAIN).put("name", target.getData());
  500. settings.get(IDENTITY_DOMAIN).put(target.getTypeName(), target.getData());
  501. try {
  502. return createIdentity(settings);
  503. } catch (InvalidIdentityFileException | IOException ex) {
  504. LOG.warn(USER_ERROR, "Unable to create identity", ex);
  505. return null;
  506. }
  507. }
  508. /**
  509. * Creates a new identity containing the specified properties.
  510. *
  511. * @param settings The settings to populate the identity with
  512. *
  513. * @return A new identity containing the specified properties
  514. *
  515. * @throws IOException If the file cannot be created
  516. * @throws InvalidIdentityFileException If the settings are invalid
  517. * @since 0.6.3m1
  518. */
  519. protected ConfigFileBackedConfigProvider createIdentity(
  520. final Map<String, Map<String, String>> settings)
  521. throws IOException, InvalidIdentityFileException {
  522. if (!settings.containsKey(IDENTITY_DOMAIN)
  523. || !settings.get(IDENTITY_DOMAIN).containsKey("name")
  524. || settings.get(IDENTITY_DOMAIN).get("name").isEmpty()) {
  525. throw new InvalidIdentityFileException("identity.name is not set");
  526. }
  527. final String name = settings.get(IDENTITY_DOMAIN).get("name").replaceAll(ILLEGAL_CHARS, "_");
  528. Path file = identitiesDirectory.resolve(name);
  529. int attempt = 1;
  530. while (Files.exists(file)) {
  531. file = identitiesDirectory.resolve(name + '-' + attempt);
  532. attempt++;
  533. }
  534. final ConfigFile configFile = new ConfigFile(file);
  535. for (Map.Entry<String, Map<String, String>> entry : settings.entrySet()) {
  536. configFile.addDomain(entry.getKey(), entry.getValue());
  537. }
  538. configFile.write();
  539. final ConfigFileBackedConfigProvider identity = new ConfigFileBackedConfigProvider(this,
  540. file, false);
  541. addConfigProvider(identity);
  542. return identity;
  543. }
  544. /**
  545. * Finds and adds sources for the given manager, and adds it as an identity listener.
  546. *
  547. * @param configManager The manager to be initialised.
  548. */
  549. private void setUpConfigManager(final ConfigManager configManager) {
  550. final List<ConfigFileBackedConfigProvider> sources = getIdentitiesForManager(configManager);
  551. for (ConfigFileBackedConfigProvider identity : sources) {
  552. LOG.trace("Found {}", identity);
  553. configManager.checkIdentity(identity);
  554. }
  555. registerIdentityListener(configManager);
  556. }
  557. @Override
  558. public ConfigProviderMigrator createMigratableConfig(final String protocol,
  559. final String ircd, final String network, final String server) {
  560. final ConfigManager configManager = new ConfigManager(clientInfo, this, protocol,
  561. ircd, network, server);
  562. setUpConfigManager(configManager);
  563. return new ConfigManagerMigrator(configManager);
  564. }
  565. @Override
  566. public ConfigProviderMigrator createMigratableConfig(final String protocol,
  567. final String ircd, final String network, final String server, final String channel) {
  568. final ConfigManager configManager = new ConfigManager(clientInfo, this, protocol,
  569. ircd, network, server, channel);
  570. setUpConfigManager(configManager);
  571. return new ConfigManagerMigrator(configManager);
  572. }
  573. @Override
  574. public AggregateConfigProvider createAggregateConfig(final String protocol, final String ircd,
  575. final String network, final String server) {
  576. final ConfigManager configManager = new ConfigManager(clientInfo, this, protocol,
  577. ircd, network, server);
  578. setUpConfigManager(configManager);
  579. return configManager;
  580. }
  581. @Override
  582. public AggregateConfigProvider createAggregateConfig(final String protocol, final String ircd,
  583. final String network, final String server, final String channel) {
  584. final ConfigManager configManager = new ConfigManager(clientInfo, this, protocol,
  585. ircd, network, server, channel);
  586. setUpConfigManager(configManager);
  587. return configManager;
  588. }
  589. }