Added method: getAvailableCataloguesByToken

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portal/ws-synchronized-module-library@164806 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-03-08 10:55:32 +00:00
parent a6a6d86632
commit 25f0145bf8
3 changed files with 264 additions and 82 deletions

View File

@ -0,0 +1,127 @@
/**
*
*/
package org.gcube.portal.wssynclibrary.shared.thredds;
import java.io.Serializable;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Mar 8, 2018
*/
public class ThCatalogueBean implements Serializable, Comparable<ThCatalogueBean>{
/**
*
*/
private static final long serialVersionUID = 56447135995876633L;
private String name;
private String path;
private Boolean isDefault;
public ThCatalogueBean() {
// TODO Auto-generated constructor stub
}
/**
* @param name
* @param path
* @param isDefault
*/
public ThCatalogueBean(String name, String path, Boolean isDefault) {
super();
this.name = name;
this.path = path;
this.isDefault = isDefault;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the path
*/
public String getPath() {
return path;
}
/**
* @return the isDefault
*/
public Boolean getIsDefault() {
return isDefault;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
/**
* @param isDefault the isDefault to set
*/
public void setIsDefault(Boolean isDefault) {
this.isDefault = isDefault;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ThCatalogueBean [name=");
builder.append(name);
builder.append(", path=");
builder.append(path);
builder.append(", isDefault=");
builder.append(isDefault);
builder.append("]");
return builder.toString();
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(ThCatalogueBean o) {
if(o==null)
return -1;
return this.getName().compareTo(o.getName());
}
}

View File

@ -3,6 +3,7 @@ package org.gcube.portal.wssynclibrary.thredds;
import java.util.function.Function;
import org.gcube.portal.wssynclibrary.shared.thredds.Status;
import org.gcube.portal.wssynclibrary.shared.thredds.ThCatalogueBean;
import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessDescriptor;
import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessStatus;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor;
@ -11,6 +12,7 @@ import org.gcube.usecases.ws.thredds.engine.impl.ProcessDescriptor;
import org.gcube.usecases.ws.thredds.engine.impl.ProcessStatus;
import org.gcube.usecases.ws.thredds.model.SyncFolderDescriptor;
import org.gcube.usecases.ws.thredds.model.SynchFolderConfiguration;
import org.gcube.usecases.ws.thredds.model.gui.CatalogBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -25,11 +27,11 @@ import org.slf4j.LoggerFactory;
* Feb 14, 2018
*/
public class ThreddsConverter {
public static final String THREDDS_FILTER_NC_NCML_ASC = "*.nc,*.ncml,*.asc";
private static Logger logger = LoggerFactory.getLogger(ThreddsConverter.class);
/** The to S sync folder descriptor. */
public static Function<SyncFolderDescriptor, ThSyncFolderDescriptor> toThSyncFolderDescriptor = new Function<SyncFolderDescriptor, ThSyncFolderDescriptor>() {
@ -39,7 +41,7 @@ public class ThreddsConverter {
logger.info("Input "+SyncFolderDescriptor.class.getSimpleName()+" is null, returning empty "+ThSyncFolderDescriptor.class.getSimpleName());
return mySync;
}
mySync.setFolderId(t.getFolderId());
mySync.setFolderPath(t.getFolderPath());
mySync.setLocked(t.isLocked());
@ -47,43 +49,43 @@ public class ThreddsConverter {
mySync.setLocalProcessDescriptor(localProcessDescriptor);
ThSynchFolderConfiguration configuration = toThSynchFolderConfiguration.apply(t.getConfiguration());
mySync.setConfiguration(configuration);
return mySync;
}
};
/** The to S sync folder descriptor. */
public static Function<ThSyncFolderDescriptor, SyncFolderDescriptor> toSyncFolderDescriptor = new Function<ThSyncFolderDescriptor, SyncFolderDescriptor>() {
public SyncFolderDescriptor apply(ThSyncFolderDescriptor t) {
if(t==null) {
logger.warn("Input "+ThSyncFolderDescriptor.class.getSimpleName()+" is null, returning null");
return null;
}
SynchFolderConfiguration configuration = toSynchFolderConfiguration.apply(t.getConfiguration());
return new SyncFolderDescriptor(t.getFolderId(), t.getFolderPath(), configuration);
}
};
/** The to synch folder configuration. */
public static Function<ThSynchFolderConfiguration, SynchFolderConfiguration> toSynchFolderConfiguration = new Function<ThSynchFolderConfiguration, SynchFolderConfiguration>() {
public SynchFolderConfiguration apply(ThSynchFolderConfiguration t) {
if(t==null) {
logger.warn("Input "+ThSynchFolderConfiguration.class.getSimpleName()+" is null, returning null");
return null;
}
String filter = t.getFilter()!=null && !t.getFilter().isEmpty()?t.getFilter():THREDDS_FILTER_NC_NCML_ASC;
return new SynchFolderConfiguration(t.getRemotePath(), filter, t.getTargetToken(), t.getToCreateCatalogName());
}
};
/** The to S synch folder configuration. */
public static Function<SynchFolderConfiguration, ThSynchFolderConfiguration> toThSynchFolderConfiguration = new Function<SynchFolderConfiguration, ThSynchFolderConfiguration>() {
@ -101,8 +103,8 @@ public class ThreddsConverter {
return mySync;
}
};
/** The to S sync folder descriptor. */
public static Function<ProcessDescriptor, ThProcessDescriptor> toThProcessDescriptor = new Function<ProcessDescriptor, ThProcessDescriptor>() {
@ -112,7 +114,7 @@ public class ThreddsConverter {
logger.info("Input "+ProcessDescriptor.class.getSimpleName()+" is null, returning empty "+ThProcessDescriptor.class.getSimpleName());
return mySync;
}
mySync.setFolderId(t.getFolderId());
mySync.setFolderPath(t.getFolderPath());
mySync.setProcessId(t.getProcessId());
@ -121,8 +123,22 @@ public class ThreddsConverter {
return mySync;
}
};
/** The to th catalogue bean. */
public static Function<CatalogBean, ThCatalogueBean> toThCatalogueBean = new Function<CatalogBean, ThCatalogueBean>() {
@Override
public ThCatalogueBean apply(CatalogBean t) {
if(t==null)
return null;
return new ThCatalogueBean(t.getName(), t.getPath(), t.getIsDefault());
}
};
/** The to th process status. */
public static Function<ProcessStatus, ThProcessStatus> toThProcessStatus = new Function<ProcessStatus, ThProcessStatus>() {
@ -132,10 +148,10 @@ public class ThreddsConverter {
logger.info("Input "+ProcessStatus.class.getSimpleName()+" is null, returning empty "+ThProcessStatus.class.getSimpleName());
return mySync;
}
mySync.setCurrentMessage(t.getCurrentMessage());
mySync.setPercentCompleted(t.getPercent());
if(t.getErrorCount()!=null)
mySync.setErrorCount(t.getErrorCount().get());
if(t.getLogBuilder()!=null)
@ -144,7 +160,7 @@ public class ThreddsConverter {
mySync.setQueuedTransfers(t.getQueuedTransfers().get());
if(t.getServedTransfers()!=null)
mySync.setServedTransfers(t.getServedTransfers().get());
if(t.getStatus()!=null) {
switch (t.getStatus()) {
@ -163,7 +179,7 @@ public class ThreddsConverter {
case STOPPED:
mySync.setStatus(Status.STOPPED);
break;
default:
break;
}
@ -174,4 +190,5 @@ public class ThreddsConverter {
};
}

View File

@ -1,8 +1,11 @@
package org.gcube.portal.wssynclibrary.thredds;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.workspace.Properties;
@ -12,6 +15,7 @@ import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.wssynclibrary.shared.ItemNotSynched;
import org.gcube.portal.wssynclibrary.shared.thredds.Status;
import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status;
import org.gcube.portal.wssynclibrary.shared.thredds.ThCatalogueBean;
import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessDescriptor;
import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessStatus;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor;
@ -28,6 +32,7 @@ import org.gcube.usecases.ws.thredds.faults.WorkspaceNotSynchedException;
import org.gcube.usecases.ws.thredds.model.SyncFolderDescriptor;
import org.gcube.usecases.ws.thredds.model.SyncOperationCallBack;
import org.gcube.usecases.ws.thredds.model.SynchFolderConfiguration;
import org.gcube.usecases.ws.thredds.model.gui.CatalogBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -40,22 +45,22 @@ import org.slf4j.LoggerFactory;
* Feb 14, 2018
*/
public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronizedRepository<ThSyncStatus, ThSyncFolderDescriptor>{
/** The logger. */
private static Logger logger = LoggerFactory.getLogger(WorkspaceThreddsSynchronize.class);
/** The engine. */
private SyncEngine engine=null;
/** The instance. */
private static WorkspaceThreddsSynchronize instance = null;
public static final String WS_SYNCH_SYNCH_STATUS = "WS-SYNCH.SYNCH-STATUS";
/** The map call back. */
// Fully synchronized HashMap
private Map<String, ThSyncStatus> mapCallBack = Collections.synchronizedMap(new HashMap<>());
/**
* Instantiates a new workspace thredds synchronize.
*/
@ -63,7 +68,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
// GET ENGINE : SINGLETON INSTANCE
engine = SyncEngine.get();
}
/**
* Gets the single instance of WorkspaceThreddsSynchronize.
@ -76,10 +81,10 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
}
return instance;
}
//setSynchronizedFolder(config, folder.getId());
/* (non-Javadoc)
* @see org.gcube.portal.wssynclibrary.WorkspaceSynchronizedRepository#isItemSynchronized(java.lang.String)
*/
@ -87,14 +92,14 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
public ThSyncFolderDescriptor checkItemSynched(String itemId) throws ItemNotSynched, Exception {
logger.debug("Perfoming checkItemSynched for id: "+itemId);
try {
// WHEN OPENING A FOLDER, INVOKE CHECK TO UPDATE SYNCH STATUS
// WHEN OPENING A FOLDER, INVOKE CHECK TO UPDATE SYNCH STATUS
SyncFolderDescriptor desc = engine.check(itemId, false);
logger.debug("The item id: "+itemId +" is synched");
ThSyncFolderDescriptor descr = ThreddsConverter.toThSyncFolderDescriptor.apply(desc);
logger.trace("CheckItemSynched for id: "+itemId+" returning descriptor: "+descr);
return descr;
}catch(WorkspaceNotSynchedException e) {
String err = "The item id: "+itemId +" is not synched";
logger.trace(err);
@ -106,7 +111,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
engine.forceUnlock(itemId);
//System.out.println("Workspace locked, going to force unlock.."); // MAINLY FOR TEST PURPOSES, OR WHEN SOMETHIGN GOES WRONG.. USE CAUTIOUSLY
//engine.forceUnlock(folder.getId());
//TODO???
return null;
//???
@ -116,13 +121,13 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
throw new Exception("Sorry, an error occurred during check syncronization due to WS interection for the itemId: "+itemId);
else if(e instanceof InternalException)
throw new Exception("Sorry, an Internal Exception occurred during check syncronization for the itemId: "+itemId);
throw new Exception("Sorry, an error occurred server side during chck syncronization for the itemId: "+itemId);
}
}
/**
* Sets the synchronized folder.
*
@ -133,16 +138,16 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
*/
public ThSyncFolderDescriptor setSynchronizedFolder(ThSynchFolderConfiguration thConfig, String itemId) throws Exception {
SynchFolderConfiguration config = ThreddsConverter.toSynchFolderConfiguration.apply(thConfig);
if(thConfig.getRemotePath()==null || thConfig.getRemotePath().isEmpty())
throw new Exception("A valid remote path must be provided");
if(thConfig.getToCreateCatalogName()==null || thConfig.getToCreateCatalogName().isEmpty())
throw new Exception("A valid Catalogue Name must be provided");
if(thConfig.getTargetToken()==null || thConfig.getTargetToken().isEmpty())
throw new Exception("A valid Target Token must be provided");
try {
engine.setSynchronizedFolder(config, itemId);
} catch (WorkspaceInteractionException | InternalException e) {
@ -152,68 +157,68 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
}
return null;
}
/* (non-Javadoc)
* @see org.gcube.portal.wssynclibrary.WorkspaceSynchronizedRepository#doSynchronization(java.lang.String)
*/
@Override
public ThSyncStatus doSync(String itemId) throws Exception {
try {
// INVOKE SYNCHRONIZATION ON FOLDER
ProcessDescriptor descriptor = engine.doSync(itemId);
ThSyncStatus synStatus = mapCallBack.get(itemId);
if(synStatus==null) {
registerCallbackForId(itemId);
descriptor = engine.getProcessDescriptorByFolderId(itemId);
ProcessStatus status = engine.getProcessStatusByFolderId(itemId);
updateMapCallback(itemId, status, descriptor);
}
logger.debug("DoSync returning status: "+synStatus);
return synStatus;
} catch (WorkspaceInteractionException | InternalException | ProcessNotFoundException e) {
logger.error("Error: ",e);
if(e instanceof WorkspaceInteractionException)
throw new Exception("Sorry, an error occurred during syncronization due to WS interection for the itemId: "+itemId);
else if(e instanceof InternalException)
throw new Exception("Sorry, an Internal Exception occurred during syncronization for the itemId: "+itemId);
throw new Exception("Sorry, an error occurred server side during syncronization for the itemId: "+itemId);
}
}
/* (non-Javadoc)
* @see org.gcube.portal.wssynclibrary.DoSyncItem#getSyncStatus(java.lang.String)
*/
@Override
public ThSyncStatus monitorSyncStatus(String itemId) throws ItemNotSynched, Exception {
try {
//ThSyncFolderDescriptor descr = checkItemSynched(itemId);
ThSyncStatus synStatus = mapCallBack.get(itemId);
if(synStatus==null) {
logger.info("The sync is not on-going...,returning process status null");
// String err = "The item id: "+itemId +" is not cached as synched";
// throw new ItemNotSynched(err);
return new ThSyncStatus(null, null);
}
if(synStatus.getProcessStatus()!=null) {
if(synStatus.getProcessStatus().getStatus()!=null) {
if(synStatus.getProcessStatus().getStatus().equals(Status.COMPLETED)) {
@ -222,10 +227,10 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
}
}
}
logger.trace("MonitorSyncStatus for item: "+itemId+" returning: "+synStatus);
return synStatus;
// } catch (ItemNotSynched e) {
// String err = "The item id: "+itemId +" is not synched repository side";
// throw new ItemNotSynched(err);
@ -242,7 +247,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
public Boolean removeSync(String itemId) {
return null;
// TODO Auto-generated method stub
}
@ -252,7 +257,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
@Override
public Boolean stopSync(String itemId) {
return false;
}
@ -262,7 +267,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
@Override
public Boolean initRepository() {
return false;
}
@ -277,7 +282,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
}catch (Exception e) {
return null;
}
}
@ -290,7 +295,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
}
/**
* Gets the synched status from item property.
*
@ -301,36 +306,36 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
* @throws Exception the exception
*/
public Sync_Status getSynchedStatusFromItemProperty(String folderId, String username) throws ItemNotSynched, Exception{
if(folderId==null)
throw new Exception("Invalid pareter itemId is null");
String scope = ScopeProvider.instance.get();
if(scope == null || scope.isEmpty())
throw new Exception("You must set a valid scope into ScopeProvider instance");
WorkspaceFolder folder = null;
try {
logger.debug("Scope provider instancied at: "+scope);
Workspace workspace = HomeLibrary.getUserWorkspace(username);
folder = (WorkspaceFolder) workspace.getItem(folderId);
}catch (Exception e) {
logger.error("Error HL side: "+folderId, e);
throw new Exception("Sorry an error occurred getting HL item for id: "+folderId);
}
if(folder==null)
throw new Exception("Sorry an error occurred getting HL item for id: "+folderId);
Properties properties = folder.getProperties();
if(properties==null || properties.getProperties()==null || properties.getProperties().size()==0) {
throw new ItemNotSynched("No properties to read");
}
String wsSyncStatus = properties.getProperties().get(WS_SYNCH_SYNCH_STATUS);
logger.info("Folder id: "+folder.getId()+" has current: "+WS_SYNCH_SYNCH_STATUS +" value at: "+wsSyncStatus);
try {
@ -341,7 +346,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
}
}
/**
* Register callback for id.
*
@ -350,11 +355,11 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
*/
@Override
public void registerCallbackForId(String itemId) throws Exception{
try {
SyncOperationCallBack callback = new SyncOperationCallBack() {
@Override
public void onStep(ProcessStatus status, ProcessDescriptor descriptor) {
logger.debug("ON STEP : "+status+" "+descriptor);
@ -365,7 +370,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
updateMapCallback(itemId, status, descriptor);
}
};
// REGISTER CALLBACK TO MONITOR PROGRESS
logger.debug("Registering callback on itemId: "+itemId);
engine.registerCallBack(itemId, callback);
@ -374,8 +379,8 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
throw new Exception("No Process found for item id: "+itemId+ ", refresh an try again");
}
}
/**
* Update map callback.
*
@ -389,4 +394,37 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized
mapCallBack.put(itemId, new ThSyncStatus(thDesc, thStatus));
logger.debug("Update map for "+itemId +" with new "+thStatus);
}
/**
* Gets the available catalogues by token.
*
* @param token the token
* @return
* @return the available catalogues by token
* @throws Exception the exception
*/
public List<ThCatalogueBean> getAvailableCataloguesByToken(String token) throws Exception{
if(token==null || token.isEmpty())
throw new Exception("Invalid parameter token null or empty");
logger.debug("Get Available Catalogues by token: "+token.substring(0, token.length()-5)+"XXXXX");
Set<CatalogBean> ctlgs = engine.getAvailableCatalogsByToken(token);
if(ctlgs==null || ctlgs.size()==0)
return new ArrayList<ThCatalogueBean>(1);
List<ThCatalogueBean> listCtlgs = new ArrayList<ThCatalogueBean>(ctlgs.size());
for(CatalogBean bean: ctlgs){
System.out.println(bean.getName()+" in "+bean.getPath()+" Default : "+bean.getIsDefault());
ThCatalogueBean toBean = ThreddsConverter.toThCatalogueBean.apply(bean);
if(toBean!=null)
listCtlgs.add(toBean);
}
Collections.sort(listCtlgs);
return listCtlgs;
}
}