• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.44
/exist-core/src/main/java/org/exist/storage/BrokerFactory.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 *
24
 * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
25
 *       The original license header is included below.
26
 *
27
 * =====================================================================
28
 *
29
 * eXist-db Open Source Native XML Database
30
 * Copyright (C) 2001 The eXist-db Authors
31
 *
32
 * info@exist-db.org
33
 * http://www.exist-db.org
34
 *
35
 * This library is free software; you can redistribute it and/or
36
 * modify it under the terms of the GNU Lesser General Public
37
 * License as published by the Free Software Foundation; either
38
 * version 2.1 of the License, or (at your option) any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful,
41
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43
 * Lesser General Public License for more details.
44
 *
45
 * You should have received a copy of the GNU Lesser General Public
46
 * License along with this library; if not, write to the Free Software
47
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
48
 */
49
package org.exist.storage;
50

51
import java.lang.invoke.*;
52
import java.util.Locale;
53
import java.util.concurrent.ConcurrentHashMap;
54
import java.util.concurrent.ConcurrentMap;
55
import java.util.function.BiFunction;
56

57
import com.evolvedbinary.j8fu.Either;
58
import com.evolvedbinary.j8fu.lazy.LazyValE;
59
import org.apache.logging.log4j.LogManager;
60
import org.apache.logging.log4j.Logger;
61
import org.exist.EXistException;
62
import org.exist.util.Configuration;
63

64
import static com.evolvedbinary.j8fu.Either.Left;
65
import static com.evolvedbinary.j8fu.Either.Right;
66
import static java.lang.invoke.MethodType.methodType;
67

68
/**
69
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
70
 */
71
public class BrokerFactory {
×
72

73
    private static final Logger LOG = LogManager.getLogger(BrokerFactory.class);
1✔
74
    private static final ConcurrentMap<String, LazyValE<BiFunction<BrokerPool, Configuration, DBBroker>, RuntimeException>> CONSTRUCTORS = new ConcurrentHashMap<>();
1✔
75
    private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
1✔
76

77
    public static final String PROPERTY_DATABASE = "database";
78

79
    public static void plug(final String brokerId, final Class<? extends DBBroker> clazz) {
80
        CONSTRUCTORS.computeIfAbsent(formatBrokerId(brokerId), key -> new LazyValE<>(() -> getConstructor(key, clazz)));
1✔
81
    }
1✔
82

83
    static {
84
        plug(formatBrokerId("NATIVE"), NativeBroker.class);
1✔
85
    }
1✔
86

87
    /**
88
     * Constructs a DBBroker instance.
89
     *
90
     * @param brokerPool the database's broker pool.
91
     * @param configuration the database's configuration.
92
     *
93
     * @return DBBroker an instance of a sub-class of {@link DBBroker}.
94
     *
95
     * @throws EXistException in case of an error
96
     * @throws RuntimeException if the database backend cannot be constructed.
97
     */
98
    public static DBBroker getInstance(final BrokerPool brokerPool, final Configuration configuration) throws RuntimeException, EXistException {
99
        final long start = System.currentTimeMillis();
1✔
100

101
        final String brokerId = getBrokerId(configuration);
1✔
102
        final LazyValE<BiFunction<BrokerPool, Configuration, DBBroker>, RuntimeException> constructor
1✔
103
                = CONSTRUCTORS.get(brokerId);
1✔
104
        if (constructor == null) {
1!
105
            throw new IllegalStateException("No database backend found for: " + brokerId);
×
106
        }
107

108
        final DBBroker broker = constructor.get().apply(brokerPool, configuration);
1✔
109

110
        if (LOG.isTraceEnabled()) {
1!
111
            final long end = System.currentTimeMillis();
×
112
            LOG.trace("Constructed DBBroker in: {} ms", end - start);
×
113
        }
114

115
        return broker;
1✔
116
    }
117

118
    /**
119
     * Creates a constructor function for a sub-class of DBBroker.
120
     *
121
     * @param brokerId the id of the DBBroker.
122
     * @param clazz the sub-class of DBBroker.
123
     *
124
     * @return Either a constructor function, or a RuntimeException.
125
     */
126
    @SuppressWarnings("unchecked")
127
    private static Either<RuntimeException, BiFunction<BrokerPool, Configuration, DBBroker>> getConstructor(final String brokerId, final Class<? extends DBBroker> clazz) {
128
        try {
129
            final MethodHandle methodHandle = LOOKUP.findConstructor(clazz, methodType(void.class, BrokerPool.class, Configuration.class));
1✔
130

131
            // see https://stackoverflow.com/questions/50211216/how-to-invoke-constructor-using-lambdametafactory#50211536
132
            return Right((BiFunction<BrokerPool, Configuration, DBBroker>) LambdaMetafactory.metafactory(
1✔
133
                    LOOKUP,
1✔
134
                    "apply",
1✔
135
                    methodType(BiFunction.class),
1✔
136
                    methodHandle.type().erase(), methodHandle, methodHandle.type()
1✔
137
            ).getTarget().invokeExact());
1✔
138

139
        } catch (final Throwable e) {
×
140
            if (e instanceof InterruptedException) {
×
141
                // NOTE: must set interrupted flag
142
                Thread.currentThread().interrupt();
×
143
            }
144

145
            return Left(new RuntimeException("Can't get database backend: " + brokerId, e));
×
146
        }
147
    }
148

149
    /**
150
     * Gets the Broker ID from a Configuration.
151
     *
152
     * @param configuration the configuration.
153
     * @return the broker ID.
154
     *
155
     * @throws IllegalArgumentException if the configuration does not define a broker ID.
156
     */
157
    private static String getBrokerId(final Configuration configuration) throws IllegalArgumentException {
158
        final String brokerId = (String) configuration.getProperty(PROPERTY_DATABASE);
1✔
159
        if (brokerId == null) {
1!
160
            throw new IllegalArgumentException("No database defined in: " + configuration.getConfigFilePath());
×
161
        }
162

163
        return formatBrokerId(brokerId);
1✔
164
    }
165

166
    /**
167
     * Ensures consistent formatting fo the Broker ID.
168
     *
169
     * Repair name {@see https://sourceforge.net/p/exist/bugs/810/}.
170
     *
171
     * @param brokerId the broker id to be formatted.
172
     *
173
     * @return consistently formatted broker id.
174
     */
175
    private static String formatBrokerId(final String brokerId) {
176
        return brokerId.toUpperCase(Locale.ENGLISH);
1✔
177
    }
178
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc