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.IOException;
024    import java.io.ObjectInput;
025    import java.io.Serializable;
026    import java.util.Collections;
027    import java.util.List;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    import org.granite.messaging.amf.io.util.instantiator.AbstractInstantiator;
031    import org.hibernate.proxy.HibernateProxy;
032    
033    /**
034     * @author Franck WOLFF
035     */
036    public class HibernateProxyInstantiator extends AbstractInstantiator<HibernateProxy> {
037    
038        private static final long serialVersionUID = 1L;
039    
040        private final ConcurrentHashMap<String, ProxyFactory> proxyFactories;
041        private final String detachedState;
042        private Serializable id;
043    
044        public HibernateProxyInstantiator(ConcurrentHashMap<String, ProxyFactory> proxyFactories, String detachedState) {
045            if (detachedState == null)
046                throw new IllegalStateException("Cannot create Hibernate proxy without detachedState");
047            this.proxyFactories = proxyFactories;
048            this.detachedState = detachedState;
049        }
050    
051        public void readId(ObjectInput in) throws IOException, ClassNotFoundException {
052            this.id = (Serializable)in.readObject();
053        }
054    
055        protected Serializable getId() {
056            return id;
057        }
058    
059        @Override
060        public List<String> getOrderedFieldNames() {
061            return Collections.emptyList();
062        }
063    
064        @Override
065        public HibernateProxy newInstance() throws IOException, ClassNotFoundException, InstantiationException {
066            final String[] tokens = detachedState.split("\\Q:\\E", -1);
067    
068            ProxyFactory factory = proxyFactories.get(tokens[0]);
069            if (factory == null) {
070                factory = newProxyFactory(tokens[0]);
071                ProxyFactory previousFactory = proxyFactories.putIfAbsent(tokens[0], factory);
072                if (previousFactory != null)
073                    factory = previousFactory;
074            }
075    
076            return factory.getProxyInstance(tokens[1], tokens[2], id);
077        }
078        
079        protected ProxyFactory newProxyFactory(String initializerClassName) {
080            return new ProxyFactory(initializerClassName);
081        }
082    }