Ok hmm strange... This toppic involves the same problem as the toppic of my
collegue quipere (ItemNotFoundException while switching between different
workspaces). The reason why I am not getting an ItemNotFoundException but
only the wrong nodes, I think is because the code I am using is not using a
NodeType which is orderable. The orginal code does use such a nodetype. When
creating the test class I couldn't use our custom nodetypes. That's the main
difference.
Will supply the class inline:
package nl.src;
import java.util.Hashtable;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.query.Query;
import javax.jcr.query.QueryResult;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.WorkspaceImpl;
import org.apache.jackrabbit.core.jndi.RegistryHelper;
/**
* @author jukuijpe
*
*/
public class MultipleWorkspaceTest {
private static Repository repos = null;
/**
* @param args
*/
public static void main(String[] args) {
runTest(true);
}
public static void runTest(boolean repositoryShutdown) {
Session session = null;
Session session1 = null;
Session session2 = null;
try {
Repository repos = getRepository();
session = repos.login(new SimpleCredentials("user",
"password".toCharArray()));
((WorkspaceImpl)
session.getWorkspace()).createWorkspace("testWorkspace1");
((WorkspaceImpl)
session.getWorkspace()).createWorkspace("testWorkspace2");
session.logout();
session1 = repos.login(new SimpleCredentials("user",
"password".toCharArray()), "testWorkspace1");
session2 = repos.login(new SimpleCredentials("user",
"password".toCharArray()), "testWorkspace2");
Node startNodeW1 = null;
Node startNodeW2 = null;
if (session1.getRootNode().hasNode("testNodes")) {
startNodeW1 =
session1.getRootNode().getNode("testNodes");
} else {
startNodeW1 =
session1.getRootNode().addNode("testNodes");
}
if (session2.getRootNode().hasNode("testNodes")) {
startNodeW2 =
session2.getRootNode().getNode("testNodes");
} else {
startNodeW2 =
session2.getRootNode().addNode("testNodes");
}
startNodeW1.addNode("workspace1Node1");
startNodeW2.addNode("workspace2Node1");
startNodeW1.addNode("workspace1Node2");
startNodeW2.addNode("workspace2Node2");
startNodeW1.addNode("workspace1Node3");
startNodeW2.addNode("workspace2Node3");
session1.save();
session2.save();
RepositoryImpl jackRabbitRepos = ((RepositoryImpl)
session2.getRepository());
session1.logout();
session2.logout();
if (repositoryShutdown) {
jackRabbitRepos.shutdown();
MultipleWorkspaceTest.repos = null;
}
listNodes();
} catch(Exception e) {
e.printStackTrace();
}finally {
if (session != null) {
session.logout();
}
if (session1 != null) {
session1.logout();
}
if (session2 != null) {
session2.logout();
}
}
}
/**
* Creates a Repository instance to be used by the example class.
*
* @return repository instance
* @throws Exception on errors
*/
private static Repository getRepository() throws Exception {
if (repos == null) {
String configFile = "c:/repotest/repository.xml";
String repHomeDir = "c:/repotest";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
env.put(Context.PROVIDER_URL, "localhost");
InitialContext ctx = new InitialContext(env);
RegistryHelper.registerRepository(ctx, "repo", configFile,
repHomeDir, true);
repos = (Repository) ctx.lookup("repo");
}
return repos;
}
private static void listNodes() {
Session session = null;
try{
Repository reposNew = getRepository();
session = reposNew.login(new SimpleCredentials("user",
"password".toCharArray()), "testWorkspace1");
Query repositoryQuery =
session.getWorkspace().getQueryManager().createQuery("testNodes/*",
Query.XPATH);
QueryResult result = repositoryQuery.execute();
NodeIterator it = result.getNodes();
while (it.hasNext()) {
Node node = it.nextNode();
System.out.println(node.getName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.logout();
}
}
}
}
Jukka Zitting-3 wrote:
>
> Hi,
>
> On 8/28/06, J Kuijpers <[EMAIL PROTECTED]> wrote:
>> Supplied repository.xml and runnable MultipleWorkspaceTest.java
>> http://www.nabble.com/user-files/235783/repository.xml repository.xml
>> http://www.nabble.com/user-files/235784/MultipleWorkspaceTest.java
>> MultipleWorkspaceTest.java
>
> The MultipleWorkspaceTest.java file appears to be empty. Could you
> resend it, inline if necessary?
>
> BR,
>
> Jukka Zitting
>
> --
> Yukatan - http://yukatan.fi/ - [EMAIL PROTECTED]
> Software craftsmanship, JCR consulting, and Java development
>
>
--
View this message in context:
http://www.nabble.com/problem-retrieving-nodes-from-different-workspaces-tf2177041.html#a6019752
Sent from the Jackrabbit - Dev forum at Nabble.com.