Change the behavior of AVMService.getAVMStore() and AVMService.lookup()

methods to return null for not found instead of throwing an exception.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3781 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Britt Park
2006-09-13 14:54:25 +00:00
parent a06fc74aef
commit 194d96082c
8 changed files with 91 additions and 76 deletions

View File

@@ -148,6 +148,10 @@ public class AVMInterpreter
} }
AVMNodeDescriptor desc = fService.lookup(Integer.parseInt(command[2]), AVMNodeDescriptor desc = fService.lookup(Integer.parseInt(command[2]),
command[1]); command[1]);
if (desc == null)
{
return "Not Found.";
}
Map<String, AVMNodeDescriptor> listing = Map<String, AVMNodeDescriptor> listing =
fService.getDirectoryListing(desc); fService.getDirectoryListing(desc);
for (String name : listing.keySet()) for (String name : listing.keySet())
@@ -424,6 +428,10 @@ public class AVMInterpreter
return "Syntax Error."; return "Syntax Error.";
} }
AVMStoreDescriptor desc = fService.getAVMStore(command[1]); AVMStoreDescriptor desc = fService.getAVMStore(command[1]);
if (desc == null)
{
return "Not Found.";
}
out.println(desc); out.println(desc);
Map<QName, PropertyValue> props = Map<QName, PropertyValue> props =
fService.getStoreProperties(command[1]); fService.getStoreProperties(command[1]);

View File

@@ -142,15 +142,7 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
*/ */
public boolean exists(StoreRef storeRef) public boolean exists(StoreRef storeRef)
{ {
try return fAVMService.getAVMStore(storeRef.getIdentifier()) != null;
{
fAVMService.getAVMStore(storeRef.getIdentifier());
return true;
}
catch (AVMNotFoundException e)
{
return false;
}
} }
/** /**
@@ -162,15 +154,7 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
Object [] avmInfo = AVMNodeConverter.ToAVMVersionPath(nodeRef); Object [] avmInfo = AVMNodeConverter.ToAVMVersionPath(nodeRef);
int version = (Integer)avmInfo[0]; int version = (Integer)avmInfo[0];
String avmPath = (String)avmInfo[1]; String avmPath = (String)avmInfo[1];
try return fAVMService.lookup(version, avmPath) != null;
{
fAVMService.lookup(version, avmPath);
return true;
}
catch (AVMException e)
{
return false;
}
} }
/** /**
@@ -195,15 +179,14 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
*/ */
public NodeRef getRootNode(StoreRef storeRef) throws InvalidStoreRefException public NodeRef getRootNode(StoreRef storeRef) throws InvalidStoreRefException
{ {
try String storeName = storeRef.getIdentifier();
if (fAVMService.getAVMStore(storeName) != null)
{ {
String storeName = storeRef.getIdentifier();
fAVMService.getAVMStore(storeName);
return AVMNodeConverter.ToNodeRef(-1, storeName + ":/"); return AVMNodeConverter.ToNodeRef(-1, storeName + ":/");
} }
catch (AVMNotFoundException e) else
{ {
throw new InvalidStoreRefException(storeRef); throw new InvalidStoreRefException("Not Found.", storeRef);
} }
} }
@@ -466,32 +449,28 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
public QName getType(NodeRef nodeRef) throws InvalidNodeRefException public QName getType(NodeRef nodeRef) throws InvalidNodeRefException
{ {
Object [] avmVersionPath = AVMNodeConverter.ToAVMVersionPath(nodeRef); Object [] avmVersionPath = AVMNodeConverter.ToAVMVersionPath(nodeRef);
try AVMNodeDescriptor desc = fAVMService.lookup((Integer)avmVersionPath[0],
{ (String)avmVersionPath[1]);
AVMNodeDescriptor desc = fAVMService.lookup((Integer)avmVersionPath[0], if (desc == null)
(String)avmVersionPath[1]);
if (desc.isPlainDirectory())
{
return ContentModel.TYPE_AVM_PLAIN_FOLDER;
}
else if (desc.isPlainFile())
{
return ContentModel.TYPE_AVM_PLAIN_CONTENT;
}
else if (desc.isLayeredDirectory())
{
return ContentModel.TYPE_AVM_LAYERED_FOLDER;
}
else
{
return ContentModel.TYPE_AVM_LAYERED_CONTENT;
}
}
catch (AVMNotFoundException e)
{ {
throw new InvalidNodeRefException("Not Found.", nodeRef); throw new InvalidNodeRefException("Not Found.", nodeRef);
} }
if (desc.isPlainDirectory())
{
return ContentModel.TYPE_AVM_PLAIN_FOLDER;
}
else if (desc.isPlainFile())
{
return ContentModel.TYPE_AVM_PLAIN_CONTENT;
}
else if (desc.isLayeredDirectory())
{
return ContentModel.TYPE_AVM_LAYERED_FOLDER;
}
else
{
return ContentModel.TYPE_AVM_LAYERED_CONTENT;
}
} }
/** /**
@@ -844,13 +823,12 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
{ {
Object [] avmVersionPath = AVMNodeConverter.ToAVMVersionPath(nodeRef); Object [] avmVersionPath = AVMNodeConverter.ToAVMVersionPath(nodeRef);
Map<QName, PropertyValue> props = null; Map<QName, PropertyValue> props = null;
AVMNodeDescriptor desc = null; AVMNodeDescriptor desc = fAVMService.lookup((Integer)avmVersionPath[0],
(String)avmVersionPath[1]);
try try
{ {
props = fAVMService.getNodeProperties((Integer)avmVersionPath[0], props = fAVMService.getNodeProperties((Integer)avmVersionPath[0],
(String)avmVersionPath[1]); (String)avmVersionPath[1]);
desc = fAVMService.lookup((Integer)avmVersionPath[0],
(String)avmVersionPath[1]);
} }
catch (AVMNotFoundException e) catch (AVMNotFoundException e)
{ {
@@ -958,13 +936,9 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
return null; return null;
} }
} }
AVMNodeDescriptor desc = null; AVMNodeDescriptor desc = fAVMService.lookup((Integer)avmVersionPath[0],
try
{
desc = fAVMService.lookup((Integer)avmVersionPath[0],
(String)avmVersionPath[1]); (String)avmVersionPath[1]);
} if (desc == null)
catch (AVMNotFoundException e)
{ {
throw new InvalidNodeRefException("Not Found.", nodeRef); throw new InvalidNodeRefException("Not Found.", nodeRef);
} }
@@ -1049,6 +1023,7 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
{ {
throw new InvalidNodeRefException("Read only store.", nodeRef); throw new InvalidNodeRefException("Read only store.", nodeRef);
} }
// TODO Not sure this try block is necessary.
try try
{ {
// Invoke policy behaviors. // Invoke policy behaviors.
@@ -1063,6 +1038,10 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
if (qName.equals(ContentModel.PROP_CONTENT)) if (qName.equals(ContentModel.PROP_CONTENT))
{ {
AVMNodeDescriptor desc = fAVMService.lookup(-1, (String)avmVersionPath[1]); AVMNodeDescriptor desc = fAVMService.lookup(-1, (String)avmVersionPath[1]);
if (desc == null)
{
throw new InvalidNodeRefException("Not Found.", nodeRef);
}
if (desc.isPlainFile()) if (desc.isPlainFile())
{ {
fAVMService.setContentData((String)avmVersionPath[1], fAVMService.setContentData((String)avmVersionPath[1],
@@ -1338,6 +1317,10 @@ public class AVMNodeService extends AbstractNodeServiceImpl implements NodeServi
{ {
AVMNodeDescriptor child = fAVMService.lookup((Integer)avmVersionPath[0], AVMNodeDescriptor child = fAVMService.lookup((Integer)avmVersionPath[0],
(String)avmVersionPath[1]); (String)avmVersionPath[1]);
if (child == null)
{
return null;
}
return AVMNodeConverter.ToNodeRef((Integer)avmVersionPath[0], return AVMNodeConverter.ToNodeRef((Integer)avmVersionPath[0],
child.getPath()); child.getPath());
} }

View File

@@ -450,6 +450,10 @@ public class AVMServiceImpl implements AVMService
*/ */
public AVMNodeDescriptor lookup(int version, String path) public AVMNodeDescriptor lookup(int version, String path)
{ {
if (path == null)
{
throw new AVMBadArgumentException("Illegal null path.");
}
return lookup(version, path, false); return lookup(version, path, false);
} }
@@ -471,8 +475,15 @@ public class AVMServiceImpl implements AVMService
{ {
throw new AVMBadArgumentException("Path is null."); throw new AVMBadArgumentException("Path is null.");
} }
Lookup lookup = fAVMRepository.lookup(version, path, includeDeleted); try
return lookup.getCurrentNode().getDescriptor(lookup); {
Lookup lookup = fAVMRepository.lookup(version, path, includeDeleted);
return lookup.getCurrentNode().getDescriptor(lookup);
}
catch (AVMNotFoundException e)
{
return null;
}
} }
/** /**
@@ -483,6 +494,10 @@ public class AVMServiceImpl implements AVMService
*/ */
public AVMNodeDescriptor lookup(AVMNodeDescriptor dir, String name) public AVMNodeDescriptor lookup(AVMNodeDescriptor dir, String name)
{ {
if (dir == null || name == null)
{
throw new AVMBadArgumentException("Illegal null argument.");
}
return lookup(dir, name, false); return lookup(dir, name, false);
} }
@@ -503,7 +518,14 @@ public class AVMServiceImpl implements AVMService
{ {
throw new AVMBadArgumentException("Illegal null argument."); throw new AVMBadArgumentException("Illegal null argument.");
} }
return fAVMRepository.lookup(dir, name, includeDeleted); try
{
return fAVMRepository.lookup(dir, name, includeDeleted);
}
catch (AVMNotFoundException e)
{
return null;
}
} }
/** /**
@@ -626,7 +648,14 @@ public class AVMServiceImpl implements AVMService
{ {
throw new AVMBadArgumentException("Null Store Name."); throw new AVMBadArgumentException("Null Store Name.");
} }
return fAVMRepository.getAVMStore(name); try
{
return fAVMRepository.getAVMStore(name);
}
catch (AVMNotFoundException e)
{
return null;
}
} }
/** /**

View File

@@ -582,7 +582,7 @@ public class AVMStoreImpl implements AVMStore, Serializable
if (child.getType() != AVMNodeType.PLAIN_DIRECTORY && if (child.getType() != AVMNodeType.PLAIN_DIRECTORY &&
child.getType() != AVMNodeType.LAYERED_DIRECTORY) child.getType() != AVMNodeType.LAYERED_DIRECTORY)
{ {
throw new AVMWrongTypeException("Not a directory: " + pathElements[i]); throw new AVMNotFoundException("Not a directory: " + pathElements[i]);
} }
result.add(child, pathElements[i], write); result.add(child, pathElements[i], write);
dir = (DirectoryNode)result.getCurrentNode(); dir = (DirectoryNode)result.getCurrentNode();

View File

@@ -20,6 +20,7 @@ package org.alfresco.repo.avm;
import java.util.List; import java.util.List;
import org.alfresco.service.cmr.avm.AVMNodeDescriptor; import org.alfresco.service.cmr.avm.AVMNodeDescriptor;
import org.alfresco.service.cmr.avm.AVMNotFoundException;
import org.alfresco.service.cmr.avm.AVMService; import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.avmsync.AVMDifference; import org.alfresco.service.cmr.avmsync.AVMDifference;
import org.alfresco.service.cmr.avmsync.AVMSyncService; import org.alfresco.service.cmr.avmsync.AVMSyncService;
@@ -114,6 +115,10 @@ public class AVMSyncServiceImpl implements AVMSyncService
public void resetLayer(String layerPath) public void resetLayer(String layerPath)
{ {
AVMNodeDescriptor desc = fAVMService.lookup(-1, layerPath); AVMNodeDescriptor desc = fAVMService.lookup(-1, layerPath);
if (desc == null)
{
throw new AVMNotFoundException("Not Found: " + layerPath);
}
String [] parts = AVMNodeConverter.SplitBase(layerPath); String [] parts = AVMNodeConverter.SplitBase(layerPath);
fAVMService.removeNode(parts[0], parts[1]); fAVMService.removeNode(parts[0], parts[1]);
fAVMService.createLayeredDirectory(desc.getIndirection(), parts[0], parts[1]); fAVMService.createLayeredDirectory(desc.getIndirection(), parts[0], parts[1]);

View File

@@ -134,21 +134,13 @@ public class AVMTestRemote extends TestCase
} }
/** /**
* Test a call that should throw an exception. * Test a call that should return null;
*/ */
public void testErrorState() public void testErrorState()
{ {
try try
{ {
try assertNull(fAVMRemote.lookup(-1, "main:/fizz/fazz"));
{
fAVMRemote.lookup(-1, "main:/fizz/fazz");
fail();
}
catch (AVMException e)
{
e.printStackTrace(System.out);
}
} }
catch (Exception e) catch (Exception e)
{ {

View File

@@ -292,6 +292,10 @@ class AVMTester implements Runnable
String name = fNames[fgRandom.nextInt(26 * 26)]; String name = fNames[fgRandom.nextInt(26 * 26)];
String path = randomPath(); String path = randomPath();
AVMNodeDescriptor desc = fService.lookup(-1, path); AVMNodeDescriptor desc = fService.lookup(-1, path);
if (desc == null)
{
return;
}
if (path.equals("main:/")) if (path.equals("main:/"))
{ {
return; return;

View File

@@ -367,8 +367,6 @@ public interface AVMService
* @param path The simple absolute path to the parent directory. * @param path The simple absolute path to the parent directory.
* @param includeDeleted Whether to see Deleted Nodes. * @param includeDeleted Whether to see Deleted Nodes.
* @return An AVMNodeDescriptor. * @return An AVMNodeDescriptor.
* @throws AVMNotFoundException If <code>path</code> does not exist or
* if <code>version</code> does not exist.
* @throws AVMWrongTypeException If <code>path</code> contains a non-terminal * @throws AVMWrongTypeException If <code>path</code> contains a non-terminal
* element that is not a directory. * element that is not a directory.
*/ */
@@ -379,8 +377,6 @@ public interface AVMService
* @param dir The descriptor for the directory node. * @param dir The descriptor for the directory node.
* @param name The name to lookup. * @param name The name to lookup.
* @return The descriptor for the child. * @return The descriptor for the child.
* @throws AVMNotFoundException If <code>name</code> does not exist or
* if <code>dir</code> is dangling.
* @throws AVMWrongTypeException If <code>dir</code> does not refer to a directory. * @throws AVMWrongTypeException If <code>dir</code> does not refer to a directory.
*/ */
public AVMNodeDescriptor lookup(AVMNodeDescriptor dir, String name); public AVMNodeDescriptor lookup(AVMNodeDescriptor dir, String name);
@@ -392,8 +388,6 @@ public interface AVMService
* @param name The name to lookup. * @param name The name to lookup.
* @param includeDeleted Whether to see Deleted Nodes. * @param includeDeleted Whether to see Deleted Nodes.
* @return The descriptor for the child. * @return The descriptor for the child.
* @throws AVMNotFoundException If <code>name</code> does not exist or
* if <code>dir</code> is dangling.
* @throws AVMWrongTypeException If <code>dir</code> does not refer to a directory. * @throws AVMWrongTypeException If <code>dir</code> does not refer to a directory.
*/ */
public AVMNodeDescriptor lookup(AVMNodeDescriptor dir, String name, boolean includeDeleted); public AVMNodeDescriptor lookup(AVMNodeDescriptor dir, String name, boolean includeDeleted);