KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > gsf > GsfModuleInstaller


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.gsf;
20
21 import java.io.File JavaDoc;
22 import java.lang.management.ManagementFactory JavaDoc;
23 import javax.management.InstanceAlreadyExistsException JavaDoc;
24 import javax.management.InstanceNotFoundException JavaDoc;
25 import javax.management.MBeanRegistrationException JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.NotCompliantMBeanException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import org.netbeans.api.retouche.source.SourceTaskFactoryManager;
31 import org.netbeans.editor.Settings;
32 import org.netbeans.modules.retouche.source.ActivatedDocumentListener;
33 import org.netbeans.modules.retouche.source.usages.ClassIndexManager;
34 import org.netbeans.modules.retouche.source.usages.RepositoryUpdater;
35 import org.netbeans.modules.retouche.source.util.LowMemoryNotifierMBean;
36 import org.netbeans.modules.retouche.source.util.LowMemoryNotifierMBeanImpl;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.modules.ModuleInstall;
40 import org.openide.windows.WindowManager;
41
42
43 public class GsfModuleInstaller extends ModuleInstall {
44     private static final boolean ENABLE_MBEANS =
45         Boolean.getBoolean("org.netbeans.modules.gsf.enableMBeans"); //NOI18N
46
private static final String JavaDoc NB_USER_DIR = "netbeans.user"; //NOI18N
47
private static final String JavaDoc LUCENE_LOCK_DIR = "org.apache.lucene.lockDir"; //NOI18N
48

49     @Override JavaDoc
50     public void restored() {
51         // add editor support for our registered editor types
52
Settings.addInitializer(new GsfEditorSettings());
53         Settings.reset();
54
55         final String JavaDoc nbUserProp = System.getProperty(NB_USER_DIR);
56         assert nbUserProp != null;
57         final File JavaDoc nbUserDir = new File JavaDoc (nbUserProp);
58         File JavaDoc lockDir = FileUtil.normalizeFile(new File JavaDoc (nbUserDir,"var"+File.separatorChar+"lock"+File.separatorChar+"lucene-gsf")); //NOI18N
59
if (lockDir.exists()) {
60             File JavaDoc[] orphanLocks = lockDir.listFiles();
61             for (File JavaDoc lock: orphanLocks) {
62                 lock.delete();
63             }
64         }
65         else {
66             lockDir.mkdirs();
67         }
68
69         
70         SourceTaskFactoryManager.register();
71         System.setProperty(LUCENE_LOCK_DIR,lockDir.getAbsolutePath());
72
73         WindowManager.getDefault().invokeWhenUIReady(new Runnable JavaDoc() {
74                 public void run() {
75                     RepositoryUpdater.getDefault();
76                     ActivatedDocumentListener.register();
77                 }
78             });
79
80         if (ENABLE_MBEANS) {
81             registerMBeans();
82         }
83     }
84
85     //XXX: API contract, should be better to turn it off when it goes to release
86
@Override JavaDoc
87     public boolean closing() {
88         final boolean ret = super.closing();
89         RepositoryUpdater.getDefault().close ();
90         ClassIndexManager.getDefault().close();
91         if (ENABLE_MBEANS) {
92             unregisterMBeans();
93         }
94
95         return ret;
96     }
97
98     private static void registerMBeans() {
99         try {
100             MBeanServer JavaDoc mgs = ManagementFactory.getPlatformMBeanServer();
101             mgs.registerMBean(new LowMemoryNotifierMBeanImpl(),
102                 new ObjectName JavaDoc(LowMemoryNotifierMBean.OBJECT_NAME));
103         } catch (NotCompliantMBeanException JavaDoc e) {
104             ErrorManager.getDefault().notify(e);
105         } catch (MalformedObjectNameException JavaDoc e) {
106             ErrorManager.getDefault().notify(e);
107         } catch (InstanceAlreadyExistsException JavaDoc e) {
108             ErrorManager.getDefault().notify(e);
109         } catch (MBeanRegistrationException JavaDoc e) {
110             ErrorManager.getDefault().notify(e);
111         }
112     }
113
114     private static void unregisterMBeans() {
115         try {
116             MBeanServer JavaDoc mgs = ManagementFactory.getPlatformMBeanServer();
117             mgs.unregisterMBean(new ObjectName JavaDoc(LowMemoryNotifierMBean.OBJECT_NAME));
118         } catch (MalformedObjectNameException JavaDoc e) {
119             ErrorManager.getDefault().notify(e);
120         } catch (InstanceNotFoundException JavaDoc e) {
121             ErrorManager.getDefault().notify(e);
122         } catch (MBeanRegistrationException JavaDoc e) {
123             ErrorManager.getDefault().notify(e);
124         }
125     }
126 }
127
Popular Tags