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/pcscforjava/TerminalFactory.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, 2006, 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.pcscforjava;
00027 
00028 import fr.redbilled.security.pcscforjava.PCSC4Java;
00029 import java.util.*;
00030 
00031 import java.security.*;
00032 
00033 import sun.security.jca.*;
00034 import sun.security.jca.GetInstance.*;
00035 
00036 import sun.security.action.GetPropertyAction;
00037 
00092 public final class TerminalFactory {
00093 
00094     private final static String PROP_NAME =
00095                         "fr.redbilled.pcscforjava.TerminalFactory.DefaultType";
00096 
00097     private final static String defaultType;
00098 
00099     private final static TerminalFactory defaultFactory;
00100 
00101     private static List m_pnpCallbacks = new ArrayList<CardTerminalsEvent>();
00102 
00103     static
00104     {
00105         // lookup up the user specified type, default to PC/SC
00106         String type = AccessController.doPrivileged
00107                             (new GetPropertyAction(PROP_NAME, "PC/SC")).trim();
00108         TerminalFactory factory = null;
00109        
00110         try
00111         {
00112             type = "PC/SC";
00113             Provider _pcsc4Java = Security.getProvider("PCSC4Java");
00114             if (_pcsc4Java == null)
00115             {
00116                 _pcsc4Java = new PCSC4Java();
00117             }
00118             factory = TerminalFactory.getInstance(type, null, _pcsc4Java);
00119         }
00120         catch (Exception e) {System.err.println("Err load library: " + e.getMessage()); }
00121         
00122         if (factory == null)
00123         {
00124             type = "None";
00125             factory = new TerminalFactory
00126                         (NoneFactorySpi.INSTANCE, NoneProvider.INSTANCE, "None");
00127         }
00128 
00129         defaultType = type;
00130         defaultFactory = factory;
00131     }
00132 
00133     public static void setPnPCallback(CardTerminalsEvent cardTerminalsEvent)
00134     {
00135         m_pnpCallbacks.add(cardTerminalsEvent);
00136     }
00137 
00138     public static void removePnPCallback(CardTerminalsEvent cardTerminalsEvent)
00139     {
00140         for(int _i = 0; _i < m_pnpCallbacks.size(); _i++)
00141         {
00142             if(m_pnpCallbacks.get(_i) == cardTerminalsEvent)
00143             {
00144                 m_pnpCallbacks.remove(_i);
00145                 break;
00146             }
00147         }
00148     }
00149     
00150     public static List getPnPCallbacks()
00151     {
00152         return m_pnpCallbacks;
00153     }
00154 
00155     public void releaseContext() {
00156         defaultFactory.spi.destroyTerminals();
00157     }
00158 
00159     private static final class NoneProvider extends Provider {
00160         final static Provider INSTANCE = new NoneProvider();
00161         private NoneProvider() {
00162             super("None", 1.0d, "none");
00163         }
00164     }
00165 
00166     private static final class NoneFactorySpi extends TerminalFactorySpi {
00167         final static TerminalFactorySpi INSTANCE = new NoneFactorySpi();
00168         private NoneFactorySpi() {
00169             // empty
00170         }
00171         protected CardTerminals engineTerminals() {
00172             return NoneCardTerminals.INSTANCE;
00173         }
00174 
00175         @Override
00176         protected boolean destroyTerminals() {
00177             return false;
00178         }
00179     }
00180 
00181     private static final class NoneCardTerminals extends CardTerminals {
00182         final static CardTerminals INSTANCE = new NoneCardTerminals();
00183         private NoneCardTerminals() {
00184             // empty
00185         }
00186         public List<CardTerminal> list(State state) throws CardException {
00187             if (state == null) {
00188                 throw new NullPointerException();
00189             }
00190             return Collections.emptyList();
00191         }
00192         public boolean waitForChange(long timeout) throws CardException {
00193             throw new IllegalStateException("no terminals");
00194         }
00195 
00196         @Override
00197         public boolean isValidContext() {
00198             return false;
00199         }
00200 
00201         @Override
00202         public void closeContext() throws CardException {
00203             // Nothing
00204         }
00205 
00206         @Override
00207         public void updateCardTerminalsListByEvent() throws CardException {
00208             // empty
00209         }
00210 
00211         @Override
00212         public boolean isPlugAndPlaySupported() throws CardException {
00213             return false;
00214         }
00215     }
00216 
00217     private final TerminalFactorySpi spi;
00218 
00219     private final Provider provider;
00220 
00221     private final String type;
00222 
00223     private TerminalFactory(TerminalFactorySpi spi, Provider provider, String type) {
00224         this.spi = spi;
00225         this.provider = provider;
00226         this.type = type;
00227     }
00228 
00249     public static String getDefaultType() {
00250         return defaultType;
00251     }
00252 
00262     public static TerminalFactory getDefault() {
00263         return defaultFactory;
00264     }
00265 
00292     public static TerminalFactory getInstance(String type, Object params)
00293             throws NoSuchAlgorithmException {
00294 
00295         Instance instance = GetInstance.getInstance("TerminalFactory",
00296             TerminalFactorySpi.class, type, params);
00297         return new TerminalFactory((TerminalFactorySpi)instance.impl,
00298             instance.provider, type);
00299     }
00300 
00332     public static TerminalFactory getInstance(String type, Object params,
00333             String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
00334         Instance instance = GetInstance.getInstance("TerminalFactory",
00335             TerminalFactorySpi.class, type, params, provider);
00336         return new TerminalFactory((TerminalFactorySpi)instance.impl,
00337             instance.provider, type);
00338     }
00339 
00364     public static TerminalFactory getInstance(String type, Object params,
00365             Provider provider) throws NoSuchAlgorithmException {
00366         Instance instance = GetInstance.getInstance("TerminalFactory",
00367             TerminalFactorySpi.class, type, params, provider);
00368         return new TerminalFactory((TerminalFactorySpi)instance.impl,
00369             instance.provider, type);
00370     }
00371 
00377     public Provider getProvider() {
00378         return provider;
00379     }
00380 
00387     public String getType() {
00388         return type;
00389     }
00390 
00400     public CardTerminals terminals() {
00401         return spi.engineTerminals();
00402     }
00403 
00409     public String toString() {
00410         return "TerminalFactory for type " + type + " from provider "
00411             + provider.getName();
00412     }
00413 
00414 }