forked from andreabellandi/LexO-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrea Bellandi
authored and
Andrea Bellandi
committed
Mar 28, 2021
0 parents
commit 215d891
Showing
63 changed files
with
3,452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<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> | ||
<groupId>it.cnr.ilc.lexo</groupId> | ||
<artifactId>LexO-backend</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>war</packaging> | ||
<name>LexO-backend</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>4.0.1</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.17</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.23</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-core</artifactId> | ||
<version>5.4.27.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-c3p0</artifactId> | ||
<version>5.4.27.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.containers</groupId> | ||
<artifactId>jersey-container-servlet</artifactId> | ||
<version>2.33</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.core</groupId> | ||
<artifactId>jersey-server</artifactId> | ||
<version>2.33</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.inject</groupId> | ||
<artifactId>jersey-hk2</artifactId> | ||
<version>2.33</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.media</groupId> | ||
<artifactId>jersey-media-moxy</artifactId> | ||
<version>2.33</version> | ||
</dependency> | ||
<!-- <dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-server</artifactId> | ||
<version>1.19.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-servlet</artifactId> | ||
<version>1.19.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-json</artifactId> | ||
<version>1.19.4</version> | ||
</dependency>--> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.12.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jsoup</groupId> | ||
<artifactId>jsoup</artifactId> | ||
<version>1.13.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.poi</groupId> | ||
<artifactId>poi</artifactId> | ||
<version>5.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-jaxrs</artifactId> | ||
<version>1.6.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-annotations</artifactId> | ||
<version>1.6.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-core</artifactId> | ||
<version>1.6.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.swagger</groupId> | ||
<artifactId>swagger-models</artifactId> | ||
<version>1.6.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.ontotext.graphdb</groupId> | ||
<artifactId>graphdb-free-runtime</artifactId> | ||
<version>9.5.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>15</source> | ||
<target>15</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.3.1</version> | ||
<configuration> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<version>3.1.2</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package it.cnr.ilc.lexo; | ||
|
||
import static it.cnr.ilc.lexo.LexoFilter.CONTEXT; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.log4j.Logger; | ||
import org.eclipse.rdf4j.repository.Repository; | ||
import org.eclipse.rdf4j.repository.RepositoryConnection; | ||
import org.eclipse.rdf4j.repository.RepositoryException; | ||
import org.eclipse.rdf4j.repository.manager.RemoteRepositoryManager; | ||
import org.eclipse.rdf4j.repository.manager.RepositoryManager; | ||
|
||
/** | ||
* | ||
* @author andreabellandi | ||
*/ | ||
public class GraphDbUtil { | ||
|
||
private static final Object LOCK = new Object(); | ||
private static final Repository REPOSITORY; | ||
private static final List<RepositoryConnection> POOL = new ArrayList<>(); | ||
private static final Map<Thread, RepositoryConnection> ACTIVES = new HashMap<>(); | ||
private static final int SIZE = Integer.parseInt(LexoProperties.getProperty("GraphDb.size", "5")); | ||
private static int current = 0; | ||
|
||
static { | ||
RepositoryManager repositoryManager = new RemoteRepositoryManager(LexoProperties.getProperty("GraphDb.url", "http://localhost:7200")); | ||
repositoryManager.init(); | ||
REPOSITORY = repositoryManager.getRepository(LexoProperties.getProperty("GraphDb.repository", "SIMPLE")); | ||
} | ||
|
||
private static void testConnection(RepositoryConnection connection) throws Exception { | ||
} | ||
|
||
public static RepositoryConnection getConnection() { | ||
synchronized (LOCK) { | ||
RepositoryConnection connection = ACTIVES.get(Thread.currentThread()); | ||
if (connection != null) { | ||
Logger.getLogger(CONTEXT).debug(Thread.currentThread().getName() + " get active connection"); | ||
return connection; | ||
} | ||
try { | ||
connection = POOL.remove(0); | ||
try { | ||
testConnection(connection); | ||
Logger.getLogger(CONTEXT).debug(Thread.currentThread().getName() + " get pool connection"); | ||
} catch (Exception ex) { | ||
Logger.getLogger(CONTEXT).debug(Thread.currentThread().getName() + " get inactive connection"); | ||
try { | ||
connection.close(); | ||
} catch (RepositoryException e) { | ||
} | ||
current--; | ||
connection = getConnection(); | ||
} | ||
} catch (IndexOutOfBoundsException ex) { | ||
if (current < SIZE) { | ||
Logger.getLogger(CONTEXT).debug(Thread.currentThread().getName() + " get repository connection"); | ||
connection = REPOSITORY.getConnection(); | ||
current++; | ||
} else { | ||
try { | ||
LOCK.wait(); | ||
} catch (InterruptedException iex) { | ||
} | ||
connection = getConnection(); | ||
} | ||
} | ||
ACTIVES.put(Thread.currentThread(), connection); | ||
return connection; | ||
} | ||
} | ||
|
||
public static void releaseConnection() { | ||
synchronized (LOCK) { | ||
RepositoryConnection connection = ACTIVES.remove(Thread.currentThread()); | ||
if (connection != null) { | ||
if (connection.isActive()) { | ||
connection.rollback(); | ||
} | ||
POOL.add(connection); | ||
LOCK.notifyAll(); | ||
} | ||
} | ||
} | ||
|
||
public static void close() { | ||
synchronized (LOCK) { | ||
while (current != POOL.size()) { | ||
try { | ||
LOCK.wait(); | ||
} catch (InterruptedException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
for (RepositoryConnection connection : POOL) { | ||
try { | ||
connection.close(); | ||
} catch (RepositoryException e) { | ||
} | ||
} | ||
POOL.clear(); | ||
current = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package it.cnr.ilc.lexo; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.boot.Metadata; | ||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.boot.registry.StandardServiceRegistry; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.transform.ResultTransformer; | ||
|
||
/** | ||
* | ||
* @author andreabellandi | ||
*/ | ||
public class HibernateUtil { | ||
|
||
private static final SessionFactory SESSION_FACTORY; | ||
|
||
static { | ||
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); | ||
Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata(); | ||
SESSION_FACTORY = metadata.buildSessionFactory(); | ||
} | ||
|
||
public static Session getSession() { | ||
return SESSION_FACTORY.getCurrentSession(); | ||
} | ||
|
||
public static Session openSession() { | ||
return SESSION_FACTORY.openSession(); | ||
} | ||
|
||
static void closeFactory() { | ||
SESSION_FACTORY.close(); | ||
} | ||
|
||
public static ResultTransformer getResultTransformer() { | ||
return new ResultTransformer() { | ||
|
||
@Override | ||
public Object transformTuple(Object[] values, String[] names) { | ||
Map<String, Object> map = new HashMap<>(); | ||
for (int i = 0; i < values.length; i++) { | ||
map.put(names[i], values[i]); | ||
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public List transformList(List list) { | ||
return list; | ||
} | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.