use of org.openecard.common.event.EventDispatcherImpl in project open-ecard by ecsec.
the class TestClient method setup.
private void setup() throws Exception {
// Set up client environment
ClientEnv env = new ClientEnv();
// Set up the IFD
IFD ifd = new IFD();
env.setIFD(ifd);
// Set up Management
TinyManagement management = new TinyManagement(env);
env.setManagement(management);
// Set up the Dispatcher
MessageDispatcher dispatcher = new MessageDispatcher(env);
env.setDispatcher(dispatcher);
// Perform an EstablishContext to get a ContextHandle
EstablishContext establishContext = new EstablishContext();
EstablishContextResponse establishContextResponse = ifd.establishContext(establishContext);
byte[] contextHandle = ifd.establishContext(establishContext).getContextHandle();
final CardRecognitionImpl recognition = new CardRecognitionImpl(env);
env.setRecognition(recognition);
env.setCIFProvider(new CIFProvider() {
@Override
public CardInfoType getCardInfo(ConnectionHandleType type, String cardType) {
return recognition.getCardInfo(cardType);
}
@Override
public boolean needsRecognition(byte[] atr) {
return true;
}
@Override
public CardInfoType getCardInfo(String cardType) throws RuntimeException {
return recognition.getCardInfo(cardType);
}
@Override
public InputStream getCardImage(String cardType) {
return recognition.getCardImage(cardType);
}
});
// Set up EventManager
EventDispatcher ed = new EventDispatcherImpl();
env.setEventDispatcher(ed);
// Set up SALStateCallback
cardStates = new CardStateMap();
SALStateCallback salCallback = new SALStateCallback(env, cardStates);
ed.add(salCallback);
// Set up SAL
sal = new TinySAL(env, cardStates);
env.setSAL(sal);
// Set up GUI
SwingUserConsent gui = new SwingUserConsent(new SwingDialogWrapper());
sal.setGUI(gui);
ifd.setGUI(gui);
// Initialize the EventManager
ed.start();
AddonManager manager = new AddonManager(env, gui, cardStates, null);
sal.setAddonManager(manager);
HttpBinding binding = new HttpBinding(24727);
binding.setAddonManager(manager);
binding.start();
}
use of org.openecard.common.event.EventDispatcherImpl in project open-ecard by ecsec.
the class TestEventManager method test.
@Test(enabled = false)
public void test() throws InterruptedException, InitializationException {
Environment env = new ClientEnv();
MiddlewareSAL mwSAL = new MiddlewareSAL(env, null, mwConfig, cb);
env.setSAL(mwSAL);
EventDispatcher ed = new EventDispatcherImpl();
env.setEventDispatcher(ed);
mwSAL.initialize(new Initialize());
ed.start();
Thread.sleep(10000);
}
use of org.openecard.common.event.EventDispatcherImpl in project open-ecard by ecsec.
the class RichClient method setup.
public void setup() {
GUIDefaults.initialize();
String title = LANG.translationForKey("client.startup.failed.headline", AppVersion.getName());
String message = null;
// Set up GUI
SwingUserConsent gui = new SwingUserConsent(new SwingDialogWrapper());
try {
tray = new AppTray(this);
tray.beginSetup();
// Set up client environment
env = new ClientEnv();
// Set up the Dispatcher
MessageDispatcher dispatcher = new MessageDispatcher(env);
env.setDispatcher(dispatcher);
// Set up EventDispatcherImpl
eventDispatcher = new EventDispatcherImpl();
env.setEventDispatcher(eventDispatcher);
// Set up Management
TinyManagement management = new TinyManagement(env);
env.setManagement(management);
// Set up MiddlewareConfig
MiddlewareConfigLoader mwConfigLoader = new MiddlewareConfigLoader();
List<MiddlewareSALConfig> mwSALConfigs = mwConfigLoader.getMiddlewareSALConfigs();
// Set up CardRecognitionImpl
recognition = new CardRecognitionImpl(env);
recognition.setGUI(gui);
env.setRecognition(recognition);
// Set up StateCallbacks
cardStates = new CardStateMap();
SALStateCallback salCallback = new SALStateCallback(env, cardStates);
eventDispatcher.add(salCallback);
// Set up the IFD
ifd = new IFD();
ifd.addProtocol(ECardConstants.Protocol.PACE, new PACEProtocolFactory());
ifd.setGUI(gui);
ifd.setEnvironment(env);
env.setIFD(ifd);
// Set up SAL
TinySAL mainSal = new TinySAL(env, cardStates);
mainSal.setGUI(gui);
sal = new SelectorSAL(mainSal, env);
env.setSAL(sal);
env.setCIFProvider(sal);
// Set up Middleware SAL
MwStateCallback mwCallback = new MwStateCallback(env, cardStates, mwConfigLoader);
for (MiddlewareSALConfig mwSALConfig : mwSALConfigs) {
if (!mwSALConfig.isDisabled()) {
MiddlewareSAL mwSal = new MiddlewareSAL(env, cardStates, mwSALConfig, mwCallback);
mwSal.setGui(gui);
sal.addSpecializedSAL(mwSal);
}
}
// Start up control interface
SettingsAndDefaultViewWrapper guiWrapper = new SettingsAndDefaultViewWrapper();
try {
manager = new AddonManager(env, gui, cardStates, guiWrapper);
guiWrapper.setAddonManager(manager);
mainSal.setAddonManager(manager);
// initialize http binding
int port = 24727;
boolean dispatcherMode = false;
WinReg.HKEY hk = WinReg.HKEY_LOCAL_MACHINE;
String regPath = "SOFTWARE\\" + OpenecardProperties.getProperty("registry.app_name");
if (Platform.isWindows()) {
LOG.debug("Checking if dispatcher mode should be used.");
try {
if (regKeyExists(hk, regPath, "Dispatcher_Mode")) {
String value = Advapi32Util.registryGetStringValue(hk, regPath, "Dispatcher_Mode");
dispatcherMode = Boolean.valueOf(value);
// let socket chose its port
port = 0;
}
} catch (Win32Exception ex) {
LOG.warn("Failed to read 'Dispatcher_Mode' registry key. Using normal operation mode.", ex);
}
}
if (!dispatcherMode) {
try {
port = Integer.parseInt(OpenecardProperties.getProperty("http-binding.port"));
} catch (NumberFormatException ex) {
LOG.warn("Error in config file, HTTP binding port is malformed.");
}
}
// start HTTP server
httpBinding = new HttpBinding(port);
httpBinding.setAddonManager(manager);
httpBinding.start();
if (dispatcherMode) {
long waitTime = getRegInt(hk, regPath, "Retry_Wait_Time", 5000L);
long timeout = getRegInt(hk, regPath, "DP_Timeout", 3600000L);
// try to register with dispatcher service
LOG.debug("Trying to register HTTP binding port with dispatcher service.");
final int realPort = httpBinding.getPort();
final URL regUrl = new URL("http://127.0.0.1:24727/dp/register");
FutureTask ft = new FutureTask(new DispatcherRegistrator(regUrl, realPort, waitTime, timeout), 1);
Thread registerThread = new Thread(ft, "Register-Dispatcher-Service");
registerThread.setDaemon(true);
registerThread.start();
// wait until thread is finished
ft.get();
}
} catch (BindException e) {
message = LANG.translationForKey("client.startup.failed.portinuse", AppVersion.getName());
throw e;
}
tray.endSetup(env, manager);
// Initialize the EventManager
eventDispatcher.add(tray.status(), EventType.TERMINAL_ADDED, EventType.TERMINAL_REMOVED, EventType.CARD_INSERTED, EventType.CARD_RECOGNIZED, EventType.CARD_REMOVED);
// start event dispatcher
eventDispatcher.start();
// initialize SAL
WSHelper.checkResult(sal.initialize(new Initialize()));
// Perform an EstablishContext to get a ContextHandle
try {
EstablishContext establishContext = new EstablishContext();
EstablishContextResponse establishContextResponse = ifd.establishContext(establishContext);
WSHelper.checkResult(establishContextResponse);
contextHandle = establishContextResponse.getContextHandle();
} catch (WSHelper.WSException ex) {
message = LANG.translationForKey("client.startup.failed.nocontext");
throw ex;
}
// perform GC to bring down originally allocated memory
new Timer().schedule(new GCTask(), 5000);
} catch (Exception ex) {
LOG.error(ex.getMessage(), ex);
if (message == null || message.isEmpty()) {
// Add exception message if no custom message is set
message = ex.getMessage();
}
// Show dialog to the user and shut down the client
String msg = String.format("%s%n%n%s", title, message);
gui.obtainMessageDialog().showMessageDialog(msg, AppVersion.getName(), DialogType.ERROR_MESSAGE);
teardown();
} catch (Throwable ex) {
LOG.error("Unexpected error occurred. Exiting client.", ex);
System.exit(1);
}
}
use of org.openecard.common.event.EventDispatcherImpl 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