KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > ObjectBinding


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.core.registry;
21
22 import org.netbeans.spi.registry.BasicContext;
23 import org.netbeans.core.registry.ConvertorBinding;
24 import org.netbeans.core.registry.InstanceBinding;
25 import org.openide.filesystems.FileObject;
26 import org.openide.ErrorManager;
27
28 import java.io.IOException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.lang.ref.WeakReference JavaDoc;
34
35 /**
36  * <P>
37  * Uses subclasses of ObjectBinding.Reader and ObjectBinding.Writer: <UL>
38  * <LI>ConvertorBinding</LI>
39  * <LI>InstanceBinding</LI>
40  * <LI>ShadowBinding</LI>
41  * <LI>ObjectRefBinding</LI>
42  * <LI>SettingBinding</LI>
43  * </UL>
44  * </P>
45  *
46  * (copy & pasted & refactored from original org.netbeans.core.registry.ObjectBinding)
47  */

48 abstract class ObjectBinding {
49     private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.core.registry"); // NOI18N
50
private static final Reader[] READERS =
51             new Reader[]{InstanceBinding.READER, ShadowBinding.READER,
52                          ObjectRefBinding.READER, SettingBinding.READER,
53                          ConvertorBinding.READER, XMLBasedBinding.READER};
54
55     private static final Writer[] WRITERS =
56             new Writer[]{ObjectRefBinding.WRITER, ConvertorBinding.WRITER,
57                          SettingBinding.WRITER};
58
59     private FileObject file;
60     private WeakReference JavaDoc cachedInstance = null;
61
62
63     ObjectBinding(FileObject fo) {
64         this.file = fo;
65     }
66
67     /**
68      * @return may return null
69      */

70     BasicContext getContext () {
71         return null;
72     }
73     
74     public static Collection JavaDoc getFileExtensions() {
75         Set JavaDoc s = new HashSet JavaDoc(READERS.length);
76         for (int i = 0; i < READERS.length; i++) {
77             Reader reader = READERS[i];
78             s.add(reader.getFileExtension());
79         }
80         return Collections.unmodifiableCollection(s);
81     }
82
83     public static ObjectBinding get(BasicContext ctx, FileObject fo) {
84         if (fo == null || !fo.isValid() || fo.isFolder()) return null;
85         
86         Reader r = getReader(fo);
87         
88         if (r == null) {
89             ErrorManager errorManager = getErrorManager();
90             if (errorManager.isLoggable(ErrorManager.INFORMATIONAL)) {
91                 errorManager.log("File " + fo + " was parsed, but it is not a .settings file nor namespace-aware XML. It will be ignored.");
92             }
93         }
94         
95         return (r != null) ? r.getObjectBinding(ctx, fo) : null;
96     }
97     
98     public void delete () throws IOException JavaDoc {
99         FileObject file = getFile();
100         if (file != null)
101             file.delete();
102     }
103
104     public ObjectBinding write(final Object JavaDoc object) throws IOException JavaDoc {
105         Writer w = getWriter(object);
106         if (w != null) {
107             w.write(getFile(), object);
108         }
109         
110         Reader r = (w != null) ? getReader(getFile()) : null;
111         ObjectBinding retVal = null;
112         if (r != null) {
113             retVal = r.getObjectBinding(getContext(),getFile ());
114             retVal.cachedInstance = new WeakReference JavaDoc(object);
115         }
116         return retVal;
117     }
118
119     public static ObjectBinding write(BasicContext ctx, FileObject folder, String JavaDoc bindingName, Object JavaDoc object) throws IOException JavaDoc {
120         Writer w = getWriter(object);
121         FileObject fo = null;
122         if (w != null) {
123             fo = w.write(folder, bindingName, object);
124         }
125
126         Reader r = (fo != null) ? getReader(fo) : null;
127         ObjectBinding retVal = null;
128         if (r != null) {
129             retVal = r.getObjectBinding(ctx,fo);
130             retVal.cachedInstance = new WeakReference JavaDoc(object);
131         }
132         return retVal;
133     }
134
135     /**
136      * Provides module descriptor that just implements method equals.
137      * Method hashCode may NOT BE PROPERLY IMPLEMENTED !!!
138      * @return module descriptor
139      */

140     public Object JavaDoc getModuleDescriptor() {
141         return getDefaultModuleDescriptor();
142     }
143
144     private static Object JavaDoc getDefaultModuleDescriptor() {
145         return new Object JavaDoc() {
146             public boolean equals(Object JavaDoc obj) {
147                 return false;
148             }
149         };
150     }
151
152
153     private static Reader getReader(FileObject fo) {
154         for (int i = 0; i < READERS.length; i++) {
155             if (READERS[i].canRead(fo)) {
156                 return READERS[i];
157             }
158         }
159         return null;
160     }
161
162     private static Writer getWriter(Object JavaDoc obj) {
163         for (int i = 0; i < WRITERS.length; i++) {
164             if (WRITERS[i].canWrite(obj)) {
165                 return WRITERS[i];
166             }
167
168         }
169         return null;
170     }
171
172     /**
173      * @return may return null
174      */

175     public final FileObject getFile() {
176         return file;
177     }
178
179
180     public String JavaDoc getBindingName() {
181         return file.getName();
182     }
183
184     public final Object JavaDoc getObject() throws IOException JavaDoc{
185         Object JavaDoc retVal = null;
186         if (cachedInstance == null || (retVal = cachedInstance.get()) == null) {
187             retVal = createInstance();
188             cachedInstance = new WeakReference JavaDoc(retVal);
189         }
190
191         return retVal;
192     }
193
194
195     public boolean isBinding(String JavaDoc bindingName) {
196         return getBindingName().equals(bindingName);
197     }
198
199     abstract public Object JavaDoc createInstance() throws IOException JavaDoc;
200
201     abstract public boolean isEnabled();
202
203     public static final ErrorManager getErrorManager() {
204         return err;
205     }
206
207     abstract static class Reader {
208         abstract boolean canRead(FileObject fo);
209
210         abstract String JavaDoc getFileExtension();
211
212         abstract ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo);
213     }
214
215     abstract static class Writer {
216         abstract boolean canWrite(Object JavaDoc obj);
217
218         abstract void write(FileObject fo, Object JavaDoc obj) throws IOException JavaDoc;
219
220         abstract String JavaDoc getFileExtension();
221
222         final FileObject write(FileObject folder, String JavaDoc bindingName, Object JavaDoc obj) throws IOException JavaDoc {
223             FileObject fo;
224             synchronized (this) {
225                 fo = folder.getFileObject(bindingName, getFileExtension());
226                 if (fo == null) {
227                     fo = folder.createData(bindingName, getFileExtension());
228                 }
229             }
230             this.write(fo, obj);
231             return fo;
232         }
233     }
234 }
235
Popular Tags