1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.transport.socket.apr;
21
22 import org.apache.tomcat.jni.Library;
23 import org.apache.tomcat.jni.Pool;
24
25 /**
26 * Internal singleton used for initializing correctly the APR native library
27 * and the associated root memory pool.
28 *
29 * It'll finalize nicely the native resources (libraries and memory pools).
30 *
31 * Each memory pool used in the APR transport module needs to be children of the
32 * root pool AprLibrary#getRootPool().
33 *
34 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35 */
36 class AprLibrary {
37
38 // is APR library was initialized (load of native libraries)
39 private static AprLibrary library = null;
40
41 /**
42 * get the shared instance of APR library, if none, initialize one
43 * @return the current APR library singleton
44 */
45 static synchronized AprLibrary getInstance() {
46 if (!isInitialized())
47 initialize();
48 return library;
49 }
50
51 /**
52 * initialize the APR Library by loading the associated native libraries
53 * and creating the associated singleton
54 */
55 private static synchronized void initialize() {
56 if (library == null)
57 library = new AprLibrary();
58 }
59
60 /**
61 * is the APR library was initialized.
62 * @return true if the Library is initialized, false otherwise
63 */
64 static synchronized boolean isInitialized() {
65 return library != null;
66 }
67
68 // APR memory pool (package wide mother pool)
69 private final long pool;
70
71 /**
72 * APR library singleton constructor. Called only when accessing the
73 * singleton the first time.
74 * It's initializing an APR memory pool for the whole package (a.k.a mother or root pool).
75 */
76 private AprLibrary() {
77 try {
78 Library.initialize(null);
79 } catch (Throwable t) {
80 throw new RuntimeException("Error loading Apache Portable Runtime (APR).", t);
81 }
82 pool = Pool.create(0);
83 }
84
85 @Override
86 protected void finalize() throws Throwable {
87 super.finalize();
88 Pool.destroy(pool);
89 }
90
91 /**
92 * get the package wide root pool, the mother of all the pool created
93 * in APR transport module.
94 * @return number identifying the root pool
95 */
96 long getRootPool() {
97 return pool;
98 }
99 }