ExternalDataSources.java 4.9 KB
Newer Older
R
test  
roo00 已提交
1 2
package com.x.base.core.project.config;

R
fix  
roo00 已提交
3 4 5
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
R
test  
roo00 已提交
6 7 8
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.lang3.BooleanUtils;
R
fix  
roo00 已提交
9 10 11 12
import org.apache.commons.lang3.StringUtils;

import com.x.base.core.container.factory.SlicePropertiesBuilder;
import com.x.base.core.project.tools.ListTools;
R
test  
roo00 已提交
13 14 15

public class ExternalDataSources extends CopyOnWriteArrayList<ExternalDataSource> {

Z
zhourui 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    private static final long serialVersionUID = 4502077979125945875L;

    public static ExternalDataSources defaultInstance() {
        return new ExternalDataSources();
    }

    public ExternalDataSources() {
        super();
    }

    public Boolean enable() {
        if (this.isEmpty()) {
            return false;
        }
        for (ExternalDataSource o : this) {
            if (BooleanUtils.isTrue(o.getEnable())) {
                return true;
            }
        }
        return false;

    }

    public String name(ExternalDataSource externalDataSource) throws Exception {
        String name = "";
        int idx = 0;
        for (ExternalDataSource o : this) {
            idx++;
            if (BooleanUtils.isTrue(o.getEnable()) && Objects.equals(o, externalDataSource)) {
                name = "s" + ("" + (1000 + idx)).substring(1);
                break;
            }
        }
        if (StringUtils.isEmpty(name)) {
            throw new Exception("externalDataSource not in externalDataSources." + externalDataSource);
        }
        return name;
    }

    public List<String> names() throws Exception {
        List<String> names = new ArrayList<>();
        int idx = 0;
        for (ExternalDataSource o : this) {
            idx++;
            if (BooleanUtils.isTrue(o.getEnable())) {
                names.add("s" + ("" + (1000 + idx)).substring(1));
            }
        }
        return names;
    }

    public List<String> findNamesOfContainerEntity(String className) throws Exception {
        List<String> names = new ArrayList<>();
        for (ExternalDataSource o : this) {
            if (BooleanUtils.isTrue(o.getEnable())) {
                List<String> list = ListTools.toList(className);
                list = ListTools.includesExcludesWildcard(list, o.getIncludes(), o.getExcludes());
                if (!list.isEmpty()) {
                    names.add(this.name(o));
                }
            }
        }
        return names;
    }

    public String log(String name) {
        int idx = 0;
        for (ExternalDataSource o : this) {
            idx++;
            if (BooleanUtils.isTrue(o.getEnable())) {
                String n = "s" + ("" + (1000 + idx)).substring(1);
                if (StringUtils.equals(n, name)) {
                    String value = o.getLogLevel();
                    if (StringUtils.contains(value, "=")) {
                        return value;
                    } else {
                        return "DefaultLevel=WARN, Tool=" + value + ", Enhance=" + value + ", METADATA=" + value
                                + ", Runtime=" + value + ", Query=" + value + ", DataCache=" + value + ", JDBC=" + value
                                + ", SQL=" + value;
                    }
                }
            }
        }
        return "DefaultLevel=WARN, Tool=WARN, Enhance=WARN, METADATA=WARN, Runtime=WARN, Query=WARN, DataCache=WARN, JDBC=ERROR, SQL=WARN";
    }
R
fix  
roo00 已提交
101

Z
update  
zhourui 已提交
102 103 104 105 106 107 108 109
    public String dictionary() throws Exception {
        for (ExternalDataSource o : this) {
            if (BooleanUtils.isTrue(o.getEnable())) {
                return o.getDictionary();
            }
        }
        throw new IllegalStateException("get dictionary error.");
    }
Z
zhourui 已提交
110

Z
update  
zhourui 已提交
111 112 113 114 115 116 117 118
    public String schema() throws Exception {
        for (ExternalDataSource o : this) {
            if (BooleanUtils.isTrue(o.getEnable())) {
                return o.getSchema();
            }
        }
        throw new IllegalStateException("get schema error.");
    }
R
fix  
roo00 已提交
119

Z
zhourui 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    public boolean hasSchema() throws Exception {
        for (ExternalDataSource o : this) {
            if (BooleanUtils.isTrue(o.getEnable())) {
                return SlicePropertiesBuilder.hasSchemaOfUrl(o.getUrl());
            }
        }
        throw new Exception("hasSchema error.");
    }

    public String getTransactionIsolation() {
        for (ExternalDataSource o : this) {
            return o.getTransactionIsolation();
        }
        return null;
    }

    public Boolean getTestConnectionOnCheckin() {
        for (ExternalDataSource o : this) {
            return o.getTestConnectionOnCheckin();
        }
        return null;
    }

    public Boolean getTestConnectionOnCheckout() {
        for (ExternalDataSource o : this) {
            return o.getTestConnectionOnCheckout();
        }
        return null;
    }

    public Integer getMaxIdleTime() {
        for (ExternalDataSource o : this) {
            return o.getMaxIdleTime();
        }
        return null;
    }
Z
zhourui 已提交
156

R
test  
roo00 已提交
157
}