KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdr > MDRManagerImpl


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.mdr;
20
21 import org.netbeans.api.mdr.*;
22 import org.netbeans.mdr.NBMDRepositoryImpl;
23 import org.openide.util.Lookup;
24 import org.openide.filesystems.*;
25 import org.openide.loaders.*;
26
27 import org.netbeans.mdr.handlers.BaseObjectHandler;
28 import org.netbeans.mdr.handlers.ClassLoaderProvider;
29
30 import java.util.*;
31 import org.openide.ErrorManager;
32
33 /**
34  *
35  * @author Martin Matula
36  * @version
37  */

38 public class MDRManagerImpl extends MDRManager implements FileChangeListener, NBMDRepositoryImpl.ShutdownListener {
39
40     private static final String JavaDoc FOLDER_REPOSITORY = "MDRepositories";
41     private static final String JavaDoc DEFAULT_REPOSITORY = "Default";
42     
43     private HashMap repositoryMap = null;
44     private FileObject repFolder = null;
45     private static MDRManagerImpl instance = null;
46     
47     static {
48         BaseObjectHandler.setClassLoaderProvider(new CLProviderImpl());
49     }
50     
51     static synchronized void uninstall() {
52         if (instance != null) {
53             instance.uninstallMe();
54         }
55     }
56     
57     private void uninstallMe() {
58         repositoryMap = null;
59         if (repFolder != null) {
60             repFolder.removeFileChangeListener(this);
61         }
62         repFolder = null;
63     }
64
65     private void refreshChildren() {
66         if (repFolder == null) {
67             repFolder = Repository.getDefault().getDefaultFileSystem().getRoot().getFileObject(FOLDER_REPOSITORY);
68             repFolder.addFileChangeListener(this);
69         }
70         
71         repositoryMap.clear();
72         
73         if (repFolder != null) {
74             for (Enumeration en = repFolder.getData(false); en.hasMoreElements();) {
75                 try {
76                     DataObject dataObject = DataObject.find((FileObject) en.nextElement());
77                     if (dataObject instanceof MDRDataObject) {
78                         repositoryMap.put(dataObject.getName(), dataObject);
79                         ErrorManager.getDefault().log("found repository: " + dataObject.getName());
80                     }
81                 } catch (DataObjectNotFoundException e) {
82                     // ignore
83
}
84             }
85         }
86     }
87     
88     private void init() {
89         if (repositoryMap == null) {
90             repositoryMap = new HashMap();
91             refreshChildren();
92         }
93     }
94     
95     /** Creates new MDRManagerImpl */
96     public MDRManagerImpl() {
97         instance = this;
98     }
99
100     public void shutdownAll() {
101         String JavaDoc names[] = getRepositoryNames();
102         MDRepository[] repos = new MDRepository[names.length];
103         int stepCount = 0;
104         for (int i = 0; i < names.length; i++) {
105             repos[i] = getRepository(names[i]);
106             if (repos[i] instanceof NBMDRepositoryImpl) {
107                 stepCount += ((NBMDRepositoryImpl) repos[i]).getShutdownSteps();
108             } else {
109                 stepCount++;
110             }
111         }
112         fireProgressListenerStart(stepCount);
113         for (int i = 0; i < names.length; i++) {
114             ((NBMDRepositoryImpl) repos[i]).addShutdownListener(this);
115             repos[i].shutdown();
116             ((NBMDRepositoryImpl) repos[i]).removeShutdownListener(this);
117         }
118         fireProgressListenerStop();
119     }
120     
121     // === ShutdownListener implementation ===
122

123     public void shutdown() {
124     }
125     
126     public void stepFinished() {
127         fireProgressListenerStep();
128     } // === end of ShutdownListener implementation ===
129

130     //=== Progress suppport ===
131

132     private ProgressListener progress = null;
133     private boolean stopFired = false;
134     
135     public boolean setProgressListener(ProgressListener pl) {
136         boolean shutdownListenerRegistered = false;
137         if (progress == null) {
138             progress = pl;
139             shutdownListenerRegistered = true;
140         }
141         return shutdownListenerRegistered && !stopFired;
142     }
143     
144     private void fireProgressListenerStart(int count) {
145         if (progress == null)
146             return;
147         progress.start(count);
148     }
149         
150     private void fireProgressListenerInitialize() {
151         if (progress == null)
152             return;
153         progress.init();
154     }
155         
156     public void fireProgressListenerStep() {
157         if (progress == null)
158             return;
159         progress.step();
160     }
161     
162     private void fireProgressListenerStop() {
163         if (progress == null) {
164             stopFired = true;
165             return;
166         }
167         progress.stop();
168     }
169     //== end of Progress Support ==
170

171     
172     public MDRepository getRepository(String JavaDoc name) {
173         init();
174         MDRDataObject dataObject = (MDRDataObject) repositoryMap.get(name);
175         if (dataObject == null) {
176             return null;
177         } else {
178             MDRDescriptor repository = dataObject.getDescriptor();
179             return repository.getMDRInstance();
180         }
181     }
182     
183     public MDRepository getDefaultRepository() {
184         return getRepository(DEFAULT_REPOSITORY);
185     }
186
187     public String JavaDoc[] getRepositoryNames() {
188         init();
189         String JavaDoc[] result = new String JavaDoc[0];
190         result = (String JavaDoc[]) repositoryMap.keySet().toArray(result);
191         return result;
192     }
193     
194     /** Fired when a new folder is created. This action can only be
195      * listened to in folders containing the created folder up to the root of
196      * file system.
197      *
198      * @param fe the event describing context where action has taken place
199      */

200     public void fileFolderCreated(FileEvent fe) {
201         refreshChildren();
202     }
203     
204     /** Fired when a new file is created. This action can only be
205      * listened in folders containing the created file up to the root of
206      * file system.
207      *
208      * @param fe the event describing context where action has taken place
209      */

210     public void fileDataCreated(FileEvent fe) {
211         refreshChildren();
212     }
213     
214     /** Fired when a file is changed.
215      * @param fe the event describing context where action has taken place
216      */

217     public void fileChanged(FileEvent fe) {
218         refreshChildren();
219     }
220     
221     /** Fired when a file is deleted.
222      * @param fe the event describing context where action has taken place
223      */

224     public void fileDeleted(FileEvent fe) {
225         refreshChildren();
226     }
227     
228     /** Fired when a file is renamed.
229      * @param fe the event describing context where action has taken place
230      * and the original name and extension.
231      */

232     public void fileRenamed(FileRenameEvent fe) {
233         refreshChildren();
234     }
235     
236     /** Fired when a file attribute is changed.
237      * @param fe the event describing context where action has taken place,
238      * the name of attribute and the old and new values.
239      */

240     public void fileAttributeChanged(FileAttributeEvent fe) {
241         refreshChildren();
242     }
243
244     void preShutdownAll() {
245         fireProgressListenerInitialize();
246     }
247     
248     private static class CLProviderImpl implements ClassLoaderProvider {
249         public ClassLoader JavaDoc getClassLoader() {
250             return (ClassLoader JavaDoc) Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
251         }
252         
253         /** Implementation of this method can define a given class.
254          * The defined class should be then visible from the ClassLoader returned
255          * from {@link #getClassLoader} method.
256          * @param className name of the class to define.
257          * @param classFile
258          * @return Defined class or null (if null is return, MDR will define the
259          * class in its own classloader. This class will then not be accessible from outside
260          * of MDR
261          */

262         public Class JavaDoc defineClass(String JavaDoc className, byte[] classFile) {
263             return null;
264         }
265     }
266     
267     public interface ProgressListener {
268         // called from AWT
269
public void init();
270         
271         // called from RP
272
public void start(final int count);
273         public void step();
274         public void stop();
275     }
276 }
277
Popular Tags