mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Merged DEV\EXTENSIONS to HEAD
svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4868 svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4869 . svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4904 svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4938 . Module management support Modularization of Records Management git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4956 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
108
source/java/org/alfresco/repo/admin/registry/RegistryKey.java
Normal file
108
source/java/org/alfresco/repo/admin/registry/RegistryKey.java
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (C) 2007 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.admin.registry;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Key for looking up registry metadata.
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class RegistryKey implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1137822242292626854L;
|
||||
|
||||
static final String REGISTRY_1_0_URI = "http://www.alfresco.org/system/registry/1.0";
|
||||
|
||||
private String namespaceUri;
|
||||
private String[] path;
|
||||
private String property;
|
||||
|
||||
/**
|
||||
* Build a registry key from a given array of elements.
|
||||
*/
|
||||
private static String buildPathString(String... elements)
|
||||
{
|
||||
if (elements.length == 0)
|
||||
{
|
||||
return "/";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String element : elements)
|
||||
{
|
||||
if (element == null || element.length() == 0)
|
||||
{
|
||||
throw new IllegalArgumentException("Key elements may not be empty or null");
|
||||
}
|
||||
sb.append("/").append(element);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* For path /a/b/c and property 'x', put in <pre>"a", "b", "c", "x"</pre>
|
||||
* The property can also be <tt>null</tt> as in <pre>"a", "b", "c", null</pre>
|
||||
*
|
||||
* @param namespaceUri the key namespace to use. If left <tt>null</tt> then the
|
||||
* {@link #REGISTRY_1_0_URI default} will be used.
|
||||
* @param key the path elements followed by the property name.
|
||||
*/
|
||||
public RegistryKey(String namespaceUri, String... key)
|
||||
{
|
||||
if (namespaceUri == null)
|
||||
{
|
||||
namespaceUri = REGISTRY_1_0_URI;
|
||||
}
|
||||
this.namespaceUri = namespaceUri;
|
||||
// The last value is the property
|
||||
int length = key.length;
|
||||
if (length == 0)
|
||||
{
|
||||
throw new IllegalArgumentException("No value supplied for the RegistryKey property");
|
||||
}
|
||||
this.property = key[length - 1];
|
||||
this.path = new String[length - 1];
|
||||
System.arraycopy(key, 0, path, 0, length - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(128);
|
||||
sb.append("RegistryKey")
|
||||
.append("[ ").append(RegistryKey.buildPathString(path)).append("/").append(property)
|
||||
.append(" ]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String getNamespaceUri()
|
||||
{
|
||||
return namespaceUri;
|
||||
}
|
||||
|
||||
public String[] getPath()
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
public String getProperty()
|
||||
{
|
||||
return property;
|
||||
}
|
||||
|
||||
}
|
@@ -29,16 +29,16 @@ public interface RegistryService
|
||||
/**
|
||||
* Assign a value to the registry key, which must be of the form <b>/a/b/c</b>.
|
||||
*
|
||||
* @param key the registry key path delimited with '/'.
|
||||
* @param key the registry key.
|
||||
* @param value any value that can be stored in the repository.
|
||||
*/
|
||||
void addValue(String key, Serializable value);
|
||||
void addValue(RegistryKey key, Serializable value);
|
||||
|
||||
/**
|
||||
* @param key the registry key path delimited with '/'.
|
||||
* @param key the registry key.
|
||||
* @return Returns the value stored in the key.
|
||||
*
|
||||
* @see #addValue(String, Serializable)
|
||||
*/
|
||||
Serializable getValue(String key);
|
||||
Serializable getValue(RegistryKey key);
|
||||
}
|
||||
|
@@ -18,7 +18,6 @@ package org.alfresco.repo.admin.registry;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.model.ContentModel;
|
||||
@@ -33,6 +32,7 @@ import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.alfresco.util.PropertyCheck;
|
||||
import org.alfresco.util.PropertyMap;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@@ -154,65 +154,44 @@ public class RegistryServiceImpl implements RegistryService
|
||||
return registryRootNodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns a pair representing the node path and the property name
|
||||
*/
|
||||
private Pair<String, String> splitKey(String key)
|
||||
{
|
||||
int index = key.lastIndexOf('/');
|
||||
Pair<String, String> result = null;
|
||||
if (index < 0) // It is just a property
|
||||
{
|
||||
result = new Pair<String, String>("/", key);
|
||||
}
|
||||
else
|
||||
{
|
||||
String propertyName = key.substring(index + 1, key.length());
|
||||
if (propertyName.length() == 0)
|
||||
{
|
||||
throw new IllegalArgumentException("The registry key is invalid: " + key);
|
||||
}
|
||||
result = new Pair<String, String>(key.substring(0, index), propertyName);
|
||||
}
|
||||
// done
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the node and property name represented by the key or <tt>null</tt>
|
||||
* if it doesn't exist and was not allowed to be created
|
||||
*/
|
||||
private Pair<NodeRef, QName> getPath(String key, boolean create)
|
||||
private Pair<NodeRef, QName> getPath(RegistryKey key, boolean create)
|
||||
{
|
||||
// Get the root
|
||||
NodeRef currentNodeRef = getRegistryRootNodeRef();
|
||||
// Split the key
|
||||
Pair<String, String> keyPair = splitKey(key);
|
||||
// Parse the key
|
||||
StringTokenizer tokenizer = new StringTokenizer(keyPair.getFirst(), "/");
|
||||
// Get the key and property
|
||||
String namespaceUri = key.getNamespaceUri();
|
||||
String[] pathElements = key.getPath();
|
||||
String property = key.getProperty();
|
||||
// Find the node and property to put the value
|
||||
while (tokenizer.hasMoreTokens())
|
||||
for (String pathElement : pathElements)
|
||||
{
|
||||
String token = tokenizer.nextToken();
|
||||
String name = QName.createValidLocalName(token);
|
||||
QName qname = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, name);
|
||||
QName assocQName = QName.createQName(
|
||||
namespaceUri,
|
||||
QName.createValidLocalName(pathElement));
|
||||
|
||||
// Find the node
|
||||
List<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(
|
||||
currentNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
qname);
|
||||
assocQName);
|
||||
int size = childAssocRefs.size();
|
||||
if (size == 0) // Found nothing with that path
|
||||
{
|
||||
if (create) // Must create the path
|
||||
{
|
||||
// Create the node
|
||||
// Create the node (with a name)
|
||||
PropertyMap properties = new PropertyMap();
|
||||
properties.put(ContentModel.PROP_NAME, pathElement);
|
||||
currentNodeRef = nodeService.createNode(
|
||||
currentNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
qname,
|
||||
ContentModel.TYPE_CONTAINER).getChildRef();
|
||||
assocQName,
|
||||
ContentModel.TYPE_CONTAINER,
|
||||
properties).getChildRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -244,15 +223,15 @@ public class RegistryServiceImpl implements RegistryService
|
||||
}
|
||||
// Create the result
|
||||
QName propertyQName = QName.createQName(
|
||||
NamespaceService.SYSTEM_MODEL_1_0_URI,
|
||||
QName.createValidLocalName(keyPair.getSecond()));
|
||||
namespaceUri,
|
||||
QName.createValidLocalName(property));
|
||||
Pair<NodeRef, QName> resultPair = new Pair<NodeRef, QName>(currentNodeRef, propertyQName);
|
||||
// done
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Converted registry key: \n" +
|
||||
" key pair: " + keyPair + "\n" +
|
||||
" result: " + resultPair);
|
||||
" Key: " + key + "\n" +
|
||||
" Result: " + resultPair);
|
||||
}
|
||||
if (resultPair.getFirst() == null)
|
||||
{
|
||||
@@ -267,7 +246,7 @@ public class RegistryServiceImpl implements RegistryService
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public void addValue(String key, Serializable value)
|
||||
public void addValue(RegistryKey key, Serializable value)
|
||||
{
|
||||
// Get the path, with creation support
|
||||
Pair<NodeRef, QName> keyPair = getPath(key, true);
|
||||
@@ -282,7 +261,7 @@ public class RegistryServiceImpl implements RegistryService
|
||||
}
|
||||
}
|
||||
|
||||
public Serializable getValue(String key)
|
||||
public Serializable getValue(RegistryKey key)
|
||||
{
|
||||
// Get the path, without creating
|
||||
Pair<NodeRef, QName> keyPair = getPath(key, false);
|
||||
|
@@ -40,7 +40,7 @@ public class RegistryServiceImplTest extends TestCase
|
||||
authenticationComponent = (AuthenticationComponent) ctx.getBean("AuthenticationComponent");
|
||||
registryService = (RegistryService) ctx.getBean("RegistryService");
|
||||
|
||||
// Run as admin
|
||||
// Run as system user
|
||||
authenticationComponent.setSystemUserAsCurrentUser();
|
||||
}
|
||||
|
||||
@@ -65,13 +65,16 @@ public class RegistryServiceImplTest extends TestCase
|
||||
private static final Long VALUE_ONE = 1L;
|
||||
private static final Long VALUE_TWO = 2L;
|
||||
private static final Long VALUE_THREE = 3L;
|
||||
private static final String KEY_A_B_C_1 = "/a/b/c/1";
|
||||
private static final String KEY_A_B_C_2 = "/a/b/c/2";
|
||||
private static final String KEY_A_B_C_3 = "/a/b/c/3";
|
||||
private static final String KEY_A_B_C_D_1 = "/a/b/c/d/1";
|
||||
private static final String KEY_A_B_C_D_2 = "/a/b/c/d/2";
|
||||
private static final String KEY_A_B_C_D_3 = "/a/b/c/d/3";
|
||||
private static final String KEY_SPECIAL = "/me & you/ whatever";
|
||||
private static final RegistryKey KEY_A_B_C_0 = new RegistryKey(null, "a", "b", "c", "0");
|
||||
private static final RegistryKey KEY_A_B_C_1 = new RegistryKey(null, "a", "b", "c", "1");
|
||||
private static final RegistryKey KEY_A_B_C_2 = new RegistryKey(null, "a", "b", "c", "2");
|
||||
private static final RegistryKey KEY_A_B_C_3 = new RegistryKey(null, "a", "b", "c", "3");
|
||||
private static final RegistryKey KEY_A_B_C_D_0 = new RegistryKey(null, "a", "b", "c", "d", "0");
|
||||
private static final RegistryKey KEY_A_B_C_D_1 = new RegistryKey(null, "a", "b", "c", "d", "1");
|
||||
private static final RegistryKey KEY_A_B_C_D_2 = new RegistryKey(null, "a", "b", "c", "d", "2");
|
||||
private static final RegistryKey KEY_A_B_C_D_3 = new RegistryKey(null, "a", "b", "c", "d", "3");
|
||||
private static final RegistryKey KEY_X_Y_Z_0 = new RegistryKey(null, "x", "y", "z", "0");
|
||||
private static final RegistryKey KEY_SPECIAL = new RegistryKey(null, "me & you", "whatever");
|
||||
/**
|
||||
* General writing and reading back.
|
||||
*/
|
||||
@@ -91,9 +94,9 @@ public class RegistryServiceImplTest extends TestCase
|
||||
assertEquals("Incorrect value from service registry", VALUE_TWO, registryService.getValue(KEY_A_B_C_D_2));
|
||||
assertEquals("Incorrect value from service registry", VALUE_THREE, registryService.getValue(KEY_A_B_C_D_3));
|
||||
|
||||
assertNull("Missing key should return null value", registryService.getValue("/a/b/c/0"));
|
||||
assertNull("Missing key should return null value", registryService.getValue("/a/b/c/d/0"));
|
||||
assertNull("Missing key should return null value", registryService.getValue("/x/y/z/0"));
|
||||
assertNull("Missing key should return null value", registryService.getValue(KEY_A_B_C_0));
|
||||
assertNull("Missing key should return null value", registryService.getValue(KEY_A_B_C_D_0));
|
||||
assertNull("Missing key should return null value", registryService.getValue(KEY_X_Y_Z_0));
|
||||
}
|
||||
|
||||
public void testSpecialCharacters()
|
||||
|
Reference in New Issue
Block a user