KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > JBrowseModule


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.java.source;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.management.ManagementFactory JavaDoc;
25 import javax.management.InstanceAlreadyExistsException JavaDoc;
26 import javax.management.InstanceNotFoundException JavaDoc;
27 import javax.management.MBeanRegistrationException JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.NotCompliantMBeanException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import org.netbeans.modules.java.source.usages.ClassIndexManager;
33 import org.netbeans.modules.java.source.usages.LuceneIndexMBean;
34 import org.netbeans.modules.java.source.usages.LuceneIndexMBeanImpl;
35 import org.netbeans.modules.java.source.usages.RepositoryUpdater;
36 import org.netbeans.modules.java.source.util.LowMemoryNotifierMBean;
37 import org.netbeans.modules.java.source.util.LowMemoryNotifierMBeanImpl;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.modules.ModuleInstall;
41 import org.openide.util.Exceptions;
42 import org.openide.windows.WindowManager;
43
44 /**
45  *
46  * @author Petr Hrebejk
47  */

48 public class JBrowseModule extends ModuleInstall {
49     
50     private static final String JavaDoc NB_USER_DIR = "netbeans.user"; //NOI18N
51
private static final String JavaDoc LUCENE_LOCK_DIR = "org.apache.lucene.lockDir"; //NOI18N
52
private static final boolean ENABLE_MBEANS = Boolean.getBoolean("org.netbeans.modules.java.source.enableMBeans"); //NOI18N
53

54     /** Creates a new instance of JBrowseModule */
55     public JBrowseModule() {
56     }
57
58     public @Override JavaDoc void restored() {
59         super.restored();
60         final String JavaDoc nbUserProp = System.getProperty(NB_USER_DIR);
61         assert nbUserProp != null;
62         final File JavaDoc nbUserDir = new File JavaDoc (nbUserProp);
63         File JavaDoc lockDir = FileUtil.normalizeFile(new File JavaDoc (nbUserDir,"var"+File.separatorChar+"lock"+File.separatorChar+"lucene")); //NOI18N
64
if (lockDir.exists()) {
65             File JavaDoc[] orphanLocks = lockDir.listFiles();
66             for (File JavaDoc lock: orphanLocks) {
67                 lock.delete();
68             }
69         }
70         else {
71             lockDir.mkdirs();
72         }
73         JavaSourceTaskFactoryManager.register();
74         System.setProperty(LUCENE_LOCK_DIR,lockDir.getAbsolutePath());
75         WindowManager.getDefault().invokeWhenUIReady(new Runnable JavaDoc() {
76             public void run () {
77                 RepositoryUpdater.getDefault();
78                 ActivatedDocumentListener.register();
79             }
80         });
81         if (ENABLE_MBEANS) {
82             registerMBeans();
83         }
84     }
85     
86     public @Override JavaDoc boolean closing () {
87         final boolean ret = super.closing();
88         RepositoryUpdater.getDefault().close();
89         try {
90             ClassIndexManager.getDefault().writeLock(new ClassIndexManager.ExceptionAction<Void JavaDoc>() {
91                  public Void JavaDoc run() throws IOException JavaDoc {
92                      ClassIndexManager.getDefault().close();
93                      return null;
94                  }
95             });
96         } catch (IOException JavaDoc ex) {
97             Exceptions.printStackTrace(ex);
98         };
99         if (ENABLE_MBEANS) {
100             unregisterMBeans();
101         }
102         return ret;
103     }
104     
105     private static void registerMBeans() {
106         try {
107             MBeanServer JavaDoc mgs = ManagementFactory.getPlatformMBeanServer();
108             mgs.registerMBean (new LowMemoryNotifierMBeanImpl(), new ObjectName JavaDoc (LowMemoryNotifierMBean.OBJECT_NAME));
109             mgs.registerMBean( LuceneIndexMBeanImpl.getDefault(), new ObjectName JavaDoc (LuceneIndexMBean.OBJECT_NAME));
110         } catch (NotCompliantMBeanException JavaDoc e) {
111             ErrorManager.getDefault ().notify (e);
112         }
113         catch (MalformedObjectNameException JavaDoc e) {
114             ErrorManager.getDefault ().notify (e);
115         }
116         catch (InstanceAlreadyExistsException JavaDoc e) {
117             ErrorManager.getDefault ().notify (e);
118         }
119         catch (MBeanRegistrationException JavaDoc e) {
120             ErrorManager.getDefault ().notify (e);
121         }
122     }
123     
124     private static void unregisterMBeans() {
125         try {
126             MBeanServer JavaDoc mgs = ManagementFactory.getPlatformMBeanServer();
127             mgs.unregisterMBean (new ObjectName JavaDoc (LowMemoryNotifierMBean.OBJECT_NAME));
128             mgs.unregisterMBean (new ObjectName JavaDoc (LuceneIndexMBean.OBJECT_NAME));
129         } catch (MalformedObjectNameException JavaDoc e) {
130             ErrorManager.getDefault ().notify (e);
131         }
132         catch (InstanceNotFoundException JavaDoc e) {
133             ErrorManager.getDefault ().notify (e);
134         }
135         catch (MBeanRegistrationException JavaDoc e) {
136             ErrorManager.getDefault ().notify (e);
137         }
138     }
139 }
140
Popular Tags