KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > AssociationLinkWrapper


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.mdr.handlers;
20
21 import java.lang.ref.WeakReference JavaDoc;
22 import java.lang.ref.ReferenceQueue JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.netbeans.mdr.storagemodel.AssociationLink;
26
27 import javax.jmi.reflect.RefAssociationLink;
28 import javax.jmi.reflect.RefObject;
29 import org.netbeans.mdr.storagemodel.StorableObject;
30
31 /**
32  *
33  * @author Martin Matula
34  */

35 class AssociationLinkWrapper implements RefAssociationLink {
36     private static final IdentityCache identityCache = new IdentityCache();
37
38     private final AssociationLink innerLink;
39
40     /** Creates new AssociationLinkWrapper */
41     private AssociationLinkWrapper(AssociationLink innerLink) {
42         this.innerLink = innerLink;
43     }
44     
45     AssociationLink getInnerLink() {
46         return innerLink;
47     }
48
49     public RefObject refFirstEnd() {
50         StorableObject fe = innerLink.getFirstEnd();
51         return (RefObject) fe.getMdrStorage().getRepository().getHandler(fe);
52     }
53     
54     public RefObject refSecondEnd() {
55         StorableObject se = innerLink.getSecondEnd();
56         return (RefObject) se.getMdrStorage().getRepository().getHandler(se);
57     }
58     
59     public boolean equals(Object JavaDoc other) {
60         if (other instanceof AssociationLinkWrapper) {
61             return this == other;
62         } else return (other instanceof RefAssociationLink) &&
63             ((RefAssociationLink) other).refFirstEnd().equals(refFirstEnd()) &&
64             ((RefAssociationLink) other).refSecondEnd().equals(refSecondEnd());
65     }
66     
67     public int hashCode() {
68         return (innerLink.getFirstEnd().getMofId().toString() + innerLink.getSecondEnd().getMofId().toString()).hashCode();
69     }
70
71     static RefAssociationLink wrapLink(AssociationLink innerLink) {
72         if (innerLink == null) {
73             return null;
74         }
75         
76         String JavaDoc linkID = innerLink.getFirstEnd().getMofId().toString() + ":" + innerLink.getSecondEnd().getMofId().toString();
77         
78         synchronized (identityCache) {
79             RefAssociationLink result = (RefAssociationLink) identityCache.get(linkID);
80             if (result == null) {
81                 result = new AssociationLinkWrapper(innerLink);
82                 identityCache.put(linkID, result);
83             }
84             return result;
85         }
86     }
87
88     private static class IdentityCache extends HashMap JavaDoc {
89         private final ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
90
91         private class LinkReference extends WeakReference JavaDoc {
92             private String JavaDoc mofId;
93
94             public LinkReference(String JavaDoc key, RefAssociationLink link) {
95                 super(link, queue);
96
97                 mofId = key;
98             }
99
100             public String JavaDoc getProxyMofId() {
101                 return mofId;
102             }
103         }
104
105         private void cleanUp() {
106             LinkReference reference;
107
108             while ((reference = (LinkReference) queue.poll()) != null) {
109 // Logger.getDefault().log("Removing: " + reference.getProxyMofId());
110
this.remove(reference.getProxyMofId());
111             }
112         }
113         
114         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
115             cleanUp();
116             Object JavaDoc result = super.put(key, new LinkReference((String JavaDoc) key, (RefAssociationLink) value));
117             if (result != null) {
118                 return ((LinkReference) result).get();
119             } else {
120                 return result;
121             }
122         }
123
124         public Object JavaDoc get(Object JavaDoc key) {
125             cleanUp();
126             Object JavaDoc result = super.get(key);
127             if (result != null) {
128                 return ((LinkReference) result).get();
129             } else {
130                 return result;
131             }
132         }
133     }
134 }
135
Popular Tags