KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > ServletPluginLoader


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

26 package org.snipsnap.net;
27
28 import org.radeox.util.Service;
29 import org.radeox.util.logging.Logger;
30 import org.snipsnap.container.Components;
31 import org.snipsnap.snip.Snip;
32 import org.snipsnap.snip.SnipSpace;
33 import org.snipsnap.snip.label.Label;
34 import org.snipsnap.snip.label.Labels;
35 import org.snipsnap.snip.label.TypeLabel;
36
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40
41 public class ServletPluginLoader {
42   // TODO make selectable via application OID
43
private static Map JavaDoc pluginServlets;
44
45   /**
46    * Get a map of all plugins (jar and snip).
47    *
48    * @return the map of plugins
49    */

50   public static Map JavaDoc getPlugins() {
51     initServletPlugins();
52
53     Map JavaDoc allPlugins = getSnipPlugins();
54     allPlugins.putAll(pluginServlets);
55     return allPlugins;
56   }
57
58   /**
59    * Fina all snip plugin handlers by checking the mime type.
60    *
61    * @return a map with snip name(handler) and type
62    */

63   private static Map JavaDoc getSnipPlugins() {
64     Map JavaDoc snipPlugins = new HashMap JavaDoc();
65     SnipSpace snipspace = (SnipSpace) Components.getComponent(SnipSpace.class);
66     Iterator JavaDoc iterator = snipspace.getAll().iterator();
67     while (iterator.hasNext()) {
68       Snip snip = (Snip) iterator.next();
69       Labels labels = snip.getLabels();
70       boolean noLabelsAll = labels.getAll().isEmpty();
71
72       if (!noLabelsAll) {
73         Label label = labels.getLabel("Type");
74         if (null != label && label instanceof TypeLabel) {
75           // only add labels that have the type text/gsp
76
String JavaDoc type = ((TypeLabel) label).getTypeValue();
77           if ("text/gsp".equalsIgnoreCase(type) || "text/groovy".equalsIgnoreCase(type)) {
78             String JavaDoc handler = snip.getName();
79             snipPlugins.put(handler, type);
80           }
81         }
82       }
83     }
84     return snipPlugins;
85   }
86
87   /**
88    * Load servlets from jar resource.
89    */

90   private static void initServletPlugins() {
91     if (null == pluginServlets) {
92       pluginServlets = new HashMap JavaDoc();
93
94       // load plugins from services api
95
Iterator JavaDoc pluginServletNames = Service.providerNames(ServletPlugin.class);
96       while (pluginServletNames.hasNext()) {
97         String JavaDoc pluginLine = (String JavaDoc) pluginServletNames.next();
98         if (!pluginLine.startsWith("#")) {
99           String JavaDoc[] pluginInfo = pluginLine.split("\\p{Space}+");
100           if (pluginInfo.length > 0) {
101             pluginServlets.put(pluginInfo[0], pluginInfo.length > 1 ? pluginInfo[1] : null);
102             Logger.log("found plugin: " + pluginInfo[0]);
103           } else {
104             Logger.warn("ignoring servlet plugin '" + pluginLine + "': missing type or servlet");
105           }
106         }
107       }
108     }
109   }
110 }
111
Popular Tags