summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/src/org/apache/subversion/javahl/NativeResources.java
blob: c140fd11c4e920a789991110a2475533d42899bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
/**
 * @copyright
 * ====================================================================
 *    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.
 * ====================================================================
 * @endcopyright
 */

package org.apache.subversion.javahl;

import org.apache.subversion.javahl.types.Version;

/**
 * Handles activities related to management of native resouces
 * (e.g. loading of native libraries).
 *
 * Public for backward compat.  This class may disappear in future versions
 * of the API.  You've been warned.
 */
public class NativeResources
{
    /**
     * @return Version information about the underlying native libraries.
     */
    private static Version version;

    /**
     * Returns version information about the underlying native libraries.
     *
     * @return version
     *
     */
    public static Version getVersion() {
        return version;
    }

    /**
     * Load the required native library whose path is specified by the
     * system property <code>subversion.native.library</code> (which
     * can be passed to the JVM on start-up using an argument like
     * <code>-Dsubversion.native.library=/usr/local/lib/libsvnjavahl-1.so</code>).
     * If the system property is not specified or cannot be loaded,
     * attempt to load the library using its expected name, and the
     * platform-dependent loading mechanism.
     *
     * @throws UnsatisfiedLinkError If the native library cannot be
     * loaded.
     * @throws LinkageError If the version of the loaded native
     * library is not compatible with this version of JavaHL's Java
     * APIs.
     */
    public static synchronized void loadNativeLibrary()
    {
        UnsatisfiedLinkError loadException = null;

        // If the user specified the fully qualified path to the
        // native library, try loading that first.
        try
        {
            String specifiedLibraryName =
                System.getProperty("subversion.native.library");
            if (specifiedLibraryName != null)
            {
                System.load(specifiedLibraryName);
                init();
                return;
            }
        }
        catch (UnsatisfiedLinkError ex)
        {
            // Since the user explicitly specified this path, this is
            // the best error to return if no other method succeeds.
            loadException = ex;
        }

        // Try to load the library by its name.  Failing that, try to
        // load it by its old name.
        String[] libraryNames = {"svnjavahl-1", "libsvnjavahl-1", "svnjavahl"};
        for (String libraryName : libraryNames)
        {
            try
            {
                System.loadLibrary(libraryName);
                init();
                return;
            }
            catch (UnsatisfiedLinkError ex)
            {
                if (loadException == null)
                {
                    loadException = ex;
                }
            }
        }

        // Re-throw the most relevant exception.
        if (loadException == null)
        {
            // This could only happen as the result of a programming error.
            loadException = new UnsatisfiedLinkError("Unable to load JavaHL " +
                                                     "native library");
        }
        throw loadException;
    }

    /**
     * Initializer for native resources to be invoked <em>after</em>
     * the native library has been loaded.  Sets library version
     * information, and initializes the re-entrance hack for native
     * code.
     * @throws LinkageError If the version of the loaded native
     * library is not compatible with this version of JavaHL's Java
     * APIs.
     */
    private static final void init()
    {
        initNativeLibrary();
        version = new Version();
        if (!version.isAtLeast(1, 8, 0))
        {
            throw new LinkageError("Native library version must be at least " +
                                   "1.8.0, but is only " + version);
        }
    }

    /**
     * Initialize the native library layer.
     */
    private static native void initNativeLibrary();
}