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.

DMDircUpdateManager.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2006-2014 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.updater.manager;
  23. import com.dmdirc.updater.UpdateComponent;
  24. import com.dmdirc.updater.checking.CheckResultConsolidator;
  25. import com.dmdirc.updater.checking.UpdateCheckStrategy;
  26. import com.dmdirc.updater.installing.UpdateInstallationStrategy;
  27. import com.dmdirc.updater.retrieving.UpdateRetrievalStrategy;
  28. import java.util.Set;
  29. import java.util.concurrent.LinkedBlockingQueue;
  30. import java.util.concurrent.ThreadFactory;
  31. import java.util.concurrent.ThreadPoolExecutor;
  32. import java.util.concurrent.TimeUnit;
  33. import javax.inject.Inject;
  34. import javax.inject.Singleton;
  35. /**
  36. * Specialised update manager for DMDirc.
  37. */
  38. @Singleton
  39. public class DMDircUpdateManager extends CachingUpdateManagerImpl {
  40. /** Number of threads to use. */
  41. private static final int THREAD_COUNT = 3;
  42. /**
  43. * Creates a new instance of the update manager.
  44. *
  45. * @param updatePolicy The policy to use to decide if components should be updated.
  46. * @param checkStrategies The strategies to use to actually perform checks.
  47. * @param consolidator The consolidator to use to pick from multiple available
  48. * updates.
  49. * @param retrievalStrategies The strategies to use to retrieve updates.
  50. * @param installationStrategies The strategies to use to install updates.
  51. * @param components The default components to add to the manager.
  52. */
  53. @Inject
  54. public DMDircUpdateManager(
  55. final UpdateComponentPolicy updatePolicy,
  56. final Set<UpdateCheckStrategy> checkStrategies,
  57. final CheckResultConsolidator consolidator,
  58. final Set<UpdateRetrievalStrategy> retrievalStrategies,
  59. final Set<UpdateInstallationStrategy> installationStrategies,
  60. final Set<UpdateComponent> components) {
  61. super(new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, 1, TimeUnit.MINUTES,
  62. new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory()),
  63. consolidator, updatePolicy);
  64. for (UpdateCheckStrategy checkStrategy : checkStrategies) {
  65. addCheckStrategy(checkStrategy);
  66. }
  67. for (UpdateRetrievalStrategy retrievalStrategy : retrievalStrategies) {
  68. addRetrievalStrategy(retrievalStrategy);
  69. }
  70. for (UpdateInstallationStrategy installationStrategy : installationStrategies) {
  71. addInstallationStrategy(installationStrategy);
  72. }
  73. for (UpdateComponent component : components) {
  74. addComponent(component);
  75. }
  76. }
  77. /**
  78. * Thread factory which returns named threads.
  79. */
  80. private static class NamedThreadFactory implements ThreadFactory {
  81. /** {@inheritDoc} */
  82. @Override
  83. public Thread newThread(final Runnable r) {
  84. return new Thread(r, "Updater thread");
  85. }
  86. }
  87. }