2018-10-02 12:05:43 +02:00
|
|
|
package org.gcube.contentmanagement.blobstorage.transport;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2020-11-12 18:11:07 +01:00
|
|
|
import java.util.Objects;
|
2018-10-02 12:05:43 +02:00
|
|
|
import java.util.ServiceLoader;
|
|
|
|
|
|
|
|
import org.gcube.contentmanagement.blobstorage.resource.MemoryType;
|
|
|
|
import org.gcube.contentmanagement.blobstorage.transport.backend.MongoOperationManager;
|
2020-11-12 18:11:07 +01:00
|
|
|
import org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants;
|
2018-10-02 12:05:43 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
//import terrastore.client.TerrastoreClient;
|
|
|
|
/**
|
|
|
|
* Transport manager factory
|
|
|
|
* @author Roberto Cirillo (ISTI - CNR)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class TransportManagerFactory {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger for this class
|
|
|
|
*/
|
|
|
|
// private static final Logger logger = Logger.getLogger(OperationFactory.class);
|
|
|
|
final Logger logger = LoggerFactory.getLogger(TransportManagerFactory.class);
|
|
|
|
// TerrastoreClient client;
|
2021-05-13 11:57:05 +02:00
|
|
|
private String[] server;
|
|
|
|
private String user;
|
|
|
|
private String password;
|
|
|
|
private MemoryType memoryType;
|
|
|
|
private String dbNames;
|
2020-11-12 18:11:07 +01:00
|
|
|
TransportManager transport;
|
2018-10-02 12:05:43 +02:00
|
|
|
|
|
|
|
public TransportManagerFactory(String server[], String user, String password){
|
|
|
|
this.server=server;
|
|
|
|
this.user=user;
|
|
|
|
this.password=password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TransportManager getTransport(String backendType, MemoryType memoryType, String[] dbNames, String writeConcern, String readConcern){
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug("getOperation(String) - start");
|
|
|
|
}
|
2021-05-13 11:57:05 +02:00
|
|
|
// if we haven't any transport layer instantiated or the transport layer is istantiated on another memory type (persistent, volatile),
|
|
|
|
// then a new transport layer is needed
|
|
|
|
if(Objects.isNull(transport) || (!transport.memoryType.equals(memoryType))) {
|
|
|
|
logger.info("new transport layer instantiated for "+memoryType+" memory");
|
2020-11-12 18:11:07 +01:00
|
|
|
return load(backendType, memoryType, dbNames, writeConcern, readConcern);
|
2021-05-13 11:57:05 +02:00
|
|
|
}
|
2020-11-12 18:11:07 +01:00
|
|
|
return transport;
|
2018-10-02 12:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private TransportManager load(String backendType, MemoryType memoryType, String[] dbNames, String writeConcern, String readConcern){
|
|
|
|
ServiceLoader<TransportManager> loader = ServiceLoader.load(TransportManager.class);
|
|
|
|
Iterator<TransportManager> iterator = loader.iterator();
|
|
|
|
List<TransportManager> impls = new ArrayList<TransportManager>();
|
2020-11-12 18:11:07 +01:00
|
|
|
logger.info("Try to load the backend...");
|
|
|
|
logger.info("the specified backend passed as input param is "+backendType);
|
2018-10-02 12:05:43 +02:00
|
|
|
while(iterator.hasNext())
|
|
|
|
impls.add(iterator.next());
|
|
|
|
int implementationCounted=impls.size();
|
|
|
|
// System.out.println("size: "+implementationCounted);
|
2020-11-12 18:11:07 +01:00
|
|
|
if((implementationCounted==0) || backendType.equals(Costants.DEFAULT_TRANSPORT_MANAGER)){
|
2018-10-02 12:05:43 +02:00
|
|
|
logger.info(" 0 implementation found. Load default implementation of TransportManager");
|
|
|
|
return new MongoOperationManager(server, user, password, memoryType, dbNames, writeConcern, readConcern);
|
2020-11-12 18:11:07 +01:00
|
|
|
}else if((implementationCounted==1) && Objects.isNull(backendType)){
|
2018-10-02 12:05:43 +02:00
|
|
|
TransportManager tm = impls.get(0);
|
|
|
|
logger.info("1 implementation of TransportManager found. Load it. "+tm.getName());
|
|
|
|
tm.initBackend(server, user, password, memoryType, dbNames, writeConcern, readConcern);
|
|
|
|
return tm;
|
|
|
|
}else{
|
|
|
|
logger.info("found "+implementationCounted+" implementations of TransportManager");
|
|
|
|
logger.info("search: "+backendType);
|
|
|
|
for(TransportManager tm : impls){
|
|
|
|
if(tm.getName().equalsIgnoreCase(backendType)){
|
|
|
|
logger.info("Found implementation "+backendType);
|
2020-11-12 18:11:07 +01:00
|
|
|
tm.initBackend(server, user, password, memoryType, dbNames, writeConcern, readConcern);
|
2018-10-02 12:05:43 +02:00
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalStateException("Mismatch Backend Type and RuntimeResource Type. The backend type expected is "+backendType);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|