1 25 26 package org.snipsnap.render.macro.loader; 27 28 import groovy.lang.GroovyClassLoader; 29 import org.radeox.macro.Macro; 30 import org.radeox.macro.MacroLoader; 31 import org.radeox.macro.Repository; 32 import org.snipsnap.container.Components; 33 import org.snipsnap.notification.Consumer; 34 import org.snipsnap.notification.Message; 35 import org.snipsnap.notification.MessageService; 36 import org.snipsnap.snip.Snip; 37 import org.snipsnap.snip.SnipSpace; 38 39 import java.io.ByteArrayInputStream ; 40 import java.io.InputStream ; 41 42 49 50 public class GroovyMacroLoader extends MacroLoader implements Consumer { 51 public GroovyMacroLoader() { 52 MessageService service = (MessageService) Components.getComponent(MessageService.class); 54 service.register(this); 55 } 56 57 public void consume(Message messsage) { 58 if (Message.SNIP_MODIFIED.equals(messsage.getType())) { 59 Snip snip = (Snip) messsage.getValue(); 60 if (snip.getName().startsWith("SnipSnap/config/macros/")) { 61 try { 62 Macro macro = compileMacro(snip.getContent()); 63 if (null != macro) { 64 add(repository, macro); 65 } 66 } catch (Exception e) { 67 System.err.println("GroovyMacroLoader: unable to reload macros: " + e); 68 e.printStackTrace(); 69 } 70 } 71 } 72 } 73 74 private Macro compileMacro(String macroSource) { 75 Macro macro = null; 76 try { 77 GroovyClassLoader gcl = new GroovyClassLoader(); 78 InputStream is = new ByteArrayInputStream (macroSource.getBytes()); 79 Class clazz = gcl.parseClass(is, ""); 80 Object aScript = clazz.newInstance(); 81 macro = (Macro) aScript; 82 } catch (Exception e) { 84 System.err.println("Cannot compile groovy macro: " + e.getMessage()); 85 e.printStackTrace(); 86 } 87 return macro; 88 } 89 90 97 public Repository loadPlugins(Repository repository, Class klass) { 98 if (null != repository) { 99 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 100 Snip[] snips = space.match("SnipSnap/config/macros/"); 101 102 for (int i = 0; i < snips.length; i++) { 103 Snip snip = snips[i]; 104 Macro macro = compileMacro(snip.getContent()); 105 add(repository, macro); 106 } 107 108 124 } 127 return repository; 128 } 129 } | Popular Tags |