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 /**
27 * Internal singleton used for initializing correctly the APR native library
28 * and the associated root memory pool.
29 *
30 * It'll finalize nicely the native resources (libraries and memory pools).
31 *
32 * Each memory pool used in the APR transport module needs to be children of the
33 * root pool {@link AprLibrary#getRootPool()}.
34 *
35 * @author The Apache MINA Project (dev@mina.apache.org)
36 * @version $Rev: 671930 $, $Date: 2008-06-26 17:58:30 +0200 (jeu, 26 jun 2008) $
37 */
38 class AprLibrary {
39
40 // is APR library was initialized (load of native libraries)
41 private static AprLibrary library = null;
42
43 /**
44 * get the shared instance of APR library, if none, initialize one
45 * @return the current APR library singleton
46 */
47 static synchronized AprLibrary getInstance() {
48 if (!isInitialized())
49 initialize();
50 return library;
51 }
52
53 /**
54 * initialize the APR Library by loading the associated native libraries
55 * and creating the associated singleton
56 */
57 private static synchronized void initialize() {
58 if (library == null)
59 library = new AprLibrary();
60 }
61
62 /**
63 * is the APR library was initialized.
64 * @return
65 */
66 static synchronized boolean isInitialized() {
67 return library != null;
68 }
69
70 // APR memory pool (package wide mother pool)
71 private final long pool;
72
73 /**
74 * APR library singleton constructor. Called only when accessing the
75 * singleton the first time.
76 * It's initializing an APR memory pool for the whole package (a.k.a mother or root pool).
77 */
78 private AprLibrary() {
79 try {
80 Library.initialize(null);
81 } catch (Exception e) {
82 throw new RuntimeException(
83 "Error loading Apache Portable Runtime (APR).", e);
84 }
85 pool = Pool.create(0);
86 }
87
88 @Override
89 protected void finalize() throws Throwable {
90 super.finalize();
91 Pool.destroy(pool);
92 }
93
94 /**
95 * get the package wide root pool, the mother of all the pool created
96 * in APR transport module.
97 * @return number identifying the root pool
98 */
99 long getRootPool() {
100 return pool;
101 }
102 }