Skip to content

Commit

Permalink
JBIDE-16481: Starting F2 twice is now possible
Browse files Browse the repository at this point in the history
  • Loading branch information
koentsje committed Feb 11, 2014
1 parent ba78ddb commit 3e83f87
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void restoreCursor() {
moveCursorTo(savedCursor);
}

void reset() {
public void reset() {
set("");
moveCursorTo(0);
currentStyleRange = getDefaultStyleRange();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,159 +1,26 @@
package org.jboss.tools.forge.ext.core;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.Callable;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.proxy.ClassLoaderAdapterCallback;
import org.jboss.forge.furnace.repositories.AddonRepository;
import org.jboss.forge.furnace.repositories.AddonRepositoryMode;
import org.jboss.forge.furnace.util.ClassLoaders;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.wiring.BundleWiring;

import bootpath.BootpathMarker;

public class ForgeCorePlugin extends Plugin {

private static final String RUNTIME_PLUGIN_ID = "org.jboss.tools.forge2.runtime";

public static final String PLUGIN_ID = "org.jboss.tools.forge.core.ext";

private static ForgeCorePlugin plugin;

private URLClassLoader loader;
private Furnace furnace;

@Override
public void start(final BundleContext context) throws Exception {
super.start(context);
furnace = getFurnace(context);
FurnaceService.INSTANCE.setFurnace(furnace);
plugin = this;
}

public void startFurnace() {
if (!isFurnaceStarted()) {
FurnaceService.INSTANCE.start(loader);
}
}

public boolean isFurnaceStarted() {
return FurnaceService.INSTANCE.getContainerStatus().isStarted();
}

public void stopFurnace() {
if (isFurnaceStarted()) {
FurnaceService.INSTANCE.stop();
}
}

private Furnace getFurnace(final BundleContext context) throws Exception {
Furnace forge = ClassLoaders.executeIn(loader, new Callable<Furnace>() {
@Override
public Furnace call() throws Exception {
BundleWiring wiring = context.getBundle().adapt(
BundleWiring.class);
Collection<String> entries = wiring.listResources("bootpath",
"*.jar", BundleWiring.LISTRESOURCES_RECURSE);
Collection<URL> resources = new HashSet<URL>();
File jarDir = File.createTempFile("forge", "jars");
if (entries != null)
for (String resource : entries) {
URL jar = BootpathMarker.class.getResource("/"
+ resource);
if (jar != null) {
resources.add(copy(jarDir, resource,
jar.openStream()));
}
}

loader = new URLClassLoader(resources.toArray(new URL[resources
.size()]), null);

Class<?> bootstrapType = loader
.loadClass("org.jboss.forge.furnace.impl.FurnaceImpl");

Object nativeForge = bootstrapType.newInstance();
Furnace furnace = (Furnace) ClassLoaderAdapterCallback.enhance(
Furnace.class.getClassLoader(), loader, nativeForge,
Furnace.class);
setupRepositories(furnace);
return furnace;
}
});
return forge;
}

/**
* Adds the addon-repository folder inside the runtime plugin as an
* {@link AddonRepository}
*/
private void setupRepositories(final Furnace furnace) throws IOException {
Bundle runtimeBundle = Platform.getBundle(RUNTIME_PLUGIN_ID);
File bundleFile = FileLocator.getBundleFile(runtimeBundle);
furnace.addRepository(AddonRepositoryMode.IMMUTABLE, new File(
bundleFile, "addon-repository"));
furnace.addRepository(AddonRepositoryMode.MUTABLE, new File(
ForgeExtPreferences.INSTANCE.getAddonDir()));
}

private URL copy(File directory, String name, InputStream input)
throws IOException {
File outputFile = new File(directory, name);

FileOutputStream output = null;
try {
directory.delete();
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();

output = new FileOutputStream(outputFile);
final byte[] buffer = new byte[4096];
int read = 0;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} catch (Exception e) {
throw new RuntimeException("Could not write out jar file " + name,
e);
} finally {
close(input);
close(output);
}
return outputFile.toURI().toURL();
}

private void close(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Exception e) {
throw new RuntimeException("Could not close stream", e);
}
}

@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
stopFurnace();
}

public static ForgeCorePlugin getDefault() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.jboss.tools.forge.ext.core;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.Callable;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.proxy.ClassLoaderAdapterCallback;
import org.jboss.forge.furnace.repositories.AddonRepositoryMode;
import org.jboss.forge.furnace.util.ClassLoaders;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleWiring;

import bootpath.BootpathMarker;

public class FurnaceProvider {

public static final FurnaceProvider INSTANCE = new FurnaceProvider();

private static final String RUNTIME_PLUGIN_ID = "org.jboss.tools.forge2.runtime";

private URLClassLoader loader;

private FurnaceProvider() {}

public Furnace createFurnace() throws Exception {

Furnace forge = ClassLoaders.executeIn(loader, new Callable<Furnace>() {
@Override
public Furnace call() throws Exception {
BundleWiring wiring = ForgeCorePlugin.getDefault().getBundle().adapt(
BundleWiring.class);
Collection<String> entries = wiring.listResources("bootpath",
"*.jar", BundleWiring.LISTRESOURCES_RECURSE);
Collection<URL> resources = new HashSet<URL>();
File jarDir = File.createTempFile("forge", "jars");
if (entries != null)
for (String resource : entries) {
URL jar = BootpathMarker.class.getResource("/"
+ resource);
if (jar != null) {
resources.add(copy(jarDir, resource,
jar.openStream()));
}
}

loader = new URLClassLoader(resources.toArray(new URL[resources
.size()]), null);

Class<?> bootstrapType = loader
.loadClass("org.jboss.forge.furnace.impl.FurnaceImpl");

Object nativeForge = bootstrapType.newInstance();
Furnace furnace = (Furnace) ClassLoaderAdapterCallback.enhance(
Furnace.class.getClassLoader(), loader, nativeForge,
Furnace.class);
setupRepositories(furnace);
return furnace;
}
});
return forge;
}

private void setupRepositories(final Furnace furnace) throws IOException {
Bundle runtimeBundle = Platform.getBundle(RUNTIME_PLUGIN_ID);
File bundleFile = FileLocator.getBundleFile(runtimeBundle);
furnace.addRepository(AddonRepositoryMode.IMMUTABLE, new File(
bundleFile, "addon-repository"));
furnace.addRepository(AddonRepositoryMode.MUTABLE, new File(
ForgeExtPreferences.INSTANCE.getAddonDir()));
}

public void startFurnace() {
try {
FurnaceService.INSTANCE.setFurnace(createFurnace());
FurnaceService.INSTANCE.start(loader);
} catch (Exception e) {
ForgeCorePlugin.log(e);
}
}

private URL copy(File directory, String name, InputStream input)
throws IOException {
File outputFile = new File(directory, name);

FileOutputStream output = null;
try {
directory.delete();
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();

output = new FileOutputStream(outputFile);
final byte[] buffer = new byte[4096];
int read = 0;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} catch (Exception e) {
throw new RuntimeException("Could not write out jar file " + name,
e);
} finally {
close(input);
close(output);
}
return outputFile.toURI().toURL();
}

private void close(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Exception e) {
throw new RuntimeException("Could not close stream", e);
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.jboss.forge.furnace.services.Imported;
import org.jboss.tools.forge.core.io.ForgeOutputListener;
import org.jboss.tools.forge.core.process.ForgeRuntime;
import org.jboss.tools.forge.ext.core.ForgeCorePlugin;
import org.jboss.tools.forge.ext.core.FurnaceProvider;
import org.jboss.tools.forge.ext.core.FurnaceService;

public class FurnaceRuntime implements ForgeRuntime {
Expand Down Expand Up @@ -57,11 +57,11 @@ public void start(IProgressMonitor progressMonitor) {
try {
progressMonitor.beginTask("Starting Forge " + getVersion(), IProgressMonitor.UNKNOWN);
setNewState(STATE_STARTING);
ForgeCorePlugin.getDefault().startFurnace();
FurnaceProvider.INSTANCE.startFurnace();
progressMonitor.worked(1);
while (FurnaceService.INSTANCE.getContainerStatus().isStarting()) {
if (progressMonitor.isCanceled()) {
ForgeCorePlugin.getDefault().stopFurnace();
FurnaceService.INSTANCE.stop();
setNewState(STATE_NOT_RUNNING);
} else {
Thread.sleep(1000);
Expand All @@ -73,7 +73,7 @@ public void start(IProgressMonitor progressMonitor) {
setNewState(STATE_RUNNING);
} catch (InterruptedException e) {
if (progressMonitor.isCanceled()) {
ForgeCorePlugin.getDefault().stopFurnace();
FurnaceService.INSTANCE.stop();
setNewState(STATE_NOT_RUNNING);
}
}
Expand All @@ -82,7 +82,7 @@ public void start(IProgressMonitor progressMonitor) {
@Override
public void stop(IProgressMonitor progressMonitor) {
setNewState(STATE_NOT_RUNNING);
ForgeCorePlugin.getDefault().stopFurnace();
FurnaceService.INSTANCE.stop();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.jboss.tools.forge.ext.core.ForgeCorePlugin;
import org.jboss.tools.forge.ext.core.FurnaceService;
import org.jboss.tools.forge.ui.ext.ForgeUIPlugin;
import org.jboss.tools.forge.ui.ext.util.FurnaceHelper;

Expand All @@ -22,7 +22,7 @@ public void run() {

@Override
public boolean isEnabled() {
return !ForgeCorePlugin.getDefault().isFurnaceStarted();
return FurnaceService.INSTANCE.getContainerStatus().isStopped();
}

private ImageDescriptor createImageDescriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.jboss.tools.forge.ext.core.ForgeCorePlugin;
import org.jboss.tools.forge.ext.core.FurnaceService;
import org.jboss.tools.forge.ui.ext.ForgeUIPlugin;
import org.jboss.tools.forge.ui.ext.util.FurnaceHelper;

Expand All @@ -22,7 +22,7 @@ public void run() {

@Override
public boolean isEnabled() {
return ForgeCorePlugin.getDefault().isFurnaceStarted();
return FurnaceService.INSTANCE.getContainerStatus().isStarted();
}

private ImageDescriptor createImageDescriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void stopConsole() {
@Override
public void run() {
aeshConsole.stop();
aeshDocument.set("");
aeshDocument.reset();
setDocument(null);
}
});
Expand Down
Loading

0 comments on commit 3e83f87

Please sign in to comment.