Skip to content

Commit

Permalink
Move threads from PluginService
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Aug 21, 2014
1 parent 0f8cd8e commit 764d521
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 103 deletions.
106 changes: 3 additions & 103 deletions src/main/java/org/jboss/forge/plugin/idea/service/PluginService.java
Expand Up @@ -12,16 +12,14 @@
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.util.Commands;
import org.jboss.forge.furnace.util.Lists;
import org.jboss.forge.plugin.idea.service.callbacks.FormUpdateCallback;
import org.jboss.forge.plugin.idea.service.threads.CommandLoadingThread;
import org.jboss.forge.plugin.idea.service.threads.ValidationThread;
import org.jboss.forge.plugin.idea.util.CommandUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

/**
* Maintains plugin state.
Expand Down Expand Up @@ -112,7 +110,7 @@ public synchronized List<UICommand> getEnabledCommands(UIContext uiContext)
}
else
{
result = loadCommands(uiContext);
result = CommandUtil.getEnabledCommands(uiContext);
}

return result;
Expand Down Expand Up @@ -146,102 +144,4 @@ private boolean isCacheCommands()
{
return ForgeService.getInstance().getState().isCacheCommands();
}

private static List<UICommand> loadCommands(UIContext uiContext)
{
return Lists.toList(Commands.getEnabledCommands(CommandUtil.getAllCommands(), uiContext));
}

private static class ValidationThread extends Thread
{
private BlockingQueue<FormUpdateCallback> queue = new LinkedBlockingQueue<>();

public ValidationThread()
{
super("ForgeValidationThread");
}

public void setNextCallback(FormUpdateCallback nextCallback)
{
// Discard previous request for this component
for (FormUpdateCallback callback : queue)
{
if (callback.getInput() == nextCallback.getInput())
{
queue.remove(callback);
break;
}
}

queue.add(nextCallback);
}

@Override
public void run()
{
while (true)
{
try
{
queue.take().run();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}

private static class CommandLoadingThread extends Thread
{
private CountDownLatch latch = new CountDownLatch(1);
private volatile List<UICommand> commands;
private volatile UIContext uiContext;

public CommandLoadingThread()
{
super("ForgeCommandLoadingThread");
}

public void invalidate()
{
commands = null;
}

public void reload(UIContext uiContext)
{
this.uiContext = uiContext;
latch.countDown();
}

// TODO It could return a Future
public List<UICommand> getCommands(UIContext uiContext)
{
if (commands == null)
{
commands = loadCommands(uiContext);
}

return commands;
}

@Override
public void run()
{
while (true)
{
try
{
latch.await();
commands = loadCommands(uiContext);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

}
}
@@ -0,0 +1,82 @@
/*
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.plugin.idea.service.threads;

import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.plugin.idea.util.CommandUtil;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
* Asynchronously loads command list. <p/>
*
* Use {@link org.jboss.forge.plugin.idea.service.PluginService} to access command cache.
*
* @author Adam Wyłuda
*/
public class CommandLoadingThread extends Thread
{
private CountDownLatch latch = new CountDownLatch(1);
private volatile List<UICommand> commands;
private volatile UIContext uiContext;

public CommandLoadingThread()
{
super("ForgeCommandLoadingThread");
}

/**
* Invalidates stored command list.
*/
public void invalidate()
{
commands = null;
}

/**
* Makes (asynchronous) request to refresh a list of commands.
*/
public void reload(UIContext uiContext)
{
this.uiContext = uiContext;
latch.countDown();
}

/**
* Returns stored commands, if they are not yet available, it loads them synchronously.
*/
public List<UICommand> getCommands(UIContext uiContext)
{
if (commands == null)
{
commands = CommandUtil.getEnabledCommands(uiContext);
}

return commands;
}

@Override
public void run()
{
while (true)
{
try
{
// Wait for reload() call
latch.await();
commands = CommandUtil.getEnabledCommands(uiContext);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

}
@@ -0,0 +1,63 @@
/*
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.plugin.idea.service.threads;

import org.jboss.forge.plugin.idea.service.callbacks.FormUpdateCallback;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* Updates and validates Forge wizard forms. Ensures that many request are performed serially.
*
* @author Adam Wyłuda
* @see org.jboss.forge.plugin.idea.service.callbacks.FormUpdateCallback
*/
public class ValidationThread extends Thread
{
private BlockingQueue<FormUpdateCallback> queue = new LinkedBlockingQueue<>();

public ValidationThread()
{
super("ForgeValidationThread");
}

/**
* Appends form update request to the queue. If there is already request pending for the
* same component, it will be replaced with new callback.
*/
public void setNextCallback(FormUpdateCallback nextCallback)
{
// Discard previous request for this component
for (FormUpdateCallback callback : queue)
{
if (callback.getInput() == nextCallback.getInput())
{
queue.remove(callback);
break;
}
}

queue.add(nextCallback);
}

@Override
public void run()
{
while (true)
{
try
{
queue.take().run();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
Expand Up @@ -12,6 +12,7 @@
import org.jboss.forge.addon.ui.metadata.UICategory;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Commands;
import org.jboss.forge.furnace.util.Lists;
import org.jboss.forge.plugin.idea.service.ForgeService;

Expand Down Expand Up @@ -192,4 +193,9 @@ private static String categoryName(UICategory category)

return name.toString();
}

public static List<UICommand> getEnabledCommands(UIContext uiContext)
{
return Lists.toList(Commands.getEnabledCommands(getAllCommands(), uiContext));
}
}

0 comments on commit 764d521

Please sign in to comment.