KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > jni > Library


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.jni;
19
20 /** Library
21  *
22  * @author Mladen Turk
23  * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
24  */

25
26 public final class Library {
27
28     /* Default library names */
29     private static String JavaDoc [] NAMES = {"tcnative-1", "libtcnative-1"};
30     /*
31      * A handle to the unique Library singleton instance.
32      */

33     static private Library _instance = null;
34
35     private Library()
36     {
37         boolean loaded = false;
38         String JavaDoc err = "";
39         for (int i = 0; i < NAMES.length; i++) {
40             try {
41                 System.loadLibrary(NAMES[i]);
42                 loaded = true;
43             }
44             catch (Throwable JavaDoc e) {
45                 if ( i > 0)
46                     err += ", ";
47                 err += e.getMessage();
48             }
49             if (loaded)
50                 break;
51         }
52         if (!loaded) {
53             err += "(";
54             err += System.getProperty("java.library.path");
55             err += ")";
56             throw new UnsatisfiedLinkError JavaDoc(err);
57         }
58     }
59
60     private Library(String JavaDoc libraryName)
61     {
62         System.loadLibrary(libraryName);
63     }
64
65     /* create global TCN's APR pool
66      * This has to be the first call to TCN library.
67      */

68     private static native boolean initialize();
69     /* destroy global TCN's APR pool
70      * This has to be the last call to TCN library.
71      */

72     public static native void terminate();
73     /* Internal function for loading APR Features */
74     private static native boolean has(int what);
75     /* Internal function for loading APR Features */
76     private static native int version(int what);
77     /* Internal function for loading APR sizes */
78     private static native int size(int what);
79
80     /* TCN_MAJOR_VERSION */
81     public static int TCN_MAJOR_VERSION = 0;
82     /* TCN_MINOR_VERSION */
83     public static int TCN_MINOR_VERSION = 0;
84     /* TCN_PATCH_VERSION */
85     public static int TCN_PATCH_VERSION = 0;
86     /* TCN_IS_DEV_VERSION */
87     public static int TCN_IS_DEV_VERSION = 0;
88     /* APR_MAJOR_VERSION */
89     public static int APR_MAJOR_VERSION = 0;
90     /* APR_MINOR_VERSION */
91     public static int APR_MINOR_VERSION = 0;
92     /* APR_PATCH_VERSION */
93     public static int APR_PATCH_VERSION = 0;
94     /* APR_IS_DEV_VERSION */
95     public static int APR_IS_DEV_VERSION = 0;
96
97     /* TCN_VERSION_STRING */
98     public static native String JavaDoc versionString();
99     /* APR_VERSION_STRING */
100     public static native String JavaDoc aprVersionString();
101
102     /* APR Feature Macros */
103     public static boolean APR_HAVE_IPV6 = false;
104     public static boolean APR_HAS_SHARED_MEMORY = false;
105     public static boolean APR_HAS_THREADS = false;
106     public static boolean APR_HAS_SENDFILE = false;
107     public static boolean APR_HAS_MMAP = false;
108     public static boolean APR_HAS_FORK = false;
109     public static boolean APR_HAS_RANDOM = false;
110     public static boolean APR_HAS_OTHER_CHILD = false;
111     public static boolean APR_HAS_DSO = false;
112     public static boolean APR_HAS_SO_ACCEPTFILTER = false;
113     public static boolean APR_HAS_UNICODE_FS = false;
114     public static boolean APR_HAS_PROC_INVOKED = false;
115     public static boolean APR_HAS_USER = false;
116     public static boolean APR_HAS_LARGE_FILES = false;
117     public static boolean APR_HAS_XTHREAD_FILES = false;
118     public static boolean APR_HAS_OS_UUID = false;
119     /* Are we big endian? */
120     public static boolean APR_IS_BIGENDIAN = false;
121     /* APR sets APR_FILES_AS_SOCKETS to 1 on systems where it is possible
122      * to poll on files/pipes.
123      */

124     public static boolean APR_FILES_AS_SOCKETS = false;
125     /* This macro indicates whether or not EBCDIC is the native character set.
126      */

127     public static boolean APR_CHARSET_EBCDIC = false;
128     /* Is the TCP_NODELAY socket option inherited from listening sockets?
129      */

130     public static boolean APR_TCP_NODELAY_INHERITED = false;
131     /* Is the O_NONBLOCK flag inherited from listening sockets?
132      */

133     public static boolean APR_O_NONBLOCK_INHERITED = false;
134
135
136     public static int APR_SIZEOF_VOIDP;
137     public static int APR_PATH_MAX;
138     public static int APRMAXHOSTLEN;
139     public static int APR_MAX_IOVEC_SIZE;
140     public static int APR_MAX_SECS_TO_LINGER;
141     public static int APR_MMAP_THRESHOLD;
142     public static int APR_MMAP_LIMIT;
143
144     /* return global TCN's APR pool */
145     public static native long globalPool();
146
147     /**
148      * Setup any APR internal data structures. This MUST be the first function
149      * called for any APR library.
150      * @param libraryName the name of the library to load
151      */

152     static public boolean initialize(String JavaDoc libraryName)
153         throws Exception JavaDoc
154     {
155         if (_instance == null) {
156             if (libraryName == null)
157                 _instance = new Library();
158             else
159                 _instance = new Library(libraryName);
160             TCN_MAJOR_VERSION = version(0x01);
161             TCN_MINOR_VERSION = version(0x02);
162             TCN_PATCH_VERSION = version(0x03);
163             TCN_IS_DEV_VERSION = version(0x04);
164             APR_MAJOR_VERSION = version(0x11);
165             APR_MINOR_VERSION = version(0x12);
166             APR_PATCH_VERSION = version(0x13);
167             APR_IS_DEV_VERSION = version(0x14);
168
169             APR_SIZEOF_VOIDP = size(1);
170             APR_PATH_MAX = size(2);
171             APRMAXHOSTLEN = size(3);
172             APR_MAX_IOVEC_SIZE = size(4);
173             APR_MAX_SECS_TO_LINGER = size(5);
174             APR_MMAP_THRESHOLD = size(6);
175             APR_MMAP_LIMIT = size(7);
176
177             APR_HAVE_IPV6 = has(0);
178             APR_HAS_SHARED_MEMORY = has(1);
179             APR_HAS_THREADS = has(2);
180             APR_HAS_SENDFILE = has(3);
181             APR_HAS_MMAP = has(4);
182             APR_HAS_FORK = has(5);
183             APR_HAS_RANDOM = has(6);
184             APR_HAS_OTHER_CHILD = has(7);
185             APR_HAS_DSO = has(8);
186             APR_HAS_SO_ACCEPTFILTER = has(9);
187             APR_HAS_UNICODE_FS = has(10);
188             APR_HAS_PROC_INVOKED = has(11);
189             APR_HAS_USER = has(12);
190             APR_HAS_LARGE_FILES = has(13);
191             APR_HAS_XTHREAD_FILES = has(14);
192             APR_HAS_OS_UUID = has(15);
193             APR_IS_BIGENDIAN = has(16);
194             APR_FILES_AS_SOCKETS = has(17);
195             APR_CHARSET_EBCDIC = has(18);
196             APR_TCP_NODELAY_INHERITED = has(19);
197             APR_O_NONBLOCK_INHERITED = has(20);
198             if (APR_MAJOR_VERSION < 1) {
199                 throw new UnsatisfiedLinkError JavaDoc("Unsupported APR Version (" +
200                                                aprVersionString() + ")");
201             }
202             if (!APR_HAS_THREADS) {
203                 throw new UnsatisfiedLinkError JavaDoc("Missing APR_HAS_THREADS");
204             }
205         }
206         return initialize();
207     }
208 }
209
Popular Tags