123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.debug.commands;
-
- import com.dmdirc.addons.debug.Debug;
- import com.dmdirc.addons.debug.DebugCommand;
- import com.dmdirc.commandparser.CommandArguments;
- import com.dmdirc.commandparser.commands.context.CommandContext;
- import com.dmdirc.interfaces.WindowModel;
- import com.dmdirc.updater.UpdateComponent;
- import com.dmdirc.updater.Version;
- import com.dmdirc.updater.checking.BaseCheckResult;
- import com.dmdirc.updater.checking.UpdateCheckResult;
- import com.dmdirc.updater.checking.UpdateCheckStrategy;
- import com.dmdirc.updater.installing.TypeSensitiveInstallationStrategy;
- import com.dmdirc.updater.installing.UpdateInstallationListener;
- import com.dmdirc.updater.manager.UpdateManager;
- import com.dmdirc.updater.retrieving.BaseRetrievalResult;
- import com.dmdirc.updater.retrieving.TypeSensitiveRetrievalStrategy;
- import com.dmdirc.updater.retrieving.UpdateRetrievalListener;
- import com.dmdirc.updater.retrieving.UpdateRetrievalResult;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.annotation.Nonnull;
- import javax.inject.Inject;
- import javax.inject.Provider;
-
- /**
- * Generates some fake updates.
- */
- public class FakeUpdates extends DebugCommand {
-
- /** The update manager to add fake updates to. */
- private final UpdateManager updateManager;
-
- /**
- * Creates a new instance of the command.
- *
- * @param commandProvider The provider to use to access the main debug command.
- * @param updateManager The update manager to add fake updates to.
- */
- @Inject
- public FakeUpdates(
- final Provider<Debug> commandProvider,
- final UpdateManager updateManager) {
- super(commandProvider);
-
- this.updateManager = updateManager;
- }
-
- @Override
- public String getName() {
- return "fakeupdates";
- }
-
- @Override
- public String getUsage() {
- return " - Initialises a fake update handling chain";
- }
-
- @Override
- public void execute(@Nonnull final WindowModel origin,
- final CommandArguments args, final CommandContext context) {
- updateManager.addCheckStrategy(new FakeUpdateCheckStrategy());
- updateManager.addRetrievalStrategy(new FakeUpdateRetriever());
- updateManager.addInstallationStrategy(new FakeUpdateInstaller());
- }
-
- /**
- * A fake update check strategy which produces fake update check results.
- */
- private static class FakeUpdateCheckStrategy
- implements UpdateCheckStrategy {
-
- @Override
- public Map<UpdateComponent, UpdateCheckResult> checkForUpdates(
- final Collection<UpdateComponent> components) {
- final Map<UpdateComponent, UpdateCheckResult> res = new HashMap<>();
-
- components.stream().filter(component -> Math.random() * components.size() < 10)
- .forEach(component -> res.put(component, new FakeUpdateCheckResult(component)));
-
- return res;
- }
-
- }
-
- /**
- * A fake update check result.
- */
- private static class FakeUpdateCheckResult extends BaseCheckResult {
-
- /**
- * Creates a new fake result for the given component.
- *
- * @param component The component this result is for
- */
- public FakeUpdateCheckResult(final UpdateComponent component) {
- super(component, true, "shiny newness", new Version("1.337"));
- }
-
- }
-
- private static class FakeUpdateRetriever
- extends TypeSensitiveRetrievalStrategy<FakeUpdateCheckResult> {
-
- private final ListenerList listeners = new ListenerList();
-
- /**
- * Creates a new {@link FakeUpdateRetriever}.
- */
- public FakeUpdateRetriever() {
- super(FakeUpdateCheckResult.class);
- }
-
- protected void fireRetrievalProgressChanged(final UpdateComponent component,
- final double progress) {
- listeners.getCallable(UpdateRetrievalListener.class).retrievalProgressChanged(component,
- progress);
- }
-
- protected void fireRetrievalFailed(final UpdateComponent component) {
- listeners.getCallable(UpdateRetrievalListener.class).retrievalFailed(component);
- }
-
- protected void fireRetrievalCompleted(final UpdateComponent component) {
- listeners.getCallable(UpdateRetrievalListener.class).retrievalCompleted(component);
- }
-
- @Override
- public void addUpdateRetrievalListener(final UpdateRetrievalListener listener) {
- listeners.add(UpdateRetrievalListener.class, listener);
- }
-
- @Override
- public void removeUpdateRetrievalListener(final UpdateRetrievalListener listener) {
- listeners.remove(UpdateRetrievalListener.class, listener);
- }
-
- @Override
- protected UpdateRetrievalResult retrieveImpl(
- final FakeUpdateCheckResult checkResult) {
- try {
- fireRetrievalProgressChanged(checkResult.getComponent(), 0);
-
- final int max = (int) (Math.random() * 50);
- for (int i = 0; i < max; i++) {
- Thread.sleep(100);
-
- fireRetrievalProgressChanged(checkResult.getComponent(),
- 100 * i / max);
- }
-
- if (Math.random() < 0.75) {
- fireRetrievalCompleted(checkResult.getComponent());
- return new FakeRetrievalResult(checkResult, true);
- } else {
- fireRetrievalFailed(checkResult.getComponent());
- return new FakeRetrievalResult(checkResult, false);
- }
- } catch (InterruptedException ex) {
- // Don't care
- }
-
- return new FakeRetrievalResult(checkResult, false);
- }
-
- }
-
- /**
- * Fake retrieval result.
- */
- private static class FakeRetrievalResult extends BaseRetrievalResult {
-
- /**
- * Creates a new fake retrieval result.
- *
- * @param checkResult The check result that was retrieved
- * @param success Whether the result is successful or not
- */
- public FakeRetrievalResult(final UpdateCheckResult checkResult,
- final boolean success) {
- super(checkResult, success);
- }
-
- }
-
- /**
- * A fake update installer.
- */
- private static class FakeUpdateInstaller
- extends TypeSensitiveInstallationStrategy<UpdateComponent, FakeRetrievalResult> {
-
- private final ListenerList listeners = new ListenerList();
-
- /**
- * Creates a new {@link FakeUpdateInstaller}.
- */
- public FakeUpdateInstaller() {
- super(UpdateComponent.class, FakeRetrievalResult.class);
- }
-
- protected void fireInstallCompleted(final UpdateComponent component) {
- listeners.getCallable(UpdateInstallationListener.class).installCompleted(component);
- }
-
- protected void fireInstallFailed(final UpdateComponent component) {
- listeners.getCallable(UpdateInstallationListener.class).installFailed(component);
- }
-
- protected void fireInstallProgressChanged(final UpdateComponent component,
- final double progress) {
- listeners.getCallable(UpdateInstallationListener.class).
- installProgressChanged(component, progress);
- }
-
- @Override
- public void addUpdateInstallationListener(final UpdateInstallationListener listener) {
- listeners.add(UpdateInstallationListener.class, listener);
- }
-
- @Override
- public void removeUpdateInstallationListener(final UpdateInstallationListener listener) {
- listeners.remove(UpdateInstallationListener.class, listener);
- }
-
- @Override
- protected void installImpl(final UpdateComponent component,
- final FakeRetrievalResult retrievalResult) {
- try {
- fireInstallProgressChanged(component, 0);
- final int max = (int) (Math.random() * 50);
- for (int i = 0; i < max; i++) {
- Thread.sleep(100);
-
- fireInstallProgressChanged(component, 100 * i / max);
- }
-
- if (Math.random() < 0.75) {
- fireInstallCompleted(component);
- } else {
- fireInstallFailed(component);
- }
- } catch (InterruptedException ex) {
- // Don't care
- }
- }
-
- }
-
- }
|