From 8303194d79591c68d69e5dbfaf864930d1b88330 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 6 Dec 2016 10:12:45 +0100 Subject: [PATCH] DATALDAP-1 - Create namespace handler and XML schema. Spring Data LDAP now uses its own Namespace: http://www.springframework.org/schema/data/ldap. Using XML configuration for Spring LDAP and Spring Data LDAP requires both namespaces to be declared. --- .../asciidoc/reference/ldap-repositories.adoc | 39 ++++++++++++++-- .../ldap/config/LdapNamespaceHandler.java | 38 ++++++++++++++++ src/main/resources/META-INF/spring.handlers | 1 + src/main/resources/META-INF/spring.schemas | 2 + .../data/ldap/config/spring-ldap-1.0.xsd | 38 ++++++++++++++++ .../config/LdapNamespaceHandlerTests.java | 44 +++++++++++++++++++ src/test/resources/infrastructure.xml | 11 +++++ .../LdapNamespaceHandlerTests-context.xml | 12 +++++ 8 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/springframework/data/ldap/config/LdapNamespaceHandler.java create mode 100644 src/main/resources/META-INF/spring.handlers create mode 100644 src/main/resources/META-INF/spring.schemas create mode 100644 src/main/resources/org/springframework/data/ldap/config/spring-ldap-1.0.xsd create mode 100644 src/test/java/org/springframework/data/ldap/config/LdapNamespaceHandlerTests.java create mode 100644 src/test/resources/infrastructure.xml create mode 100644 src/test/resources/org/springframework/data/ldap/config/LdapNamespaceHandlerTests-context.xml diff --git a/src/main/asciidoc/reference/ldap-repositories.adoc b/src/main/asciidoc/reference/ldap-repositories.adoc index ecb7b076..449691e7 100644 --- a/src/main/asciidoc/reference/ldap-repositories.adoc +++ b/src/main/asciidoc/reference/ldap-repositories.adoc @@ -4,9 +4,9 @@ [[ldap.repo-intro]] == Introduction -This chapter will point out the specialties for repository support for MongoDB. This builds on the core repository support explained in <>. So make sure you've got a sound understanding of the basic concepts explained there. +This chapter will point out the specialties for repository support for LDAP. This builds on the core repository support explained in <>. So make sure you've got a sound understanding of the basic concepts explained there. -* Spring LDAP repositories can be enabled using an `` tag in your XML configuration or using an `@EnableLdapRepositories` annotation on a configuration class. +* Spring LDAP repositories can be enabled using an `` tag in your XML configuration or using an `@EnableLdapRepositories` annotation on a configuration class. * To include support for `LdapQuery` parameters in automatically generated repositories, have your interface extend `LdapRepository` rather than `CrudRepository`. * All Spring LDAP repositories must work with entities annotated with the ODM annotations, as described in http://docs.spring.io/spring-ldap/docs/{springLdapVersion}.RELEASE/reference/#odm[Object-Directory Mapping]. * Since all ODM managed classes must have a Distinguished Name as ID, all Spring LDAP repositories must have the ID type parameter set to `javax.naming.Name`. @@ -67,6 +67,37 @@ public interface PersonRepository extends CrudRepository { Right now this interface simply serves typing purposes but we will add additional methods to it later. In your Spring configuration simply add +.General LDAP repository Spring configuration +==== +[source,xml] +---- + + + + + + + + + + +---- +==== + +This namespace element will cause the base packages to be scanned for interfaces extending `LdapRepository` and create Spring beans for each of them found. By default the repositories will get a `LdapTemplate` Spring bean wired that is called `ldapTemplate`, so you only need to configure `ldap-template-ref` explicitly if you deviate from this convention. + +If you'd rather like to go with JavaConfig use the `@EnableLdapRepositories` annotation. The annotation carries the very same attributes like the namespace element. If no base package is configured the infrastructure will scan the package of the annotated configuration class. .JavaConfig for repositories ==== @@ -120,7 +151,7 @@ The sample creates an application context with Spring's unit test support which [[ldap.repositories.queries]] == Query methods -Most of the data access operations you usually trigger on a repository result a query being executed against the MongoDB databases. Defining such a query is just a matter of declaring a method on the repository interface +Most of the data access operations you usually trigger on a repository result a query being executed against the LDAP directory. Defining such a query is just a matter of declaring a method on the repository interface .PersonRepository with query methods ==== @@ -134,7 +165,7 @@ public interface PersonRepository extends PagingAndSortingRepository The method shows a query for all people with the given lastname. The query will be derived parsing the method name for constraints which can be concatenated with `And` and `Or`. Thus the method name will result in a query expression of `(&(objectclass=person)(lastname=lastname))`. -<1> The method shows a query for all people with the given lastname and firstname. The query will be derived parsing the method name.Thus the method name will result in a query expression of `(&(objectclass=person)(lastname=lastname)(firstname=firstname))`. +<2> The method shows a query for all people with the given lastname and firstname. The query will be derived parsing the method name.Thus the method name will result in a query expression of `(&(objectclass=person)(lastname=lastname)(firstname=firstname))`. ==== [cols="1,2,3", options="header"] diff --git a/src/main/java/org/springframework/data/ldap/config/LdapNamespaceHandler.java b/src/main/java/org/springframework/data/ldap/config/LdapNamespaceHandler.java new file mode 100644 index 00000000..8060a7b6 --- /dev/null +++ b/src/main/java/org/springframework/data/ldap/config/LdapNamespaceHandler.java @@ -0,0 +1,38 @@ +/* + * Copyright 2016 the original author or authors. + * + * 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.springframework.data.ldap.config; + +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; +import org.springframework.data.ldap.repository.config.LdapRepositoryConfigurationExtension; +import org.springframework.data.repository.config.RepositoryBeanDefinitionParser; + +/** + * {@link org.springframework.beans.factory.xml.NamespaceHandler} for LDAP configuration. + * + * @author Mark Paluch + */ +public class LdapNamespaceHandler extends NamespaceHandlerSupport { + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.xml.NamespaceHandler#init() + */ + public void init() { + + registerBeanDefinitionParser("repositories", + new RepositoryBeanDefinitionParser(new LdapRepositoryConfigurationExtension())); + } +} diff --git a/src/main/resources/META-INF/spring.handlers b/src/main/resources/META-INF/spring.handlers new file mode 100644 index 00000000..3cacdad4 --- /dev/null +++ b/src/main/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.springframework.org/schema/data/ldap=org.springframework.data.ldap.config.LdapNamespaceHandler diff --git a/src/main/resources/META-INF/spring.schemas b/src/main/resources/META-INF/spring.schemas new file mode 100644 index 00000000..1aa76fbf --- /dev/null +++ b/src/main/resources/META-INF/spring.schemas @@ -0,0 +1,2 @@ +http\://www.springframework.org/schema/data/ldap/spring-ldap-1.0.xsd=org/springframework/data/ldap/config/spring-ldap-1.0.xsd +http\://www.springframework.org/schema/data/ldap/spring-ldap.xsd=org/springframework/data/ldap/config/spring-ldap-1.0.xsd diff --git a/src/main/resources/org/springframework/data/ldap/config/spring-ldap-1.0.xsd b/src/main/resources/org/springframework/data/ldap/config/spring-ldap-1.0.xsd new file mode 100644 index 00000000..aa83d6f6 --- /dev/null +++ b/src/main/resources/org/springframework/data/ldap/config/spring-ldap-1.0.xsd @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/springframework/data/ldap/config/LdapNamespaceHandlerTests.java b/src/test/java/org/springframework/data/ldap/config/LdapNamespaceHandlerTests.java new file mode 100644 index 00000000..f89a8ff9 --- /dev/null +++ b/src/test/java/org/springframework/data/ldap/config/LdapNamespaceHandlerTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2016 the original author or authors. + * + * 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.springframework.data.ldap.config; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Unit test for {@link LdapNamespaceHandler}. + * + * @author Mark Paluch + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class LdapNamespaceHandlerTests { + + @Autowired private ApplicationContext context; + + @Test + public void shouldCreateRepository() { + assertThat(context.getBean(DummyLdapRepository.class), is(notNullValue())); + } +} diff --git a/src/test/resources/infrastructure.xml b/src/test/resources/infrastructure.xml new file mode 100644 index 00000000..1cddefb6 --- /dev/null +++ b/src/test/resources/infrastructure.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/src/test/resources/org/springframework/data/ldap/config/LdapNamespaceHandlerTests-context.xml b/src/test/resources/org/springframework/data/ldap/config/LdapNamespaceHandlerTests-context.xml new file mode 100644 index 00000000..79b19b7b --- /dev/null +++ b/src/test/resources/org/springframework/data/ldap/config/LdapNamespaceHandlerTests-context.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file