Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Struckmann2014-10-13 14:14:32 +0000
committerSebastian Struckmann2014-10-13 14:18:54 +0000
commit18d23782e90de74618c8165d324a65c5d820e0ff (patch)
tree9b06e275a2eab3160bc43df6ad1d6d9fc9838614 /org.eclipse.jubula.client.api
parent1dbfcd855a99d46ce77d149510f257b35579ae2e (diff)
downloadorg.eclipse.jubula.core-18d23782e90de74618c8165d324a65c5d820e0ff.tar.gz
org.eclipse.jubula.core-18d23782e90de74618c8165d324a65c5d820e0ff.tar.xz
org.eclipse.jubula.core-18d23782e90de74618c8165d324a65c5d820e0ff.zip
Sprint task - Java API - Moved ObjectMappingLoader and SerilizationUtils to o.e.j.client.api
Diffstat (limited to 'org.eclipse.jubula.client.api')
-rw-r--r--org.eclipse.jubula.client.api/META-INF/MANIFEST.MF2
-rw-r--r--org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/ObjectMappingLoader.java117
-rw-r--r--org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/exceptions/LoadResourceException.java35
-rw-r--r--org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/internal/utils/SerilizationUtils.java66
4 files changed, 220 insertions, 0 deletions
diff --git a/org.eclipse.jubula.client.api/META-INF/MANIFEST.MF b/org.eclipse.jubula.client.api/META-INF/MANIFEST.MF
index f8f040c07..3aeb7f7ec 100644
--- a/org.eclipse.jubula.client.api/META-INF/MANIFEST.MF
+++ b/org.eclipse.jubula.client.api/META-INF/MANIFEST.MF
@@ -8,9 +8,11 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
Require-Bundle: org.slf4j.api;bundle-version="[1.7.2,2.0.0)",
+ org.apache.commons.codec;bundle-version="[1.3.0,2.0.0)",
org.eclipse.jubula.tools;bundle-version="[2.4.0,2.5.0)",
org.eclipse.jubula.communication;bundle-version="[2.4.0,2.5.0)"
Export-Package: org.eclipse.jubula.client.internal,
org.eclipse.jubula.client.internal.exceptions,
+ org.eclipse.jubula.client.internal.utils,
org.eclipse.jubula.client.launch
Eclipse-BuddyPolicy: registered
diff --git a/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/ObjectMappingLoader.java b/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/ObjectMappingLoader.java
new file mode 100644
index 000000000..c117aae7a
--- /dev/null
+++ b/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/ObjectMappingLoader.java
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * Copyright (c) 2014 BREDEX GmbH.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * BREDEX GmbH - initial API and implementation and/or initial documentation
+ *******************************************************************************/
+package org.eclipse.jubula.client;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Map;
+import java.util.Properties;
+import java.util.TreeMap;
+
+import org.eclipse.jubula.client.exceptions.LoadResourceException;
+import org.eclipse.jubula.client.internal.utils.SerilizationUtils;
+import org.eclipse.jubula.tools.internal.objects.IComponentIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class for loading object mapping associations
+ * @author BREDEX GmbH
+ * @created Oct 09, 2014
+ */
+public class ObjectMappingLoader {
+ /** the logger */
+ private static Logger log = LoggerFactory.getLogger(
+ ObjectMappingLoader.class);
+
+ /** object mapping associations */
+ private Properties m_objectMappingAssociations = new Properties();
+
+ /** object mapping associations */
+ private Map<String, IComponentIdentifier> m_map =
+ new TreeMap<String, IComponentIdentifier>();
+
+ /**
+ * Utility class for loading object mapping association
+ */
+ public ObjectMappingLoader() {
+ super();
+ }
+
+ /**
+ * Initializes the object mapping associations
+ * @param resourceURL the URL to the resource properties file
+ */
+ public void init (URL resourceURL) {
+ try {
+ m_objectMappingAssociations.load(resourceURL.openStream());
+ for (Object obj : m_objectMappingAssociations.keySet()) {
+ if (obj instanceof String) {
+ String compName = (String) obj;
+ if (m_map.containsKey(compName)) {
+ log.error("There is already a mapping for the component name " //$NON-NLS-1$
+ + compName);
+ } else {
+ try {
+ m_map.put(compName, getIdentifier(compName));
+ } catch (LoadResourceException e) {
+ log.error(e.getLocalizedMessage(), e);
+ }
+ }
+ }
+ }
+ } catch (IOException e) {
+ log.error("Error while initialising the ObjectMappingLoader", e); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Returns the component identifier for a component name from the cache
+ *
+ * @param compName
+ * the component name
+ * @return the component identifier or <code>null</code> if no identifier
+ * was found
+ */
+ public IComponentIdentifier get(String compName) {
+ return m_map.get(compName);
+ }
+
+ /**
+ * Returns the component identifier for a component name
+ *
+ * @param compName
+ * the component name
+ * @return the component identifier or <code>null</code> if no identifier
+ * was found
+ * @throws LoadResourceException
+ */
+ private IComponentIdentifier getIdentifier(String compName) throws
+ LoadResourceException {
+ try {
+ String encodedString =
+ m_objectMappingAssociations.getProperty(compName);
+ if (encodedString != null) {
+ Object decodedObject = SerilizationUtils.decode(encodedString);
+ if (decodedObject instanceof IComponentIdentifier) {
+ return (IComponentIdentifier) decodedObject;
+ }
+ throw new LoadResourceException("The decoded object is " //$NON-NLS-1$
+ + "not of type 'IComponentIdentfier'."); //$NON-NLS-1$
+ }
+ } catch (IOException e) {
+ throw new LoadResourceException("Could load the given component name", e); //$NON-NLS-1$
+ } catch (ClassNotFoundException e) {
+ throw new LoadResourceException("Problems during deserialization...", e); //$NON-NLS-1$
+ }
+ return null;
+ }
+}
diff --git a/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/exceptions/LoadResourceException.java b/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/exceptions/LoadResourceException.java
new file mode 100644
index 000000000..c066e6a7f
--- /dev/null
+++ b/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/exceptions/LoadResourceException.java
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Copyright (c) 2014 BREDEX GmbH.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * BREDEX GmbH - initial API and implementation and/or initial documentation
+ *******************************************************************************/
+package org.eclipse.jubula.client.exceptions;
+
+/**
+ * Exception for the case that a resource could not be loaded correctly.
+ *
+ * @author BREDEX GmbH
+ * @created 13.10.2014
+ */
+public class LoadResourceException extends Exception {
+
+ /**
+ * @param message The detailed message for this exception.
+ */
+ public LoadResourceException(String message) {
+ super(message);
+ }
+
+ /**
+ * @param message The detailed message for this exception.
+ * @param cause The cause for the exception.
+ */
+ public LoadResourceException(String message, Throwable cause) {
+ super(message, cause);
+ }
+} \ No newline at end of file
diff --git a/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/internal/utils/SerilizationUtils.java b/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/internal/utils/SerilizationUtils.java
new file mode 100644
index 000000000..6b80165bc
--- /dev/null
+++ b/org.eclipse.jubula.client.api/src/org/eclipse/jubula/client/internal/utils/SerilizationUtils.java
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2014 BREDEX GmbH.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * BREDEX GmbH - initial API and implementation and/or initial documentation
+ *******************************************************************************/
+package org.eclipse.jubula.client.internal.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
+import org.apache.commons.codec.binary.Base64;
+
+
+/**
+ * Utility class for encoding/decoding serializable objects into/from Base64 Strings
+ * @author BREDEX GmbH
+ * @created Oct 09, 2014
+ */
+public class SerilizationUtils {
+
+ /** private constructor */
+ private SerilizationUtils() {
+ // utility class
+ }
+
+
+ /**
+ * Encodes a serializable object to a Base64 string.
+ * @param serializableObject the serializable object
+ * @return a String representation of the serializable
+ */
+ public static String encode(Serializable serializableObject)
+ throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(serializableObject);
+ oos.close();
+ return new String(
+ Base64.encodeBase64(baos.toByteArray()));
+ }
+
+ /**
+ * Decodes a serializable from a base64 encoded string.
+ * @param encodedString Base64 encoded String
+ * @return the translated object
+ */
+ public static Object decode(String encodedString) throws IOException,
+ ClassNotFoundException {
+ byte [] data = Base64.decodeBase64(encodedString);
+ ObjectInputStream ois = new ObjectInputStream(
+ new ByteArrayInputStream(data));
+ Object decodedObject = ois.readObject();
+ ois.close();
+ return decodedObject;
+ }
+
+}

Back to the top