This commit is contained in:
Fabio Sinibaldi 2018-12-17 16:03:28 +00:00
parent 20215c2894
commit 4a7150ba1b
27 changed files with 752 additions and 0 deletions

36
.classpath Normal file
View File

@ -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/JavaSE-1.8">
<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>

29
.project Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>perform-service</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<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>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

61
pom.xml Normal file
View File

@ -0,0 +1,61 @@
<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>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>LATEST</version>
</parent>
<groupId>org.gcube.application</groupId>
<artifactId>perform-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Perform Service</name>
<description>Service for Farm Perform Data Management</description>
<packaging>war</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>maven-smartgears-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,16 @@
package org.gcube.application.perform.service;
public class LocalConfiguration {
public static final String POOL_MAX_IDLE="db.pools.max_idle";
public static final String POOL_MAX_TOTAL="db.pools.max_total";
public static final String POOL_MIN_IDLE="db.pools.min_total";
public static final String getProperty(String propertyName) {
throw new RuntimeException("Need to implement this");
}
}

View File

@ -0,0 +1,25 @@
package org.gcube.application.perform.service;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent.Start;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent.Stop;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler;
@XmlRootElement(name = "perform-lifecycle")
public class PerformServiceLifecycleManager extends ApplicationLifecycleHandler {
@Override
public void onStart(Start e) {
super.onStart(e);
}
@Override
public void onStop(Stop e) {
super.onStop(e);
}
}

View File

@ -0,0 +1,21 @@
package org.gcube.application.perform.service;
import org.gcube.smartgears.ApplicationManager;
public class PerformServiceManager implements ApplicationManager{
@Override
public void onInit() {
// TODO Auto-generated method stub
}
@Override
public void onShutdown() {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,8 @@
package org.gcube.application.perform.service;
public interface ServiceConstants {
public static final String ANAGRAPHIC_INTERFACE="anagraphic";
}

View File

@ -0,0 +1,15 @@
package org.gcube.application.perform.service.engine;
import java.sql.SQLException;
import org.gcube.application.perform.service.engine.model.BeanNotFound;
import org.gcube.application.perform.service.engine.model.DBQueryDescriptor;
import org.gcube.application.perform.service.engine.model.anagraphic.Batch;
import org.gcube.application.perform.service.engine.model.anagraphic.Farm;
public interface AnagraphicManager {
public Batch getBatch(DBQueryDescriptor desc) throws BeanNotFound, SQLException;
public Farm getFarm(DBQueryDescriptor desc)throws BeanNotFound, SQLException;
}

View File

@ -0,0 +1,106 @@
package org.gcube.application.perform.service.engine;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.gcube.application.perform.service.engine.model.BeanNotFound;
import org.gcube.application.perform.service.engine.model.DBField;
import org.gcube.application.perform.service.engine.model.DBQueryDescriptor;
import org.gcube.application.perform.service.engine.model.ISQueryDescriptor;
import org.gcube.application.perform.service.engine.model.InternalException;
import org.gcube.application.perform.service.engine.model.anagraphic.Batch;
import org.gcube.application.perform.service.engine.model.anagraphic.Farm;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class AnagraphicManagerImpl implements AnagraphicManager {
private static final String INSERT_BATCH="";
private static final String GET_BATCH="";
private static final String INSERT_FARM="";
private static final String GET_FARM="";
private ISQueryDescriptor getISQueryDescriptor() {
throw new RuntimeException("Implment this");
}
@Override
public Batch getBatch(DBQueryDescriptor desc) throws BeanNotFound,SQLException{
DataBaseManager db=DataBaseManager.get(getISQueryDescriptor());
Connection conn=db.getConnection();
PreparedStatement psInsert=conn.prepareStatement(INSERT_BATCH);
// TODO set values
boolean inserted=psInsert.executeUpdate()==1;
if(inserted)
conn.commit();
PreparedStatement psGet=conn.prepareStatement(GET_BATCH);
// TODO set values
ResultSet rs=psGet.executeQuery();
if(!rs.next())
throw new BeanNotFound("Batch not found. Condition was "+desc);
Batch toReturn=rowToBatch(rs);
if(inserted)
log.trace("Inserted Batch {} ",toReturn);
return toReturn;
}
@Override
public Farm getFarm(DBQueryDescriptor desc) throws BeanNotFound,SQLException{
DataBaseManager db=DataBaseManager.get(getISQueryDescriptor());
Connection conn=db.getConnection();
PreparedStatement psInsert=conn.prepareStatement(INSERT_FARM);
// TODO set values
boolean inserted=psInsert.executeUpdate()==1;
if(inserted)
conn.commit();
PreparedStatement psGet=conn.prepareStatement(GET_FARM);
// TODO set values
ResultSet rs=psGet.executeQuery();
if(!rs.next())
throw new BeanNotFound("Farm not found. Condition was "+desc);
Farm toReturn=rowToFarm(rs);
if(inserted)
log.trace("Inserted Batch {} ",toReturn);
return toReturn;
}
private static Batch rowToBatch(ResultSet rs) throws SQLException {
Batch toReturn=new Batch();
toReturn.setFarmId(rs.getLong(DBField.Batch.FARM_ID));
toReturn.setId(rs.getLong(DBField.Batch.BATCH_ID));
toReturn.setName(rs.getString(DBField.Batch.BATCH_NAME));
toReturn.setType(rs.getString(DBField.Batch.BATCH_TYPE));
return toReturn;
}
private static Farm rowToFarm(ResultSet rs) throws SQLException {
Farm toReturn=new Farm();
toReturn.setAssociationId(rs.getLong(DBField.Farm.ASSOCIATION_ID));
toReturn.setAssociationUUID(rs.getString(DBField.Farm.ASSOCIATION_UUID));
toReturn.setCompanyId(rs.getLong(DBField.Farm.COMPANY_ID));
toReturn.setCompanyUUID(rs.getString(DBField.Farm.COMPANY_UUID));
toReturn.setId(rs.getLong(DBField.Farm.FARM_ID));
toReturn.setUUID(rs.getString(DBField.Farm.UUID));
return toReturn;
}
}

View File

@ -0,0 +1,19 @@
package org.gcube.application.perform.service.engine;
import java.sql.Connection;
import java.sql.SQLException;
import org.gcube.application.perform.service.engine.model.ISQueryDescriptor;
public interface DataBaseManager {
public static DataBaseManager get(ISQueryDescriptor desc) {
return new DataBaseManagerImpl(desc);
}
public Connection getConnection() throws SQLException;
}

View File

@ -0,0 +1,119 @@
package org.gcube.application.perform.service.engine;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.gcube.application.perform.service.LocalConfiguration;
import org.gcube.application.perform.service.engine.model.DBType;
import org.gcube.application.perform.service.engine.model.DatabaseConnectionDescriptor;
import org.gcube.application.perform.service.engine.model.ISQueryDescriptor;
import org.gcube.application.perform.service.engine.utils.ISUtils;
import org.gcube.application.perform.service.engine.utils.ScopeUtils;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DataBaseManagerImpl implements DataBaseManager{
// private static final ConcurrentHashMap<DBType,DataBaseManagerImpl> instanceMap=new ConcurrentHashMap<>();
//
// @Synchronized
// public static final DataBaseManagerImpl get(ISQueryDescriptor desc) {
// if(!instanceMap.containsKey(desc.getType())){
// return instanceMap.put(desc.getType(), new DataBaseManagerImpl(desc));
// }
//
// return instanceMap.get(desc.getType());
// }
// ************************************** INSTANCE
// Endpoint -> datasource
private static ConcurrentHashMap<String,DataSource> datasources=new ConcurrentHashMap<>();
/**
* Manages db connection pools by scope
*
*
*/
private ISQueryDescriptor queryDescriptor;
private DatabaseConnectionDescriptor dbDescriptor=null;
public DataBaseManagerImpl(ISQueryDescriptor query) {
queryDescriptor=query;
}
private synchronized DatabaseConnectionDescriptor getDB() {
if(dbDescriptor==null) {
dbDescriptor=ISUtils.queryForDatabase(queryDescriptor);
}
return dbDescriptor;
}
@Override
public Connection getConnection() throws SQLException {
DataSource ds=getDataSource();
return ds.getConnection();
}
@Synchronized
private DataSource getDataSource() {
DatabaseConnectionDescriptor dbDescriptor=getDB();
if(!datasources.containsKey(dbDescriptor.getUrl())) {
datasources.put(dbDescriptor.getUrl(), setupDataSource(dbDescriptor));
}
return datasources.get(dbDescriptor.getUrl());
}
private static DataSource setupDataSource(DatabaseConnectionDescriptor db) {
log.trace("Setting up data source for {} ",db);
GenericObjectPoolConfig poolConfig=new GenericObjectPoolConfig();
poolConfig.setMaxIdle(Integer.parseInt(LocalConfiguration.getProperty(LocalConfiguration.POOL_MAX_IDLE)));
poolConfig.setMaxTotal(Integer.parseInt(LocalConfiguration.getProperty(LocalConfiguration.POOL_MAX_TOTAL)));
poolConfig.setMinIdle(Integer.parseInt(LocalConfiguration.getProperty(LocalConfiguration.POOL_MIN_IDLE)));
ConnectionFactory connectionFactory =
new DriverManagerConnectionFactory(db.getUrl(),db.getUsername(),db.getPassword());
PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory, null);
ObjectPool<PoolableConnection> connectionPool =
new GenericObjectPool<>(poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
PoolingDataSource<PoolableConnection> dataSource =
new PoolingDataSource<>(connectionPool);
return dataSource;
}
}

View File

@ -0,0 +1,29 @@
package org.gcube.application.perform.service.engine.model;
public class BeanNotFound extends InternalException {
public BeanNotFound() {
// TODO Auto-generated constructor stub
}
public BeanNotFound(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public BeanNotFound(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public BeanNotFound(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public BeanNotFound(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,59 @@
package org.gcube.application.perform.service.engine.model;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class DBField {
public static class Batch{
public static final Map<String,DBField> fields=new HashMap<>();
public static final String BATCH_ID="id";
public static final String UUID="uuid";
public static final String FARM_ID="farmid";
public static final String BATCH_TYPE="type";
public static final String BATCH_NAME="name";
static {
fields.put(BATCH_ID, new DBField(Types.BIGINT,BATCH_ID));
fields.put(UUID, new DBField(Types.NVARCHAR,UUID));
fields.put(FARM_ID, new DBField(Types.BIGINT,FARM_ID));
fields.put(BATCH_TYPE, new DBField(Types.NVARCHAR,BATCH_TYPE));
fields.put(BATCH_NAME, new DBField(Types.NVARCHAR,BATCH_NAME));
}
}
public static class Farm{
public static final Map<String,DBField> fields=new HashMap<>();
public static final String FARM_ID="id";
public static final String COMPANY_ID="companyid";
public static final String ASSOCIATION_ID="associationid";
public static final String UUID="uuid";
public static final String COMPANY_UUID="companyuuid";
public static final String ASSOCIATION_UUID="associationuuid";
static {
fields.put(FARM_ID, new DBField(Types.BIGINT,FARM_ID));
fields.put(COMPANY_ID, new DBField(Types.BIGINT,COMPANY_ID));
fields.put(ASSOCIATION_ID, new DBField(Types.BIGINT,ASSOCIATION_ID));
fields.put(UUID, new DBField(Types.NVARCHAR,UUID));
fields.put(COMPANY_UUID, new DBField(Types.NVARCHAR,COMPANY_UUID));
fields.put(ASSOCIATION_UUID, new DBField(Types.NVARCHAR,ASSOCIATION_UUID));
}
}
private int type;
private String fieldName;
}

View File

@ -0,0 +1,5 @@
package org.gcube.application.perform.service.engine.model;
public class DBQueryDescriptor {
}

View File

@ -0,0 +1,7 @@
package org.gcube.application.perform.service.engine.model;
public enum DBType {
Anagraphic,Performances
}

View File

@ -0,0 +1,14 @@
package org.gcube.application.perform.service.engine.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class DatabaseConnectionDescriptor {
private String username;
private String url;
private String password;
}

View File

@ -0,0 +1,17 @@
package org.gcube.application.perform.service.engine.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
@Getter
@AllArgsConstructor
@ToString
public class ISQueryDescriptor {
private DBType type;
private String resourceName;
private String platformName;
private String category;
}

View File

@ -0,0 +1,29 @@
package org.gcube.application.perform.service.engine.model;
public class InternalException extends Exception {
public InternalException() {
// TODO Auto-generated constructor stub
}
public InternalException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public InternalException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public InternalException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public InternalException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,20 @@
package org.gcube.application.perform.service.engine.model.anagraphic;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Batch {
private Long id;
private String uuid;
private String name;
private String type;
private Long farmId;
}

View File

@ -0,0 +1,21 @@
package org.gcube.application.perform.service.engine.model.anagraphic;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Farm {
private Long id;
private Long companyId;
private Long associationId;
private String UUID;
private String companyUUID;
private String associationUUID;
}

View File

@ -0,0 +1,14 @@
package org.gcube.application.perform.service.engine.utils;
import org.gcube.application.perform.service.engine.model.DatabaseConnectionDescriptor;
import org.gcube.application.perform.service.engine.model.ISQueryDescriptor;
public class ISUtils {
public static DatabaseConnectionDescriptor queryForDatabase(ISQueryDescriptor desc) {
throw new RuntimeException("Implment me, asshole!");
}
}

View File

@ -0,0 +1,46 @@
package org.gcube.application.perform.service.engine.utils;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ScopeUtils {
public static String getCurrentScope(){
// try{
// String token=SecurityTokenProvider.instance.get();
// log.debug("Token is : "+token);
// if(token==null) throw new Exception("Security Token is null");
// AuthorizationEntry entry = authorizationService().get(token);
// return entry.getContext();
// }catch(Exception e ){
// log.debug("Unable to resolve token, checking scope provider..",e);
// return ScopeProvider.instance.get();
// }
String scope=ScopeProvider.instance.get();
if(scope!=null) {
log.debug("Found scope provider {}, skipping token",scope);
return scope;
}else{
try{
log.debug("Scope provider not set, reverting to token");
String token=SecurityTokenProvider.instance.get();
log.debug("Token is : "+token);
if(token==null) throw new Exception("Security Token is null");
AuthorizationEntry entry = authorizationService().get(token);
return entry.getContext();
}catch(Exception e){
throw new RuntimeException("Unable to evaluate scope ",e);
}
}
}
}

View File

@ -0,0 +1,20 @@
package org.gcube.application.perform.service.rest;
import javax.ws.rs.Path;
import org.gcube.application.perform.service.PerformServiceManager;
import org.gcube.smartgears.annotations.ManagedBy;
import org.gcube.application.perform.service.ServiceConstants;
@Path(ServiceConstants.ANAGRAPHIC_INTERFACE)
@ManagedBy(PerformServiceManager.class)
public class Anagraphic {
}

View File

@ -0,0 +1 @@
org.gcube.application.perform.service.PerformServiceLifecycleManager