001package headfirst.proxy.javaproxy;
002
003import java.lang.reflect.Proxy;
004import java.util.Hashtable;
005
006public class MatchMakingTestDrive {
007        Hashtable<String,PersonBean> datingDB = new Hashtable<String,PersonBean>();
008
009        public static void main(String[] args) {
010                MatchMakingTestDrive test = new MatchMakingTestDrive();
011                test.drive();
012        }
013
014        public MatchMakingTestDrive() {
015                initializeDatabase();
016        }
017
018        public void drive() {
019                PersonBean joe = getPersonFromDatabase("Joe Javabean");
020                PersonBean ownerProxy = getOwnerProxy(joe);
021                System.out.println("Name is " + ownerProxy.getName());
022                ownerProxy.setInterests("bowling, Go");
023                System.out.println("Interests set from owner proxy");
024                try {
025                        ownerProxy.setHotOrNotRating(10);
026                } catch (Exception e) {
027                        System.out.println("Can't set rating from owner proxy");
028                }
029                System.out.println("Rating is " + ownerProxy.getHotOrNotRating());
030
031                PersonBean nonOwnerProxy = getNonOwnerProxy(joe);
032                System.out.println("Name is " + nonOwnerProxy.getName());
033                try {
034                        nonOwnerProxy.setInterests("bowling, Go");
035                } catch (Exception e) {
036                        System.out.println("Can't set interests from non owner proxy");
037                }
038                nonOwnerProxy.setHotOrNotRating(3);
039                System.out.println("Rating set from non owner proxy");
040                System.out.println("Rating is " + nonOwnerProxy.getHotOrNotRating());
041        }
042
043        PersonBean getOwnerProxy(PersonBean person) {
044
045                return (PersonBean) Proxy.newProxyInstance(
046                                person.getClass().getClassLoader(),
047                                person.getClass().getInterfaces(),
048                                new OwnerInvocationHandler(person));
049        }
050
051        PersonBean getNonOwnerProxy(PersonBean person) {
052
053                return (PersonBean) Proxy.newProxyInstance(
054                                person.getClass().getClassLoader(),
055                                person.getClass().getInterfaces(),
056                                new NonOwnerInvocationHandler(person));
057        }
058
059        PersonBean getPersonFromDatabase(String name) {
060                return datingDB.get(name);
061        }
062
063        void initializeDatabase() {
064                PersonBean joe = new PersonBeanImpl();
065                joe.setName("Joe Javabean");
066                joe.setInterests("cars, computers, music");
067                joe.setHotOrNotRating(7);
068                datingDB.put(joe.getName(), joe);
069
070                PersonBean kelly = new PersonBeanImpl();
071                kelly.setName("Kelly Klosure");
072                kelly.setInterests("ebay, movies, music");
073                kelly.setHotOrNotRating(6);
074                datingDB.put(kelly.getName(), kelly);
075        }
076}