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.

ServerList.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.addons.serverlists;
  23. import com.dmdirc.Precondition;
  24. import com.dmdirc.config.Identity;
  25. import com.dmdirc.config.IdentityListener;
  26. import com.dmdirc.config.IdentityManager;
  27. import com.dmdirc.addons.serverlists.io.ServerGroupReader;
  28. import com.dmdirc.addons.serverlists.io.ServerGroupWriter;
  29. import com.dmdirc.addons.serverlists.service.ServerListServiceProvider;
  30. import java.io.IOException;
  31. import java.util.Collection;
  32. import java.util.Collections;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. /**
  36. * Maintains a list of top level {@link ServerGroup}s and handles reading and
  37. * writing of the lists to disk.
  38. *
  39. * @since 0.6.4
  40. * @author chris
  41. */
  42. public class ServerList implements IdentityListener {
  43. /** A list of all known groups. */
  44. private final Map<ServerGroup, ServerGroupWriter> groups
  45. = new HashMap<ServerGroup, ServerGroupWriter>();
  46. /**
  47. * Creates a new ServerList and loads groups and servers.
  48. */
  49. public ServerList() {
  50. IdentityManager.addIdentityListener("servergroup", this);
  51. for (Identity identity : IdentityManager.getCustomIdentities("servergroup")) {
  52. identityAdded(identity);
  53. }
  54. new ServerListServiceProvider(this).register();
  55. }
  56. /**
  57. * Adds a server group to the master server list.
  58. *
  59. * @param group The group to be added
  60. * @param writer The writer to use to write the group to disk
  61. */
  62. public void addServerGroup(final ServerGroup group, final ServerGroupWriter writer) {
  63. groups.put(group, writer);
  64. }
  65. /**
  66. * Adds a server group to the master server list, and creates a new
  67. * writer which will write the group to an identity.
  68. *
  69. * @param group The group to be added
  70. * @throws IOException if the new identity cannot be written
  71. */
  72. public void addServerGroup(final ServerGroup group) throws IOException {
  73. final ServerGroupWriter writer = new ServerGroupWriter(
  74. Identity.buildIdentity(group.getName(), "servergroup"));
  75. group.setModified(true);
  76. addServerGroup(group, writer);
  77. }
  78. /**
  79. * Saves all entries in this list.
  80. */
  81. public void save() {
  82. for (Map.Entry<ServerGroup, ServerGroupWriter> pair : groups.entrySet()) {
  83. if (pair.getKey().isModified()) {
  84. pair.getValue().write(pair.getKey());
  85. }
  86. }
  87. }
  88. /**
  89. * Saves the specified group.
  90. *
  91. * @param group The group to be saved
  92. */
  93. @Precondition("Specified group is a known top-level group in this list")
  94. public void save(final ServerGroup group) {
  95. groups.get(group).write(group);
  96. }
  97. /**
  98. * Retrieves a list of all known server groups.
  99. *
  100. * @return An immutable list of server groups.
  101. */
  102. public Collection<ServerGroup> getServerGroups() {
  103. return Collections.unmodifiableCollection(groups.keySet());
  104. }
  105. /**
  106. * Retrieves a ServerGroup with the specified name, if one exists. This
  107. * method ignores the case of group's name when comparing.
  108. *
  109. * @param name The name of the group to be retrieved
  110. * @return A correspondingly named server group, or null if none exists
  111. */
  112. public ServerGroup getGroupByName(final String name) {
  113. for (ServerGroup group : getServerGroups()) {
  114. if (group.getName().equalsIgnoreCase(name)) {
  115. return group;
  116. }
  117. }
  118. return null;
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. public void identityAdded(final Identity identity) {
  123. try {
  124. final ServerGroupReader reader = new ServerGroupReader(identity);
  125. addServerGroup(reader.read(), reader.getWriter());
  126. } catch (IllegalArgumentException ex) {
  127. // Silently ignore
  128. // TODO: Raise error if the identity isn't a server group being
  129. // currently added by addServerGroup()
  130. }
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public void identityRemoved(final Identity identity) {
  135. // TODO: Remove server group
  136. }
  137. }