|
|
|
@ -19,6 +19,8 @@ import javax.jcr.NodeIterator;
|
|
|
|
|
import javax.jcr.PathNotFoundException;
|
|
|
|
|
import javax.jcr.RepositoryException;
|
|
|
|
|
import javax.jcr.Session;
|
|
|
|
|
import javax.jcr.lock.Lock;
|
|
|
|
|
import javax.jcr.lock.LockException;
|
|
|
|
|
import javax.jcr.version.Version;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
|
@ -27,6 +29,7 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
|
|
|
import org.gcube.common.storagehub.model.Excludes;
|
|
|
|
|
import org.gcube.common.storagehub.model.Paths;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.ItemLockedException;
|
|
|
|
|
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
|
|
|
|
import org.gcube.common.storagehub.model.items.FolderItem;
|
|
|
|
|
import org.gcube.common.storagehub.model.items.GCubeItem;
|
|
|
|
@ -76,13 +79,35 @@ public class Utils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void acquireLockWithWait(Session ses, String nodePath, boolean isDeep, String login, int maxTries) throws RepositoryException, ItemLockedException {
|
|
|
|
|
|
|
|
|
|
Lock lock = null;
|
|
|
|
|
int tries = 0;
|
|
|
|
|
while(lock==null && tries<=maxTries) {
|
|
|
|
|
try {
|
|
|
|
|
lock = ses.getWorkspace().getLockManager().lock(nodePath, isDeep, true, 0,login);
|
|
|
|
|
logger.info("lock acquired(try n. {})", tries);
|
|
|
|
|
}catch (LockException e) {
|
|
|
|
|
try {
|
|
|
|
|
if (maxTries>=tries) {
|
|
|
|
|
int waitTime = (int)(Math.random()*5000);
|
|
|
|
|
logger.info("lock NOT acquired, waiting (try n. {}) for {}", tries, waitTime);
|
|
|
|
|
Thread.sleep(waitTime);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e1) {}
|
|
|
|
|
tries++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lock==null) throw new ItemLockedException("the item is locked");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static <T extends Item> List<T> getItemList(Node parent, List<String> excludes, Range range, boolean showHidden, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
|
|
|
|
|
|
|
|
|
|
logger.debug("getting children of node {}", parent.getIdentifier());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<T> returnList = new ArrayList<T>();
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
NodeIterator iterator = parent.getNodes();
|
|
|
|
@ -95,12 +120,12 @@ public class Utils {
|
|
|
|
|
Node current = iterator.nextNode();
|
|
|
|
|
|
|
|
|
|
logger.debug("current node "+current.getName());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isToExclude(current, showHidden))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.debug("current node not excluded "+current.getName());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (range==null || (count>=range.getStart() && returnList.size()<range.getLimit())) {
|
|
|
|
|
T item = node2Item.getFilteredItem(current, excludes, nodeTypeToInclude);
|
|
|
|
|
if (item==null) continue;
|
|
|
|
@ -129,7 +154,7 @@ public class Utils {
|
|
|
|
|
public static org.gcube.common.storagehub.model.Path getHome(String login){
|
|
|
|
|
return Paths.getPath(String.format("/Home/%s",login));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static StorageClient getStorageClient(String login){
|
|
|
|
|
return new StorageClient(SERVICE_CLASS, SERVICE_NAME, login, AccessType.SHARED, MemoryType.PERSISTENT);
|
|
|
|
|
|
|
|
|
@ -211,7 +236,7 @@ public class Utils {
|
|
|
|
|
public static boolean hasSharedChildren(Node node) throws RepositoryException, BackendGenericError{
|
|
|
|
|
Node2ItemConverter node2Item = new Node2ItemConverter();
|
|
|
|
|
NodeIterator children = node.getNodes();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (children.hasNext()) {
|
|
|
|
|
Node child= children.nextNode();
|
|
|
|
|
if (node2Item.checkNodeType(child, SharedFolder.class)) return true;
|
|
|
|
@ -243,7 +268,7 @@ public class Utils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String checkExistanceAndGetUniqueName(Session ses, Node destination, String name) throws BackendGenericError{
|
|
|
|
|
try {
|
|
|
|
|
destination.getNode(name);
|
|
|
|
@ -277,11 +302,11 @@ public class Utils {
|
|
|
|
|
throw new BackendGenericError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Node createFolderInternally(Session ses, Node destinationNode, String name, String description, boolean hidden, String login, AccountingHandler accountingHandler) throws BackendGenericError {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FolderItem item = new FolderItem();
|
|
|
|
|
Calendar now = Calendar.getInstance();
|
|
|
|
|
item.setName(uniqueName);
|
|
|
|
@ -294,18 +319,18 @@ public class Utils {
|
|
|
|
|
item.setLastModifiedBy(login);
|
|
|
|
|
item.setOwner(login);
|
|
|
|
|
item.setPublicItem(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//to inherit hidden property
|
|
|
|
|
//item.setHidden(destinationItem.isHidden());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Node newNode = new Item2NodeConverter().getNode(destinationNode, item);
|
|
|
|
|
if (accountingHandler!=null)
|
|
|
|
|
accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
|
|
|
|
|
return newNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Node createGcubeItemInternally(Session ses, Node destinationNode, String name, String description, String login, GCubeItem gcubeItem, AccountingHandler accountingHandler) throws BackendGenericError {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Calendar now = Calendar.getInstance();
|
|
|
|
|
gcubeItem.setName(name);
|
|
|
|
|
gcubeItem.setTitle(name);
|
|
|
|
@ -324,7 +349,7 @@ public class Utils {
|
|
|
|
|
//accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
|
|
|
|
|
return newNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void setPropertyOnChangeNode(Node node, String login, ItemAction action) throws RepositoryException {
|
|
|
|
|
node.setProperty(NodeProperty.LAST_MODIFIED.toString(), Calendar.getInstance());
|
|
|
|
|
node.setProperty(NodeProperty.LAST_MODIFIED_BY.toString(), login);
|
|
|
|
|