Skip to content

Package: KapuaConfigurableServiceSchemaUtilsWithResources

KapuaConfigurableServiceSchemaUtilsWithResources

nameinstructionbranchcomplexitylinemethod
scriptSession(String, String)
M: 50 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.commons.configuration;
14:
15: import org.eclipse.kapua.KapuaException;
16: import org.eclipse.kapua.commons.jpa.CommonsEntityManagerFactory;
17: import org.eclipse.kapua.commons.jpa.EntityManager;
18: import org.eclipse.kapua.commons.jpa.ResourceSqlScriptExecutor;
19: import org.slf4j.Logger;
20: import org.slf4j.LoggerFactory;
21:
22: /**
23: * Configurable service database schema utilities with resources based
24: * SQL scripts.
25: *
26: * @since 1.0
27: */
28: public class KapuaConfigurableServiceSchemaUtilsWithResources {
29:
30: private static final Logger logger = LoggerFactory.getLogger(KapuaConfigurableServiceSchemaUtilsWithResources.class);
31:
32: private KapuaConfigurableServiceSchemaUtilsWithResources() {
33: }
34:
35: /**
36: * Executes the database script in the specified path matching the filename
37: *
38: * @param path path to resource in project resources
39: * @param filename file name
40: */
41: public static void scriptSession(String path, String filename) {
42: EntityManager em = null;
43: try {
44:
45: logger.info("Running database script from resources...");
46:
47: em = CommonsEntityManagerFactory.getEntityManager();
48: em.beginTransaction();
49:
50: ResourceSqlScriptExecutor sqlScriptExecutor = new ResourceSqlScriptExecutor();
51: sqlScriptExecutor.addQuery(path + "/" + filename);
52: sqlScriptExecutor.executeUpdate(em);
53:
54: em.commit();
55:
56: logger.info("...database scripts from resources done!");
57: } catch (KapuaException e) {
58: logger.error("Database scripts from resources failed: {}", e.getMessage());
59:• if (em != null) {
60: em.rollback();
61: }
62: } finally {
63:• if (em != null) {
64: em.close();
65: }
66: }
67:
68: }
69:
70: }