2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.database;
26 import org.onap.dmaap.dbcapi.util.Singleton;
28 public class DBSingleton<C> extends TableHandler<C> implements Singleton<C> {
30 public DBSingleton(Class<C> cls, String tabname) throws Exception {
31 this(ConnectionFactory.getDefaultInstance(), cls, tabname);
33 public DBSingleton(ConnectionFactory cf, Class<C> cls, String tabname) throws Exception {
34 super(cf, cls, tabname, null);
35 singleton = cls.newInstance();
38 return((new ConnWrapper<C, Object>() {
39 protected C run(Object junk) throws Exception {
40 ps = c.prepareStatement(getstmt);
41 rs = ps.executeQuery();
45 for (DBFieldHandler f: fields) {
46 f.fromSQL(rs, singleton);
50 }).protect(cf, null));
52 public void init(C val) {
56 (new ConnWrapper<Void, C>() {
57 protected Void run(C val) throws Exception {
58 ps = c.prepareStatement(initstmt);
59 for (DBFieldHandler f: fields) {
63 if (val != singleton) {
64 for (DBFieldHandler f: fields) {
65 f.copy(val, singleton);
72 public void update(C val) {
73 (new ConnWrapper<Void, C>() {
74 protected Void run(C val) throws Exception {
75 ps = c.prepareStatement(insorreplstmt);
76 for (DBFieldHandler f: fields) {
80 if (val != singleton) {
81 for (DBFieldHandler f: fields) {
82 f.copy(val, singleton);
89 public void remove() throws DBException {
90 (new ConnWrapper<Void, Object>() {
91 protected Void run(Object junk) throws Exception {
92 ps = c.prepareStatement(delstmt);