KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > php > Library


1 /*
2 /*
3  * JBoss, Home of Professional Open Source
4  * Copyright 2006, JBoss Inc., and individual contributors as indicated
5  * by the @authors tag. See the copyright.txt in the distribution for a
6  * full listing of individual contributors.
7  *
8  * This is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as
10  * published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This software is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this software; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22  */

23
24 package org.jboss.web.php;
25
26 /**
27  * Library class.
28  *
29  * @author Mladen Turk
30  * @version $Revision: 4354 $, $Date: 2006-05-22 19:53:20 +0200 (lun., 22 mai 2006) $
31  * @since 1.0
32  */

33 public class Library {
34
35     /* Default library names */
36     private static String JavaDoc [] NAMES = { "php5servlet", "libphp5servlet" };
37
38     /*
39      * A handle to the unique Library singleton instance.
40      */

41     private static Library engine = null;
42     private static boolean inited = false;
43
44     private Library()
45     {
46         boolean loaded = false;
47         String JavaDoc err = "";
48         for (int i = 0; i < NAMES.length; i++) {
49             try {
50                 System.loadLibrary(NAMES[i]);
51                 loaded = true;
52             } catch (Throwable JavaDoc e) {
53                 if ( i > 0)
54                     err += ", ";
55                 err += e.getMessage();
56             }
57             if (loaded)
58                 break;
59         }
60         if (!loaded) {
61             err += "(";
62             err += System.getProperty("java.library.path");
63             err += ")";
64             throw new UnsatisfiedLinkError JavaDoc(err);
65         }
66     }
67
68     private Library(String JavaDoc libraryName)
69     {
70         System.loadLibrary(libraryName);
71     }
72
73     /* PHP_MAJOR_VERSION */
74     public static int PHP_MAJOR_VERSION = 0;
75     /* PHP_MINOR_VERSION */
76     public static int PHP_MINOR_VERSION = 0;
77     /* PHP_PATCH_VERSION */
78     public static int PHP_PATCH_VERSION = 0;
79
80     /* Initialize PHP Engine
81      * This has to be the first call to PHP Module.
82      */

83     private static native boolean startup();
84
85     /* destroy global PHP Engine
86      * This has to be the last call to PHP Module.
87      */

88     public static native void shutdown();
89
90     /* Internal function for obtaining PHP Module version */
91     private static native int version(int index);
92
93     /**
94      * Setup any PHP internal data structures. This MUST be the
95      * first function called for PHP module.
96      * @param libraryName the name of the library to load
97      */

98     public static boolean initialize(String JavaDoc libraryName)
99         throws Exception JavaDoc
100     {
101         if (engine == null) {
102             if (libraryName == null)
103                 engine = new Library();
104             else
105                 engine = new Library(libraryName);
106             PHP_MAJOR_VERSION = version(1);
107             PHP_MINOR_VERSION = version(2);
108             PHP_PATCH_VERSION = version(3);
109         }
110         inited = startup();
111         return inited;
112     }
113
114     /**
115      * Check if PHP module is initialized.
116      */

117     public static boolean isInitialized()
118     {
119         return inited;
120     }
121
122     /**
123      * Terminate PHP Engine.
124      */

125     public static void terminate()
126     {
127         if (engine != null) {
128             shutdown();
129             engine = null;
130             inited = false;
131         }
132     }
133 }
134
Popular Tags