/projects/springframework-3.0.5/projects/org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/FilterDefinitionFactoryBean.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 163 lines · 72 code · 24 blank · 67 comment · 6 complexity · 642ad6477ddd8057c1c2e6113d05b612 MD5 · raw file

  1. /*
  2. * Copyright 2002-2010 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.orm.hibernate3;
  17. import java.lang.reflect.Method;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.hibernate.engine.FilterDefinition;
  21. import org.hibernate.type.Type;
  22. import org.hibernate.type.TypeFactory;
  23. import org.springframework.beans.factory.BeanNameAware;
  24. import org.springframework.beans.factory.FactoryBean;
  25. import org.springframework.beans.factory.InitializingBean;
  26. import org.springframework.util.ReflectionUtils;
  27. /**
  28. * Convenient FactoryBean for defining Hibernate FilterDefinitions.
  29. * Exposes a corresponding Hibernate FilterDefinition object.
  30. *
  31. * <p>Typically defined as an inner bean within a LocalSessionFactoryBean
  32. * definition, as the list element for the "filterDefinitions" bean property.
  33. * For example:
  34. *
  35. * <pre>
  36. * &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
  37. * ...
  38. * &lt;property name="filterDefinitions"&gt;
  39. * &lt;list&gt;
  40. * &lt;bean class="org.springframework.orm.hibernate3.FilterDefinitionFactoryBean"&gt;
  41. * &lt;property name="filterName" value="myFilter"/&gt;
  42. * &lt;property name="parameterTypes"&gt;
  43. * &lt;map&gt;
  44. * &lt;entry key="myParam" value="string"/&gt;
  45. * &lt;entry key="myOtherParam" value="long"/&gt;
  46. * &lt;/map&gt;
  47. * &lt;/property&gt;
  48. * &lt;/bean&gt;
  49. * &lt;/list&gt;
  50. * &lt;/property&gt;
  51. * ...
  52. * &lt;/bean&gt;</pre>
  53. *
  54. * Alternatively, specify a bean id (or name) attribute for the inner bean,
  55. * instead of the "filterName" property.
  56. *
  57. * @author Juergen Hoeller
  58. * @since 1.2
  59. * @see org.hibernate.engine.FilterDefinition
  60. * @see LocalSessionFactoryBean#setFilterDefinitions
  61. */
  62. public class FilterDefinitionFactoryBean implements FactoryBean<FilterDefinition>, BeanNameAware, InitializingBean {
  63. private static Method heuristicTypeMethod;
  64. private static Object typeResolver;
  65. static {
  66. // Hibernate 3.6 TypeResolver class available?
  67. try {
  68. Class trClass = FilterDefinitionFactoryBean.class.getClassLoader().loadClass(
  69. "org.hibernate.type.TypeResolver");
  70. heuristicTypeMethod = trClass.getMethod("heuristicType", String.class);
  71. typeResolver = trClass.newInstance();
  72. }
  73. catch (Exception ex) {
  74. try {
  75. heuristicTypeMethod = TypeFactory.class.getMethod("heuristicType", String.class);
  76. typeResolver = null;
  77. }
  78. catch (Exception ex2) {
  79. throw new IllegalStateException("Cannot find Hibernate's heuristicType method", ex2);
  80. }
  81. }
  82. }
  83. private String filterName;
  84. private Map<String, Type> parameterTypeMap = new HashMap<String, Type>();
  85. private String defaultFilterCondition;
  86. private FilterDefinition filterDefinition;
  87. /**
  88. * Set the name of the filter.
  89. */
  90. public void setFilterName(String filterName) {
  91. this.filterName = filterName;
  92. }
  93. /**
  94. * Set the parameter types for the filter,
  95. * with parameter names as keys and type names as values.
  96. * @see org.hibernate.type.TypeFactory#heuristicType(String)
  97. */
  98. public void setParameterTypes(Map<String, String> parameterTypes) {
  99. if (parameterTypes != null) {
  100. this.parameterTypeMap = new HashMap<String, Type>(parameterTypes.size());
  101. for (Map.Entry<String, String> entry : parameterTypes.entrySet()) {
  102. this.parameterTypeMap.put(entry.getKey(),
  103. (Type) ReflectionUtils.invokeMethod(heuristicTypeMethod, typeResolver, entry.getValue()));
  104. }
  105. }
  106. else {
  107. this.parameterTypeMap = new HashMap<String, Type>();
  108. }
  109. }
  110. /**
  111. * Specify a default filter condition for the filter, if any.
  112. */
  113. public void setDefaultFilterCondition(String defaultFilterCondition) {
  114. this.defaultFilterCondition = defaultFilterCondition;
  115. }
  116. /**
  117. * If no explicit filter name has been specified, the bean name of
  118. * the FilterDefinitionFactoryBean will be used.
  119. * @see #setFilterName
  120. */
  121. public void setBeanName(String name) {
  122. if (this.filterName == null) {
  123. this.filterName = name;
  124. }
  125. }
  126. public void afterPropertiesSet() {
  127. this.filterDefinition =
  128. new FilterDefinition(this.filterName, this.defaultFilterCondition, this.parameterTypeMap);
  129. }
  130. public FilterDefinition getObject() {
  131. return this.filterDefinition;
  132. }
  133. public Class<FilterDefinition> getObjectType() {
  134. return FilterDefinition.class;
  135. }
  136. public boolean isSingleton() {
  137. return true;
  138. }
  139. }