You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2004/10/20 18:12:29 UTC
svn commit: rev 55160 - in incubator/jackrabbit/trunk/src: java/org/apache/jackrabbit/core java/org/apache/jackrabbit/core/jndi/provider test/org/apache/jackrabbit/test
Author: stefan
Date: Wed Oct 20 09:12:27 2004
New Revision: 55160
Added:
incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/jndi/provider/
incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/jndi/provider/DummyContext.java (contents, props changed)
incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/jndi/provider/DummyInitialContextFactory.java (contents, props changed)
Modified:
incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Test.java
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/JackrabbitRepositoryStub.java
Log:
adding simple in-memory jndi context for testing purposes
Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Test.java
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Test.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Test.java Wed Oct 20 09:12:27 2004
@@ -51,23 +51,14 @@
// fallback to cwd
repHomeDir = System.getProperty("user.dir");
}
-
- RepositoryConfig repConf = RepositoryConfig.create(configDir + "/" + "repository.xml", repHomeDir);
- Repository r = RepositoryImpl.create(repConf);
-/*
- // Set up the environment for creating the initial context
+ // set up the environment for creating the initial context
Hashtable env = new Hashtable();
- //env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
- //env.put(Context.PROVIDER_URL, "file:./jndi");
-
- env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ervacon.xnam.XMLInitialContextFactory");
- env.put(Context.PROVIDER_URL, "d:/temp/jndi.xml");
-
- //env.put(Context.INITIAL_CONTEXT_FACTORY, "org.codehaus.spice.jndikit.memory.StaticMemoryInitialContextFactory");
+ env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
InitialContext ctx = new InitialContext(env);
+
RegistryHelper.registerRepository(ctx, "repo", configFile, repHomeDir, true);
Repository r = (Repository) ctx.lookup("repo");
-*/
+
Session session = r.login(new SimpleCredentials("anonymous", "".toCharArray()), null);
Workspace wsp = session.getWorkspace();
Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/jndi/provider/DummyContext.java
==============================================================================
--- (empty file)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/jndi/provider/DummyContext.java Wed Oct 20 09:12:27 2004
@@ -0,0 +1,398 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.apache.jackrabbit.core.jndi.provider;
+
+import javax.naming.*;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Properties;
+
+/**
+ * <code>DummyContext</code> is a simple service provider that
+ * implements a flat namespace in memory. It is intended to be used for
+ * testing purposes only.
+ */
+class DummyContext extends Hashtable implements Context, Cloneable {
+
+ private transient Hashtable environment;
+
+ private static final NameParser nameParser = new FlatNameParser();
+
+ /**
+ * Constructs a new <code>DummyContext</code> instance
+ */
+ DummyContext() {
+ this(null);
+ }
+
+ /**
+ * Constructs a new <code>DummyContext</code> instance
+ *
+ * @param environment
+ */
+ DummyContext(Hashtable environment) {
+ if (environment == null) {
+ this.environment = new Hashtable();
+ } else {
+ this.environment = (Hashtable) environment.clone();
+ }
+ }
+
+ protected String getComponentName(Name name) throws NamingException {
+ if (name instanceof CompositeName) {
+ if (name.size() > 1) {
+ throw new InvalidNameException(name.toString() + " has more components than namespace can handle");
+ }
+ return name.get(0);
+ } else {
+ // compound name
+ return name.toString();
+ }
+ }
+
+ protected Object getBoundObject(String name) throws NamingException {
+ Object obj = get(name);
+ if (obj == null) {
+ throw new NameNotFoundException();
+ } else {
+ return obj;
+ }
+ }
+
+ public Object clone() {
+ Object obj = super.clone();
+ ((DummyContext) obj).environment = (Hashtable) environment.clone();
+ return obj;
+ }
+
+ //--------------------------------------------------------------< Context >
+ /**
+ * @see Context#bind(Name, Object)
+ */
+ public void bind(Name name, Object obj) throws NamingException {
+ if (name.isEmpty()) {
+ throw new InvalidNameException("empty name");
+ }
+ String n = getComponentName(name);
+ if (containsKey(n)) {
+ throw new NameAlreadyBoundException();
+ }
+ put(n, obj);
+ }
+
+ /**
+ * @see Context#bind(String, Object)
+ */
+ public void bind(String name, Object obj) throws NamingException {
+ bind(new CompositeName(name), obj);
+ }
+
+ /**
+ * @see Context#close()
+ */
+ public void close() throws NamingException {
+ }
+
+ /**
+ * @see Context#composeName(Name, Name)
+ */
+ public Name composeName(Name name, Name prefix) throws NamingException {
+ Name newName = (Name) prefix.clone();
+ return newName.addAll(name);
+ }
+
+ /**
+ * @see Context#composeName(String, String)
+ */
+ public String composeName(String name, String prefix) throws NamingException {
+ return composeName(new CompositeName(name), new CompositeName(prefix)).toString();
+ }
+
+ /**
+ * @see Context#createSubcontext(Name)
+ */
+ public Context createSubcontext(Name name) throws NamingException {
+ throw new OperationNotSupportedException("subcontexts are not supported");
+ }
+
+ /**
+ * @see Context#createSubcontext(String)
+ */
+ public Context createSubcontext(String name) throws NamingException {
+ return createSubcontext(new CompositeName(name));
+ }
+
+ /**
+ * @see Context#destroySubcontext(Name)
+ */
+ public void destroySubcontext(Name name) throws NamingException {
+ throw new OperationNotSupportedException("subcontexts are not supported");
+ }
+
+ /**
+ * @see Context#destroySubcontext(String)
+ */
+ public void destroySubcontext(String name) throws NamingException {
+ destroySubcontext(new CompositeName(name));
+ }
+
+ /**
+ * @see Context#getEnvironment()
+ */
+ public Hashtable getEnvironment() throws NamingException {
+ return (Hashtable) environment.clone();
+ }
+
+ /**
+ * @see Context#getNameInNamespace()
+ */
+ public String getNameInNamespace() throws NamingException {
+ throw new OperationNotSupportedException();
+ }
+
+ /**
+ * @see Context#getNameParser(Name)
+ */
+ public NameParser getNameParser(Name name) throws NamingException {
+ return nameParser;
+ }
+
+ /**
+ * @see Context#getNameParser(String)
+ */
+ public NameParser getNameParser(String name) throws NamingException {
+ return nameParser;
+ }
+
+ /**
+ * @see Context#list(Name)
+ */
+ public NamingEnumeration list(Name name) throws NamingException {
+ if (name.isEmpty()) {
+ return new NamingEnum(this);
+ }
+ String n = getComponentName(name);
+ Object obj = getBoundObject(n);
+ if (obj instanceof Context) {
+ return ((Context) obj).list("");
+ } else {
+ throw new NotContextException(name + " is not bound to a context");
+ }
+ }
+
+ /**
+ * @see Context#list(String)
+ */
+ public NamingEnumeration list(String name) throws NamingException {
+ return list(new CompositeName(name));
+ }
+
+ /**
+ * @see Context#listBindings(Name)
+ */
+ public NamingEnumeration listBindings(Name name) throws NamingException {
+ if (name.isEmpty()) {
+ return new BindingEnum(this);
+ }
+ String n = getComponentName(name);
+ Object obj = getBoundObject(n);
+ if (obj instanceof Context) {
+ return ((Context) obj).listBindings("");
+ } else {
+ throw new NotContextException(name + " is not bound to a context");
+ }
+ }
+
+ /**
+ * @see Context#listBindings(String)
+ */
+ public NamingEnumeration listBindings(String name) throws NamingException {
+ return listBindings(new CompositeName(name));
+ }
+
+ /**
+ * @see Context#lookup(Name)
+ */
+ public Object lookup(Name name) throws NamingException {
+ if (name.isEmpty()) {
+ return clone();
+ }
+ String n = getComponentName(name);
+ return getBoundObject(n);
+ }
+
+ /**
+ * @see Context#lookup(String)
+ */
+ public Object lookup(String name) throws NamingException {
+ return lookup(new CompositeName(name));
+ }
+
+ /**
+ * @see Context#lookupLink(Name)
+ */
+ public Object lookupLink(Name name) throws NamingException {
+ // no special handling of links, delegate to lookup(Name)
+ return lookup(name);
+ }
+
+ /**
+ * @see Context#lookupLink(String)
+ */
+ public Object lookupLink(String name) throws NamingException {
+ return lookupLink(new CompositeName(name));
+ }
+
+ /**
+ * @see Context#rebind(Name, Object)
+ */
+ public void rebind(Name name, Object obj) throws NamingException {
+ if (name.isEmpty()) {
+ throw new InvalidNameException("empty name");
+ }
+ String n = getComponentName(name);
+ put(n, obj);
+ }
+
+ /**
+ * @see Context#rebind(String, Object)
+ */
+ public void rebind(String name, Object obj) throws NamingException {
+ rebind(new CompositeName(name), obj);
+ }
+
+ /**
+ * @see Context#removeFromEnvironment(String)
+ */
+ public Object removeFromEnvironment(String propName) throws NamingException {
+ return environment.remove(propName);
+ }
+
+ /**
+ * @see Context#rename(Name, Name)
+ */
+ public void rename(Name oldName, Name newName) throws NamingException {
+ if (oldName.isEmpty() || newName.isEmpty()) {
+ throw new InvalidNameException("empty name");
+ } else {
+ Object obj = lookup(oldName);
+ bind(newName, obj);
+ unbind(oldName);
+ }
+ }
+
+ /**
+ * @see Context#rename(String, String)
+ */
+ public void rename(String oldName, String newName) throws NamingException {
+ rename(new CompositeName(oldName), new CompositeName(newName));
+ }
+
+ /**
+ * @see Context#unbind(Name)
+ */
+ public void unbind(Name name) throws NamingException {
+ if (name.isEmpty()) {
+ throw new InvalidNameException("empty name");
+ }
+ String n = getComponentName(name);
+ remove(n);
+ }
+
+ /**
+ * @see Context#unbind(String)
+ */
+ public void unbind(String name) throws NamingException {
+ unbind(new CompositeName(name));
+ }
+
+ /**
+ * @see Context#addToEnvironment(String, Object)
+ */
+ public Object addToEnvironment(String propName, Object propVal) throws NamingException {
+ return environment.put(propName, propVal);
+ }
+
+ //--------------------------------------------------------< inner classes >
+ /**
+ * <code>FlatNameParser</code> ...
+ */
+ static class FlatNameParser implements NameParser {
+
+ private static final Properties syntax = new Properties();
+
+ static {
+ syntax.put("jndi.syntax.direction", "flat");
+ syntax.put("jndi.syntax.ignorecase", "false");
+ }
+
+ /**
+ * @see NameParser#parse(String)
+ */
+ public Name parse(String name) throws NamingException {
+ return new CompoundName(name, syntax);
+ }
+ }
+
+ /**
+ * <code>NamingEnum</code> ...
+ */
+ class NamingEnum implements NamingEnumeration {
+ protected Enumeration namesEnum;
+ protected Hashtable bindings;
+
+ NamingEnum(Hashtable bindings) {
+ namesEnum = bindings.keys();
+ this.bindings = bindings;
+ }
+
+ public boolean hasMoreElements() {
+ return namesEnum.hasMoreElements();
+ }
+
+ public boolean hasMore() throws NamingException {
+ return hasMoreElements();
+ }
+
+ public Object next() throws NamingException {
+ return nextElement();
+ }
+
+ public Object nextElement() {
+ String name = (String) namesEnum.nextElement();
+ String className = bindings.get(name).getClass().getName();
+ return new NameClassPair(name, className);
+ }
+
+ public void close() throws NamingException {
+ }
+ }
+
+ /**
+ * <code>BindingEnum</code> ...
+ */
+ class BindingEnum extends NamingEnum {
+
+ BindingEnum(Hashtable bindings) {
+ super(bindings);
+ }
+
+ public Object nextElement() {
+ String name = (String) namesEnum.nextElement();
+ return new Binding(name, bindings.get(name));
+ }
+ }
+}
Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/jndi/provider/DummyInitialContextFactory.java
==============================================================================
--- (empty file)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/jndi/provider/DummyInitialContextFactory.java Wed Oct 20 09:12:27 2004
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.apache.jackrabbit.core.jndi.provider;
+
+import javax.naming.spi.InitialContextFactory;
+import javax.naming.Context;
+import javax.naming.NamingException;
+import java.util.Hashtable;
+
+/**
+ * <code>DummyInitialContextFactory</code> ...
+ */
+public class DummyInitialContextFactory implements InitialContextFactory {
+
+ /**
+ * @see InitialContextFactory#getInitialContext(java.util.Hashtable)
+ */
+ public Context getInitialContext(Hashtable environment) throws NamingException {
+ return new DummyContext(environment);
+ }
+}
Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/JackrabbitRepositoryStub.java
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/JackrabbitRepositoryStub.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/JackrabbitRepositoryStub.java Wed Oct 20 09:12:27 2004
@@ -23,7 +23,9 @@
import javax.jcr.RepositoryException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
+import javax.naming.Context;
import java.util.Properties;
+import java.util.Hashtable;
/**
* Implements the <code>RepositoryStub</code> for the JCR Reference Implementation.
@@ -74,13 +76,13 @@
String repConfig = environment.getProperty(PROP_REPOSITORY_CONFIG);
String repHome = environment.getProperty(PROP_REPOSITORY_HOME);
- RepositoryConfig repConf = RepositoryConfig.create(repConfig, repHome);
- repository = RepositoryImpl.create(repConf);
-/*
- InitialContext ctx = new InitialContext();
+ // register repository instance
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
+ InitialContext ctx = new InitialContext(env);
RegistryHelper.registerRepository(ctx, repName, repConfig, repHome, true);
+
repository = (Repository) ctx.lookup(repName);
-*/
} catch (Exception e) {
throw new RepositoryStubException(e.toString());
}