Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ConfigManager.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (c) 2006-2013 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.ConfigChangeListener;
  24. import com.dmdirc.util.collections.MapList;
  25. import com.dmdirc.util.validators.Validator;
  26. import java.util.ArrayList;
  27. import java.util.Collections;
  28. import java.util.HashMap;
  29. import java.util.HashSet;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Set;
  33. import java.util.TreeMap;
  34. import lombok.Getter;
  35. import lombok.extern.slf4j.Slf4j;
  36. /**
  37. * The config manager manages the various config sources for each entity.
  38. */
  39. @Slf4j
  40. @SuppressWarnings("PMD.UnusedPrivateField")
  41. public class ConfigManager extends ConfigSource implements ConfigChangeListener,
  42. IdentityListener {
  43. /** Temporary map for lookup stats. */
  44. private static final Map<String, Integer> STATS = new TreeMap<String, Integer>();
  45. /** Magical domain to redirect to the version identity. */
  46. private static final String VERSION_DOMAIN = "version";
  47. /** A list of sources for this config manager. */
  48. private final List<Identity> sources;
  49. /** The listeners registered for this manager. */
  50. private final MapList<String, ConfigChangeListener> listeners
  51. = new MapList<String, ConfigChangeListener>();
  52. /** The config binder to use for this manager. */
  53. @Getter
  54. private final ConfigBinder binder = new ConfigBinder(this);
  55. /** The protocol this manager is for. */
  56. private String protocol;
  57. /** The ircd this manager is for. */
  58. private String ircd;
  59. /** The network this manager is for. */
  60. private String network;
  61. /** The server this manager is for. */
  62. private String server;
  63. /** The channel this manager is for. */
  64. private String channel;
  65. /**
  66. * Creates a new instance of ConfigManager.
  67. *
  68. * @param protocol The protocol for this manager
  69. * @param ircd The name of the ircd for this manager
  70. * @param network The name of the network for this manager
  71. * @param server The name of the server for this manager
  72. * @since 0.6.3
  73. */
  74. public ConfigManager(final String protocol, final String ircd,
  75. final String network, final String server) {
  76. this(protocol, ircd, network, server, "<Unknown>");
  77. }
  78. /**
  79. * Creates a new instance of ConfigManager.
  80. *
  81. * @param protocol The protocol for this manager
  82. * @param ircd The name of the ircd for this manager
  83. * @param network The name of the network for this manager
  84. * @param server The name of the server for this manager
  85. * @param channel The name of the channel for this manager
  86. * @since 0.6.3
  87. */
  88. public ConfigManager(final String protocol, final String ircd,
  89. final String network, final String server, final String channel) {
  90. final String chanName = channel + "@" + network;
  91. this.protocol = protocol;
  92. this.ircd = ircd;
  93. this.network = network;
  94. this.server = server;
  95. this.channel = chanName;
  96. sources = IdentityManager.getIdentityManager().getIdentitiesForManager(this);
  97. log.debug("Found {} source(s) for protocol: {}, ircd: {}, network: {}, server: {}, channel: {}",
  98. new Object[] { sources.size(), protocol, ircd, network, server, chanName });
  99. for (Identity identity : sources) {
  100. log.trace("Found {}", identity);
  101. identity.addListener(this);
  102. }
  103. IdentityManager.getIdentityManager().registerIdentityListener(this);
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public String getOption(final String domain, final String option,
  108. final Validator<String> validator) {
  109. doStats(domain, option);
  110. if (VERSION_DOMAIN.equals(domain)) {
  111. return IdentityManager.getIdentityManager()
  112. .getGlobalVersionIdentity().getOption(domain, option, validator);
  113. }
  114. synchronized (sources) {
  115. for (Identity source : sources) {
  116. if (source.hasOption(domain, option, validator)) {
  117. return source.getOption(domain, option, validator);
  118. }
  119. }
  120. }
  121. return null;
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. protected boolean hasOption(final String domain, final String option,
  126. final Validator<String> validator) {
  127. doStats(domain, option);
  128. if (VERSION_DOMAIN.equals(domain)) {
  129. return IdentityManager.getIdentityManager()
  130. .getGlobalVersionIdentity().hasOption(domain, option, validator);
  131. }
  132. synchronized (sources) {
  133. for (Identity source : sources) {
  134. if (source.hasOption(domain, option, validator)) {
  135. return true;
  136. }
  137. }
  138. }
  139. return false;
  140. }
  141. /**
  142. * Returns the name of all the options in the specified domain. If the
  143. * domain doesn't exist, an empty list is returned.
  144. *
  145. * @param domain The domain to search
  146. * @return A list of options in the specified domain
  147. */
  148. public Map<String, String> getOptions(final String domain) {
  149. if (VERSION_DOMAIN.equals(domain)) {
  150. return IdentityManager.getIdentityManager()
  151. .getGlobalVersionIdentity().getOptions(domain);
  152. }
  153. final Map<String, String> res = new HashMap<String, String>();
  154. synchronized (sources) {
  155. for (int i = sources.size() - 1; i >= 0; i--) {
  156. res.putAll(sources.get(i).getOptions(domain));
  157. }
  158. }
  159. return res;
  160. }
  161. /**
  162. * Removes the specified identity from this manager.
  163. *
  164. * @param identity The identity to be removed
  165. */
  166. public void removeIdentity(final Identity identity) {
  167. if (!sources.contains(identity)) {
  168. return;
  169. }
  170. final List<String[]> changed = new ArrayList<String[]>();
  171. // Determine which settings will have changed
  172. for (String domain : identity.getDomains()) {
  173. for (String option : identity.getOptions(domain).keySet()) {
  174. if (identity.equals(getScope(domain, option))) {
  175. changed.add(new String[]{domain, option});
  176. }
  177. }
  178. }
  179. synchronized (sources) {
  180. identity.removeListener(this);
  181. sources.remove(identity);
  182. }
  183. // Fire change listeners
  184. for (String[] setting : changed) {
  185. configChanged(setting[0], setting[1]);
  186. }
  187. }
  188. /**
  189. * Retrieves the identity that currently defines the specified domain and
  190. * option.
  191. *
  192. * @param domain The domain to search for
  193. * @param option The option to search for
  194. * @return The identity that defines that setting, or null on failure
  195. */
  196. protected Identity getScope(final String domain, final String option) {
  197. if (VERSION_DOMAIN.equals(domain)) {
  198. return IdentityManager.getIdentityManager()
  199. .getGlobalVersionIdentity();
  200. }
  201. synchronized (sources) {
  202. for (Identity source : sources) {
  203. if (source.hasOptionString(domain, option)) {
  204. return source;
  205. }
  206. }
  207. }
  208. return null;
  209. }
  210. /**
  211. * Checks whether the specified identity applies to this config manager.
  212. *
  213. * @param identity The identity to test
  214. * @return True if the identity applies, false otherwise
  215. */
  216. public boolean identityApplies(final Identity identity) {
  217. String comp;
  218. switch (identity.getTarget().getType()) {
  219. case PROTOCOL:
  220. comp = protocol;
  221. break;
  222. case IRCD:
  223. comp = ircd;
  224. break;
  225. case NETWORK:
  226. comp = network;
  227. break;
  228. case SERVER:
  229. comp = server;
  230. break;
  231. case CHANNEL:
  232. comp = channel;
  233. break;
  234. case CUSTOM:
  235. // We don't want custom identities
  236. comp = null;
  237. break;
  238. default:
  239. comp = "";
  240. break;
  241. }
  242. final boolean result = comp != null
  243. && identityTargetMatches(identity.getTarget().getData(), comp);
  244. log.trace("Checking if identity {} applies. Comparison: {}, target: {}, result: {}",
  245. new Object[] { identity, comp, identity.getTarget().getData(), result });
  246. return result;
  247. }
  248. /**
  249. * Determines whether the specified identity target matches the desired
  250. * target. If the desired target is prefixed with "re:", it is treated
  251. * as a regular expression; otherwise the strings are compared
  252. * lexigraphically to determine a match.
  253. *
  254. * @param desired The target string required by this config manager
  255. * @param actual The target string supplied by the identity
  256. * @return True if the identity should be applied, false otherwise
  257. * @since 0.6.3m2
  258. */
  259. protected boolean identityTargetMatches(final String actual, final String desired) {
  260. return actual.startsWith("re:") ? desired.matches(actual.substring(3))
  261. : actual.equalsIgnoreCase(desired);
  262. }
  263. /**
  264. * Called whenever there is a new identity available. Checks if the
  265. * identity is relevant for this manager, and adds it if it is.
  266. *
  267. * @param identity The identity to be checked
  268. */
  269. public void checkIdentity(final Identity identity) {
  270. if (!sources.contains(identity) && identityApplies(identity)) {
  271. synchronized (sources) {
  272. sources.add(identity);
  273. identity.addListener(this);
  274. Collections.sort(sources);
  275. }
  276. // Determine which settings will have changed
  277. for (String domain : identity.getDomains()) {
  278. for (String option : identity.getOptions(domain).keySet()) {
  279. if (identity.equals(getScope(domain, option))) {
  280. configChanged(domain, option);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * Returns the name of all domains known by this manager.
  288. *
  289. * @return A list of domains known to this manager
  290. */
  291. public Set<String> getDomains() {
  292. final Set<String> res = new HashSet<String>();
  293. synchronized (sources) {
  294. for (Identity source : sources) {
  295. res.addAll(source.getDomains());
  296. }
  297. }
  298. return res;
  299. }
  300. /**
  301. * Retrieves a list of sources for this config manager.
  302. * @return This config manager's sources.
  303. */
  304. public List<Identity> getSources() {
  305. return new ArrayList<Identity>(sources);
  306. }
  307. /**
  308. * Migrates this ConfigManager from its current configuration to the
  309. * appropriate one for the specified new parameters, firing listeners where
  310. * settings have changed.
  311. *
  312. * @param protocol The protocol for this manager
  313. * @param ircd The new name of the ircd for this manager
  314. * @param network The new name of the network for this manager
  315. * @param server The new name of the server for this manager
  316. * @since 0.6.3
  317. */
  318. public void migrate(final String protocol, final String ircd,
  319. final String network, final String server) {
  320. migrate(protocol, ircd, network, server, "<Unknown>");
  321. }
  322. /**
  323. * Migrates this ConfigManager from its current configuration to the
  324. * appropriate one for the specified new parameters, firing listeners where
  325. * settings have changed.
  326. *
  327. * @param protocol The protocol for this manager
  328. * @param ircd The new name of the ircd for this manager
  329. * @param network The new name of the network for this manager
  330. * @param server The new name of the server for this manager
  331. * @param channel The new name of the channel for this manager
  332. * @since 0.6.3
  333. */
  334. public void migrate(final String protocol, final String ircd,
  335. final String network, final String server, final String channel) {
  336. log.debug("Migrating from {{}, {}, {}, {}, {}} to {{}, {}, {}, {}, {}}",
  337. new Object[] {
  338. this.protocol, this.ircd, this.network, this.server, this.channel,
  339. protocol, ircd, network, server, channel,
  340. });
  341. this.protocol = protocol;
  342. this.ircd = ircd;
  343. this.network = network;
  344. this.server = server;
  345. this.channel = channel + "@" + network;
  346. for (Identity identity : new ArrayList<Identity>(sources)) {
  347. if (!identityApplies(identity)) {
  348. log.debug("Removing identity that no longer applies: {}", identity);
  349. removeIdentity(identity);
  350. }
  351. }
  352. final List<Identity> newSources = IdentityManager.getIdentityManager()
  353. .getIdentitiesForManager(this);
  354. for (Identity identity : newSources) {
  355. log.trace("Testing new identity: {}", identity);
  356. checkIdentity(identity);
  357. }
  358. log.debug("New identities: {}", sources);
  359. }
  360. /**
  361. * Records the lookup request for the specified domain & option.
  362. *
  363. * @param domain The domain that is being looked up
  364. * @param option The option that is being looked up
  365. */
  366. @SuppressWarnings("PMD.AvoidCatchingNPE")
  367. protected static void doStats(final String domain, final String option) {
  368. final String key = domain + "." + option;
  369. try {
  370. STATS.put(key, 1 + (STATS.containsKey(key) ? STATS.get(key) : 0));
  371. } catch (NullPointerException ex) {
  372. // JVM bugs ftl.
  373. }
  374. }
  375. /**
  376. * Retrieves the statistic map.
  377. *
  378. * @return A map of config options to lookup counts
  379. */
  380. public static Map<String, Integer> getStats() {
  381. return STATS;
  382. }
  383. /**
  384. * Adds a change listener for the specified domain.
  385. *
  386. * @param domain The domain to be monitored
  387. * @param listener The listener to register
  388. */
  389. public void addChangeListener(final String domain,
  390. final ConfigChangeListener listener) {
  391. addListener(domain, listener);
  392. }
  393. /**
  394. * Adds a change listener for the specified domain and key.
  395. *
  396. * @param domain The domain of the option
  397. * @param key The option to be monitored
  398. * @param listener The listener to register
  399. */
  400. public void addChangeListener(final String domain, final String key,
  401. final ConfigChangeListener listener) {
  402. addListener(domain + "." + key, listener);
  403. }
  404. /**
  405. * Removes the specified listener for all domains and options.
  406. *
  407. * @param listener The listener to be removed
  408. */
  409. public void removeListener(final ConfigChangeListener listener) {
  410. synchronized (listeners) {
  411. listeners.removeFromAll(listener);
  412. }
  413. }
  414. /**
  415. * Adds the specified listener to the internal map/list.
  416. *
  417. * @param key The key to use (domain or domain.key)
  418. * @param listener The listener to register
  419. */
  420. private void addListener(final String key,
  421. final ConfigChangeListener listener) {
  422. synchronized (listeners) {
  423. listeners.add(key, listener);
  424. }
  425. }
  426. /** {@inheritDoc} */
  427. @Override
  428. public void configChanged(final String domain, final String key) {
  429. final List<ConfigChangeListener> targets
  430. = new ArrayList<ConfigChangeListener>();
  431. if (listeners.containsKey(domain)) {
  432. targets.addAll(listeners.get(domain));
  433. }
  434. if (listeners.containsKey(domain + "." + key)) {
  435. targets.addAll(listeners.get(domain + "." + key));
  436. }
  437. for (ConfigChangeListener listener : targets) {
  438. listener.configChanged(domain, key);
  439. }
  440. }
  441. /** {@inheritDoc} */
  442. @Override
  443. public void identityAdded(final Identity identity) {
  444. checkIdentity(identity);
  445. }
  446. /** {@inheritDoc} */
  447. @Override
  448. public void identityRemoved(final Identity identity) {
  449. removeIdentity(identity);
  450. }
  451. }