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.

ConfigManager.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright (c) 2006-2011 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. package com.dmdirc.config;
  23. import com.dmdirc.interfaces.ConfigChangeListener;
  24. import com.dmdirc.util.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 java.util.logging.Level;
  35. /**
  36. * The config manager manages the various config sources for each entity.
  37. */
  38. public class ConfigManager extends ConfigSource implements ConfigChangeListener,
  39. IdentityListener {
  40. /** Temporary map for lookup stats. */
  41. private static final Map<String, Integer> STATS = new TreeMap<String, Integer>();
  42. /** Magical domain to redirect to the version identity. */
  43. private static final String VERSION_DOMAIN = "version";
  44. /** A logger for this class. */
  45. private static final java.util.logging.Logger LOGGER = java.util.logging
  46. .Logger.getLogger(ConfigManager.class.getName());
  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 protocol this manager is for. */
  53. private String protocol;
  54. /** The ircd this manager is for. */
  55. private String ircd;
  56. /** The network this manager is for. */
  57. private String network;
  58. /** The server this manager is for. */
  59. private String server;
  60. /** The channel this manager is for. */
  61. private String channel;
  62. /**
  63. * Creates a new instance of ConfigManager.
  64. *
  65. * @param protocol The protocol for this manager
  66. * @param ircd The name of the ircd for this manager
  67. * @param network The name of the network for this manager
  68. * @param server The name of the server for this manager
  69. * @since 0.6.3
  70. */
  71. public ConfigManager(final String protocol, final String ircd,
  72. final String network, final String server) {
  73. this(protocol, ircd, network, server, "<Unknown>");
  74. }
  75. /**
  76. * Creates a new instance of ConfigManager.
  77. *
  78. * @param protocol The protocol for this manager
  79. * @param ircd The name of the ircd for this manager
  80. * @param network The name of the network for this manager
  81. * @param server The name of the server for this manager
  82. * @param channel The name of the channel for this manager
  83. * @since 0.6.3
  84. */
  85. public ConfigManager(final String protocol, final String ircd,
  86. final String network, final String server, final String channel) {
  87. final String chanName = channel + "@" + network;
  88. this.protocol = protocol;
  89. this.ircd = ircd;
  90. this.network = network;
  91. this.server = server;
  92. this.channel = chanName;
  93. sources = IdentityManager.getSources(this);
  94. if (LOGGER.isLoggable(Level.FINE)) {
  95. LOGGER.fine("Found " + sources.size() + " source(s) for: "
  96. + this.protocol + ", " + this.ircd + ", " + this.network
  97. + ", " + this.server + ", " + this.channel);
  98. }
  99. for (Identity identity : sources) {
  100. identity.addListener(this);
  101. }
  102. IdentityManager.addIdentityListener(this);
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public String getOption(final String domain, final String option,
  107. final Validator<String> validator) {
  108. doStats(domain, option);
  109. if (VERSION_DOMAIN.equals(domain)) {
  110. return IdentityManager.getVersionIdentity().getOption(domain, option, validator);
  111. }
  112. synchronized (sources) {
  113. for (Identity source : sources) {
  114. if (source.hasOption(domain, option, validator)) {
  115. return source.getOption(domain, option, validator);
  116. }
  117. }
  118. }
  119. return null;
  120. }
  121. /** {@inheritDoc} */
  122. @Override
  123. protected boolean hasOption(final String domain, final String option,
  124. final Validator<String> validator) {
  125. doStats(domain, option);
  126. if (VERSION_DOMAIN.equals(domain)) {
  127. return IdentityManager.getVersionIdentity().hasOption(domain, option, validator);
  128. }
  129. synchronized (sources) {
  130. for (Identity source : sources) {
  131. if (source.hasOption(domain, option, validator)) {
  132. return true;
  133. }
  134. }
  135. }
  136. return false;
  137. }
  138. /**
  139. * Returns the name of all the options in the specified domain. If the
  140. * domain doesn't exist, an empty list is returned.
  141. *
  142. * @param domain The domain to search
  143. * @return A list of options in the specified domain
  144. */
  145. public Map<String, String> getOptions(final String domain) {
  146. if (VERSION_DOMAIN.equals(domain)) {
  147. return IdentityManager.getVersionIdentity().getOptions(domain);
  148. }
  149. final Map<String, String> res = new HashMap<String, String>();
  150. synchronized (sources) {
  151. for (int i = sources.size() - 1; i >= 0; i--) {
  152. res.putAll(sources.get(i).getOptions(domain));
  153. }
  154. }
  155. return res;
  156. }
  157. /**
  158. * Removes the specified identity from this manager.
  159. *
  160. * @param identity The identity to be removed
  161. */
  162. public void removeIdentity(final Identity identity) {
  163. if (!sources.contains(identity)) {
  164. return;
  165. }
  166. final List<String[]> changed = new ArrayList<String[]>();
  167. // Determine which settings will have changed
  168. for (String domain : identity.getDomains()) {
  169. for (String option : identity.getOptions(domain).keySet()) {
  170. if (identity.equals(getScope(domain, option))) {
  171. changed.add(new String[]{domain, option});
  172. }
  173. }
  174. }
  175. synchronized (sources) {
  176. identity.removeListener(this);
  177. sources.remove(identity);
  178. }
  179. // Fire change listeners
  180. for (String[] setting : changed) {
  181. configChanged(setting[0], setting[1]);
  182. }
  183. }
  184. /**
  185. * Retrieves the identity that currently defines the specified domain and
  186. * option.
  187. *
  188. * @param domain The domain to search for
  189. * @param option The option to search for
  190. * @return The identity that defines that setting, or null on failure
  191. */
  192. protected Identity getScope(final String domain, final String option) {
  193. if (VERSION_DOMAIN.equals(domain)) {
  194. return IdentityManager.getVersionIdentity();
  195. }
  196. synchronized (sources) {
  197. for (Identity source : sources) {
  198. if (source.hasOptionString(domain, option)) {
  199. return source;
  200. }
  201. }
  202. }
  203. return null;
  204. }
  205. /**
  206. * Checks whether the specified identity applies to this config manager.
  207. *
  208. * @param identity The identity to test
  209. * @return True if the identity applies, false otherwise
  210. */
  211. public boolean identityApplies(final Identity identity) {
  212. String comp;
  213. switch (identity.getTarget().getType()) {
  214. case PROTOCOL:
  215. comp = protocol;
  216. break;
  217. case IRCD:
  218. comp = ircd;
  219. break;
  220. case NETWORK:
  221. comp = network;
  222. break;
  223. case SERVER:
  224. comp = server;
  225. break;
  226. case CHANNEL:
  227. comp = channel;
  228. break;
  229. case CUSTOM:
  230. // We don't want custom identities
  231. comp = null;
  232. break;
  233. default:
  234. comp = "";
  235. break;
  236. }
  237. final boolean result = comp != null
  238. && identityTargetMatches(identity.getTarget().getData(), comp);
  239. if (LOGGER.isLoggable(Level.FINEST)) {
  240. LOGGER.finest("Comparison: " + comp + ", target: "
  241. + identity.getTarget().getData() + " (" + identity + "). Result: "
  242. + result);
  243. }
  244. return result;
  245. }
  246. /**
  247. * Determines whether the specified identity target matches the desired
  248. * target. If the desired target is prefixed with "re:", it is treated
  249. * as a regular expression; otherwise the strings are compared
  250. * lexigraphically to determine a match.
  251. *
  252. * @param desired The target string required by this config manager
  253. * @param actual The target string supplied by the identity
  254. * @return True if the identity should be applied, false otherwise
  255. * @since 0.6.3m2
  256. */
  257. protected boolean identityTargetMatches(final String actual, final String desired) {
  258. return actual.startsWith("re:") ? desired.matches(actual.substring(3))
  259. : actual.equalsIgnoreCase(desired);
  260. }
  261. /**
  262. * Called whenever there is a new identity available. Checks if the
  263. * identity is relevant for this manager, and adds it if it is.
  264. *
  265. * @param identity The identity to be checked
  266. */
  267. public void checkIdentity(final Identity identity) {
  268. if (!sources.contains(identity) && identityApplies(identity)) {
  269. synchronized (sources) {
  270. sources.add(identity);
  271. identity.addListener(this);
  272. Collections.sort(sources);
  273. }
  274. // Determine which settings will have changed
  275. for (String domain : identity.getDomains()) {
  276. for (String option : identity.getOptions(domain).keySet()) {
  277. if (identity.equals(getScope(domain, option))) {
  278. configChanged(domain, option);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. /**
  285. * Returns the name of all domains known by this manager.
  286. *
  287. * @return A list of domains known to this manager
  288. */
  289. public Set<String> getDomains() {
  290. final Set<String> res = new HashSet<String>();
  291. synchronized (sources) {
  292. for (Identity source : sources) {
  293. res.addAll(source.getDomains());
  294. }
  295. }
  296. return res;
  297. }
  298. /**
  299. * Retrieves a list of sources for this config manager.
  300. * @return This config manager's sources.
  301. */
  302. public List<Identity> getSources() {
  303. return new ArrayList<Identity>(sources);
  304. }
  305. /**
  306. * Migrates this ConfigManager from its current configuration to the
  307. * appropriate one for the specified new parameters, firing listeners where
  308. * settings have changed.
  309. *
  310. * @param protocol The protocol for this manager
  311. * @param ircd The new name of the ircd for this manager
  312. * @param network The new name of the network for this manager
  313. * @param server The new name of the server for this manager
  314. * @since 0.6.3
  315. */
  316. public void migrate(final String protocol, final String ircd,
  317. final String network, final String server) {
  318. migrate(protocol, ircd, network, server, "<Unknown>");
  319. }
  320. /**
  321. * Migrates this ConfigManager from its current configuration to the
  322. * appropriate one for the specified new parameters, firing listeners where
  323. * settings have changed.
  324. *
  325. * @param protocol The protocol for this manager
  326. * @param ircd The new name of the ircd for this manager
  327. * @param network The new name of the network for this manager
  328. * @param server The new name of the server for this manager
  329. * @param channel The new name of the channel for this manager
  330. * @since 0.6.3
  331. */
  332. public void migrate(final String protocol, final String ircd,
  333. final String network, final String server, final String channel) {
  334. if (LOGGER.isLoggable(Level.INFO)) {
  335. LOGGER.info("Migrating from " + this.protocol + ", " + this.ircd
  336. + ", " + this.network + ", " + this.server + ", "
  337. + this.channel + " to " + protocol + ", " + ircd + ", "
  338. + network + ", " + server + ", " + channel);
  339. }
  340. this.protocol = protocol;
  341. this.ircd = ircd;
  342. this.network = network;
  343. this.server = server;
  344. this.channel = channel + "@" + network;
  345. for (Identity identity : new ArrayList<Identity>(sources)) {
  346. if (!identityApplies(identity)) {
  347. LOGGER.fine("Removing identity that no longer applies: " + identity);
  348. removeIdentity(identity);
  349. }
  350. }
  351. final List<Identity> newSources = IdentityManager.getSources(this);
  352. for (Identity identity : newSources) {
  353. LOGGER.fine("Testing new identity: " + identity);
  354. checkIdentity(identity);
  355. }
  356. if (LOGGER.isLoggable(Level.INFO)) {
  357. LOGGER.info("New identities: " + sources);
  358. }
  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. }