= H2 Console

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


The `H2ManagerMenu` provides a single menu item to redirect to the H2 web console.
This is only enabled for prototyping, and only if H2 is detected in the underlying JDBC URL.

The menu appears under the "Prototyping" menu.


== Maven Configuration

include::docs:mavendeps:partial$setup-and-configure-dependencyManagement.adoc[leveloffset=+2]

In addition, add an entry for the BOM of all the testing support libraries:

[source,xml,subs="attributes+"]
.pom.xml
----
<dependencyManagement>
    <dependencies>
        <dependency>
        	<groupId>org.apache.causeway.testing</groupId>
	        <artifactId>causeway-testing</artifactId>
            <scope>import</scope>
            <type>pom</type>
            <version>{page-causewayrel}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
----

[#dependencies]
=== Dependencies

In the domain module(s) of your application, add the following dependency:

[source,xml]
.pom.xml
----
<dependencies>
    <dependency>
        <groupId>org.apache.causeway.testing</groupId>
        <artifactId>causeway-testing-h2console-ui</artifactId>
    </dependency>
</dependencies>
----

=== Update AppManifest

In your application's `AppManifest` (top-level Spring `@Configuration` used to bootstrap the app), import the `CausewayModuleTestingH2ConsoleUi` module:

[source,java]
.AppManifest.java
----
@Configuration
@Import({
        ...
        CausewayModuleTestingH2ConsoleUi.class,
        ...
})
public class AppManifest {
}
----


== API & Implementation

The API of the service is:

[source,java]
----
public class H2ManagerMenu {
    public void openH2Console() { /* ... */ }
}
----

Note that this launches the manager on the same host that the webapp runs, and so is only appropriate to use when running on `localhost`.


== Additional Configuration

The following additional configuration is also required (eg as in the xref:docs:starters:helloworld.adoc[] and xref:docs:starters:simpleapp.adoc[] starter apps):

* the h2 jdbc driver is required on the classpath of the webapp:
+
[source,xml]
.pom.xml
----
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
----

* the `jdbc:h2` URL configuration properties must be defined, eg in `application.properties`:
+
[source,ini]
.application.properties
----
spring.sql.init.platform=h2
spring.datasource.url=jdbc:h2:mem
----

* because this is a potential security risk, a further configuration property must also be explicitly enabled:
+
[source,ini]
.application.properties
----
causeway.prototyping.h2-console.web-allow-remote-access=true
----

* and if so, then the password should be randomly generated (it will be printed to the log); this is enabled by default:
+
[source,ini]
.application.properties
----
causeway.prototyping.h2-console.generate-random-web-admin-password=true
----




== Disabling/hiding the menu

The menu can be hidden or disabled by subscribing to its domain event, eg:

[source,java]
----
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

@Service
public class HideH2ManagerMenu {

    @EventListener(H2ManagerMenu.ActionDomainEvent.class)
    public void on(H2ManagerMenu.ActionDomainEvent ev) {
        ev.hide();
    }
}
----


== Using with Secman

If xref:security:secman:about.adoc[] is in use, then you will also need to grant the appropriate role (xref:refguide:extensions:index/secman/applib/role/seed/CausewayExtH2ConsoleRoleAndPermissions.adoc[]) to the user.

