001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.hibernate;
022
023 import java.io.Serializable;
024 import java.lang.reflect.Field;
025 import java.lang.reflect.Method;
026 import java.lang.reflect.Type;
027 import java.util.concurrent.ConcurrentHashMap;
028
029 import javax.persistence.EmbeddedId;
030 import javax.persistence.Id;
031
032 import org.granite.config.GraniteConfig;
033 import org.granite.context.GraniteContext;
034 import org.granite.messaging.service.ServiceException;
035 import org.granite.util.TypeUtil;
036 import org.granite.util.Introspector;
037 import org.granite.util.PropertyDescriptor;
038 import org.hibernate.engine.SessionImplementor;
039 import org.hibernate.proxy.HibernateProxy;
040 import org.hibernate.type.AbstractComponentType;
041
042 /**
043 * @author Franck WOLFF
044 */
045 @SuppressWarnings("deprecation")
046 public class ProxyFactory {
047
048 private static final Class<?>[] INTERFACES = new Class[]{HibernateProxy.class};
049
050 protected final ConcurrentHashMap<Class<?>, Object[]> identifierInfos = new ConcurrentHashMap<Class<?>, Object[]>();
051
052 private final Method getProxyFactory;
053 private final Method getProxy;
054
055 public ProxyFactory(String initializerClassName) {
056 try {
057 // Get proxy methods: even if CGLIB/Javassist LazyInitializer implementations share a common
058 // superclass, getProxyFactory/getProxy methods are declared as static in each inherited
059 // class with the same signature.
060 Class<?> initializerClass = TypeUtil.forName(initializerClassName);
061 getProxyFactory = initializerClass.getMethod("getProxyFactory", new Class[]{Class.class, Class[].class});
062 Class<?> componentTypeClass = AbstractComponentType.class;
063 try {
064 // Hibernate 3.6
065 componentTypeClass = TypeUtil.forName("org.hibernate.type.CompositeType");
066 }
067 catch (ClassNotFoundException e) {
068 // Hibernate until 3.5
069 }
070 getProxy = initializerClass.getMethod("getProxy", new Class[]{
071 Class.class, String.class, Class.class, Class[].class, Method.class, Method.class,
072 componentTypeClass, Serializable.class, SessionImplementor.class
073 });
074 }
075 catch (Exception e) {
076 throw new ServiceException("Could not introspect initializer class: " + initializerClassName, e);
077 }
078 }
079
080 public HibernateProxy getProxyInstance(String persistentClassName, String entityName, Serializable id) {
081 try {
082 // Get ProxyFactory.
083 Class<?> persistentClass = TypeUtil.forName(persistentClassName);
084 Class<?> factory = (Class<?>)getProxyFactory.invoke(null, new Object[]{persistentClass, INTERFACES});
085
086 // Convert id (if necessary).
087 Object[] identifierInfo = getIdentifierInfo(persistentClass);
088 Type identifierType = (Type)identifierInfo[0];
089 Method identifierGetter = (Method)identifierInfo[1];
090 if (id == null || !identifierType.equals(id.getClass())) {
091 GraniteConfig config = GraniteContext.getCurrentInstance().getGraniteConfig();
092 id = (Serializable)config.getConverters().convert(id, identifierType);
093 }
094
095 // Get Proxy
096 return (HibernateProxy)getProxy.invoke(null, new Object[]{factory, entityName, persistentClass, INTERFACES, identifierGetter, null, null, id, null});
097 }
098 catch (Exception e) {
099 throw new ServiceException("Error with proxy description: " + persistentClassName + '/' + entityName + " and id: " + id, e);
100 }
101 }
102
103 protected Object[] getIdentifierInfo(Class<?> persistentClass) {
104
105 Object[] info = identifierInfos.get(persistentClass);
106 if (info != null)
107 return info;
108
109 Type type = null;
110 Method getter = null;
111 for (Class<?> clazz = persistentClass; clazz != Object.class && clazz != null; clazz = clazz.getSuperclass()) {
112 for (Field field : clazz.getDeclaredFields()) {
113 if (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) {
114 type = field.getGenericType();
115 break;
116 }
117 }
118 }
119
120 if (type == null) {
121 PropertyDescriptor[] propertyDescriptors = Introspector.getPropertyDescriptors(persistentClass);
122
123 for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
124 Method method = propertyDescriptor.getReadMethod();
125 if (method != null && (
126 method.isAnnotationPresent(Id.class) ||
127 method.isAnnotationPresent(EmbeddedId.class))) {
128 type = method.getGenericReturnType();
129 getter = method;
130 break;
131 }
132 method = propertyDescriptor.getWriteMethod();
133 if (method != null && (
134 method.isAnnotationPresent(Id.class) ||
135 method.isAnnotationPresent(EmbeddedId.class))) {
136 type = method.getGenericParameterTypes()[0];
137 break;
138 }
139 }
140 }
141
142 if (type != null) {
143 info = new Object[] { type, getter };
144 Object[] previousInfo = identifierInfos.putIfAbsent(persistentClass, info);
145 if (previousInfo != null)
146 info = previousInfo; // should be the same...
147 return info;
148 }
149
150 throw new IllegalArgumentException("Could not find id in: " + persistentClass);
151 }
152 }