KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coi > tools > os > izpack > COIOSHelper


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2005 Klaus Bartz
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.coi.tools.os.izpack;
23
24 import com.izforge.izpack.util.Librarian;
25 import com.izforge.izpack.util.NativeLibraryClient;
26
27 /**
28  *
29  * Base class to handle multiple native methods of multiple classes in one shared library. This is a
30  * singelton class.
31  *
32  * @author Klaus Bartz
33  *
34  */

35 public class COIOSHelper
36 {
37
38     private static COIOSHelper self = null;
39
40     private static int used = 0;
41
42     private static boolean destroyed = false;
43
44     /**
45      * This method is used to free the library at the end of progam execution. After this call, any
46      * instance of this class will not be usable any more!
47      *
48      * @param name the name of the library to free. Use only the name and extension but not the
49      * path.
50      */

51     private native void FreeLibrary(String JavaDoc name);
52
53     /**
54      * Default constructor, do not use
55      */

56     private COIOSHelper()
57     {
58         super();
59     }
60
61     /**
62      * Returns the one existent object of this class.
63      *
64      * @return the one existent object of this class
65      */

66     public static synchronized COIOSHelper getInstance()
67     {
68         if (self == null) self = new COIOSHelper();
69         return (self);
70
71     }
72
73     /*--------------------------------------------------------------------------*/
74     /**
75      * This method is used to free the library at the end of progam execution. This is the method of
76      * the helper class which will be called from other objects. After this call, any instance of
77      * this class will not be usable any more! <b><i><u>Note that this method does NOT return </u>
78      * at the first call, but at any other </i> </b> <br>
79      * <br>
80      * <b>DO NOT CALL THIS METHOD DIRECTLY! </b> <br>
81      * It is used by the librarian to free the native library before physically deleting it from its
82      * temporary loaction. A call to this method will freeze the application irrecoverably!
83      *
84      * @param name the name of the library to free. Use only the name and extension but not the
85      * path.
86      *
87      * @see com.izforge.izpack.util.NativeLibraryClient#freeLibrary
88      */

89     /*--------------------------------------------------------------------------*/
90     /**
91      * @param name
92      */

93     public void freeLibrary(String JavaDoc name)
94     {
95         used--;
96         if (!destroyed)
97         {
98             FreeLibrary(name);
99             destroyed = true;
100         }
101     }
102
103     /**
104      * Add a NativeLibraryClient as dependant to this object. The method tries to load the shared
105      * library COIOSHelper which should contain native methods for the dependant.
106      *
107      * @param dependant to be added
108      * @throws Exception if loadLibrary for the needed lib fails
109      */

110     public void addDependant(NativeLibraryClient dependant) throws Exception JavaDoc
111     {
112         used++;
113         try
114         {
115             Librarian.getInstance().loadLibrary("COIOSHelper", dependant);
116         }
117         catch (UnsatisfiedLinkError JavaDoc exception)
118         {
119             throw (new Exception JavaDoc("could not locate native library"));
120         }
121
122     }
123
124 }
125
Popular Tags