KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > feeder > GroovyFeederLoader


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
41 import java.io.InputStream JavaDoc;
42
43 /**
44  * Plugin loader for feeders with Groovy source
45  * instead of Java
46  *
47  * @author Stephan J. Schmidt
48  * @version $Id: GroovyFeederLoader.java 1623 2004-06-07 12:06:03Z leo $
49  */

50
51 public class GroovyFeederLoader extends FeederLoader implements Consumer {
52   public final static String JavaDoc SYSTEM_FEEDER_PATH = "SnipSnap/config/feeder/";
53   private final static Roles EXEC_ROLES = new Roles(Roles.ADMIN);
54
55   public GroovyFeederLoader() {
56     // We're interested in changed snips
57
MessageService service = (MessageService) Components.getComponent(MessageService.class);
58     service.register(this);
59   }
60
61   public void consume(Message messsage) {
62 // System.out.println("GroovyFeederLoader: Message received.");
63
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 JavaDoc e) {
73           System.err.println("GroovyFeederLoader: unable to reload feeders: " + e);
74           e.printStackTrace();
75         }
76       }
77     }
78   }
79
80   private Feeder compileFeeder(String JavaDoc macroSource) {
81     Feeder feeder = null;
82     try {
83       GroovyClassLoader gcl = new GroovyClassLoader();
84       InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(macroSource.getBytes());
85       Class JavaDoc clazz = gcl.parseClass(is, "");
86       Object JavaDoc aScript = clazz.newInstance();
87       feeder = (Feeder) aScript;
88       System.out.println("Compiled: " + feeder.getName());
89       //System.out.println("Script="+macroSource);
90
} catch (Exception JavaDoc e) {
91       System.err.println("Cannot compile groovy feeder: " + e.getMessage());
92       e.printStackTrace();
93     }
94     return feeder;
95   }
96
97   /**
98    * Load all plugins of Class klass
99    *
100    * @param repository
101    * @param klass
102    * @return
103    */

104   public Repository loadPlugins(Repository repository, Class JavaDoc 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 // String macroSource = "import java.io.Writer\n" +
116
// "import org.radeox.macro.parameter.MacroParameter\n" +
117
// "\n" +
118
// "class GroovyMacro extends org.radeox.macro.BaseMacro {\n" +
119
// " void execute(Writer writer, MacroParameter params) {\n" +
120
// " writer.write(\"Yipee ay ey, schweinebacke\")\n" +
121
// " }\n" +
122
// " String getName() {" +
123
// " return \"groovy\"\n" +
124
// " }\n " +
125
// "}";
126
// Macro macro = compileMacro(macroSource);
127
// if (null != macro) {
128
// add(repository, macro);
129
// }
130

131       //System.out.println(clazz.getName());
132
// System.out.println(aScriptSource);
133
}
134     return repository;
135   }
136 }
Popular Tags