From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../wag/egov/egiz/sig/ConnectorFactory.java | 326 +++++++++++++++++++++ 1 file changed, 326 insertions(+) create mode 100644 src/main/java/at/knowcenter/wag/egov/egiz/sig/ConnectorFactory.java (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/sig/ConnectorFactory.java') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/sig/ConnectorFactory.java b/src/main/java/at/knowcenter/wag/egov/egiz/sig/ConnectorFactory.java new file mode 100644 index 0000000..f24f3a5 --- /dev/null +++ b/src/main/java/at/knowcenter/wag/egov/egiz/sig/ConnectorFactory.java @@ -0,0 +1,326 @@ +/** + * Copyright (c) 2006 by Know-Center, Graz, Austria + * + * This software is the confidential and proprietary information of Know-Center, + * Graz, Austria. You shall not disclose such Confidential Information and shall + * use it only in accordance with the terms of the license agreement you entered + * into with Know-Center. + * + * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR + * NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY + * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. + * + * $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.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; + +/** + * This is a factory for creating the appropriate connector according to the + * connector identifier. + * + * @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("bku") || connector_identifier.equals("a1"); + } + + /** + * 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) + { + return !connector.equals("moa"); + } + +} -- cgit v1.2.3