KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > dd > WeakMetadataUnit


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
20 package org.netbeans.modules.j2ee.persistence.dd;
21
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.Collections JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
27 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
28 import org.netbeans.spi.java.classpath.PathResourceImplementation;
29 import org.openide.filesystems.FileObject;
30
31 /**
32  * A metadata unit implementation which holds the classpath weakly. Needed when the
33  * metadata unit will be used as a value in a HashMap referenced from a static field.
34  * Holding the classpath strongly would have caused projects to leak,
35  * since a classpath can come from a project and can hold a reference
36  * to this project. When the claspath is GCd an empty classpath
37  * will be returned by {@link #getClassPath}.
38  *
39  * <p><strong>The classpath must be hold strongly by someone else!</strong>
40  * Usually it will be held by the project it came from.</p>
41  *
42  * @author Andrei Badea
43  */

44 public final class WeakMetadataUnit implements MetadataUnit {
45
46     private FileObject deploymentDescriptor;
47
48     // WeakReference<ClassPath> or empty ClassPath
49
private Object JavaDoc classPath;
50
51     public WeakMetadataUnit(FileObject deploymentDescriptor, ClassPath classPath) {
52         synchronized (this) {
53             this.deploymentDescriptor = deploymentDescriptor;
54             if (classPath != null) {
55                 this.classPath = new WeakReference JavaDoc<ClassPath>(classPath);
56             } else {
57                 this.classPath = ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
58             }
59         }
60     }
61
62     public synchronized FileObject getDeploymentDescriptor() {
63         return deploymentDescriptor;
64     }
65
66     public synchronized ClassPath getClassPath() {
67         ClassPath result;
68
69         if (classPath instanceof Reference JavaDoc) {
70             Reference JavaDoc ref = (Reference JavaDoc)classPath;
71             result = (ClassPath)ref.get();
72             if (result == null) {
73                 result = ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
74                 classPath = result;
75             }
76         } else {
77             result = (ClassPath)classPath;
78         }
79
80         return result;
81     }
82
83     public synchronized void setDeploymentDescriptor(FileObject deploymentDescriptor) {
84         // XXX should send some property change event here
85
this.deploymentDescriptor = deploymentDescriptor;
86     }
87 }
88
Popular Tags