Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ActionServerPerformMigrator.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.commandparser.auto;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  25. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  26. import com.dmdirc.events.AppErrorEvent;
  27. import com.dmdirc.interfaces.Migrator;
  28. import com.dmdirc.logger.ErrorLevel;
  29. import com.dmdirc.util.io.ConfigFile;
  30. import com.dmdirc.util.io.InvalidConfigFileException;
  31. import com.google.common.base.Joiner;
  32. import java.util.Optional;
  33. import java.io.IOException;
  34. import java.nio.file.DirectoryStream;
  35. import java.nio.file.Files;
  36. import java.nio.file.Path;
  37. import java.util.Map;
  38. import javax.inject.Inject;
  39. import javax.inject.Singleton;
  40. /**
  41. * Migrates server/network-based action performs into auto commands.
  42. */
  43. @Singleton
  44. public class ActionServerPerformMigrator implements Migrator {
  45. private final Path directory;
  46. private final AutoCommandManager autoCommandManager;
  47. private final DMDircMBassador eventBus;
  48. @Inject
  49. public ActionServerPerformMigrator(
  50. @Directory(DirectoryType.ACTIONS) final Path directory,
  51. final AutoCommandManager autoCommandManager,
  52. final DMDircMBassador eventBus) {
  53. this.directory = directory.resolve("performs");
  54. this.autoCommandManager = autoCommandManager;
  55. this.eventBus = eventBus;
  56. }
  57. @Override
  58. public boolean needsMigration() {
  59. return Files.isDirectory(directory);
  60. }
  61. @Override
  62. public void migrate() {
  63. try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
  64. for (Path path : directoryStream) {
  65. if (migrate(path)) {
  66. Files.delete(path);
  67. }
  68. }
  69. Files.delete(directory);
  70. } catch (IOException ex) {
  71. eventBus.publish(new AppErrorEvent(ErrorLevel.MEDIUM, ex,
  72. "Unable to migrate performs", ex.getMessage()));
  73. }
  74. }
  75. /**
  76. * Migrates the specified file.
  77. *
  78. * @param file The file to be migrated.
  79. *
  80. * @return True if the file was migrated successfully, false otherwise.
  81. */
  82. private boolean migrate(final Path file) {
  83. try {
  84. final ConfigFile configFile = new ConfigFile(file);
  85. configFile.read();
  86. final String response = Joiner.on('\n').join(configFile.getFlatDomain("response"));
  87. final Optional<String> server = getCondition(configFile, "SERVER_NAME");
  88. final Optional<String> network = getCondition(configFile, "SERVER_NETWORK");
  89. final Optional<String> profile = getCondition(configFile,
  90. "SERVER_PROFILE.IDENTITY_NAME");
  91. autoCommandManager.addAutoCommand(
  92. AutoCommand.create(server, network, profile, response));
  93. return true;
  94. } catch (IOException | InvalidConfigFileException | NumberFormatException ex) {
  95. return false;
  96. }
  97. }
  98. private Optional<String> getCondition(final ConfigFile configFile, final String component) {
  99. for (Map.Entry<String, Map<String, String>> section : configFile.getKeyDomains().entrySet()) {
  100. if (section.getKey().startsWith("condition")
  101. && component.equals(section.getValue().get("component"))) {
  102. return Optional.ofNullable(section.getValue().get("target"));
  103. }
  104. }
  105. return Optional.empty();
  106. }
  107. }