1 25 26 package org.snipsnap.feeder; 27 28 import groovy.lang.GroovyClassLoader; 29 import org.radeox.macro.Repository; 30 import org.snipsnap.container.Components; 31 import org.snipsnap.notification.Consumer; 32 import org.snipsnap.notification.Message; 33 import org.snipsnap.notification.MessageService; 34 import org.snipsnap.snip.Snip; 35 import org.snipsnap.snip.SnipSpace; 36 import org.snipsnap.user.Permissions; 37 import org.snipsnap.user.Roles; 38 import org.snipsnap.user.Security; 39 40 import java.io.ByteArrayInputStream ; 41 import java.io.InputStream ; 42 43 50 51 public class GroovyFeederLoader extends FeederLoader implements Consumer { 52 public final static String SYSTEM_FEEDER_PATH = "SnipSnap/config/feeder/"; 53 private final static Roles EXEC_ROLES = new Roles(Roles.ADMIN); 54 55 public GroovyFeederLoader() { 56 MessageService service = (MessageService) Components.getComponent(MessageService.class); 58 service.register(this); 59 } 60 61 public void consume(Message messsage) { 62 if (Message.SNIP_MODIFIED.equals(messsage.getType())) { 64 Snip snip = (Snip) messsage.getValue(); 65 if (snip.getName().startsWith(SYSTEM_FEEDER_PATH) && 66 Security.existsPermission(Permissions.EDIT_SNIP, snip, EXEC_ROLES)) { 67 try { 68 Feeder feeder = compileFeeder(snip.getContent()); 69 if (null != feeder) { 70 add(repository, feeder); 71 } 72 } catch (Exception e) { 73 System.err.println("GroovyFeederLoader: unable to reload feeders: " + e); 74 e.printStackTrace(); 75 } 76 } 77 } 78 } 79 80 private Feeder compileFeeder(String macroSource) { 81 Feeder feeder = null; 82 try { 83 GroovyClassLoader gcl = new GroovyClassLoader(); 84 InputStream is = new ByteArrayInputStream (macroSource.getBytes()); 85 Class clazz = gcl.parseClass(is, ""); 86 Object aScript = clazz.newInstance(); 87 feeder = (Feeder) aScript; 88 System.out.println("Compiled: " + feeder.getName()); 89 } catch (Exception e) { 91 System.err.println("Cannot compile groovy feeder: " + e.getMessage()); 92 e.printStackTrace(); 93 } 94 return feeder; 95 } 96 97 104 public Repository loadPlugins(Repository repository, Class klass) { 105 if (null != repository) { 106 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 107 Snip[] snips = space.match(SYSTEM_FEEDER_PATH); 108 109 for (int i = 0; i < snips.length; i++) { 110 Snip snip = snips[i]; 111 Feeder feeder = compileFeeder(snip.getContent()); 112 add(repository, feeder); 113 } 114 115 131 } 134 return repository; 135 } 136 } | Popular Tags |