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  /**
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   */
37  class AprLibrary {
38  
39      // is APR library was initialized (load of native libraries)
40      private static AprLibrary library = null;
41  
42      /**
43       * get the shared instance of APR library, if none, initialize one
44       * @return the current APR library singleton
45       */
46      static synchronized AprLibrary getInstance() {
47          if (!isInitialized())
48              initialize();
49          return library;
50      }
51  
52      /**
53       * initialize the APR Library by loading the associated native libraries
54       * and creating the associated singleton
55       */
56      private static synchronized void initialize() {
57          if (library == null)
58              library = new AprLibrary();
59      }
60  
61      /**
62       * is the APR library was initialized.
63       * @return true if the Library is initialized, false otherwise
64       */
65      static synchronized boolean isInitialized() {
66          return library != null;
67      }
68  
69      // APR memory pool (package wide mother pool)
70      private final long pool;
71  
72      /**
73       * APR library singleton constructor. Called only when accessing the
74       * singleton the first time.
75       * It's initializing an APR memory pool for the whole package (a.k.a mother or root pool).
76       */
77      private AprLibrary() {
78          try {
79              Library.initialize(null);
80          } catch (Exception e) {
81              throw new RuntimeException(
82                      "Error loading Apache Portable Runtime (APR).", e);
83          }
84          pool = Pool.create(0);
85      }
86  
87      @Override
88      protected void finalize() throws Throwable {
89          super.finalize();
90          Pool.destroy(pool);
91      }
92  
93      /**
94       * get the package wide root pool, the mother of all the pool created 
95       * in APR transport module.
96       * @return number identifying the root pool 
97       */
98      long getRootPool() {
99          return pool;
100     }
101 }