PCSC4Java  0.2
Library PCSC for Java language.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
D:/Affaire/Perso/SmartCardToolBox/pcsc4java-framework-0.2/src/fr/redbilled/security/pcscforjava/PlatformPCSC.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
00003  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00004  *
00005  * This code is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License version 2 only, as
00007  * published by the Free Software Foundation.  Oracle designates this
00008  * particular file as subject to the "Classpath" exception as provided
00009  * by Oracle in the LICENSE file that accompanied this code.
00010  *
00011  * This code is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00014  * version 2 for more details (a copy is included in the LICENSE file that
00015  * accompanied this code).
00016  *
00017  * You should have received a copy of the GNU General Public License version
00018  * 2 along with this work; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
00020  *
00021  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
00022  * or visit www.oracle.com if you need additional information or have any
00023  * questions.
00024  */
00025 
00026 package fr.redbilled.security.pcscforjava;
00027 
00028 import fr.redbilled.pcscforjava.PCSCResource;
00029 import java.io.File;
00030 import java.io.FileOutputStream;
00031 import java.io.IOException;
00032 import java.io.InputStream;
00033 import java.net.URL;
00034 import java.security.AccessController;
00035 import java.security.PrivilegedAction;
00036 import javax.swing.JOptionPane;
00037 
00038 
00039 // Platform specific code and constants
00040 class PlatformPCSC {
00041 
00042     static final Throwable initException;
00043     
00044     PlatformPCSC() {
00045         // empty
00046     }
00047 
00048     static {
00049         initException = loadLibrary();
00050     }
00051     
00057     private static String getPlatformName()
00058     {
00059         return(System.getProperty("os.name"));
00060     }
00061 
00066     private static boolean isPlatformLinux()
00067     {
00068         return(getPlatformName().contains("Linux"));
00069     }
00070 
00075     private static boolean isPlatformMac()
00076     {
00077         return(getPlatformName().contains("Mac"));
00078     }
00079 
00084     private static boolean isPlatformWindows()
00085     {
00086         return(getPlatformName().contains("Windows"));
00087     }
00088 
00093     private static boolean isPlatform64Bits()
00094     {
00095         // X86_64 ; amd64 ...
00096         return(System.getProperty("os.arch").indexOf("64") != -1);
00097     }
00098     
00104     private static boolean loadPCSCLibrary (String sName)    
00105     {
00106         boolean _bSuccess;
00107         String _sPrefix = "";
00108         String _sExtension = "";
00109         String _sFinalName = "";
00110 
00111         try {
00112             if(isPlatformMac())
00113             {
00114                 _sPrefix = "lib";
00115                 _sExtension = ".jnilib";
00116             }
00117             else if(isPlatformLinux())
00118             {
00119                 sName += "lux";
00120                 _sPrefix = "lib";
00121                 _sExtension = ".so";
00122             }
00123             else if(isPlatformWindows())
00124                 _sExtension = ".dll";
00125             else
00126                 throw new Exception();
00127 
00128             sName += (isPlatform64Bits()) ? "64" : "32";
00129             sName += "bits";
00130             
00131             _sFinalName = _sPrefix + sName + _sExtension;
00132 
00133             deleteAllTemporariesFiles(_sFinalName, _sExtension);
00134             
00135             // Retrieve the temporary DLL
00136             File _theDll = createTemporaryFile(_sFinalName, _sExtension);
00137             // Load the DLL from the filesystem
00138             System.load(_theDll.getAbsolutePath());
00139             
00140             _theDll.deleteOnExit();
00141         } catch (Exception _e) 
00142         {
00143             // The library is not available
00144             JOptionPane.showMessageDialog(null, "The library: " + _sFinalName +
00145                     " was not found!\n\n"
00146                     + "There is some possible reasons for "
00147                     + "this:\n"
00148                     + "\tThe platform may be not supported by the PCSC4Java - "
00149                     + "framework.\n"
00150                     + "\tThe library has been deleted in the PCSC4Java -"
00151                     + "framework file.\n\n"
00152                     + "The consequence is that the PCSC service will not be "
00153                     + "work from your application on this platform.\n\n"
00154                     + "If you have any doubt please contact the application "
00155                     + "developer.",
00156                     "Error: PCSC4Java - framework -> Library not found!",
00157                     JOptionPane.ERROR_MESSAGE);
00158             
00159             PCSCResource.setLibraryName(_sFinalName + "###");
00160             
00161             return false;
00162         }
00163         
00164         PCSCResource.setLibraryName(_sFinalName);
00165         
00166         //System.out.println("lib " + _sFinalName + " loaded");
00167         return true;
00168         }
00169 
00170     private static void deleteAllTemporariesFiles(String sName, String sExtension)
00171     {
00172         String  _sPrefix = sName.replaceAll(sExtension, "");
00173         File    _tmp;
00174         File    _folderTmp = new File(System.getProperty("java.io.tmpdir"));
00175         String[] _files = _folderTmp.list();
00176         int      _i = 0;
00177         
00178         while(_i < _files.length)
00179         {
00180             if(_files[_i].contains(_sPrefix))
00181             {
00182                 _tmp = new File(System.getProperty("java.io.tmpdir") + 
00183                         File.separator + _files[_i]);
00184                 _tmp.delete();
00185             }
00186             _i++;
00187         }
00188     }
00189     
00197     private static File createTemporaryFile(String sName, String sExtension)
00198     {
00199         InputStream _is = null;
00200         try {
00201             URL _url = PlatformPCSC.class.getResource(sName);
00202             _is = _url.openStream();
00203             /* Define the destination file */
00204             File _theDll = File.createTempFile(sName.replace(sExtension, ""), sExtension);
00205             
00206             _theDll.deleteOnExit();
00207             /* Open the destination file */
00208             FileOutputStream _fos = new FileOutputStream(_theDll);
00209             /* Copy the DLL from the JAR to the filesystem */
00210             byte[] _array = new byte[1024*4];
00211             for (int _i = _is.read(_array); _i != -1; _i = _is.read(_array)) {
00212                 _fos.write(_array, 0, _i);
00213             }
00214             /* Close all streams */
00215             _fos.close();
00216             _is.close();
00217             
00218             return _theDll;
00219         } catch (IOException ex) {
00220             return null;
00221         }
00222     }
00223 
00224     private static Throwable loadLibrary() {
00225         try {
00226             //AccessController.doPrivileged(new LoadLibraryAction("PCSC"));
00227             AccessController.doPrivileged(new PrivilegedAction() {
00228                 public Object run()
00229                 {
00230                     //System.load("D:\\Affaire\\Perso\\SmartCardToolBox\\pcsc4java-framework-0.1\\jni\\PCSC\\binaries\\release\\64_bits\\PCSC.dll");
00231                     boolean _bLoad = loadPCSCLibrary("PCSC4Java");
00232                     return null;
00233                 }
00234             });
00235             
00236             return null;
00237         } catch (Throwable e) {
00238             // The library is not available
00239             JOptionPane.showMessageDialog(null, "The library: " +
00240                     " was not found!\n\n"
00241                     + e.getMessage(),
00242                     "Error: PCSC4Java - framework -> Library not found!",
00243                     JOptionPane.ERROR_MESSAGE);
00244             
00245             return e;
00246         }
00247     }
00248    
00249     public static int unsignedByteToInt(byte b) 
00250     {
00251         return (int) b & 0xFF;
00252     }
00253 
00254     // PCSC constants defined differently under Windows and MUSCLE
00255     // Windows version
00256     final static int SCARD_PROTOCOL_UNDEFINED   =  0x0000;
00257     final static int SCARD_PROTOCOL_T0     =  0x0001;
00258     final static int SCARD_PROTOCOL_T1     =  0x0002;
00259     final static int SCARD_PROTOCOL_RAW    =  0x10000;
00260     final static int SCARD_PROTOCOL_Tx = (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1);
00261 
00262     final static int SCARD_UNKNOWN         =  0x0000;
00263     final static int SCARD_ABSENT          =  0x0001;
00264     final static int SCARD_PRESENT         =  0x0002;
00265     final static int SCARD_SWALLOWED       =  0x0003;
00266     final static int SCARD_POWERED         =  0x0004;
00267     final static int SCARD_NEGOTIABLE      =  0x0005;
00268     final static int SCARD_SPECIFIC        =  0x0006;
00269 
00270 }