diff --git a/plugins/org.eclipse.gemoc.commons.eclipse.messagingsystem.ui/src/main/java/org/eclipse/gemoc/commons/eclipse/messagingsystem/ui/Activator.java b/plugins/org.eclipse.gemoc.commons.eclipse.messagingsystem.ui/src/main/java/org/eclipse/gemoc/commons/eclipse/messagingsystem/ui/Activator.java index b1c8912aa..41aea3c8b 100644 --- a/plugins/org.eclipse.gemoc.commons.eclipse.messagingsystem.ui/src/main/java/org/eclipse/gemoc/commons/eclipse/messagingsystem/ui/Activator.java +++ b/plugins/org.eclipse.gemoc.commons.eclipse.messagingsystem.ui/src/main/java/org/eclipse/gemoc/commons/eclipse/messagingsystem/ui/Activator.java @@ -12,6 +12,7 @@ import java.io.PrintStream; +import org.eclipse.gemoc.commons.eclipse.messagingsystem.ui.helper.TeeOutputStream; import org.eclipse.gemoc.commons.eclipse.messagingsystem.ui.internal.EclipseConsoleOutputStream; import org.eclipse.gemoc.commons.eclipse.messagingsystem.ui.internal.console.ConsoleIO; import org.eclipse.gemoc.commons.eclipse.messagingsystem.ui.internal.console.EclipseConsoleIO; @@ -106,8 +107,8 @@ public ConsoleIO getConsoleIO() { return consoleIO; } - protected PrintStream OriginalSystemOut = null; - protected PrintStream OriginalSystemErr = null; + protected PrintStream originalSystemOut = null; + protected PrintStream originalSystemErr = null; /** * set the current System.out and System.err so they are redirected to our @@ -115,17 +116,19 @@ public ConsoleIO getConsoleIO() { */ public void captureSystemOutAndErr() { Activator.getDefault().getConsoleIO().print("Redirecting System.out and System.err to this console.\n"); - if (OriginalSystemOut != System.out) { - OriginalSystemOut = System.out; - PrintStream outPrintStream = new PrintStream( + if (originalSystemOut != System.out) { + originalSystemOut = System.out; + TeeOutputStream teeOutputStream = new TeeOutputStream( + originalSystemOut, new EclipseConsoleOutputStream(Activator.getDefault().getConsoleIO(), false)); - System.setOut(outPrintStream); + System.setOut(new PrintStream(teeOutputStream)); } - if (OriginalSystemErr != System.err) { - OriginalSystemOut = System.out; - PrintStream errPrintStream = new PrintStream( + if (originalSystemErr != System.err) { + originalSystemErr = System.err; + TeeOutputStream teeOutputStream = new TeeOutputStream( + originalSystemErr, new EclipseConsoleOutputStream(Activator.getDefault().getConsoleIO(), true)); - System.setErr(errPrintStream); + System.setErr(new PrintStream(teeOutputStream)); } } @@ -139,12 +142,12 @@ public void releaseSystemOutAndErr() { System.err.flush(); if(consoleIO != null) consoleIO.print("Stopping redirection of System.out and System.err to this console.\n"); - if (OriginalSystemOut != null) - System.setOut(OriginalSystemOut); - if (OriginalSystemErr != null) - System.setErr(OriginalSystemErr); - OriginalSystemOut = null; - OriginalSystemErr = null; + if (originalSystemOut != null) + System.setOut(originalSystemOut); + if (originalSystemErr != null) + System.setErr(originalSystemErr); + originalSystemOut = null; + originalSystemErr = null; } } diff --git a/plugins/org.eclipse.gemoc.commons.eclipse.messagingsystem.ui/src/main/java/org/eclipse/gemoc/commons/eclipse/messagingsystem/ui/helper/TeeOutputStream.java b/plugins/org.eclipse.gemoc.commons.eclipse.messagingsystem.ui/src/main/java/org/eclipse/gemoc/commons/eclipse/messagingsystem/ui/helper/TeeOutputStream.java new file mode 100644 index 000000000..1120043d1 --- /dev/null +++ b/plugins/org.eclipse.gemoc.commons.eclipse.messagingsystem.ui/src/main/java/org/eclipse/gemoc/commons/eclipse/messagingsystem/ui/helper/TeeOutputStream.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2018 Inria and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Inria - initial API and implementation + *******************************************************************************/ +package org.eclipse.gemoc.commons.eclipse.messagingsystem.ui.helper; + +import java.io.IOException; +import java.io.OutputStream; + +public final class TeeOutputStream extends OutputStream { + + private final OutputStream out; + private final OutputStream tee; + + public TeeOutputStream(OutputStream out, OutputStream tee) { + if (out == null) + throw new NullPointerException(); + else if (tee == null) + throw new NullPointerException(); + + this.out = out; + this.tee = tee; + } + + @Override + public void write(int b) throws IOException { + out.write(b); + tee.write(b); + } + + @Override + public void write(byte[] b) throws IOException { + out.write(b); + tee.write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); + tee.write(b, off, len); + } + + @Override + public void flush() throws IOException { + out.flush(); + tee.flush(); + } + + @Override + public void close() throws IOException { + out.close(); + tee.close(); + } +} diff --git a/plugins/org.eclipse.gemoc.commons.eclipse.pde/src/org/eclipse/gemoc/commons/eclipse/pde/manifest/ManifestChanger.java b/plugins/org.eclipse.gemoc.commons.eclipse.pde/src/org/eclipse/gemoc/commons/eclipse/pde/manifest/ManifestChanger.java index c8fe5a35c..d6ae6a91f 100644 --- a/plugins/org.eclipse.gemoc.commons.eclipse.pde/src/org/eclipse/gemoc/commons/eclipse/pde/manifest/ManifestChanger.java +++ b/plugins/org.eclipse.gemoc.commons.eclipse.pde/src/org/eclipse/gemoc/commons/eclipse/pde/manifest/ManifestChanger.java @@ -15,6 +15,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; import java.util.Map.Entry; import java.util.jar.Attributes.Name; import java.util.jar.Manifest; @@ -107,7 +110,24 @@ private void writeManifest(IFile outputFile) throws IOException, CoreException { ByteArrayOutputStream out = new ByteArrayOutputStream(); writeManifest(out); ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray()); - outputFile.setContents(is, 1, new NullProgressMonitor()); + + try{ + if (outputFile.exists()) { + if(!isStreamEqual(is, outputFile.getContents(true))){ + // use a new stream because isStreamEqual has used and closed the previous one + outputFile.setContents(new ByteArrayInputStream(out.toByteArray()), true, true, new NullProgressMonitor()); + } + } else { + outputFile.create(is, true, new NullProgressMonitor()); + } + } + finally{ + try { + if (out != null) out.close(); + } catch(IOException e) { + //closing quielty + } + } } } public void addPluginDependency(String plugin) throws BundleException, IOException, CoreException { @@ -131,4 +151,38 @@ public void addSingleton() throws BundleException, IOException, CoreException { public void addAttributes(String attributeName, String value){ _manifest.getMainAttributes().putValue(attributeName, value); } + + private static boolean isStreamEqual(InputStream i1, InputStream i2) + throws IOException { + + ReadableByteChannel ch1 = Channels.newChannel(i1); + ReadableByteChannel ch2 = Channels.newChannel(i2); + + ByteBuffer buf1 = ByteBuffer.allocateDirect(1024); + ByteBuffer buf2 = ByteBuffer.allocateDirect(1024); + + try { + while (true) { + + int n1 = ch1.read(buf1); + int n2 = ch2.read(buf2); + + if (n1 == -1 || n2 == -1) return n1 == n2; + + buf1.flip(); + buf2.flip(); + + for (int i = 0; i < Math.min(n1, n2); i++) + if (buf1.get() != buf2.get()) + return false; + + buf1.compact(); + buf2.compact(); + } + + } finally { + if (i1 != null) i1.close(); + if (i2 != null) i2.close(); + } + } }