use of org.openecard.addon.AddonManager in project open-ecard by ecsec.
the class OpeneCardContext method initialize.
// /
// / Initialization & Shutdown
// /
void initialize() throws UnableToInitialize, NfcUnavailable, NfcDisabled, ApduExtLengthNotSupported {
String errorMsg = SERVICE_RESPONSE_FAILED;
if (initialized) {
throw new UnableToInitialize(SERVICE_ALREADY_INITIALIZED);
}
if (appCtx == null) {
throw new IllegalStateException(NO_APPLICATION_CONTEXT);
}
// initialize gui
eacNavFac = new EacNavigatorFactory();
List<UserConsentNavigatorFactory<?>> factories = Arrays.asList(eacNavFac, new InsertCardNavigatorFactory());
gui = new AndroidUserConsent(factories);
// set up nfc and android marshaller
IFDProperties.setProperty(IFD_FACTORY_KEY, IFD_FACTORY_VALUE);
WsdefProperties.setProperty(WSDEF_MARSHALLER_KEY, WSDEF_MARSHALLER_VALUE);
NFCFactory.setContext(appCtx);
try {
boolean nfcAvailable = NFCFactory.isNFCAvailable();
boolean nfcEnabled = NFCFactory.isNFCEnabled();
boolean nfcExtendedLengthSupport = NfcUtils.supportsExtendedLength(appCtx);
if (!nfcAvailable) {
throw new NfcUnavailable();
} else if (!nfcEnabled) {
throw new NfcDisabled();
} else if (!nfcExtendedLengthSupport) {
throw new ApduExtLengthNotSupported(NFC_NO_EXTENDED_LENGTH_SUPPORT);
}
terminalFactory = IFDTerminalFactory.getInstance();
LOG.info("Terminal factory initialized.");
} catch (IFDException ex) {
errorMsg = UNABLE_TO_INITIALIZE_TF;
throw new UnableToInitialize(errorMsg, ex);
}
try {
// set up client environment
env = new ClientEnv();
// set up dispatcher
dispatcher = new MessageDispatcher(env);
env.setDispatcher(dispatcher);
LOG.info("Message Dispatcher initialized.");
// set up management
management = new TinyManagement(env);
env.setManagement(management);
LOG.info("Management initialized.");
// set up event dispatcher
eventDispatcher = new EventDispatcherImpl();
env.setEventDispatcher(eventDispatcher);
LOG.info("Event Dispatcher initialized.");
// set up SALStateCallback
cardStates = new CardStateMap();
SALStateCallback salCallback = new SALStateCallback(env, cardStates);
eventDispatcher.add(salCallback);
// set up ifd
ifd = new IFD();
ifd.addProtocol(ECardConstants.Protocol.PACE, new PACEProtocolFactory());
ifd.setGUI(gui);
ifd.setEnvironment(env);
env.setIFD(ifd);
LOG.info("IFD initialized.");
// set up card recognition
try {
recognition = new CardRecognitionImpl(env);
recognition.setGUI(gui);
env.setRecognition(recognition);
LOG.info("CardRecognition initialized.");
} catch (Exception ex) {
errorMsg = CARD_REC_INIT_FAILED;
throw ex;
}
// set up SAL
TinySAL mainSAL = new TinySAL(env, cardStates);
mainSAL.setGUI(gui);
sal = new SelectorSAL(mainSAL, env);
env.setSAL(sal);
env.setCIFProvider(sal);
LOG.info("SAL initialized.");
ViewController viewController = new ViewController() {
@Override
public void showSettingsUI() {
}
@Override
public void showDefaultViewUI() {
}
};
// set up addon manager
try {
manager = new AddonManager(env, gui, cardStates, viewController, new ClasspathRegistry());
mainSAL.setAddonManager(manager);
} catch (Exception ex) {
errorMsg = ADD_ON_INIT_FAILED;
throw ex;
}
// Initialize the Event Dispatcher
eventDispatcher.add(this, EventType.TERMINAL_ADDED, EventType.TERMINAL_REMOVED, EventType.CARD_INSERTED, EventType.CARD_RECOGNIZED, EventType.CARD_REMOVED);
// start event dispatcher
eventDispatcher.start();
LOG.info("Event dispatcher started.");
// initialize SAL
try {
WSHelper.checkResult(sal.initialize(new Initialize()));
} catch (WSHelper.WSException ex) {
errorMsg = ex.getMessage();
throw ex;
}
// establish context
try {
EstablishContext establishContext = new EstablishContext();
EstablishContextResponse establishContextResponse = ifd.establishContext(establishContext);
WSHelper.checkResult(establishContextResponse);
contextHandle = establishContextResponse.getContextHandle();
LOG.info("ContextHandle: {}", ByteUtils.toHexString(contextHandle));
} catch (WSHelper.WSException ex) {
errorMsg = ESTABLISH_IFD_CONTEXT_FAILED;
throw ex;
}
initialized = true;
} catch (Exception ex) {
LOG.error(errorMsg, ex);
throw new UnableToInitialize(errorMsg, ex);
}
}
Aggregations