KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > Snapshot


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore;
20
21 import java.lang.reflect.InvocationHandler JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23 import java.util.*;
24 import javax.jmi.reflect.RefObject;
25
26 /**
27  *
28  * @author Martin Matula
29  */

30 public class Snapshot implements InvocationHandler JavaDoc {
31     private final HashMap results = new HashMap();
32     private RefObject wrappedObject;
33     private HashSet snapshots;
34
35     private Snapshot(RefObject wrappedObject) {
36         this.wrappedObject = wrappedObject;
37     }
38
39     public Object JavaDoc invoke(Object JavaDoc proxy, java.lang.reflect.Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
40         Object JavaDoc result;
41         if (wrappedObject == null) {
42             result = results.get(method);
43             if (result == null && !results.containsKey(method)) {
44                 throw new IllegalStateException JavaDoc("Value for " + method + " not cached.");
45             }
46         } else {
47             result = method.invoke(wrappedObject, args);
48             result = wrap(result);
49             results.put(method, result);
50         }
51         return result;
52     }
53     
54     private void addSnapshot(Object JavaDoc snapshot) {
55         if (snapshots == null) {
56             snapshots = new HashSet();
57         }
58         snapshots.add(snapshot);
59     }
60     
61     private Object JavaDoc wrap(Object JavaDoc obj) {
62         if (obj instanceof Collection) {
63             return wrapCollection((Collection) obj);
64         } else if (obj instanceof RefObject) {
65             Object JavaDoc result = createSnapshot((RefObject) obj);
66             addSnapshot(result);
67             return result;
68         } else {
69             return obj;
70         }
71     }
72     
73     private Object JavaDoc wrapCollection(Collection col) {
74         Collection result = new ArrayList();
75         for (Iterator it = col.iterator(); it.hasNext();) {
76             result.add(wrap(it.next()));
77         }
78         return result;
79     }
80     
81     private void freeze() {
82         if (snapshots != null) {
83             for (Iterator it = snapshots.iterator(); it.hasNext();) {
84                 ((Snapshot) it.next()).freeze();
85             }
86             snapshots = null;
87         }
88         wrappedObject = null;
89     }
90     
91     public static RefObject createSnapshot(RefObject wrappedObject) {
92         Snapshot ss = new Snapshot(wrappedObject);
93         Class JavaDoc clz = wrappedObject.getClass();
94         return (RefObject) Proxy.newProxyInstance(clz.getClassLoader(), clz.getInterfaces(), ss);
95     }
96     
97     public static void freeze(RefObject snapshot) {
98         ((Snapshot) Proxy.getInvocationHandler(snapshot)).freeze();
99     }
100 }
101
Popular Tags