Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,31 @@ public class NamespaceHelper {
*/
private final Session session;

/**
* Current namespace registry.
*/
private NamespaceRegistry namespaceRegistry;

/**
* Creates a namespace helper for the given session.
*
* @param session current session
*/
public NamespaceHelper(Session session) {
this.session = session;
// will be set on lazily
this.namespaceRegistry = null;
}

/**
Get the namespace registry; needs to be done on-demand because the constructor
does not allow RepositoryException
*/
private NamespaceRegistry getNamespaceRegistry() throws RepositoryException {
if (namespaceRegistry == null) {
namespaceRegistry = session.getWorkspace().getNamespaceRegistry();
}
return namespaceRegistry;
}

/**
Expand Down Expand Up @@ -197,8 +215,7 @@ public String getJcrName(String name)
*/
public String registerNamespace(String prefix, String uri)
throws RepositoryException {
NamespaceRegistry registry =
session.getWorkspace().getNamespaceRegistry();
NamespaceRegistry registry = getNamespaceRegistry();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any pointers on how much overhead this call is? I was assuming this is not leading to complex and expensive object creations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently it does in Oak - see https://issues.apache.org/jira/browse/OAK-11784 - it's of course a good question why it is expensive in Oak. @mbaedke - any idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is is an expensive operation by itself. It's rather a problem if there are a lot of registered prefixes (see the excerpt of the stacktrace in https://issues.apache.org/jira/browse/OAK-11784), which leads to many invocations of this method.

try {
// Check if the namespace is registered
registry.getPrefix(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,8 @@ public void testRegisterNamespace() throws RepositoryException {
assertEquals("test1", nsReg.getPrefix("test1:"));
assertEquals("test2", nsReg.getPrefix("test2"));
assertEquals("ns6", nsReg.getPrefix("test3:"));

// check invocation count for getNamespaceRegistry (JCR-5161)
Mockito.verify(workspace, Mockito.times(1)).getNamespaceRegistry();
}
}