Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ActionManager.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Copyright (c) 2006-2012 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.actions;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.Precondition;
  25. import com.dmdirc.actions.internal.WhoisNumericFormatter;
  26. import com.dmdirc.actions.wrappers.AliasWrapper;
  27. import com.dmdirc.actions.wrappers.PerformWrapper;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.interfaces.ActionController;
  30. import com.dmdirc.interfaces.ActionListener;
  31. import com.dmdirc.interfaces.ConfigChangeListener;
  32. import com.dmdirc.interfaces.IdentityController;
  33. import com.dmdirc.interfaces.actions.ActionComparison;
  34. import com.dmdirc.interfaces.actions.ActionComponent;
  35. import com.dmdirc.interfaces.actions.ActionType;
  36. import com.dmdirc.logger.ErrorLevel;
  37. import com.dmdirc.logger.Logger;
  38. import com.dmdirc.updater.components.ActionGroupComponent;
  39. import com.dmdirc.util.collections.MapList;
  40. import com.dmdirc.util.resourcemanager.ZipResourceManager;
  41. import java.io.File;
  42. import java.io.IOException;
  43. import java.util.ArrayList;
  44. import java.util.Collections;
  45. import java.util.HashMap;
  46. import java.util.List;
  47. import java.util.Map;
  48. import java.util.logging.Level;
  49. /**
  50. * Manages all actions for the client.
  51. */
  52. public class ActionManager implements ActionController {
  53. /** A singleton instance of the ActionManager. */
  54. private static final ActionManager INSTANCE
  55. = new ActionManager(IdentityManager.getIdentityManager());
  56. /** A logger for the action manager to use. */
  57. private static final java.util.logging.Logger LOGGER
  58. = java.util.logging.Logger.getLogger(ActionManager.class.getName());
  59. /** The identity manager to load configuration from. */
  60. private final IdentityController identityManager;
  61. /** A list of registered action types. */
  62. private final List<ActionType> types
  63. = new ArrayList<ActionType>();
  64. /** A list of registered action components. */
  65. private final List<ActionComponent> components
  66. = new ArrayList<ActionComponent>();
  67. /** A list of registered action comparisons. */
  68. private final List<ActionComparison> comparisons
  69. = new ArrayList<ActionComparison>();
  70. /** A map linking types and a list of actions that're registered for them. */
  71. private final MapList<ActionType, Action> actions
  72. = new MapList<ActionType, Action>();
  73. /** A map linking groups and a list of actions that're in them. */
  74. private final Map<String, ActionGroup> groups
  75. = new HashMap<String, ActionGroup>();
  76. /** A map of objects to synchronise on for concurrency groups. */
  77. private final Map<String, Object> locks
  78. = new HashMap<String, Object>();
  79. /** A map of the action type groups to the action types within. */
  80. private final MapList<String, ActionType> typeGroups
  81. = new MapList<String, ActionType>();
  82. /** The listeners that we have registered. */
  83. private final MapList<ActionType, ActionListener> listeners
  84. = new MapList<ActionType, ActionListener>();
  85. /** Indicates whether or not user actions should be killed (not processed). */
  86. private boolean killSwitch;
  87. /**
  88. * Creates a new instance of ActionManager.
  89. *
  90. * @param identityManager The IdentityManager to load configuration from.
  91. */
  92. public ActionManager(final IdentityController identityManager) {
  93. this.identityManager = identityManager;
  94. this.killSwitch = identityManager.getGlobalConfiguration().getOptionBool("actions", "killswitch");
  95. }
  96. /**
  97. * Returns a singleton instance of the Action Manager.
  98. *
  99. * @return A singleton ActionManager instance
  100. */
  101. public static ActionManager getActionManager() {
  102. return INSTANCE;
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public void initialise() {
  107. registerTypes(CoreActionType.values());
  108. registerComparisons(CoreActionComparison.values());
  109. registerComponents(CoreActionComponent.values());
  110. addGroup(AliasWrapper.getAliasWrapper());
  111. addGroup(PerformWrapper.getPerformWrapper());
  112. new WhoisNumericFormatter(identityManager.getGlobalAddonIdentity()).register();
  113. // Register a listener for the closing event, so we can save actions
  114. registerListener(new ActionListener() {
  115. /** {@inheritDoc} */
  116. @Override
  117. public void processEvent(final ActionType type, final StringBuffer format,
  118. final Object... arguments) {
  119. saveAllActions();
  120. }
  121. }, CoreActionType.CLIENT_CLOSED);
  122. // Make sure we listen for the killswitch
  123. identityManager.getGlobalConfiguration().addChangeListener("actions", "killswitch",
  124. new ConfigChangeListener() {
  125. /** {@inheritDoc} */
  126. @Override
  127. public void configChanged(final String domain, final String key) {
  128. killSwitch = identityManager.getGlobalConfiguration()
  129. .getOptionBool("actions", "killswitch");
  130. }
  131. });
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public void saveAllActions() {
  136. for (ActionGroup group : groups.values()) {
  137. for (Action action : group) {
  138. action.save();
  139. }
  140. }
  141. }
  142. /** {@inheritDoc} */
  143. @Override
  144. public void registerSetting(final String name, final String value) {
  145. identityManager.getGlobalAddonIdentity().setOption("actions", name, value);
  146. }
  147. /** {@inheritDoc} */
  148. @Override
  149. public void addGroup(final ActionGroup group) {
  150. groups.put(group.getName(), group);
  151. }
  152. /** {@inheritDoc} */
  153. @Override
  154. public void registerTypes(final ActionType[] newTypes) {
  155. for (ActionType type : newTypes) {
  156. Logger.assertTrue(type != null);
  157. if (!types.contains(type)) {
  158. LOGGER.log(Level.FINEST, "Registering action type: {0}", type);
  159. types.add(type);
  160. typeGroups.add(type.getType().getGroup(), type);
  161. }
  162. }
  163. }
  164. /** {@inheritDoc} */
  165. @Override
  166. public void registerComponents(final ActionComponent[] comps) {
  167. for (ActionComponent comp : comps) {
  168. Logger.assertTrue(comp != null);
  169. LOGGER.log(Level.FINEST, "Registering action component: {0}", comp);
  170. components.add(comp);
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public void registerComparisons(final ActionComparison[] comps) {
  176. for (ActionComparison comp : comps) {
  177. Logger.assertTrue(comp != null);
  178. LOGGER.log(Level.FINEST, "Registering action comparison: {0}", comp);
  179. comparisons.add(comp);
  180. }
  181. }
  182. /** {@inheritDoc} */
  183. @Override
  184. public Map<String, ActionGroup> getGroupsMap() {
  185. return Collections.unmodifiableMap(groups);
  186. }
  187. /** {@inheritDoc} */
  188. @Override
  189. public MapList<String, ActionType> getGroupedTypes() {
  190. return new MapList<String, ActionType>(typeGroups);
  191. }
  192. /** {@inheritDoc} */
  193. @Override
  194. public void loadUserActions() {
  195. actions.clear();
  196. for (ActionGroup group : groups.values()) {
  197. group.clear();
  198. }
  199. final File dir = new File(getDirectory());
  200. if (!dir.exists()) {
  201. try {
  202. dir.mkdirs();
  203. dir.createNewFile();
  204. } catch (IOException ex) {
  205. Logger.userError(ErrorLevel.HIGH, "I/O error when creating actions directory: "
  206. + ex.getMessage());
  207. }
  208. }
  209. if (dir.listFiles() == null) {
  210. Logger.userError(ErrorLevel.MEDIUM, "Unable to load user action files");
  211. } else {
  212. for (File file : dir.listFiles()) {
  213. if (file.isDirectory()) {
  214. loadActions(file);
  215. }
  216. }
  217. }
  218. registerComponents();
  219. }
  220. /**
  221. * Creates new ActionGroupComponents for each action group.
  222. */
  223. private void registerComponents() {
  224. for (ActionGroup group : groups.values()) {
  225. new ActionGroupComponent(group);
  226. }
  227. }
  228. /**
  229. * Loads action files from a specified group directory.
  230. *
  231. * @param dir The directory to scan.
  232. */
  233. @Precondition("The specified File is not null and represents a directory")
  234. private void loadActions(final File dir) {
  235. Logger.assertTrue(dir != null);
  236. Logger.assertTrue(dir.isDirectory());
  237. LOGGER.log(Level.FINER, "Loading actions from directory: {0}", dir.getAbsolutePath());
  238. if (!groups.containsKey(dir.getName())) {
  239. groups.put(dir.getName(), new ActionGroup(dir.getName()));
  240. }
  241. for (File file : dir.listFiles()) {
  242. new Action(dir.getName(), file.getName());
  243. }
  244. }
  245. /** {@inheritDoc} */
  246. @Override
  247. public void addAction(final Action action) {
  248. Logger.assertTrue(action != null);
  249. LOGGER.log(Level.FINE, "Registering action: {0}/{1} (status: {2})",
  250. new Object[] { action.getGroup(), action.getName(), action.getStatus() });
  251. if (action.getStatus() != ActionStatus.FAILED) {
  252. for (ActionType trigger : action.getTriggers()) {
  253. LOGGER.log(Level.FINER, "Action has trigger {0}", trigger);
  254. actions.add(trigger, action);
  255. }
  256. }
  257. getOrCreateGroup(action.getGroup()).add(action);
  258. }
  259. /** {@inheritDoc} */
  260. @Override
  261. public ActionGroup getOrCreateGroup(final String name) {
  262. if (!groups.containsKey(name)) {
  263. groups.put(name, new ActionGroup(name));
  264. }
  265. return groups.get(name);
  266. }
  267. /** {@inheritDoc} */
  268. @Override
  269. public void removeAction(final Action action) {
  270. Logger.assertTrue(action != null);
  271. actions.removeFromAll(action);
  272. getOrCreateGroup(action.getGroup()).remove(action);
  273. }
  274. /** {@inheritDoc} */
  275. @Override
  276. public void reregisterAction(final Action action) {
  277. removeAction(action);
  278. addAction(action);
  279. }
  280. /** {@inheritDoc} */
  281. @Override
  282. public boolean triggerEvent(final ActionType type,
  283. final StringBuffer format, final Object ... arguments) {
  284. Logger.assertTrue(type != null);
  285. Logger.assertTrue(type.getType() != null);
  286. Logger.assertTrue(type.getType().getArity() == arguments.length);
  287. boolean res = false;
  288. if (listeners.containsKey(type)) {
  289. for (ActionListener listener
  290. : new ArrayList<ActionListener>(listeners.get(type))) {
  291. try {
  292. listener.processEvent(type, format, arguments);
  293. } catch (Exception e) {
  294. Logger.appError(ErrorLevel.MEDIUM, "Error processing action: "
  295. + e.getMessage(), e);
  296. }
  297. }
  298. }
  299. if (!killSwitch) {
  300. res |= triggerActions(type, format, arguments);
  301. }
  302. return !res;
  303. }
  304. /**
  305. * Triggers actions that respond to the specified type.
  306. *
  307. * @param type The type of the event to process
  308. * @param format The format of the message that's going to be displayed for
  309. * the event. Actions may change this format.
  310. * @param arguments The arguments for the event
  311. * @return True if the event should be skipped, or false if it can continue
  312. */
  313. @Precondition("The specified ActionType is not null")
  314. private boolean triggerActions(final ActionType type,
  315. final StringBuffer format, final Object ... arguments) {
  316. Logger.assertTrue(type != null);
  317. boolean res = false;
  318. if (actions.containsKey(type)) {
  319. for (Action action : new ArrayList<Action>(actions.get(type))) {
  320. try {
  321. if (action.getConcurrencyGroup() == null) {
  322. res |= action.trigger(format, arguments);
  323. } else {
  324. synchronized (locks) {
  325. if (!locks.containsKey(action.getConcurrencyGroup())) {
  326. locks.put(action.getConcurrencyGroup(), new Object());
  327. }
  328. }
  329. synchronized (locks.get(action.getConcurrencyGroup())) {
  330. res |= action.trigger(format, arguments);
  331. }
  332. }
  333. } catch (LinkageError e) {
  334. Logger.appError(ErrorLevel.MEDIUM, "Error processing action: "
  335. + e.getMessage(), e);
  336. } catch (Exception e) {
  337. Logger.appError(ErrorLevel.MEDIUM, "Error processing action: "
  338. + e.getMessage(), e);
  339. }
  340. }
  341. }
  342. return res;
  343. }
  344. /**
  345. * Returns the directory that should be used to store actions.
  346. *
  347. * @return The directory that should be used to store actions
  348. */
  349. public static String getDirectory() {
  350. return Main.getConfigDir() + "actions" + System.getProperty("file.separator");
  351. }
  352. /** {@inheritDoc} */
  353. @Override
  354. public ActionGroup createGroup(final String group) {
  355. Logger.assertTrue(group != null);
  356. Logger.assertTrue(!group.isEmpty());
  357. Logger.assertTrue(!groups.containsKey(group));
  358. final File file = new File(getDirectory() + group);
  359. if (file.isDirectory() || file.mkdir()) {
  360. final ActionGroup actionGroup = new ActionGroup(group);
  361. groups.put(group, actionGroup);
  362. return actionGroup;
  363. } else {
  364. throw new IllegalArgumentException("Unable to create action group directory"
  365. + "\n\nDir: " + getDirectory() + group);
  366. }
  367. }
  368. /** {@inheritDoc} */
  369. @Override
  370. public void deleteGroup(final String group) {
  371. Logger.assertTrue(group != null);
  372. Logger.assertTrue(!group.isEmpty());
  373. Logger.assertTrue(groups.containsKey(group));
  374. for (Action action : groups.get(group).getActions()) {
  375. removeAction(action);
  376. }
  377. final File dir = new File(getDirectory() + group);
  378. if (dir.isDirectory()) {
  379. for (File file : dir.listFiles()) {
  380. if (!file.delete()) {
  381. Logger.userError(ErrorLevel.MEDIUM, "Unable to remove file: "
  382. + file.getAbsolutePath());
  383. return;
  384. }
  385. }
  386. }
  387. if (!dir.delete()) {
  388. Logger.userError(ErrorLevel.MEDIUM, "Unable to remove directory: "
  389. + dir.getAbsolutePath());
  390. return;
  391. }
  392. groups.remove(group);
  393. }
  394. /** {@inheritDoc} */
  395. @Override
  396. public void changeGroupName(final String oldName, final String newName) {
  397. Logger.assertTrue(oldName != null);
  398. Logger.assertTrue(!oldName.isEmpty());
  399. Logger.assertTrue(newName != null);
  400. Logger.assertTrue(!newName.isEmpty());
  401. Logger.assertTrue(groups.containsKey(oldName));
  402. Logger.assertTrue(!groups.containsKey(newName));
  403. Logger.assertTrue(!newName.equals(oldName));
  404. createGroup(newName);
  405. for (Action action : groups.get(oldName).getActions()) {
  406. action.setGroup(newName);
  407. getOrCreateGroup(oldName).remove(action);
  408. getOrCreateGroup(newName).add(action);
  409. }
  410. deleteGroup(oldName);
  411. }
  412. /** {@inheritDoc} */
  413. @Override
  414. public ActionType getType(final String type) {
  415. if (type == null || type.isEmpty()) {
  416. return null;
  417. }
  418. for (ActionType target : types) {
  419. if (target.name().equals(type)) {
  420. return target;
  421. }
  422. }
  423. return null;
  424. }
  425. /** {@inheritDoc} */
  426. @Override
  427. public List<ActionType> findCompatibleTypes(final ActionType type) {
  428. Logger.assertTrue(type != null);
  429. final List<ActionType> res = new ArrayList<ActionType>();
  430. for (ActionType target : types) {
  431. if (!target.equals(type) && target.getType().equals(type.getType())) {
  432. res.add(target);
  433. }
  434. }
  435. return res;
  436. }
  437. /** {@inheritDoc} */
  438. @Override
  439. public List<ActionComponent> findCompatibleComponents(final Class<?> target) {
  440. Logger.assertTrue(target != null);
  441. final List<ActionComponent> res = new ArrayList<ActionComponent>();
  442. for (ActionComponent subject : components) {
  443. if (subject.appliesTo().equals(target)) {
  444. res.add(subject);
  445. }
  446. }
  447. return res;
  448. }
  449. /** {@inheritDoc} */
  450. @Override
  451. public List<ActionComparison> findCompatibleComparisons(final Class<?> target) {
  452. Logger.assertTrue(target != null);
  453. final List<ActionComparison> res = new ArrayList<ActionComparison>();
  454. for (ActionComparison subject : comparisons) {
  455. if (subject.appliesTo().equals(target)) {
  456. res.add(subject);
  457. }
  458. }
  459. return res;
  460. }
  461. /** {@inheritDoc} */
  462. @Override
  463. public List<ActionType> getAllTypes() {
  464. return Collections.unmodifiableList(types);
  465. }
  466. /** {@inheritDoc} */
  467. @Override
  468. public List<ActionComparison> getAllComparisons() {
  469. return Collections.unmodifiableList(comparisons);
  470. }
  471. /** {@inheritDoc} */
  472. @Override
  473. public ActionComponent getComponent(final String type) {
  474. Logger.assertTrue(type != null);
  475. Logger.assertTrue(!type.isEmpty());
  476. for (ActionComponent target : components) {
  477. if (target.name().equals(type)) {
  478. return target;
  479. }
  480. }
  481. return null;
  482. }
  483. /** {@inheritDoc} */
  484. @Override
  485. public ActionComparison getComparison(final String type) {
  486. Logger.assertTrue(type != null);
  487. Logger.assertTrue(!type.isEmpty());
  488. for (ActionComparison target : comparisons) {
  489. if (target.name().equals(type)) {
  490. return target;
  491. }
  492. }
  493. return null;
  494. }
  495. /**
  496. * Installs an action pack located at the specified path.
  497. *
  498. * @param path The full path of the action pack .zip.
  499. * @throws IOException If the zip cannot be extracted
  500. */
  501. public static void installActionPack(final String path) throws IOException {
  502. final ZipResourceManager ziprm = ZipResourceManager.getInstance(path);
  503. ziprm.extractResources("", getDirectory());
  504. getActionManager().loadUserActions();
  505. new File(path).delete();
  506. }
  507. /** {@inheritDoc} */
  508. @Override
  509. public void registerListener(final ActionListener listener, final ActionType ... types) {
  510. for (ActionType type : types) {
  511. listeners.add(type, listener);
  512. }
  513. }
  514. /** {@inheritDoc} */
  515. @Override
  516. public void unregisterListener(final ActionListener listener, final ActionType ... types) {
  517. for (ActionType type : types) {
  518. listeners.remove(type, listener);
  519. }
  520. }
  521. /** {@inheritDoc} */
  522. @Override
  523. public void unregisterListener(final ActionListener listener) {
  524. listeners.removeFromAll(listener);
  525. }
  526. }