001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.hibernate4;
023    
024    import java.io.IOException;
025    import java.io.ObjectInput;
026    import java.io.Serializable;
027    import java.util.Collections;
028    import java.util.List;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    import org.granite.messaging.amf.io.util.instantiator.AbstractInstantiator;
032    import org.hibernate.proxy.HibernateProxy;
033    
034    /**
035     * @author Franck WOLFF
036     */
037    public class HibernateProxyInstantiator extends AbstractInstantiator<HibernateProxy> {
038    
039        private static final long serialVersionUID = 1L;
040    
041        private final ConcurrentHashMap<String, ProxyFactory> proxyFactories;
042        private final String detachedState;
043        private Serializable id;
044    
045        public HibernateProxyInstantiator(ConcurrentHashMap<String, ProxyFactory> proxyFactories, String detachedState) {
046            if (detachedState == null)
047                throw new IllegalStateException("Cannot create Hibernate proxy without detachedState");
048            this.proxyFactories = proxyFactories;
049            this.detachedState = detachedState;
050        }
051    
052        public void readId(ObjectInput in) throws IOException, ClassNotFoundException {
053            this.id = (Serializable)in.readObject();
054        }
055    
056        protected Serializable getId() {
057            return id;
058        }
059    
060        @Override
061        public List<String> getOrderedFieldNames() {
062            return Collections.emptyList();
063        }
064    
065        @Override
066        public HibernateProxy newInstance() throws IOException, ClassNotFoundException, InstantiationException {
067            final String[] tokens = detachedState.split("\\Q:\\E", -1);
068    
069            ProxyFactory factory = proxyFactories.get(tokens[0]);
070            if (factory == null) {
071                factory = newProxyFactory(tokens[0]);
072                ProxyFactory previousFactory = proxyFactories.putIfAbsent(tokens[0], factory);
073                if (previousFactory != null)
074                    factory = previousFactory;
075            }
076    
077            return factory.getProxyInstance(tokens[1], tokens[2], id);
078        }
079        
080        protected ProxyFactory newProxyFactory(String initializerClassName) {
081            return new ProxyFactory(initializerClassName);
082        }
083    }