Skip to content

Commit

Permalink
HSEARCH-3386 Move the responsibility of passing the BeanReference dat…
Browse files Browse the repository at this point in the history
…a (name, class, ...) to the BeanReference itself
  • Loading branch information
yrodiere committed Dec 4, 2018
1 parent 9a02c6c commit 6030990
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 118 deletions.
Expand Up @@ -30,14 +30,14 @@ public interface BeanProvider {
* Retrieve a bean referenced by its type.
* @param <T> The expected return type.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param typeReference The type used as a reference to the bean to retrieve.
* @param typeReference The type used as a reference to the bean to retrieve. Must be non-null.
* @return The resolved bean.
* @throws SearchException if the reference is invalid (null) or the bean cannot be resolved.
*/
<T> T getBean(Class<T> expectedClass, Class<?> typeReference);

/**
* Retrieve a bean referenced by its type.
* Retrieve a bean referenced by its name.
* @param <T> The expected return type.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param nameReference The name used as a reference to the bean to retrieve. Must be non-null and non-empty.
Expand All @@ -47,13 +47,14 @@ public interface BeanProvider {
<T> T getBean(Class<T> expectedClass, String nameReference);

/**
* Retrieve a bean referenced by its type, name, or both, depending on the content of the {@link BeanReference}.
* Retrieve a bean referenced by its type and name.
* @param <T> The expected return type.
* @param expectedClass The expected class. Must be non-null. The returned bean will implement this class.
* @param reference The reference to the bean to retrieve. Must be non-null.
* @param typeReference The type used as a reference to the bean to retrieve. Must be non-null.
* @param nameReference The name used as a reference to the bean to retrieve. Must be non-null and non-empty.
* @return The resolved bean.
* @throws SearchException if the reference is invalid (null or empty) or the bean cannot be resolved.
*/
<T> T getBean(Class<T> expectedClass, BeanReference reference);
<T> T getBean(Class<T> expectedClass, Class<?> typeReference, String nameReference);

}
Expand Up @@ -12,49 +12,47 @@
public interface BeanReference {

/**
* @return The type of the referenced bean.
* {@code null} implies no type reference.
*/
Class<?> getType();

/**
* @return The name of the referenced bean.
* {@code null} implies no name reference.
* Get the bean this reference points to using the given provider.
*
* @param beanProvider A provider to get the bean from.
* @param expectedType The expected type of the bean.
* @param <T> The expected type of the bean.
* @return The bean instance.
*/
String getName();
<T> T getBean(BeanProvider beanProvider, Class<T> expectedType);

/**
* Create a {@link BeanReference} referencing a bean by its name.
* <p>
* Note: when no dependency injection framework is used, Hibernate Search uses reflection to resolve beans,
* and in that case "names" are interpreted as fully qualified class names.
*
* @param name The bean name.
* @param name The bean name. Must not be null nor empty.
* @return The corresponding {@link BeanReference}.
*/
static BeanReference ofName(String name) {
return new ImmutableBeanReference( null, name );
return new NameBeanReference( name );
}

/**
* Create a {@link BeanReference} referencing a bean by its type.
*
* @param type The bean type.
* @param type The bean type. Must not be null.
* @return The corresponding {@link BeanReference}.
*/
static BeanReference ofType(Class<?> type) {
return new ImmutableBeanReference( type, null );
return new TypeBeanReference( type );
}

/**
* Create a {@link BeanReference} referencing a bean by its name and type.
* Create a {@link BeanReference} referencing a bean by its name or type, or both.
*
* @param type The bean type.
* @param name The bean name.
* @param type The bean type. May be null, but only if {@code name} is not null.
* @param name The bean name. May be null, but only if {@code type} is not null.
* @return The corresponding {@link BeanReference}.
*/
static BeanReference of(Class<?> type, String name) {
return new ImmutableBeanReference( type, name );
return TypeAndNameBeanReference.createLenient( type, name );
}

}

This file was deleted.

@@ -0,0 +1,37 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.engine.environment.bean;

import java.lang.invoke.MethodHandles;

import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.util.impl.common.LoggerFactory;
import org.hibernate.search.util.impl.common.StringHelper;

final class NameBeanReference implements BeanReference {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

private final String name;

NameBeanReference(String name) {
if ( StringHelper.isEmpty( name ) ) {
throw log.invalidBeanReferenceNameNullOrEmpty();
}
this.name = name;
}

@Override
public String toString() {
return getClass().getSimpleName() + "[name=" + name + "]";
}

@Override
public <T> T getBean(BeanProvider beanProvider, Class<T> expectedType) {
return beanProvider.getBean( expectedType, name );
}
}
@@ -0,0 +1,55 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.engine.environment.bean;

import java.lang.invoke.MethodHandles;

import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.util.impl.common.LoggerFactory;
import org.hibernate.search.util.impl.common.StringHelper;

final class TypeAndNameBeanReference implements BeanReference {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

static BeanReference createLenient(Class<?> type, String name) {
boolean nameProvided = StringHelper.isNotEmpty( name );
boolean typeProvided = type != null;

if ( nameProvided && typeProvided ) {
return new TypeAndNameBeanReference( type, name );
}
else if ( nameProvided ) {
return new NameBeanReference( name );
}
else if ( typeProvided ) {
return new TypeBeanReference( type );
}
else {
throw log.invalidBeanReferenceTypeIsNullAndNameNullOrEmpty();
}
}

private final Class<?> type;
private final String name;

private TypeAndNameBeanReference(Class<?> type, String name) {
this.type = type;
this.name = name;
}

@Override
public String toString() {
return getClass().getSimpleName() + "[type=" + type + ", name=" + name + "]";
}

@Override
public <T> T getBean(BeanProvider beanProvider, Class<T> expectedType) {
return beanProvider.getBean( expectedType, type, name );
}

}
@@ -0,0 +1,37 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.engine.environment.bean;

import java.lang.invoke.MethodHandles;

import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.util.impl.common.LoggerFactory;

final class TypeBeanReference implements BeanReference {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

private final Class<?> type;

TypeBeanReference(Class<?> type) {
if ( type == null ) {
throw log.invalidBeanReferenceNameNullOrEmpty();
}
this.type = type;
}

@Override
public String toString() {
return getClass().getSimpleName() + "[type=" + type + "]";
}

@Override
public <T> T getBean(BeanProvider beanProvider, Class<T> expectedType) {
return beanProvider.getBean( expectedType, type );
}

}
Expand Up @@ -10,7 +10,6 @@
import java.util.Map;

import org.hibernate.search.engine.environment.bean.BeanProvider;
import org.hibernate.search.engine.environment.bean.BeanReference;
import org.hibernate.search.engine.environment.bean.spi.BeanConfigurer;
import org.hibernate.search.engine.environment.bean.spi.BeanCreationContext;
import org.hibernate.search.engine.environment.bean.spi.BeanFactory;
Expand Down Expand Up @@ -46,43 +45,28 @@ public BeanProviderImpl(ClassResolver classResolver, BeanResolver beanResolver)
@Override
public <T> T getBean(Class<T> expectedClass, Class<?> typeReference) {
if ( typeReference == null ) {
throw log.emptyBeanReferenceTypeNull();
throw log.invalidBeanReferenceTypeNull();
}
return beanResolver.resolve( expectedClass, typeReference );
}

@Override
public <T> T getBean(Class<T> expectedClass, String nameReference) {
if ( StringHelper.isEmpty( nameReference ) ) {
throw log.emptyBeanReferenceNameNullOrEmpty();
throw log.invalidBeanReferenceNameNullOrEmpty();
}
return getBeanFromBeanResolverOrConfiguredBeans( expectedClass, nameReference );
}

@Override
public <T> T getBean(Class<T> expectedClass, BeanReference reference) {
if ( reference == null ) {
throw log.emptyBeanReferenceNull();
}

String nameReference = reference.getName();
Class<?> typeReference = reference.getType();
boolean nameProvided = StringHelper.isNotEmpty( nameReference );
boolean typeProvided = typeReference != null;

if ( nameProvided && typeProvided ) {
return beanResolver.resolve( expectedClass, nameReference, typeReference );
}
else if ( nameProvided ) {
// This is the only situation where querying configured beans make sense
return getBeanFromBeanResolverOrConfiguredBeans( expectedClass, nameReference );
}
else if ( typeProvided ) {
return beanResolver.resolve( expectedClass, typeReference );
public <T> T getBean(Class<T> expectedClass, Class<?> typeReference, String nameReference) {
if ( typeReference == null ) {
throw log.invalidBeanReferenceTypeNull();
}
else {
throw log.emptyBeanReferenceNoNameNoType();
if ( StringHelper.isEmpty( nameReference ) ) {
throw log.invalidBeanReferenceNameNullOrEmpty();
}
return beanResolver.resolve( expectedClass, nameReference, typeReference );
}

private <T> T getBeanFromBeanResolverOrConfiguredBeans(Class<T> expectedClass, String nameReference) {
Expand Down
Expand Up @@ -207,17 +207,14 @@ SearchException relativeFieldNameCannotContainDot(String relativeFieldName,
@Message(id = ID_OFFSET_2 + 34, value = "No index manager registered for index manager name: '%1$s'.")
SearchException noIndexManagerRegistered(String indexManagerName);

@Message(id = ID_OFFSET_2 + 35, value = "Got an empty bean reference (type is null).")
SearchException emptyBeanReferenceTypeNull();
@Message(id = ID_OFFSET_2 + 35, value = "Invalid bean reference: type is null.")
SearchException invalidBeanReferenceTypeNull();

@Message(id = ID_OFFSET_2 + 36, value = "Got an empty bean reference (name is null or empty).")
SearchException emptyBeanReferenceNameNullOrEmpty();
@Message(id = ID_OFFSET_2 + 36, value = "Invalid bean reference: name is null or empty.")
SearchException invalidBeanReferenceNameNullOrEmpty();

@Message(id = ID_OFFSET_2 + 37, value = "Got an empty bean reference (reference is null.)")
SearchException emptyBeanReferenceNull();

@Message(id = ID_OFFSET_2 + 38, value = "Got an empty bean reference (no name, no type).")
SearchException emptyBeanReferenceNoNameNoType();
@Message(id = ID_OFFSET_2 + 37, value = "Invalid bean reference: type is null and name is null or empty.")
SearchException invalidBeanReferenceTypeIsNullAndNameNullOrEmpty();

@Message(id = ID_OFFSET_2 + 39, value = "This bean resolver does not support bean references using both a name and a type. Got both '%1$s' and '%2$s' in the same reference.")
SearchException resolveBeanUsingBothNameAndType(String nameReference, @FormatWith(ClassFormatter.class) Class<?> typeReference);
Expand Down
Expand Up @@ -30,6 +30,6 @@ public String toString() {
@Override
public T build(BridgeBuildContext buildContext) {
BeanProvider beanProvider = buildContext.getBeanProvider();
return beanProvider.getBean( expectedType, beanReference );
return beanReference.getBean( beanProvider, expectedType );
}
}

0 comments on commit 6030990

Please sign in to comment.