Commit 1fd32fc7 authored by Romain Bioteau's avatar Romain Bioteau Committed by Romain Bioteau

feat(BDM): As Dave I want to browse my H2 database for BDM (#152)

Open H2 Console in a browser on Business data DB

fixes [BS-15680](https://bonitasoft.atlassian.net/browse/BS-15680)
parent 42529c2d
......@@ -42,7 +42,8 @@ Require-Bundle: org.bonitasoft.studio.engine;bundle-version="1.0.0",
org.bonitasoft.studio.browser;bundle-version="6.5.0",
com.google.guava,
org.eclipse.e4.core.services,
org.eclipse.osgi.services;bundle-version="3.4.0"
org.eclipse.osgi.services;bundle-version="3.4.0",
org.eclipse.jdt.launching
Export-Package: org.bonitasoft.studio.businessobject.core.operation,or
g.bonitasoft.studio.businessobject.core.repository,org.bonitasoft.stu
dio.businessobject.ui,org.bonitasoft.studio.businessobject.ui.wizard
......
BusinessDataModel=Business Data Model
Export=Export...
manageBusinessObjects=Manage...
createBusinessDataLabel=<a>Create a Business data...</a>
\ No newline at end of file
createBusinessDataLabel=<a>Create a Business data...</a>
openH2Console=Browse data (h2 console)...
\ No newline at end of file
......@@ -19,6 +19,11 @@
id="org.bonitasoft.studio.businessobject.exportCommand"
name="%Export">
</command>
<command
defaultHandler="org.bonitasoft.studio.businessobject.ui.handler.OpenH2ConsoleHandler"
id="org.bonitasoft.studio.businessobject.openH2Console"
name="%openH2Console">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
......@@ -34,6 +39,10 @@
id="org.bonitasoft.studio.businessobject.manage.menu"
style="push">
</command>
<command
commandId="org.bonitasoft.studio.businessobject.openH2Console"
style="push">
</command>
<separator
name="org.bonitasoft.studio.businessobject.separator"
visible="true">
......
/**
* Copyright (C) 2016 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.businessobject.ui.handler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import org.eclipse.core.runtime.IStatus;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class OpenH2ConsoleHandlerTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Mock
private Runtime runtime;
@Test
public void should_locate_h2_jar_from_tomcat_folder() throws Exception {
final OpenH2ConsoleHandler openH2ConsoleHandler = new OpenH2ConsoleHandler();
final String path = openH2ConsoleHandler.locateH2jar(rootFile());
assertThat(path).contains("tomcat");
}
private File rootFile() throws MalformedURLException, URISyntaxException {
return new File(OpenH2ConsoleHandlerTest.class.getResource("/workspace").toURI().toURL().getFile());
}
@Test
public void should_throw_FileNotFoundException_if_h2_jar_is_missing() throws Exception {
final OpenH2ConsoleHandler openH2ConsoleHandler = new OpenH2ConsoleHandler();
expectedException.expect(FileNotFoundException.class);
openH2ConsoleHandler.locateH2jar(new File(OpenH2ConsoleHandlerTest.class.getResource("/workspaceWithoutH2").toURI().toURL().getFile()));
}
@Test
public void should_execute_java_command() throws Exception {
final OpenH2ConsoleHandler openH2ConsoleHandler = spy(new OpenH2ConsoleHandler());
doReturn(runtime).when(openH2ConsoleHandler).getRuntime();
doReturn(rootFile()).when(openH2ConsoleHandler).rootFile();
doReturn("h2.jar").when(openH2ConsoleHandler).locateH2jar(any(File.class));
final Object status = openH2ConsoleHandler.execute(null);
assertThat(status).isEqualTo(IStatus.OK);
final ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(runtime).exec(argument.capture());
assertThat(argument.getValue()).contains("-webPort").startsWith("java -jar h2.jar -browser").endsWith(
"-tcp -user sa -url jdbc:h2:tcp://localhost:9091/business_data.db;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;IGNORECASE=TRUE; -driver org.h2.Driver");
}
}
/**
* 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.businessobject.ui.handler;
import static com.google.common.collect.Iterables.tryFind;
import static com.google.common.io.Files.fileTreeTraverser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.bonitasoft.studio.common.repository.RepositoryManager;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.launching.SocketUtil;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
/**
* @author Romain Bioteau
*
*/
public class OpenH2ConsoleHandler extends AbstractHandler {
private static final String URL = "jdbc:h2:tcp://localhost:9091/business_data.db;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;IGNORECASE=TRUE;";
private static final String DRIVER = "org.h2.Driver";
private static final String USER = "sa";
/*
* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
try {
final String h2JarPath = locateH2jar(
rootFile());
getRuntime().exec(String.format("java -jar %s -browser -webPort %s -tcp -user %s -url %s -driver %s", h2JarPath, SocketUtil.findFreePort(),
USER, URL, DRIVER));
} catch (final IOException e) {
throw new ExecutionException("Failed to locate h2 jar", e);
}
return IStatus.OK;
}
protected File rootFile() {
return RepositoryManager.getInstance().getCurrentRepository().getProject().getWorkspace().getRoot().getLocation().toFile();
}
protected Runtime getRuntime() {
return Runtime.getRuntime();
}
protected String locateH2jar(File root) throws IOException {
final Path path = Paths.get(root.toURI()).resolve(Paths.get("tomcat", "lib", "bonita"));
final Optional<File> candidate = tryFind(fileTreeTraverser().children(path.toFile()), h2Jar());
if (!candidate.isPresent()) {
throw new FileNotFoundException("Cannot find h2 jar file in tomcat/lib/bonita folder.");
}
return candidate.get().getAbsolutePath();
}
private Predicate<? super File> h2Jar() {
return new Predicate<File>() {
@Override
public boolean apply(File file) {
final String name = file.getName();
return name.startsWith("h2-") && name.endsWith(".jar");
}
};
}
}
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