/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. * * $Id: ConnectorFactory.java,v 1.4 2006/10/31 08:18:12 wprinz Exp $ */ package at.knowcenter.wag.egov.egiz.sig; import java.lang.reflect.Field; import org.apache.log4j.Logger; import at.gv.egiz.pdfas.api.commons.Constants; import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.exceptions.ConnectorFactoryException; import at.knowcenter.wag.egov.egiz.exceptions.SettingsException; import at.knowcenter.wag.egov.egiz.sig.connectors.A1Connector; import at.knowcenter.wag.egov.egiz.sig.connectors.BKUConnector; import at.knowcenter.wag.egov.egiz.sig.connectors.ConnectorConfigurationKeys; import at.knowcenter.wag.egov.egiz.sig.connectors.MOAConnector; import at.knowcenter.wag.egov.egiz.sig.connectors.mocca.LocRefDetachedMOCCAConnector; /** * This is a factory for creating the appropriate connector according to the * connector identifier. * * @deprecated this code is far too complicated * * @see at.knowcenter.wag.egov.egiz.sig.ConnectorInformation * @author wprinz */ public abstract class ConnectorFactory { /** * The name of the field that holds the Connector implementation's unique * identifier. * *

* This must be a public static final String on the Connector implementation * class. *

*/ protected static final String CONNECTOR_INFORMATION_FIELD_NAME = "CONNECTOR_INFORMATION"; /** * The list of available Connector implementations. * *

* Note that this could also be generated dynamically from a config file, * preferably enveloped by a Singleton. *

*/ protected static Class[] AVAILABLE_CONNECTORS = { MOAConnector.class, BKUConnector.class, A1Connector.class }; /** * The logger definition. */ private static final Logger logger_ = ConfigLogger.getLogger(ConnectorFactory.class); /** * Retrieves the ConnectorInformation from the connector Class. * * @param connector_class * The connector Class. * @return Returns the ConnectorInformation. * @throws IllegalArgumentException * F.e. * @throws IllegalAccessException * F.e. * @throws SecurityException * F.e. * @throws NoSuchFieldException * F.e. */ protected static ConnectorInformation getConnectorInformationFromClass( Class connector_class) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException { Field type_field = connector_class.getField(CONNECTOR_INFORMATION_FIELD_NAME); ConnectorInformation connector_information = (ConnectorInformation) type_field.get(null); return connector_information; } /** * Gathers the ConnectorInformation objects of all registered connectors. * *

* This is used by the user interface to provide a list of all available * connectors. *

* * @return Returns the ConnectorInformation objects. * @throws ConnectorFactoryException * F.e. */ public static ConnectorInformation[] getConnectorInformationArray() throws ConnectorFactoryException { ConnectorInformation[] coninf = new ConnectorInformation[AVAILABLE_CONNECTORS.length]; for (int i = 0; i < coninf.length; i++) { try { coninf[i] = getConnectorInformationFromClass(AVAILABLE_CONNECTORS[i]); } catch (Exception e) { throw new ConnectorFactoryException(e); } } return coninf; } /** * Retrieves the connector Class belonging to the connector id. * * @param connector_identifier * The connector id. * @return Returns the corresponding connector class. * @throws ConnectorFactoryException * Thrown, if the id is invalid. */ protected static Class getConnectorClass(String connector_identifier) throws ConnectorFactoryException { ConnectorInformation[] conids = getConnectorInformationArray(); for (int i = 0; i < conids.length; i++) { String connector_id = conids[i].getIdentifier(); if (connector_id.equals(connector_identifier)) { Class conn_class = AVAILABLE_CONNECTORS[i]; return conn_class; } } throw new ConnectorFactoryException("The connector '" + connector_identifier + "' couldn't be found in the list of available connectors."); } /** * Creates a new connector given by the connector_identifier. * * @param connector_identifier * The connector identifier of the new connector. * @return Returns the new connector. * @throws ConnectorFactoryException * F.e. */ public static Connector createConnector(String connector_identifier) throws ConnectorFactoryException { Class conn_class = getConnectorClass(connector_identifier); try { Connector connector_obj = (Connector) conn_class.newInstance(); return connector_obj; } catch (Exception e) { throw new ConnectorFactoryException(e); } } /** * Tells, if the given connector identifier is valid. * * @param connector_identifier * The connector identifier. * @return Returns true, if the identifier is valid, false otherwise. * @throws ConnectorFactoryException * F.e. */ public static boolean isValidConnectorIdentifier(String connector_identifier) throws ConnectorFactoryException { ConnectorInformation[] conids = getConnectorInformationArray(); for (int i = 0; i < conids.length; i++) { if (conids[i].getIdentifier().equals(connector_identifier)) { return true; } } return false; } /** * Retrieves the availability of the connector from the flags specified in the * config file. * * @param connector_identifier * The connector. * @param availability_key * The key of the availability flag to be retrieved. * @param default_value * The default value to be used if the flag is not set in the config * file. * @return Returns true, if the flag was set to true, false, if the flag was * set otherwise, or the default_value if the flag wasn't set at all. * @throws ConnectorFactoryException * Thrown, if the connector is invalid. */ protected static boolean getAvailabilityUsingDefault(String connector_identifier, String availability_key, boolean default_value) throws ConnectorFactoryException { if (!isValidConnectorIdentifier(connector_identifier)) { throw new ConnectorFactoryException("The connector '" + connector_identifier + "' couldn't be found in the list of available connectors."); } SettingsReader settings_ = null; try { settings_ = SettingsReader.getInstance(); } catch (SettingsException e) { String log_message = "Can not load signature settings. Cause:\n" + e.getMessage(); logger_.error(log_message); throw new RuntimeException(e); } String value = settings_.getValueFromKey(connector_identifier + "." + availability_key); if (value == null) { return default_value; } return value.equals("true"); } /** * Tells, if the connector is available for being used in the Commandline * (synchron) environment. * *

* A connector is available for commandline processing if it requires no * active user interaction for being executed or if it handles the user * interaction itself. *

*

* A commandline connector is executed synchronously. The client waits until * the Connector has finished. *

*

* Usually a synchron connector can also be used in a web environment. *

*

* Examples for commandline connectors are: MOA, BKU. A1 is not suitible for * commandline because it requires HTTP/HTML interaction, log in, etc. *

* * @return Returns true, if the Connector is available for Commandline * processing. */ public static boolean isAvailableForCommandline(String connector_identifier) throws ConnectorFactoryException { return getAvailabilityUsingDefault(connector_identifier, ConnectorConfigurationKeys.AVAILABLE_FOR_COMMANDLINE, false); } /** * Tells, if the Connector is available for being used in a Web (asynchron, * local) environment. * *

* A connector is available for Web if it can be used in a web environment. * Often a web connector is also a local connector. *

*

* Typical examples are the local BKU and A1. The later requires HTML log in * and session handling. *

* * @return Returns true, if the Connector is available for the Web * application. */ public static boolean isAvailableForWeb(String connector_identifier) throws ConnectorFactoryException { return getAvailabilityUsingDefault(connector_identifier, ConnectorConfigurationKeys.AVAILABLE_FOR_WEB, false); } /** * Tells, if the given connector is local. * * @param connector_identifier * The connector. * @return Returns true, if the given connector is local, false otherwise. * @throws ConnectorFactoryException * F.e. */ public static boolean isConnectorLocal(String connector_identifier) throws ConnectorFactoryException { return connector_identifier.equals(Constants.SIGNATURE_DEVICE_BKU) || connector_identifier.equals(Constants.SIGNATURE_DEVICE_A1) || connector_identifier.equals(Constants.SIGNATURE_DEVICE_MOC); } /** * Key value in property file */ // dferbas: not used anymore with dynamic algorithm support. // field has to be showed/embedded except for default algorithm suites // use signature block layout to show/hide //public static final String MOA_ID_VISIBLE_PROPERTY_KEY = "moa.id.field.visible"; // dferbas: not used anymore // /** // * Tells, if the given connector needs or produces SIG_IDs. // * // *

// * This method is used when pre formatted signature blocks have to be created // * that have to know if there will be a SIG_ID field or not. // *

// *

// * Connectors like BKU produce SIG_IDs when signing that are needed when // * verifying. // *

// * // * @param connector // * The connector. // * @return Returns true, if the given connector uses SIG_IDs, false otherwise. // */ // public static boolean needsSIG_ID(String connector) // { // // all modernn detached signatures have the SIG_ID field. // if(connector.equals("moa")) // { // String is_id_field_visible = null; // // try // { // is_id_field_visible = SettingsReader.getInstance().getValueFromKey(MOA_ID_VISIBLE_PROPERTY_KEY); // } catch (SettingsException e) // { // logger_.error(e.getMessage(), e); // } // // // if not setted in config, show it // if(is_id_field_visible == null) // return true; // if(is_id_field_visible.equals("true")) // return true; // else // return false; // } // // return true; // //return !connector.equals("moa"); // } }