001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.jgroups;
025    
026    import java.io.Serializable;
027    import java.net.InetAddress;
028    import java.net.UnknownHostException;
029    import java.util.Collection;
030    import java.util.Map;
031    import java.util.Set;
032    import java.util.Vector;
033    
034    import org.jgroups.Address;
035    import org.jgroups.ChannelException;
036    import org.jgroups.ChannelFactory;
037    import org.jgroups.JChannelFactory;
038    import org.jgroups.View;
039    import org.jgroups.blocks.ReplicatedHashMap;
040    import org.jgroups.blocks.ReplicatedHashMap.Notification;
041    
042    public class JGroupsMap<K extends Serializable, V extends Serializable> implements Map<K, V>,
043                    Notification<K, V> {
044    
045            private ChannelFactory channelFactory = null;
046    
047            private String props = null;
048    
049            private final boolean persist = false;
050    
051            private String name = null;
052    
053            private final int stateTimeout = 10000;
054    
055            private ReplicatedHashMap<K, V> map = null;
056    
057            public JGroupsMap() throws UnknownHostException, ChannelException {
058                    this(null);
059            }
060    
061            public JGroupsMap(String name) throws UnknownHostException, ChannelException {
062                    this.name = name;
063    
064                    if (this.channelFactory == null) {
065                            this.channelFactory = new JChannelFactory();
066                    }
067                    if (this.props == null) {
068                            PropsUDP propsUDP = new PropsUDP();
069                            propsUDP.setBindAddress(InetAddress.getLocalHost().getHostAddress());
070                            this.props = propsUDP.toString();
071                    }
072                    if (this.name == null) {
073                            this.name = "jgroupsmap" + System.nanoTime();
074                    }
075    
076                    this.map = new ReplicatedHashMap<K, V>(name, channelFactory, props, persist, stateTimeout);
077                    this.map.addNotifier(this);
078            }
079    
080            @Override
081            public void clear() {
082                    map.clear();
083            }
084    
085            @Override
086            public boolean containsKey(Object key) {
087                    return map.containsKey(key);
088            }
089    
090            @Override
091            public boolean containsValue(Object value) {
092                    return map.containsValue(value);
093            }
094    
095            @Override
096            public Set<java.util.Map.Entry<K, V>> entrySet() {
097                    return map.entrySet();
098            }
099    
100            @Override
101            public V get(Object key) {
102                    return map.get(key);
103            }
104    
105            @Override
106            public boolean isEmpty() {
107                    return map.isEmpty();
108            }
109    
110            @Override
111            public Set<K> keySet() {
112                    return map.keySet();
113            }
114    
115            @Override
116            public V put(K key, V value) {
117                    return map.put(key, value);
118            }
119    
120            @Override
121            public void putAll(Map<? extends K, ? extends V> m) {
122                    map.putAll(m);
123            }
124    
125            @Override
126            public V remove(Object key) {
127                    return map.remove(key);
128            }
129    
130            @Override
131            public int size() {
132                    return map.size();
133            }
134    
135            @Override
136            public Collection<V> values() {
137                    return map.values();
138            }
139    
140            @Override
141            public void contentsCleared() {
142    
143            }
144    
145            @Override
146            public void contentsSet(Map<K, V> new_entries) {
147    
148            }
149    
150            @Override
151            public void entryRemoved(K key) {
152    
153            }
154    
155            @Override
156            public void entrySet(K key, V value) {
157    
158            }
159    
160            @Override
161            public void viewChange(View view, Vector<Address> new_mbrs, Vector<Address> old_mbrs) {
162    
163            }
164    
165    }