Commit 48a18052 authored by Aurelien Pupier's avatar Aurelien Pupier
Browse files

BS-14553: Warn user when a Groovy scripts that is planned to be embedded

doesn't compile during bar creation.
parent 4883b276
......@@ -5,4 +5,7 @@ otherCatLabel=Others
collectionCatLabel=Collection
groovyScriptRepository=Groovy scripts
groovyProvidedScriptRepository=Groovy scripts provided
noDocumentationAvailable = No documentation available
\ No newline at end of file
noDocumentationAvailable = No documentation available
errorBuildingJarForGroovyScriptsForProcess=Error while building Jar for Groovy scripts in Process dependencies. \nPlease check scripts in Development -> Manage Groovy scripts...
errorBuildingJarForProvidedGroovyScriptsForProcess=Error while building Jar for Groovy scripts provided by {0}.
errorBuildingJarForGroovyScriptsFor6xApplication=Error while building Jar for Groovy scripts in 6.x Application dependencies. \nPlease check scripts in Development -> Manage Groovy scripts...
\ No newline at end of file
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.groovy.provider;
import static org.assertj.core.api.Assertions.assertThat;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.junit.Test;
public class JarExportFailedExceptionTest {
@Test
public void testMessageWithSimpleStatus() throws Exception {
final JarExportFailedException jarExportFailedException = new JarExportFailedException("global", new Status(IStatus.ERROR, "none", "message status"));
assertThat(jarExportFailedException.getMessage()).startsWith("global").contains("message status");
}
@Test
public void testMessageWithMultiStatus() throws Exception {
final MultiStatus multi = new MultiStatus("none", IStatus.ERROR, "multi message", null);
multi.add(new Status(IStatus.ERROR, "none", "message status 1"));
multi.add(new Status(IStatus.ERROR, "none", "message status 2"));
final JarExportFailedException jarExportFailedException = new JarExportFailedException("global", multi);
assertThat(jarExportFailedException.getMessage())
.startsWith("global")
.doesNotContain("multi message")
.contains("message status 1")
.contains("message status 2");
}
}
/**
* Copyright (C) 2009 BonitaSoft S.A.
* BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
......@@ -22,7 +22,7 @@ import org.eclipse.osgi.util.NLS;
public final class Messages extends NLS {
private static final String BUNDLE_NAME = "messages";//$NON-NLS-1$
public static String userDefinedCatLabel;
public static String stringCatLabel;
public static String numberCatLabel;
......@@ -30,8 +30,10 @@ public final class Messages extends NLS {
public static String otherCatLabel;
public static String groovyScriptRepository;
public static String groovyProvidedScriptRepository;
public static String noDocumentationAvailable;
public static String errorBuildingJarForGroovyScriptsForProcess;
public static String errorBuildingJarForProvidedGroovyScriptsForProcess;
public static String errorBuildingJarForGroovyScriptsFor6xApplication;
private Messages() {
// Do not instantiate
......
......@@ -35,6 +35,7 @@ import org.bonitasoft.studio.common.extension.BARResourcesProvider;
import org.bonitasoft.studio.common.repository.Repository;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.bonitasoft.studio.common.repository.jdt.CreateJarOperation;
import org.bonitasoft.studio.groovy.Messages;
import org.bonitasoft.studio.groovy.repository.GroovyFileStore;
import org.bonitasoft.studio.groovy.repository.GroovyRepositoryStore;
import org.bonitasoft.studio.groovy.repository.ProvidedGroovyRepositoryStore;
......@@ -43,8 +44,10 @@ import org.bonitasoft.studio.model.configuration.ConfigurationPackage;
import org.bonitasoft.studio.model.configuration.Fragment;
import org.bonitasoft.studio.model.configuration.FragmentContainer;
import org.bonitasoft.studio.model.process.AbstractProcess;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.osgi.util.NLS;
/**
* @author Romain Bioteau
......@@ -77,30 +80,38 @@ public class GroovyScriptBarResourceProvider implements BARResourcesProvider {
for (final GroovyFileStore file : providedStore.getChildren()) {
compilationUnits.add(file.getCompilationUnit());
}
if (!compilationUnits.isEmpty()) {
final File targetJar = new File(ProjectUtil.getBonitaStudioWorkFolder(), ProvidedGroovyRepositoryStore.EXPORTED_PROVIDED_JAR_NAME);
final CreateJarOperation createJarOperation = new CreateJarOperation(targetJar, toArray(compilationUnits, ICompilationUnit.class));
createJarOperation.run(Repository.NULL_PROGRESS_MONITOR);
if (createJarOperation.getStatus().isOK()) {
builder.addClasspathResource(new BarResource(targetJar.getName(), toByteArray(targetJar)));
}
targetJar.delete();
}
addGroovyCompilationUnitToClasspath(builder, compilationUnits, ProvidedGroovyRepositoryStore.EXPORTED_PROVIDED_JAR_NAME);
}
protected void addGroovyScriptDependenciesToClasspath(
final BusinessArchiveBuilder builder,
final Configuration configuration,
final List<FragmentContainer> containers) throws InvocationTargetException, InterruptedException, IOException {
final ICompilationUnit[] compilationUnits = collectCompilationUnits(configuration, containers);
if (compilationUnits.length > 0) {
final File targetJar = new File(ProjectUtil.getBonitaStudioWorkFolder(), GroovyRepositoryStore.EXPORTED_JAR_NAME);
final CreateJarOperation createJarOperation = new CreateJarOperation(targetJar, compilationUnits);
final Set<ICompilationUnit> compilationUnits = collectCompilationUnits(configuration, containers);
addGroovyCompilationUnitToClasspath(builder, compilationUnits, GroovyRepositoryStore.EXPORTED_JAR_NAME);
}
protected void addGroovyCompilationUnitToClasspath(final BusinessArchiveBuilder builder, final Set<ICompilationUnit> compilationUnits,
final String exportedProvidedJarName) throws InvocationTargetException, InterruptedException, IOException {
if (!compilationUnits.isEmpty()) {
final File targetJar = new File(ProjectUtil.getBonitaStudioWorkFolder(), exportedProvidedJarName);
final CreateJarOperation createJarOperation = new CreateJarOperation(targetJar, toArray(compilationUnits, ICompilationUnit.class));
createJarOperation.run(Repository.NULL_PROGRESS_MONITOR);
if (createJarOperation.getStatus().isOK()) {
final IStatus status = createJarOperation.getStatus();
if (status.isOK()) {
builder.addClasspathResource(new BarResource(targetJar.getName(), toByteArray(targetJar)));
targetJar.delete();
} else {
targetJar.delete();
if (GroovyRepositoryStore.EXPORTED_JAR_NAME.equals(exportedProvidedJarName)) {
throw new JarExportFailedException(
Messages.errorBuildingJarForGroovyScriptsForProcess + " ",
status);
} else {
throw new JarExportFailedException(NLS.bind(Messages.errorBuildingJarForProvidedGroovyScriptsForProcess + " ",
org.bonitasoft.studio.common.Messages.bosProductName), status);
}
}
targetJar.delete();
}
}
......@@ -108,19 +119,26 @@ public class GroovyScriptBarResourceProvider implements BARResourcesProvider {
final BusinessArchiveBuilder builder,
final Configuration configuration,
final List<FragmentContainer> containers) throws InvocationTargetException, InterruptedException, IOException {
final ICompilationUnit[] compilationUnits = collectCompilationUnits(configuration, containers);
if (compilationUnits.length > 0) {
final File targetJar = new File(ProjectUtil.getBonitaStudioWorkFolder(), GroovyRepositoryStore.EXPORTED_JAR_NAME);
final CreateJarOperation createJarOperation = new CreateJarOperation(targetJar, compilationUnits);
final Set<ICompilationUnit> compilationUnits = collectCompilationUnits(configuration, containers);
final String exportedJarName = GroovyRepositoryStore.EXPORTED_JAR_NAME;
if (!compilationUnits.isEmpty()) {
final File targetJar = new File(ProjectUtil.getBonitaStudioWorkFolder(), exportedJarName);
final CreateJarOperation createJarOperation = new CreateJarOperation(targetJar, toArray(compilationUnits, ICompilationUnit.class));
createJarOperation.run(Repository.NULL_PROGRESS_MONITOR);
if (createJarOperation.getStatus().isOK()) {
final IStatus status = createJarOperation.getStatus();
if (status.isOK()) {
builder.addExternalResource(new BarResource(EXTERNAL_LIB_BAR_LOCATION + targetJar.getName(), toByteArray(targetJar)));
targetJar.delete();
} else {
targetJar.delete();
throw new JarExportFailedException(
Messages.errorBuildingJarForGroovyScriptsFor6xApplication + " ",
status);
}
targetJar.delete();
}
}
private ICompilationUnit[] collectCompilationUnits(final Configuration configuration, final List<FragmentContainer> containers) {
private Set<ICompilationUnit> collectCompilationUnits(final Configuration configuration, final List<FragmentContainer> containers) {
final Set<ICompilationUnit> result = new HashSet<ICompilationUnit>();
if (configuration != null) {
final GroovyRepositoryStore store = repositoryAccessor.getRepositoryStore(GroovyRepositoryStore.class);
......@@ -137,7 +155,7 @@ public class GroovyScriptBarResourceProvider implements BARResourcesProvider {
}
}
}
return toArray(result, ICompilationUnit.class);
return result;
}
}
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.groovy.provider;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
public class JarExportFailedException extends RuntimeException {
private static final long serialVersionUID = -542078154433791781L;
private final IStatus status;
private final String globalMessage;
public JarExportFailedException(final String globalMessage, final IStatus status) {
this.globalMessage = globalMessage;
this.status = status;
}
@Override
public String getMessage() {
final StringBuilder sb = new StringBuilder(globalMessage + "\n");
if (status instanceof MultiStatus) {
for (final IStatus childStatus : status.getChildren()) {
sb.append(childStatus.getMessage());
sb.append(" \n");
}
} else {
sb.append(status.getMessage());
sb.append(" \n");
}
return sb.toString();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment