|
From: <ha...@us...> - 2006-10-18 20:54:19
|
Revision: 1011
http://svn.sourceforge.net/cogkit/?rev=1011&view=rev
Author: hategan
Date: 2006-10-18 13:54:16 -0700 (Wed, 18 Oct 2006)
Log Message:
-----------
async cached resource shutdown; mkdirs() file operation
Modified Paths:
--------------
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceCache.java
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/TaskHandlerImpl.java
Added Paths:
-----------
trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceUtil.java
Modified: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceCache.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceCache.java 2006-10-18 20:51:35 UTC (rev 1010)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceCache.java 2006-10-18 20:54:16 UTC (rev 1011)
@@ -51,41 +51,52 @@
private Timer timer;
private int maxIdleResources = DEFAULT_MAX_IDLE_RESOURCES;
private long maxIdleTime = DEFAULT_MAX_IDLE_TIME;
+ private ResourceStopper stopper;
public FileResourceCache() {
fileResources = new Hashtable();
inUse = new HashSet();
order = new LinkedList();
releaseTimes = new Hashtable();
+ stopper = new ResourceStopper();
}
- public synchronized FileResource getResource(Service service) throws InvalidProviderException,
+ public FileResource getResource(Service service) throws InvalidProviderException,
ProviderMethodException, IllegalHostException, InvalidSecurityContextException,
GeneralException {
logger.debug("Got request for resource for " + service);
checkTimer();
ServiceContact contact = service.getServiceContact();
- if (fileResources.containsKey(contact)) {
- List resources = (List) fileResources.get(contact);
- Iterator i = resources.iterator();
- while (i.hasNext()) {
- FileResource fileResource = (FileResource) i.next();
- if (!inUse.contains(fileResource)) {
- inUse.add(fileResource);
- order.remove(fileResource);
- releaseTimes.remove(fileResource);
- logger.debug("Found cached resource");
- return fileResource;
+ FileResource fileResource;
+ synchronized (this) {
+ if (fileResources.containsKey(contact)) {
+ List resources = (List) fileResources.get(contact);
+ Iterator i = resources.iterator();
+ while (i.hasNext()) {
+ fileResource = (FileResource) i.next();
+ if (!inUse.contains(fileResource)) {
+ inUse.add(fileResource);
+ order.remove(fileResource);
+ releaseTimes.remove(fileResource);
+ logger.debug("Found cached resource");
+ return fileResource;
+ }
}
}
+ fileResource = newResource(service);
}
- return newResource(service);
+ synchronized (fileResource) {
+ if (!fileResource.isStarted()) {
+ fileResource.start();
+ }
+ }
+ return fileResource;
}
private FileResource newResource(Service service) throws InvalidProviderException,
ProviderMethodException, IllegalHostException, InvalidSecurityContextException,
GeneralException {
- //TODO Are file resources reentrant?
+ // TODO Are file resources reentrant?
logger.debug("Instantiating new resource for " + service);
String provider = service.getProvider();
ServiceContact contact = service.getServiceContact();
@@ -99,7 +110,6 @@
FileResource fileResource = AbstractionFactory.newFileResource(provider);
fileResource.setServiceContact(contact);
fileResource.setSecurityContext(securityContext);
- fileResource.start();
List resources;
if (fileResources.containsKey(contact)) {
resources = (List) fileResources.get(contact);
@@ -113,30 +123,29 @@
return fileResource;
}
- public synchronized void releaseResource(FileResource resource) {
+ public void releaseResource(FileResource resource) {
if (resource == null) {
return;
}
- logger.debug("Releasing resource for " + resource.getServiceContact());
- if (!inUse.contains(resource)) {
- throw new RuntimeException("Attempted to release resource that is not in use");
+ synchronized (this) {
+ logger.debug("Releasing resource for " + resource.getServiceContact());
+ if (!inUse.contains(resource)) {
+ throw new RuntimeException("Attempted to release resource that is not in use");
+ }
+ inUse.remove(resource);
+ order.addLast(resource);
+ releaseTimes.put(resource, new Long(System.currentTimeMillis()));
}
- inUse.remove(resource);
- order.addLast(resource);
- releaseTimes.put(resource, new Long(System.currentTimeMillis()));
checkIdleResourceCount();
}
private void removeResource(FileResource resource) {
- if (fileResources.containsKey(resource.getServiceContact())) {
- List resources = (List) fileResources.get(resource.getServiceContact());
- resources.remove(resource);
- try {
- resource.stop();
+ synchronized (this) {
+ if (fileResources.containsKey(resource.getServiceContact())) {
+ List resources = (List) fileResources.get(resource.getServiceContact());
+ resources.remove(resource);
+ stopper.addResource(resource);
}
- catch (GeneralException e) {
- logger.warn("Failed to stop resource", e);
- }
}
}
@@ -188,8 +197,7 @@
+ resource.getServiceContact());
removeResource(resource);
}
- }
- while (last < threshold);
+ } while (last < threshold);
}
private class ResourceSwipe extends TimerTask {
@@ -203,4 +211,54 @@
cache.checkIdleResourceAges();
}
}
+
+ public static class ResourceStopper implements Runnable {
+ private LinkedList resources;
+ private boolean running;
+
+ public ResourceStopper () {
+ resources = new LinkedList();
+ running = false;
+ }
+
+ public void addResource(FileResource fr) {
+ synchronized(this) {
+ resources.add(fr);
+ if (!running) {
+ running = true;
+ Thread t = new Thread(this);
+ t.setName("File resource stopper");
+ t.setDaemon(true);
+ t.start();
+ }
+ }
+ }
+
+ public void run() {
+ FileResource fr = nextResource();
+ while (fr != null) {
+ try {
+ fr.stop();
+ }
+ catch (GeneralException e) {
+ logger.warn("Failed to stop resource", e);
+ }
+ fr = nextResource();
+ }
+ synchronized(this) {
+ running = false;
+ }
+ }
+
+ private FileResource nextResource() {
+ synchronized(this) {
+ if (resources.isEmpty()) {
+ return null;
+ }
+ else {
+ return (FileResource) resources.removeFirst();
+ }
+ }
+ }
+ }
}
\ No newline at end of file
Added: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceUtil.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceUtil.java (rev 0)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/FileResourceUtil.java 2006-10-18 20:54:16 UTC (rev 1011)
@@ -0,0 +1,42 @@
+//----------------------------------------------------------------------
+//This code is developed as part of the Java CoG Kit project
+//The terms of the license can be found at http://www.cogkit.org/license
+//This message may not be removed or altered.
+//----------------------------------------------------------------------
+
+/*
+ * Created on Oct 18, 2006
+ */
+package org.globus.cog.abstraction.impl.file;
+
+import java.util.StringTokenizer;
+
+import org.globus.cog.abstraction.interfaces.FileResource;
+
+public class FileResourceUtil {
+
+ public static void createDirectories(FileResource fr, String dir) throws GeneralException {
+ // TODO this should be in an AbstractFileResource
+ // there is an assumption here on the path separators
+ // I'd really suggest enforcing only one of them at the level of the
+ // interface
+ StringTokenizer st = new StringTokenizer(dir, "/");
+ StringBuffer sb = new StringBuffer();
+ if (dir.startsWith("/")) {
+ sb.append('/');
+ }
+ while (st.hasMoreTokens()) {
+ try {
+ String partial = sb.toString();
+ if (!fr.exists(partial)) {
+ fr.createDirectory(partial);
+ }
+ sb.append(st.nextToken());
+ sb.append('/');
+ }
+ catch (Exception e) {
+ throw new GeneralException("Could not create directory structure " + dir, e);
+ }
+ }
+ }
+}
Modified: trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/TaskHandlerImpl.java
===================================================================
--- trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/TaskHandlerImpl.java 2006-10-18 20:51:35 UTC (rev 1010)
+++ trunk/current/src/cog/modules/abstraction-common/src/org/globus/cog/abstraction/impl/file/TaskHandlerImpl.java 2006-10-18 20:54:16 UTC (rev 1011)
@@ -8,7 +8,9 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
import java.util.Hashtable;
+import java.util.Set;
import java.util.Vector;
import org.apache.log4j.Logger;
@@ -48,9 +50,39 @@
private Hashtable activeFileResources;
private Identity defaultSessionId = null;
- private Vector oneWordCommands = null;
- private Vector twoWordCommands = null;
- private Vector threeWordCommands = null;
+ private static final Set oneWordCommands, twoWordCommands, threeWordCommands;
+
+ static {
+ oneWordCommands = new HashSet();
+ twoWordCommands = new HashSet();
+ threeWordCommands = new HashSet();
+
+ // Add all one word commands
+ oneWordCommands.add(FileOperationSpecification.START);
+ oneWordCommands.add(FileOperationSpecification.STOP);
+ oneWordCommands.add(FileOperationSpecification.LS);
+ oneWordCommands.add(FileOperationSpecification.PWD);
+
+ // Add all two word commands
+ twoWordCommands.add(FileOperationSpecification.LS);
+ twoWordCommands.add(FileOperationSpecification.MKDIR);
+ twoWordCommands.add(FileOperationSpecification.MKDIRS);
+ twoWordCommands.add(FileOperationSpecification.RMDIR);
+ twoWordCommands.add(FileOperationSpecification.RMFILE);
+ twoWordCommands.add(FileOperationSpecification.EXISTS);
+ twoWordCommands.add(FileOperationSpecification.CD);
+ twoWordCommands.add(FileOperationSpecification.ISDIRECTORY);
+
+ // Add all three word commands
+ threeWordCommands.add(FileOperationSpecification.RMDIR);
+ threeWordCommands.add(FileOperationSpecification.GETFILE);
+ threeWordCommands.add(FileOperationSpecification.PUTFILE);
+ threeWordCommands.add(FileOperationSpecification.GETDIR);
+ threeWordCommands.add(FileOperationSpecification.PUTDIR);
+ threeWordCommands.add(FileOperationSpecification.RENAME);
+ threeWordCommands.add(FileOperationSpecification.CHMOD);
+ }
+
private int type;
private Task task = null;
@@ -59,7 +91,6 @@
static Logger logger = Logger.getLogger(TaskHandlerImpl.class.getName());
public TaskHandlerImpl() {
-
this.submittedList = new Vector();
this.activeList = new Vector();
this.suspendedList = new Vector();
@@ -70,11 +101,6 @@
this.handleMap = new Hashtable();
this.type = TaskHandler.FILE_OPERATION;
this.activeFileResources = new Hashtable();
- oneWordCommands = new Vector();
- twoWordCommands = new Vector();
- threeWordCommands = new Vector();
- addValidCommands();
-
}
/** set type of task handler */
@@ -267,6 +293,10 @@
&& spec.getArgumentSize() == 1) {
fileResource.createDirectory(spec.getArgument(0));
} else if (operation
+ .equalsIgnoreCase(FileOperationSpecification.MKDIRS)
+ && spec.getArgumentSize() == 1) {
+ fileResource.createDirectories(spec.getArgument(0));
+ } else if (operation
.equalsIgnoreCase(FileOperationSpecification.RMDIR)
&& spec.getArgumentSize() == 2) {
fileResource.deleteDirectory(spec.getArgument(0), Boolean
@@ -499,33 +529,6 @@
}
}
- /** Add a list of valid commands to a vector */
- public void addValidCommands() {
- // Add all one word commands
- oneWordCommands.add(FileOperationSpecification.START);
- oneWordCommands.add(FileOperationSpecification.STOP);
- oneWordCommands.add(FileOperationSpecification.LS);
- oneWordCommands.add(FileOperationSpecification.PWD);
-
- // Add all two word commands
- twoWordCommands.add(FileOperationSpecification.LS);
- twoWordCommands.add(FileOperationSpecification.MKDIR);
- twoWordCommands.add(FileOperationSpecification.RMDIR);
- twoWordCommands.add(FileOperationSpecification.RMFILE);
- twoWordCommands.add(FileOperationSpecification.EXISTS);
- twoWordCommands.add(FileOperationSpecification.CD);
- twoWordCommands.add(FileOperationSpecification.ISDIRECTORY);
-
- // Add all three word commands
- threeWordCommands.add(FileOperationSpecification.RMDIR);
- threeWordCommands.add(FileOperationSpecification.GETFILE);
- threeWordCommands.add(FileOperationSpecification.PUTFILE);
- threeWordCommands.add(FileOperationSpecification.GETDIR);
- threeWordCommands.add(FileOperationSpecification.PUTDIR);
- threeWordCommands.add(FileOperationSpecification.RENAME);
- threeWordCommands.add(FileOperationSpecification.CHMOD);
- }
-
private SecurityContext getSecurityContext() {
SecurityContext securityContext = this.task.getService(0)
.getSecurityContext();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|