001    package biz.hammurapi.jms.adapter.jca;
002    
003    import java.util.HashMap;
004    import java.util.Iterator;
005    import java.util.Map;
006    
007    import biz.hammurapi.jms.adapter.ProxyService;
008    
009    public class ConnectionImpl implements Connection {
010                    
011            private boolean stateful;
012            
013            public ConnectionImpl(ManagedConnectionImpl managedConnection, boolean stateful, Object state) {
014                    this.managedConnection = managedConnection;
015                    init(stateful, state);
016            }
017    
018            public synchronized void init(boolean stateful, Object state) {
019                    this.stateful = stateful;
020                    cache.clear();          
021                    if (stateful && state instanceof Map) {
022                            synchronized (cache) {
023                                    Iterator it = ((Map) state).entrySet().iterator();
024                                    while (it.hasNext()) {
025                                            Map.Entry entry = (Map.Entry) it.next();
026                                            String name = String.valueOf(entry.getKey());
027                                            Object toCache = managedConnection.get(name);
028                                            ProxyService.setProxyState(toCache, entry.getValue());
029                                            cache.put(name, toCache);
030                                    }
031                            }
032                    }
033            }       
034            
035            public void close() {
036                    if (managedConnection!=null) {
037                            managedConnection.close();
038                    }
039            }
040                    
041            private Map cache = new HashMap();
042    
043            public void cleanup() {
044                    cache.clear();
045            }
046    
047            private ManagedConnectionImpl managedConnection;
048    
049            public Object get(String name) {
050                    if (managedConnection == null) {
051                            throw new IllegalStateException("Connection is closed");
052                    }
053                    
054                    if (stateful) {
055                            synchronized (cache) {
056                                    Object ret = cache.get(name);
057                                    if (ret==null) {
058                                            ret = managedConnection.get(name);
059                                            cache.put(name, ret);
060                                    }
061                                    return ret;
062                            }
063                    }
064                    
065                    return managedConnection.get(name); 
066            }
067    
068            public Object getState() {
069                    Map ret = new HashMap();
070                    synchronized (cache) {
071                            Iterator it= cache.entrySet().iterator();
072                            while (it.hasNext()) {
073                                    Map.Entry entry = (Map.Entry) it.next();
074                                    Object state = ProxyService.getProxyState(entry.getValue());
075                                    if (state!=null) {
076                                            ret.put(entry.getKey(), state);
077                                    }
078                            }
079                    }
080                    return ret;
081            }
082            
083    }