View Javadoc

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 corretcly the APR native library
27   * and the associated memory pool.
28   * 
29   * @author The Apache MINA Project (dev@mina.apache.org)
30   * @version $Rev: 596971 $, $Date: 2007-11-21 00:43:06 -0700 (Wed, 21 Nov 2007) $
31   */
32  class AprLibrary {
33  
34      // is APR library was initialized (load of native libraries)
35      private static AprLibrary library = null;
36  
37      static synchronized AprLibrary getInstance() {
38          if (!isInitialized())
39              initialize();
40          return library;
41      }
42  
43      static synchronized void initialize() {
44          if (library == null)
45              library = new AprLibrary();
46      }
47  
48      static synchronized boolean isInitialized() {
49          return library != null;
50      }
51  
52      // APR memory pool (package wide mother pool)	
53      private final long pool;
54  
55      private AprLibrary() {
56          try {
57              Library.initialize(null);
58          } catch (Exception e) {
59              throw new RuntimeException(
60                      "Error loading Apache Portable Runtime (APR).", e);
61          }
62          pool = Pool.create(0);
63      }
64  
65      @Override
66      protected void finalize() throws Throwable {
67          super.finalize();
68          Pool.destroy(pool);
69      }
70  
71      long getRootPool() {
72          return pool;
73      }
74  }