aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Lenz <tlenz@iaik.tugraz.at>2013-06-07 11:50:13 +0200
committerThomas Lenz <tlenz@iaik.tugraz.at>2013-06-07 11:50:13 +0200
commit1c0d27a1d5da91019ed2f51d4184282ea7ffe4f0 (patch)
tree305292fcf059560379307278edd0e99471ef2ae0
parent261294df86c33105eb2113808fc6f171e9250ac7 (diff)
downloadmoa-id-spss-1c0d27a1d5da91019ed2f51d4184282ea7ffe4f0.tar.gz
moa-id-spss-1c0d27a1d5da91019ed2f51d4184282ea7ffe4f0.tar.bz2
moa-id-spss-1c0d27a1d5da91019ed2f51d4184282ea7ffe4f0.zip
Common classes for Hibernate initializing and database handling
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/Empty.java31
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java316
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java41
-rw-r--r--id/pom.xml1
-rw-r--r--id/server/moa-id-commons/.classpath36
-rw-r--r--id/server/moa-id-commons/.project23
-rw-r--r--id/server/moa-id-commons/pom.xml112
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/DBUtils.java33
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/HibernateUtil.java159
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/AssertionStore.java74
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ex/MOADatabaseException.java22
-rw-r--r--id/server/moa-id-commons/src/main/resources/hibernate.cfg.xml11
-rw-r--r--id/server/pom.xml1
13 files changed, 860 insertions, 0 deletions
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/Empty.java b/common/src/main/java/at/gv/egovernment/moa/util/Empty.java
new file mode 100644
index 000000000..533b39b6b
--- /dev/null
+++ b/common/src/main/java/at/gv/egovernment/moa/util/Empty.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2011 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package at.gv.egovernment.moa.util;
+
+/**
+ * @author <a href="mailto:thomas.knall@iaik.tugraz.at">Thomas Knall</a>
+ */
+public interface Empty {
+
+ /**
+ * Returns {@code true} if underlying object is empty.
+ *
+ * @return {@code true} if empty, {@code false} if not empty.
+ */
+ boolean isEmpty();
+
+}
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java b/common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java
new file mode 100644
index 000000000..043baea0e
--- /dev/null
+++ b/common/src/main/java/at/gv/egovernment/moa/util/MiscUtil.java
@@ -0,0 +1,316 @@
+/*
+ * Copyright 2011 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package at.gv.egovernment.moa.util;
+
+import iaik.logging.Log;
+import iaik.logging.LogFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Iterator;
+import java.util.Properties;
+
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+import at.gv.egovernment.moa.util.ex.EgovUtilException;
+
+
+
+/**
+ * Class providing several utility methods.
+ *
+ * @author <a href="mailto:Arne.Tauber@egiz.gv.at">Arne Tauber</a>
+ *
+ */
+public class MiscUtil {
+
+ public static final String DEFAULT_SLASH = "/";
+
+ public static Log log = LogFactory.getLog(MiscUtil.class);
+
+ private static final int IO_BUFFER_SIZE = 4 * 1024;
+
+ public static void copyStream(InputStream is, OutputStream os) throws IOException {
+ byte[] b = new byte[IO_BUFFER_SIZE];
+ int read;
+ while ((read = is.read(b)) != -1) {
+ os.write(b, 0, read);
+ }
+ }
+
+ public static void assertNotNull(Object param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ }
+
+ public static boolean areAllNull(Object... objects) {
+ for (Object o : objects) {
+ if (o != null) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static String extractContentType(String contentTypeString) {
+ if (contentTypeString == null) {
+ return "";
+ }
+ if (contentTypeString.indexOf(";") != -1) {
+ return contentTypeString.substring(0, contentTypeString.indexOf(";"));
+ }
+ return contentTypeString;
+ }
+
+ public static XMLGregorianCalendar getXMLGregorianCalendar(Date date)
+ throws DatatypeConfigurationException {
+ GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
+ cal.setTime(date);
+ return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
+ }
+
+ public static XMLGregorianCalendar getXMLGregorianCalendar(String str)
+ throws DatatypeConfigurationException {
+ return DatatypeFactory.newInstance().newXMLGregorianCalendar(str);
+ }
+
+ public static X509Certificate readCertificate(InputStream certStream)
+ throws CertificateException {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ return (X509Certificate) cf.generateCertificate(certStream);
+ }
+
+ public static boolean isEmpty(String str) {
+ return str == null || "".equals(str);
+ }
+
+ public static boolean isNotEmpty(String str) {
+ return str != null && !"".equals(str);
+ }
+
+ public static byte[] sourceToByteArray(Source result)
+ throws TransformerException {
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ StreamResult streamResult = new StreamResult();
+ streamResult.setOutputStream(out);
+ transformer.transform(result, streamResult);
+ return out.toByteArray();
+ }
+
+ public static Document parseDocument(InputStream inputStream)
+ throws IOException {
+ try {
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
+ .newInstance();
+ docBuilderFactory.setNamespaceAware(true);
+ DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+ return docBuilder.parse(inputStream);
+ } catch (ParserConfigurationException e) {
+ throw new IOException(e);
+ } catch (SAXException e) {
+ throw new IOException(e);
+ }
+ }
+
+ public static String removePrecedingSlash(String path, String slash) {
+ assertNotEmpty(slash, "Shash");
+ if (!isEmpty(path)) {
+ while (path.startsWith(slash)) {
+ path = path.substring(slash.length(), path.length());
+ }
+ }
+ return path;
+ }
+
+ public static String removePrecedingSlash(String path) {
+ return removePrecedingSlash(path, DEFAULT_SLASH);
+ }
+
+ public static void assertNotEmpty(String param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (param.length() == 0) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ public static boolean isEmpty(Properties props) {
+ if (props == null || props.isEmpty()) {
+ return true;
+ }
+ Iterator it = props.values().iterator();
+ while (it.hasNext()) {
+ if (MiscUtil.isNotEmpty((String) it.next())) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static boolean isEmpty(Empty empty) {
+ return empty == null || empty.isEmpty();
+ }
+
+ public static boolean isNotEmpty(Empty empty) {
+ return !isEmpty(empty);
+ }
+
+ public static boolean isEmpty(byte[] data) {
+ return data == null || data.length == 0;
+ }
+
+ public static boolean isNotEmpty(byte[] data) {
+ return !isEmpty(data);
+ }
+
+ public static <T> boolean isEmpty(Collection<T> c) {
+ return c == null || c.isEmpty();
+ }
+
+ public static <T> boolean isNotEmpty(Collection<T> c) {
+ return !isEmpty(c);
+ }
+
+ public static boolean areAllEmpty(String... strings) {
+ for (String s : strings) {
+ if (s != null && s.trim().length() != 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static boolean areAllEmpty(Empty... empties) {
+ if (empties != null) {
+ for (Empty e : empties) {
+ if (e != null && !e.isEmpty()) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ public static <T> void assertNotEmpty(T[] param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (param.length == 0) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ public static void assertNotEmpty(Empty empty, String name) {
+ if (empty == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (empty.isEmpty()) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ public static void assertNotEmpty(byte[] param, String name) {
+ if (param == null) {
+ throw new NullPointerException(name + " must not be null.");
+ }
+ if (param.length == 0) {
+ throw new IllegalArgumentException(name + " must not be empty.");
+ }
+ }
+
+ public static Date parseXMLDate(String xmlDate) throws EgovUtilException {
+ if (xmlDate == null) {
+ return null;
+ }
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+ try {
+ return sdf.parse(xmlDate);
+ } catch (ParseException e) {
+ throw new EgovUtilException(e);
+ }
+ }
+
+ public static <T> boolean isEmpty(T[] array) {
+ return array == null || array.length == 0;
+ }
+
+ public static <T> boolean isNotEmpty(T[] array) {
+ return !isEmpty(array);
+ }
+
+ public static String convertDateFromStandardToXML(String dateString) {
+ MiscUtil.assertNotNull(dateString, "dateString");
+ Date date = parseDate(dateString);
+ return formatDate(date, "yyyy-MM-dd");
+ }
+
+ public static Date parseDate(String dateString) {
+ return parseDate(dateString, "dd.MM.yyyy");
+ }
+
+ public static Date parseDate(String dateString, String pattern) {
+ MiscUtil.assertNotNull(dateString, "dateString");
+ MiscUtil.assertNotNull(pattern, "pattern");
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
+ try {
+ return sdf.parse(dateString);
+ } catch (ParseException e) {
+ log.warn(null, "Error parsing date.", e);
+ return null;
+ }
+ }
+
+ public static String formatDate(Date date, String format) {
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
+ return sdf.format(date);
+ }
+
+}
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java b/common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java
new file mode 100644
index 000000000..733a2a845
--- /dev/null
+++ b/common/src/main/java/at/gv/egovernment/moa/util/ex/EgovUtilException.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2011 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package at.gv.egovernment.moa.util.ex;
+
+public class EgovUtilException extends Exception {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ public EgovUtilException() {
+ }
+
+ public EgovUtilException(String message) {
+ super(message);
+ }
+
+ public EgovUtilException(Throwable cause) {
+ super(cause);
+ }
+
+ public EgovUtilException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
diff --git a/id/pom.xml b/id/pom.xml
index c1dfc03d3..f91f7874a 100644
--- a/id/pom.xml
+++ b/id/pom.xml
@@ -21,6 +21,7 @@
<properties>
<repositoryPath>${basedir}/../repository</repositoryPath>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
diff --git a/id/server/moa-id-commons/.classpath b/id/server/moa-id-commons/.classpath
new file mode 100644
index 000000000..9fc2de7b0
--- /dev/null
+++ b/id/server/moa-id-commons/.classpath
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" output="target/classes" path="src/main/java">
+ <attributes>
+ <attribute name="optional" value="true"/>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
+ <attributes>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="src" output="target/test-classes" path="src/test/java">
+ <attributes>
+ <attribute name="optional" value="true"/>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
+ <attributes>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
+ <attributes>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+ <attributes>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
diff --git a/id/server/moa-id-commons/.project b/id/server/moa-id-commons/.project
new file mode 100644
index 000000000..fea8c6ede
--- /dev/null
+++ b/id/server/moa-id-commons/.project
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>moa-id-commons</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.m2e.core.maven2Builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>org.eclipse.m2e.core.maven2Nature</nature>
+ </natures>
+</projectDescription>
diff --git a/id/server/moa-id-commons/pom.xml b/id/server/moa-id-commons/pom.xml
new file mode 100644
index 000000000..0abd80602
--- /dev/null
+++ b/id/server/moa-id-commons/pom.xml
@@ -0,0 +1,112 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>MOA.id</groupId>
+ <artifactId>moa-id</artifactId>
+ <version>1.5.2</version>
+ </parent>
+ <artifactId>moa-id-commons</artifactId>
+ <name>moa-id-commons</name>
+ <groupId>MOA.id.server</groupId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-core</artifactId>
+ <version>4.2.1.Final</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.1</version>
+ </dependency>
+ <dependency>
+ <groupId>MOA</groupId>
+ <artifactId>moa-common</artifactId>
+ <type>jar</type>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-c3p0</artifactId>
+ <version>4.2.1.Final</version>
+ </dependency>
+
+ <dependency>
+ <groupId>mysql</groupId>
+ <artifactId>mysql-connector-java</artifactId>
+ <version>5.1.25</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <skipTests>true</skipTests>
+ <archive>
+ <addMavenDescriptor>false</addMavenDescriptor>
+ </archive>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <version>2.5</version>
+ <configuration>
+ <charset>UTF-8</charset>
+ <docencoding>UTF-8</docencoding>
+ <quiet>true</quiet>
+ <author>false</author>
+ <version>false</version>
+ <use>true</use>
+ <excludePackageNames>at.gv.egovernment.moa.spss.server.*;at.gv.egovernment.moa.spss.api.impl.*;at.gv.egovernment.moa.spss.impl.*</excludePackageNames>
+ <tags>
+ <tag>
+ <name>pre</name>
+ <placement>a</placement>
+ <head>Preconditions:</head>
+ </tag>
+ <tag>
+ <name>post</name>
+ <placement>a</placement>
+ <head>Postconditions:</head>
+ </tag>
+ </tags>
+ <links>
+ <link>http://java.sun.com/j2se/1.4/docs/api/</link>
+ <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
+ <link>http://logging.apache.org/log4j/docs/api/</link>
+ </links>
+ <target>1.5</target>
+ </configuration>
+ <executions>
+ <execution>
+ <id>generate-javadoc</id>
+ <phase>package</phase>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ <version>1.5.2</version>
+</project> \ No newline at end of file
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/DBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/DBUtils.java
new file mode 100644
index 000000000..aab0b281d
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/DBUtils.java
@@ -0,0 +1,33 @@
+package at.gv.egovernment.moa.id.commons.db;
+
+import java.util.List;
+
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import at.gv.egovernment.moa.id.commons.db.dao.AssertionStore;
+import at.gv.egovernment.moa.logging.Logger;
+import at.gv.egovernment.moa.util.MiscUtil;
+
+public class DBUtils {
+
+ @SuppressWarnings("rawtypes")
+ public static AssertionStore getAssertion(String artifact) {
+ MiscUtil.assertNotNull(artifact, "artifact");
+ Logger.trace("Getting Assertion with Artifact " + artifact + " from database.");
+
+ Session session = HibernateUtil.getCurrentSession();
+ session.beginTransaction();
+ Query query = session.getNamedQuery("getAssertionWithArtifact");
+ query.setString("artifact", artifact);
+ List result = query.list();
+ Logger.trace("Found entries: " + result.size());
+
+ if (result.size() == 0) {
+ Logger.trace("No entries found.");
+ return null;
+ }
+ return (AssertionStore) result.get(0);
+ }
+
+}
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/HibernateUtil.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/HibernateUtil.java
new file mode 100644
index 000000000..59398c922
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/HibernateUtil.java
@@ -0,0 +1,159 @@
+package at.gv.egovernment.moa.id.commons.db;
+
+import java.util.Properties;
+
+import org.apache.commons.lang3.StringUtils;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.service.ServiceRegistry;
+import org.hibernate.service.ServiceRegistryBuilder;
+
+import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException;
+import at.gv.egovernment.moa.logging.Logger;
+
+public final class HibernateUtil {
+
+ private static SessionFactory sessionFactory;
+ private static ServiceRegistry serviceRegistry;
+
+ @SuppressWarnings("rawtypes")
+ private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();
+ private static boolean automaticSessionHandling = false;
+
+ private static final String[] AUTOMATIC_SESSION_HANDLING_VALUES = new String[] { "jta", "thread" };
+ private static final String SESSION_HANDLING_KEY = "hibernate.current_session_context_class";
+
+ private static Configuration configuration;
+
+ protected HibernateUtil() { }
+
+ public static void initHibernate(Configuration config, Properties hibernateProperties) {
+
+ String scm = StringUtils.trimToNull(hibernateProperties.getProperty(SESSION_HANDLING_KEY));
+ if (scm != null) {
+ automaticSessionHandling = scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[0]) != -1 || scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[1]) != -1;
+ }
+ Logger.debug("Evaluating hibernate property \"" + SESSION_HANDLING_KEY + "\".");
+ if (automaticSessionHandling) {
+ Logger.info("Hibernate is automatically handling session context management.");
+ } else {
+ Logger.info("Hibernate is NOT automatically handling session context management. Using build-in ThreadLocal session handling.");
+ }
+ try {
+ //Create the SessionFactory
+ Logger.debug("Creating initial session factory...");
+
+ config.configure();
+ serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
+ sessionFactory = config.buildSessionFactory(serviceRegistry);
+ Logger.debug("Initial session factory successfully created.");
+
+ } catch (Throwable ex) {
+ Logger.error("Initial session factory creation failed: " + ex.getMessage());
+ throw new ExceptionInInitializerError(ex);
+ }
+ }
+
+ /**
+ * Checks if a session factory is currently available. If necessary a new
+ * session factory is created.
+ *
+ * @return current (or new) session factory
+ * @throws HibernateException
+ * thrown if a hibernate error occurs
+ */
+ public static Session getCurrentSession() {
+ if (automaticSessionHandling) {
+ return sessionFactory.getCurrentSession();
+ }
+ Session session = (Session) THREAD_LOCAL.get();
+ // Open a new Session, if this Thread has none yet
+ if (session == null || !session.isConnected()) {
+ session = getNewSession();
+ }
+ return session;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Session getNewSession() {
+ if (automaticSessionHandling) {
+ Logger.warn("Session is being automatically handled by hibernate. Therefore this session maybe not being newly created. Use HibernateUtil.getCurrentSession() instead.");
+ return sessionFactory.getCurrentSession();
+ }
+ Session session = (Session) THREAD_LOCAL.get();
+ if (session != null) {
+ Logger.warn("Previous session has not been closed; closing session now.");
+ closeSession();
+ }
+ Logger.debug("Opening new hibernate session...");
+ try {
+ session = sessionFactory.openSession();
+ THREAD_LOCAL.set(session);
+ } catch (HibernateException hex) {
+ Logger.error(hex.getMessage());
+ }
+ return session;
+ }
+
+ /**
+ * Closes the current session.
+ *
+ * @throws HibernateException
+ * thrown if session is already closed or a hibernate error
+ * occurs.
+ */
+ @SuppressWarnings("unchecked")
+ public static void closeSession() {
+ if (automaticSessionHandling) {
+ Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand.");
+ return;
+ }
+ Logger.debug("Closing current hibernate session...");
+ Session session = (Session) THREAD_LOCAL.get();
+ THREAD_LOCAL.set(null);
+ if (session != null) {
+ try {
+ session.close();
+
+ } catch (HibernateException hex) {
+ Logger.error(hex.getMessage());
+ }
+ }
+ }
+
+ public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException {
+ Transaction tx = null;
+ try {
+ Session session = HibernateUtil.getCurrentSession();
+ tx = session.beginTransaction();
+ session.saveOrUpdate(dbo);
+ tx.commit();
+ return true;
+
+ } catch(HibernateException e) {
+ Logger.warn("Error during database saveOrUpdate. Rollback.", e);
+ tx.rollback();
+ throw new MOADatabaseException(e);
+ }
+ }
+
+ public static boolean delete(Object dbo) {
+ Transaction tx = null;
+ try {
+ Session session = HibernateUtil.getCurrentSession();
+ tx = session.beginTransaction();
+ session.delete(dbo);
+ tx.commit();
+ return true;
+
+ } catch(HibernateException e) {
+ Logger.warn("Error during database delete. Rollback.", e);
+ tx.rollback();
+ return false;
+ }
+ }
+
+}
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/AssertionStore.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/AssertionStore.java
new file mode 100644
index 000000000..9dff193d6
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/AssertionStore.java
@@ -0,0 +1,74 @@
+package at.gv.egovernment.moa.id.commons.db.dao;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.DynamicUpdate;
+import org.hibernate.annotations.NamedQueries;
+import org.hibernate.annotations.NamedQuery;
+
+
+@Entity
+@DynamicUpdate(value=true)
+@Table(name = "assertionstore")
+@NamedQueries({
+ @NamedQuery(name="getAssertionWithArtifact", query = "select assertionstore from AssertionStore assertionstore where assertionstore.artifact = :artifact")
+})
+
+public class AssertionStore implements Serializable{
+
+ private static final long serialVersionUID = 1L;
+
+ @Id
+ @Column(name = "artifact", unique=true, nullable=false)
+ private String artifact;
+
+ @Column(name = "type", nullable=false)
+ private String type;
+
+ @Column(name = "assertion", nullable=false)
+ @Lob private byte [] assertion;
+
+ @Column(name = "datetime", nullable=false)
+ Date datatime;
+
+ public String getArtifact() {
+ return artifact;
+ }
+
+ public void setArtifact(String artifact) {
+ this.artifact = artifact;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public byte[] getAssertion() {
+ return assertion;
+ }
+
+ public void setAssertion(byte[] assertion) {
+ this.assertion = assertion;
+ }
+
+ public Date getDatatime() {
+ return datatime;
+ }
+
+ public void setDatatime(Date datatime) {
+ this.datatime = datatime;
+ }
+
+
+}
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ex/MOADatabaseException.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ex/MOADatabaseException.java
new file mode 100644
index 000000000..169d31aac
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ex/MOADatabaseException.java
@@ -0,0 +1,22 @@
+package at.gv.egovernment.moa.id.commons.db.ex;
+
+public class MOADatabaseException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ public MOADatabaseException() {
+ super();
+ }
+
+ public MOADatabaseException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public MOADatabaseException(String message) {
+ super(message);
+ }
+
+ public MOADatabaseException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/id/server/moa-id-commons/src/main/resources/hibernate.cfg.xml b/id/server/moa-id-commons/src/main/resources/hibernate.cfg.xml
new file mode 100644
index 000000000..32dd7d9f6
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/resources/hibernate.cfg.xml
@@ -0,0 +1,11 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!DOCTYPE hibernate-configuration PUBLIC
+"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+ <session-factory>
+ <!-- Mapping files -->
+ <mapping class="at.gv.egovernment.moa.id.commons.db.dao.AssertionStore"/>
+ </session-factory>
+</hibernate-configuration> \ No newline at end of file
diff --git a/id/server/pom.xml b/id/server/pom.xml
index 386f38ed6..777715c3b 100644
--- a/id/server/pom.xml
+++ b/id/server/pom.xml
@@ -19,6 +19,7 @@
<module>proxy</module>
<module>auth</module>
<module>stork-saml-engine</module>
+ <module>moa-id-commons</module>
</modules>
<properties>