001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.hibernate3;
018:
019: import java.util.Properties;
020:
021: import org.springframework.beans.factory.BeanNameAware;
022: import org.springframework.beans.factory.InitializingBean;
023:
024: /**
025: * Bean that encapsulates a Hibernate type definition.
026: *
027: * <p>Typically defined as inner bean within a LocalSessionFactoryBean
028: * definition, as list element for the "typeDefinitions" bean property.
029: * For example:
030: *
031: * <pre>
032: * <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
033: * ...
034: * <property name="typeDefinitions">
035: * <list>
036: * <bean class="org.springframework.orm.hibernate3.TypeDefinitionBean">
037: * <property name="typeName" value="myType"/>
038: * <property name="typeClass" value="mypackage.MyTypeClass"/>
039: * </bean>
040: * </list>
041: * </property>
042: * ...
043: * </bean></pre>
044: *
045: * Alternatively, specify a bean id (or name) attribute for the inner bean,
046: * instead of the "typeName" property.
047: *
048: * @author Juergen Hoeller
049: * @since 1.2
050: * @see LocalSessionFactoryBean#setTypeDefinitions(TypeDefinitionBean[])
051: */
052: public class TypeDefinitionBean implements BeanNameAware,
053: InitializingBean {
054:
055: private String typeName;
056:
057: private String typeClass;
058:
059: private Properties parameters = new Properties();
060:
061: /**
062: * Set the name of the type.
063: * @see org.hibernate.cfg.Mappings#addTypeDef(String, String, java.util.Properties)
064: */
065: public void setTypeName(String typeName) {
066: this .typeName = typeName;
067: }
068:
069: /**
070: * Return the name of the type.
071: */
072: public String getTypeName() {
073: return typeName;
074: }
075:
076: /**
077: * Set the type implementation class.
078: * @see org.hibernate.cfg.Mappings#addTypeDef(String, String, java.util.Properties)
079: */
080: public void setTypeClass(String typeClass) {
081: this .typeClass = typeClass;
082: }
083:
084: /**
085: * Return the type implementation class.
086: */
087: public String getTypeClass() {
088: return typeClass;
089: }
090:
091: /**
092: * Specify default parameters for the type.
093: * This only applies to parameterized types.
094: * @see org.hibernate.cfg.Mappings#addTypeDef(String, String, java.util.Properties)
095: * @see org.hibernate.usertype.ParameterizedType
096: */
097: public void setParameters(Properties parameters) {
098: this .parameters = parameters;
099: }
100:
101: /**
102: * Return the default parameters for the type.
103: */
104: public Properties getParameters() {
105: return parameters;
106: }
107:
108: /**
109: * If no explicit type name has been specified, the bean name of
110: * the TypeDefinitionBean will be used.
111: * @see #setTypeName
112: */
113: public void setBeanName(String name) {
114: if (this .typeName == null) {
115: this .typeName = name;
116: }
117: }
118:
119: public void afterPropertiesSet() {
120: if (this .typeName == null) {
121: throw new IllegalArgumentException("typeName is required");
122: }
123: if (this .typeClass == null) {
124: throw new IllegalArgumentException("typeClass is required");
125: }
126: }
127:
128: }
|