📄 pluginjar.java
字号:
// 4.1-style plugins) is to load them on startup String activate = jEdit.getProperty("plugin." + className + ".activate"); if(activate == null) { // 4.1 plugin if(!jEdit.isMainThread()) { breakPlugin(); jEdit.pluginError(path,"plugin-error.not-42",null); } else activatePlugin(); } else { // 4.2 plugin // if at least one property listed here is true, // load the plugin boolean load = false; StringTokenizer st = new StringTokenizer(activate); while(st.hasMoreTokens()) { String prop = st.nextToken(); boolean value = jEdit.getBooleanProperty(prop); if(value) { Log.log(Log.DEBUG,this,"Activating " + className + " because of " + prop); load = true; break; } } if(load) activatePlugin(); } } //}}} //{{{ deactivatePlugin() method /** * Unloads the plugin core class. Does nothing if the plugin core class * has not been loaded. * This method can only be called from the AWT event dispatch thread! * @see EditPlugin#stop() * * @since jEdit 4.2pre3 */ public void deactivatePlugin(boolean exit) { synchronized(this) { if(!activated) return; activated = false; if(!exit) { // buffers retain a reference to the fold handler in // question... and the easiest way to handle fold // handler unloading is this... Buffer buffer = jEdit.getFirstBuffer(); while(buffer != null) { if(buffer.getFoldHandler() != null && buffer.getFoldHandler().getClass() .getClassLoader() == classLoader) { buffer.setFoldHandler( new DummyFoldHandler()); } buffer = buffer.getNext(); } } if(plugin != null && !(plugin instanceof EditPlugin.Broken)) { if(plugin instanceof EBPlugin) EditBus.removeFromBus((EBPlugin)plugin); try { plugin.stop(); } catch(Throwable t) { Log.log(Log.ERROR,this,"Error while " + "stopping plugin:"); Log.log(Log.ERROR,this,t); } plugin = new EditPlugin.Deferred( plugin.getClassName()); plugin.jar = (EditPlugin.JAR)this; EditBus.send(new PluginUpdate(this, PluginUpdate.DEACTIVATED,exit)); if(!exit) { // see if this is a 4.1-style plugin String activate = jEdit.getProperty("plugin." + plugin.getClassName() + ".activate"); if(activate == null) { breakPlugin(); jEdit.pluginError(path,"plugin-error.not-42",null); } } } } } //}}} //{{{ getDockablesURI() method /** * Returns the location of the plugin's * <code>dockables.xml</code> file. * @since jEdit 4.2pre1 */ public URL getDockablesURI() { return dockablesURI; } //}}} //{{{ getServicesURI() method /** * Returns the location of the plugin's * <code>services.xml</code> file. * @since jEdit 4.2pre1 */ public URL getServicesURI() { return servicesURI; } //}}} //{{{ toString() method public String toString() { if(plugin == null) return path; else return path + ",class=" + plugin.getClassName(); } //}}} //{{{ Package-private members //{{{ Static methods //{{{ getPluginCache() method static PluginCacheEntry getPluginCache(PluginJAR plugin) { String jarCachePath = plugin.getCachePath(); if(jarCachePath == null) return null; DataInputStream din = null; try { PluginCacheEntry cache = new PluginCacheEntry(); cache.plugin = plugin; cache.modTime = plugin.getFile().lastModified(); din = new DataInputStream( new BufferedInputStream( new FileInputStream(jarCachePath))); if(cache.read(din)) return cache; else { // returns false with outdated cache return null; } } catch(FileNotFoundException fnf) { return null; } catch(IOException io) { Log.log(Log.ERROR,PluginJAR.class,io); return null; } finally { try { if(din != null) din.close(); } catch(IOException io) { Log.log(Log.ERROR,PluginJAR.class,io); } } } //}}} //{{{ setPluginCache() method static void setPluginCache(PluginJAR plugin, PluginCacheEntry cache) { String jarCachePath = plugin.getCachePath(); if(jarCachePath == null) return; Log.log(Log.DEBUG,PluginJAR.class,"Writing " + jarCachePath); DataOutputStream dout = null; try { dout = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(jarCachePath))); cache.write(dout); dout.close(); } catch(IOException io) { Log.log(Log.ERROR,PluginJAR.class,io); try { dout.close(); } catch(IOException io2) { Log.log(Log.ERROR,PluginJAR.class,io2); } new File(jarCachePath).delete(); } } //}}} //}}} //{{{ PluginJAR constructor PluginJAR(File file) { this.path = file.getPath(); String jarCacheDir = jEdit.getJARCacheDirectory(); if(jarCacheDir != null) { cachePath = MiscUtilities.constructPath( jarCacheDir,file.getName() + ".summary"); } this.file = file; classLoader = new JARClassLoader(this); actions = new ActionSet(); } //}}} //{{{ init() method void init() { boolean initialized = false; PluginCacheEntry cache = getPluginCache(this); if(cache != null) { loadCache(cache); classLoader.activate(); initialized = true; } else { try { cache = generateCache(); if(cache != null) { setPluginCache(this,cache); classLoader.activate(); initialized = true; } } catch(IOException io) { Log.log(Log.ERROR,this,"Cannot load" + " plugin " + path); Log.log(Log.ERROR,this,io); String[] args = { io.toString() }; jEdit.pluginError(path,"plugin-error.load-error",args); uninit(false); } } } //}}} //{{{ uninit() method void uninit(boolean exit) { deactivatePlugin(exit); if(!exit) { Iterator iter = weRequireThese.iterator(); while(iter.hasNext()) { String path = (String)iter.next(); PluginJAR jar = jEdit.getPluginJAR(path); if(jar != null) jar.theseRequireMe.remove(this.path); } classLoader.deactivate(); BeanShell.resetClassManager(); if(actions != null) jEdit.getActionContext().removeActionSet(actions); if(browserActions != null) VFSBrowser.getActionContext().removeActionSet(browserActions); DockableWindowManager.unloadDockableWindows(this); ServiceManager.unloadServices(this); jEdit.removePluginProps(properties); try { if(zipFile != null) { zipFile.close(); zipFile = null; } } catch(IOException io) { Log.log(Log.ERROR,this,io); } } } //}}} //{{{ getClasses() method String[] getClasses() { return classes; } //}}} //}}} //{{{ Private members //{{{ Instance variables private String path; private String cachePath; private File file; private JARClassLoader classLoader; private ZipFile zipFile; private Properties properties; private String[] classes; private ActionSet actions; private ActionSet browserActions; private EditPlugin plugin; private URL dockablesURI; private URL servicesURI; private boolean activated; private List theseRequireMe = new LinkedList(); private List weRequireThese = new LinkedList(); //}}} //{{{ actionsPresentButNotCoreClass() method private void actionsPresentButNotCoreClass() { Log.log(Log.WARNING,this,getPath() + " has an actions.xml but no plugin core class"); actions.setLabel("MISSING PLUGIN CORE CLASS"); } //}}} //{{{ loadCache() method private void loadCache(PluginCacheEntry cache) { classes = cache.classes; /* this should be before dockables are initialized */ if(cache.cachedProperties != null) { properties = cache.cachedProperties; jEdit.addPluginProps(cache.cachedProperties); } if(cache.actionsURI != null && cache.cachedActionNames != null) { actions = new ActionSet(this, cache.cachedActionNames, cache.cachedActionToggleFlags, cache.actionsURI); } if(cache.browserActionsURI != null && cache.cachedBrowserActionNames != null) { browserActions = new ActionSet(this, cache.cachedBrowserActionNames, cache.cachedBrowserActionToggleFlags, cache.browserActionsURI); VFSBrowser.getActionContext().addActionSet(browserActions); } if(cache.dockablesURI != null && cache.cachedDockableNames != null && cache.cachedDockableActionFlags != null) { dockablesURI = cache.dockablesURI; DockableWindowManager.cacheDockableWindows(this, cache.cachedDockableNames, cache.cachedDockableActionFlags); } if(actions.size() != 0) jEdit.addActionSet(actions); if(cache.servicesURI != null && cache.cachedServices != null) { servicesURI = cache.servicesURI; for(int i = 0; i < cache.cachedServices.length; i++) { ServiceManager.Descriptor d = cache.cachedServices[i]; ServiceManager.registerService(d); } } if(cache.pluginClass != null) { // Check if a plugin with the same name // is already loaded if(jEdit.getPlugin(cache.pluginClass) != null) { jEdit.pluginError(path, "plugin-error.already-loaded", null); uninit(false); } else { String label = jEdit.getProperty( "plugin." + cache.pluginClass + ".name"); actions.setLabel(jEdit.getProperty( "action-set.plugin", new String[] { label })); plugin = new EditPlugin.Deferred( cache.pluginClass); plugin.jar = (EditPlugin.JAR)this; } } else { if(actions.size() != 0) actionsPresentButNotCoreClass(); } } //}}} //{{{ generateCache() method private PluginCacheEntry generateCache() throws IOException { properties = new Properties(); LinkedList classes = new LinkedList(); ZipFile zipFile = getZipFile(); List plugins = new LinkedList(); PluginCacheEntry cache = new PluginCacheEntry(); cache.modTime = file.lastModified(); cache.cachedProperties = new Properties(); Enumeration entries = zipFile.entries(); while(entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); String name = entry.getName(); String lname = name.toLowerCase(); if(lname.equals("actions.xml")) { cache.actionsURI = classLoader.getResource(name); } else if(lname.equals("browser.actions.xml")) { cache.browserActionsURI = classLoader.getResource(name); } else if(lname.equals("dockables.xml")) { dockablesURI = classLoader.getResource(name); cache.dockablesURI = dockablesURI; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -