1 19 20 package org.netbeans.modules.java.source; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.management.ManagementFactory ; 25 import javax.management.InstanceAlreadyExistsException ; 26 import javax.management.InstanceNotFoundException ; 27 import javax.management.MBeanRegistrationException ; 28 import javax.management.MBeanServer ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.NotCompliantMBeanException ; 31 import javax.management.ObjectName ; 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 48 public class JBrowseModule extends ModuleInstall { 49 50 private static final String NB_USER_DIR = "netbeans.user"; private static final String LUCENE_LOCK_DIR = "org.apache.lucene.lockDir"; private static final boolean ENABLE_MBEANS = Boolean.getBoolean("org.netbeans.modules.java.source.enableMBeans"); 54 55 public JBrowseModule() { 56 } 57 58 public @Override void restored() { 59 super.restored(); 60 final String nbUserProp = System.getProperty(NB_USER_DIR); 61 assert nbUserProp != null; 62 final File nbUserDir = new File (nbUserProp); 63 File lockDir = FileUtil.normalizeFile(new File (nbUserDir,"var"+File.separatorChar+"lock"+File.separatorChar+"lucene")); if (lockDir.exists()) { 65 File [] orphanLocks = lockDir.listFiles(); 66 for (File 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 () { 76 public void run () { 77 RepositoryUpdater.getDefault(); 78 ActivatedDocumentListener.register(); 79 } 80 }); 81 if (ENABLE_MBEANS) { 82 registerMBeans(); 83 } 84 } 85 86 public @Override boolean closing () { 87 final boolean ret = super.closing(); 88 RepositoryUpdater.getDefault().close(); 89 try { 90 ClassIndexManager.getDefault().writeLock(new ClassIndexManager.ExceptionAction<Void >() { 91 public Void run() throws IOException { 92 ClassIndexManager.getDefault().close(); 93 return null; 94 } 95 }); 96 } catch (IOException 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 mgs = ManagementFactory.getPlatformMBeanServer(); 108 mgs.registerMBean (new LowMemoryNotifierMBeanImpl(), new ObjectName (LowMemoryNotifierMBean.OBJECT_NAME)); 109 mgs.registerMBean( LuceneIndexMBeanImpl.getDefault(), new ObjectName (LuceneIndexMBean.OBJECT_NAME)); 110 } catch (NotCompliantMBeanException e) { 111 ErrorManager.getDefault ().notify (e); 112 } 113 catch (MalformedObjectNameException e) { 114 ErrorManager.getDefault ().notify (e); 115 } 116 catch (InstanceAlreadyExistsException e) { 117 ErrorManager.getDefault ().notify (e); 118 } 119 catch (MBeanRegistrationException e) { 120 ErrorManager.getDefault ().notify (e); 121 } 122 } 123 124 private static void unregisterMBeans() { 125 try { 126 MBeanServer mgs = ManagementFactory.getPlatformMBeanServer(); 127 mgs.unregisterMBean (new ObjectName (LowMemoryNotifierMBean.OBJECT_NAME)); 128 mgs.unregisterMBean (new ObjectName (LuceneIndexMBean.OBJECT_NAME)); 129 } catch (MalformedObjectNameException e) { 130 ErrorManager.getDefault ().notify (e); 131 } 132 catch (InstanceNotFoundException e) { 133 ErrorManager.getDefault ().notify (e); 134 } 135 catch (MBeanRegistrationException e) { 136 ErrorManager.getDefault ().notify (e); 137 } 138 } 139 } 140 | Popular Tags |