001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2005 Klaus Bartz
008 *
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.coi.tools.os.izpack;
023
024import com.izforge.izpack.util.Librarian;
025import com.izforge.izpack.util.NativeLibraryClient;
026
027/**
028 * 
029 * Base class to handle multiple native methods of multiple classes in one shared library. This is a
030 * singelton class.
031 * 
032 * @author Klaus Bartz
033 * 
034 */
035public class COIOSHelper
036{
037
038    private static COIOSHelper self = null;
039
040    private static int used = 0;
041
042    private static boolean destroyed = false;
043
044    /**
045     * This method is used to free the library at the end of progam execution. After this call, any
046     * instance of this class will not be usable any more!
047     */
048    private native void FreeLibrary(String name);
049
050    /**
051     * Default constructor, do not use
052     */
053    private COIOSHelper()
054    {
055        super();
056    }
057
058    /**
059     * Returns the one existent object of this class.
060     */
061    public static synchronized COIOSHelper getInstance()
062    {
063        if (self == null) self = new COIOSHelper();
064        return (self);
065
066    }
067
068    /*--------------------------------------------------------------------------*/
069    /**
070     * This method is used to free the library at the end of progam execution. This is the method of
071     * the helper class which will be called from other objects. After this call, any instance of
072     * this class will not be usable any more! <b><i><u>Note that this method does NOT return </u>
073     * at the first call, but at any other </i> </b> <br>
074     * <br>
075     * <b>DO NOT CALL THIS METHOD DIRECTLY! </b> <br>
076     * It is used by the librarian to free the native library before physically deleting it from its
077     * temporary loaction. A call to this method will freeze the application irrecoverably!
078     * 
079     * @param name the name of the library to free. Use only the name and extension but not the
080     * path.
081     * 
082     * @see com.izforge.izpack.util.NativeLibraryClient#freeLibrary
083     */
084    /*--------------------------------------------------------------------------*/
085    /**
086     * @param name
087     */
088    public void freeLibrary(String name)
089    {
090        used--;
091        if (!destroyed)
092        {
093            FreeLibrary(name);
094            destroyed = true;
095        }
096    }
097
098    /**
099     * Add a NativeLibraryClient as dependant to this object. The method tries to load the shared
100     * library COIOSHelper which should contain native methods for the dependant.
101     * 
102     * @param dependant to be added
103     */
104    public void addDependant(NativeLibraryClient dependant) throws Exception
105    {
106        used++;
107        try
108        {
109            Librarian.getInstance().loadLibrary("COIOSHelper", dependant);
110        }
111        catch (UnsatisfiedLinkError exception)
112        {
113            throw (new Exception("could not locate native library"));
114        }
115
116    }
117
118}