Author: nextgens
Date: 2007-09-13 11:24:55 +0000 (Thu, 13 Sep 2007)
New Revision: 15135

Modified:
   trunk/freenet/src/freenet/clients/http/PproxyToadlet.java
   trunk/freenet/src/freenet/pluginmanager/PluginManager.java
Log:
plugins: Move some code around... sorry for the big diff

Basically, checking for <plugname># should be done on the toadlet and not on 
the plugin manager ... otherwise we end up storing the # version in the 
configuration ... and we actually re-download everything on each startup :(

nb: current loaded plugins using # will be "forgotten"

Modified: trunk/freenet/src/freenet/clients/http/PproxyToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/PproxyToadlet.java   2007-09-13 
09:08:18 UTC (rev 15134)
+++ trunk/freenet/src/freenet/clients/http/PproxyToadlet.java   2007-09-13 
11:24:55 UTC (rev 15135)
@@ -1,7 +1,12 @@
 package freenet.clients.http;

+import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URL;
 import java.util.Date;
 import java.util.Iterator;

@@ -20,6 +25,7 @@
 import freenet.support.Logger;
 import freenet.support.MultiValueTable;
 import freenet.support.api.HTTPRequest;
+import freenet.support.io.FileUtil;

 public class PproxyToadlet extends Toadlet {
        private static final int MAX_PLUGIN_NAME_LENGTH = 1024;
@@ -39,7 +45,7 @@
        }

        public void handlePost(URI uri, HTTPRequest request, ToadletContext ctx)
-               throws ToadletContextClosedException, IOException {
+       throws ToadletContextClosedException, IOException {

                MultiValueTable headers = new MultiValueTable();

@@ -55,7 +61,7 @@
                        super.sendErrorPage(ctx, 403, "Unauthorized", 
l10n("unauthorized"));
                        return;
                }
-               
+
                String path=request.getPath();

                // remove leading / and plugins/ from path
@@ -65,7 +71,7 @@
                if(Logger.shouldLog(Logger.MINOR, this)) Logger.minor(this, 
"Pproxy received POST on "+path);

                PluginManager pm = node.pluginManager;
-               
+
                if(path.length()>0)
                {
                        try
@@ -114,10 +120,85 @@
                {

                        if (request.isPartSet("load")) {
-                               if(Logger.shouldLog(Logger.MINOR, this)) 
Logger.minor(this, "Loading "+request.getPartAsString("load", 
MAX_PLUGIN_NAME_LENGTH));
-                               pm.startPlugin(request.getPartAsString("load", 
MAX_PLUGIN_NAME_LENGTH), true);
-                               //writeReply(ctx, 200, "text/html", "OK", 
mkForwardPage("Loading plugin", "Loading plugin...", ".", 5));
+                               String filename = 
request.getPartAsString("load", MAX_PLUGIN_NAME_LENGTH);
+                               final boolean logMINOR = 
Logger.shouldLog(Logger.MINOR, this);
+                               boolean downloaded = false;

+                               if(logMINOR) Logger.minor(this, "Loading 
"+filename);
+                               if (filename.endsWith("#")) {
+                                       for (int tries = 0; (tries <= 5) && 
(downloaded == false); tries++) {
+                                               if (filename.indexOf('@') > -1) 
{
+                                                       Logger
+                                                       .error(this,
+                                                       "We don't allow 
downloads from anywhere else but our server");
+                                                       return;
+                                               }
+                                               String pluginname = 
filename.substring(0,
+                                                               
filename.length() - 1);
+                                               filename = null;
+
+                                               URL url;
+                                               InputStream is = null;
+
+                                               try {
+                                                       url = new URL(
+                                                                       
"http://downloads.freenetproject.org/alpha/plugins/";
+                                                                       + 
pluginname + ".jar.url");
+                                                       if (logMINOR)
+                                                               
Logger.minor(this, "Downloading " + url);
+                                                       is = url.openStream();
+
+                                                       File pluginsDirectory = 
new File("plugins");
+                                                       if 
(!pluginsDirectory.exists()) {
+                                                               Logger
+                                                               .normal(this,
+                                                               "The plugin 
directory hasn't been found, let's create it");
+                                                               if 
(!pluginsDirectory.mkdir())
+                                                                       return;
+                                                       }
+
+                                                       File finalFile = new 
File("plugins/" + pluginname
+                                                                       + 
".jar");
+                                                       if 
(!FileUtil.writeTo(is, finalFile))
+                                                               
Logger.error(this,
+                                                                               
"Failed to rename the temporary file into "
+                                                                               
+ finalFile);
+
+                                                       filename = "*@file://"
+                                                               + 
FileUtil.getCanonicalFile(finalFile);
+                                                       if (logMINOR)
+                                                               
Logger.minor(this, "Rewritten to " + filename);
+                                                       downloaded = true;
+                                               } catch (MalformedURLException 
mue) {
+                                                       Logger.error(this,
+                                                                       
"MalformedURLException has occured : " + mue,
+                                                                       mue);
+                                                       return;
+                                               } catch (FileNotFoundException 
e) {
+                                                       Logger.error(this,
+                                                                       
"FileNotFoundException has occured : " + e, e);
+                                                       return;
+                                               } catch (IOException ioe) {
+                                                       
System.out.println("Caught :" + ioe.getMessage());
+                                                       ioe.printStackTrace();
+                                                       return;
+                                               } finally {
+                                                       try {
+                                                               if (is != null)
+                                                                       
is.close();
+                                                       } catch (IOException 
ioe) {
+                                                       }
+                                               }
+                                               if (filename == null)
+                                                       return;
+                                               else if(!downloaded) {
+                                                       Logger.error(this, 
"Can't load the given plugin; giving up");
+                                                       return;
+                                               }
+                                       }
+                               }
+
+                               pm.startPlugin(filename, true);
                                headers.put("Location", ".");
                                ctx.sendReplyHeaders(302, "Found", headers, 
null, 0);
                                return;
@@ -192,7 +273,7 @@
        }

        public void handleGet(URI uri, HTTPRequest request, ToadletContext ctx)
-               throws ToadletContextClosedException, IOException {
+       throws ToadletContextClosedException, IOException {

                //String basepath = "/plugins/";
                String path = request.getPath();
@@ -202,7 +283,7 @@
                if(path.startsWith("plugins/")) path = 
path.substring("plugins/".length());

                PluginManager pm = node.pluginManager;
-               
+
                if(Logger.shouldLog(Logger.MINOR, this))
                        Logger.minor(this, "Pproxy fetching "+path);
                try {
@@ -281,18 +362,18 @@
                                        if(pi.isStopping()) {
                                                actionCell.addChild("#", 
l10n("pluginStopping"));
                                        } else {
-                                       if (pi.isPproxyPlugin()) {
-                                               HTMLNode visitForm = 
actionCell.addChild("form", new String[] { "method", "action", "target" }, new 
String[] { "get", pi.getPluginClassName(), "_new" });
-                                               visitForm.addChild("input", new 
String[] { "type", "name", "value" }, new String[] { "hidden", "formPassword", 
core.formPassword });
-                                               visitForm.addChild("input", new 
String[] { "type", "value" }, new String[] { "submit", 
L10n.getString("PluginToadlet.visit") });
+                                               if (pi.isPproxyPlugin()) {
+                                                       HTMLNode visitForm = 
actionCell.addChild("form", new String[] { "method", "action", "target" }, new 
String[] { "get", pi.getPluginClassName(), "_new" });
+                                                       
visitForm.addChild("input", new String[] { "type", "name", "value" }, new 
String[] { "hidden", "formPassword", core.formPassword });
+                                                       
visitForm.addChild("input", new String[] { "type", "value" }, new String[] { 
"submit", L10n.getString("PluginToadlet.visit") });
+                                               }
+                                               HTMLNode unloadForm = 
ctx.addFormChild(actionCell, ".", "unloadPluginForm");
+                                               unloadForm.addChild("input", 
new String[] { "type", "name", "value" }, new String[] { "hidden", "unload", 
pi.getThreadName() });
+                                               unloadForm.addChild("input", 
new String[] { "type", "value" }, new String[] { "submit", l10n("unload") });
+                                               HTMLNode reloadForm = 
ctx.addFormChild(actionCell, ".", "reloadPluginForm");
+                                               reloadForm.addChild("input", 
new String[] { "type", "name", "value" }, new String[] { "hidden", "reload", 
pi.getThreadName() });
+                                               reloadForm.addChild("input", 
new String[] { "type", "value" }, new String[] { "submit", l10n("reload") });
                                        }
-                                       HTMLNode unloadForm = 
ctx.addFormChild(actionCell, ".", "unloadPluginForm");
-                                       unloadForm.addChild("input", new 
String[] { "type", "name", "value" }, new String[] { "hidden", "unload", 
pi.getThreadName() });
-                                       unloadForm.addChild("input", new 
String[] { "type", "value" }, new String[] { "submit", l10n("unload") });
-                                       HTMLNode reloadForm = 
ctx.addFormChild(actionCell, ".", "reloadPluginForm");
-                                       reloadForm.addChild("input", new 
String[] { "type", "name", "value" }, new String[] { "hidden", "reload", 
pi.getThreadName() });
-                                       reloadForm.addChild("input", new 
String[] { "type", "value" }, new String[] { "submit", l10n("reload") });
-                                       }
                                }
                        }


Modified: trunk/freenet/src/freenet/pluginmanager/PluginManager.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/PluginManager.java  2007-09-13 
09:08:18 UTC (rev 15134)
+++ trunk/freenet/src/freenet/pluginmanager/PluginManager.java  2007-09-13 
11:24:55 UTC (rev 15135)
@@ -4,13 +4,10 @@
 package freenet.pluginmanager;

 import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.JarURLConnection;
-import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -34,7 +31,6 @@
 import freenet.support.URIPreEncoder;
 import freenet.support.api.HTTPRequest;
 import freenet.support.api.StringArrCallback;
-import freenet.support.io.FileUtil;

 public class PluginManager {

@@ -68,15 +64,15 @@
                // Start plugins in the config
                pmconfig.register("loadplugin", null, 9, true, false, 
"PluginManager.loadedOnStartup", "PluginManager.loadedOnStartupLong",
                                new StringArrCallback() {
-                                       public String[] get() {
-                                               return getConfigLoadString();
-                                       }
-                                       public void set(String[] val) throws 
InvalidConfigValueException {
-                                               //if(storeDir.equals(new 
File(val))) return;
-                                               // FIXME
-                                               throw new 
InvalidConfigValueException(L10n.getString("PluginManager.cannotSetOnceLoaded"));
-                                       }
-                               });
+                       public String[] get() {
+                               return getConfigLoadString();
+                       }
+                       public void set(String[] val) throws 
InvalidConfigValueException {
+                               //if(storeDir.equals(new File(val))) return;
+                               // FIXME
+                               throw new 
InvalidConfigValueException(L10n.getString("PluginManager.cannotSetOnceLoaded"));
+                       }
+               });

                String fns[] = pmconfig.getStringArr("loadplugin");
                if (fns != null) {
@@ -93,7 +89,7 @@
                  for (int i = 0 ; i < fns.length ; i++)
                  System.err.println("Load: " + StringArrOption.decode(fns[i]));
                  System.err.println("=================================");
-                 */
+                */
        }

        private String[] getConfigLoadString() {
@@ -135,9 +131,9 @@
                                System.err.println("Plugin "+filename+" appears 
to require a later JVM");
                                Logger.error(this, "Plugin "+filename+" appears 
to require a later JVM");
                                core.alerts.register(new SimpleUserAlert(true, 
-                                                       
l10n("pluginReqNewerJVMTitle", "name", filename),
-                                                       
l10n("pluginReqNewerJVM", "name", filename),
-                                                       UserAlert.ERROR));
+                                               l10n("pluginReqNewerJVMTitle", 
"name", filename),
+                                               l10n("pluginReqNewerJVM", 
"name", filename),
+                                               UserAlert.ERROR));
                        }
                }
                if(store) core.storeConfig();
@@ -156,7 +152,7 @@
                        
node.ipDetector.registerPortForwardPlugin((FredPluginPortForward) plug);
                }
        }
-       
+
        private String l10n(String key, String pattern, String value) {
                return L10n.getString("PluginManager."+key, pattern, value);
        }
@@ -178,7 +174,7 @@
                }
                core.storeConfig();
        }
-       
+
        public void unregisterPluginToadlet(PluginInfoWrapper pi) {
                synchronized (toadletList) {
                        try {
@@ -260,7 +256,7 @@
                }
                /*if (handler == null)
                  return null;
-                 */
+                */

                if (handler instanceof FredPluginHTTP)
                        return ((FredPluginHTTP)handler).handleHTTPGet(request);
@@ -275,7 +271,7 @@
                }
                /*if (handler == null)
                  return null;
-                 */
+                */

                if (handler instanceof FredPluginHTTP)
                        return 
((FredPluginHTTP)handler).handleHTTPPost(request);
@@ -310,94 +306,19 @@
         * @throws PluginNotFoundException      If anything goes wrong.
         */
        private FredPlugin LoadPlugin(String origFilename)
-                       throws PluginNotFoundException {
+       throws PluginNotFoundException {
                logMINOR = Logger.shouldLog(Logger.MINOR, this);
                Class cls = null;
                for (int tries = 0; (tries <= 5) && (cls == null); tries++) {
                        String filename = origFilename;
+                       if (filename.endsWith("*")) {
+                               filename = 
"*@http://downloads.freenetproject.org/alpha/plugins/";
+                                               + 
filename.substring(filename.lastIndexOf(".") + 1,
+                                                               
filename.length() - 1) + ".jar.url";
+                               if (logMINOR)
+                                       Logger.minor(this, "Rewritten to " + 
filename);
+                       }
                        try {
-                               if (filename.endsWith("*")) {
-                                       filename = 
"*@http://downloads.freenetproject.org/alpha/plugins/";
-                                                       + 
filename.substring(filename.lastIndexOf(".") + 1,
-                                                                       
filename.length() - 1) + ".jar.url";
-                                       // System.out.println(filename);
-                                       if (logMINOR)
-                                               Logger.minor(this, "Rewritten 
to " + filename);
-                               }
-                               if (filename.endsWith("#")) {
-                                       if (filename.indexOf('@') > -1) {
-                                               Logger
-                                                               .error(this,
-                                                                               
"We don't allow downloads from anywhere else but our server");
-                                               return null;
-                                       }
-                                       String pluginname = 
filename.substring(0,
-                                                       filename.length() - 1);
-                                       filename = null;
-
-                                       URL url;
-                                       InputStream is = null;
-
-                                       try {
-                                               url = new URL(
-                                                               
"http://downloads.freenetproject.org/alpha/plugins/";
-                                                                               
+ pluginname + ".jar.url");
-                                               if (logMINOR)
-                                                       Logger.minor(this, 
"Downloading " + url);
-                                               is = url.openStream();
-
-                                               File pluginsDirectory = new 
File("plugins");
-                                               if (!pluginsDirectory.exists()) 
{
-                                                       Logger
-                                                                       
.normal(this,
-                                                                               
        "The plugin directory hasn't been found, let's create it");
-                                                       if 
(!pluginsDirectory.mkdir())
-                                                               return null;
-                                               }
-
-                                               File finalFile = new 
File("plugins/" + pluginname
-                                                               + ".jar");
-                                               if (!FileUtil.writeTo(is, 
finalFile)) {
-                                                       Logger.error(this,
-                                                                       "Failed 
to rename the temporary file into "
-                                                                               
        + finalFile);
-                                                       throw new 
PluginNotFoundException(
-                                                                       "Cannot 
write plugin to "
-                                                                               
        + finalFile
-                                                                               
        + " from "
-                                                                               
        + url
-                                                                               
        + " - check for permissions problem and disk full!");
-                                               }
-
-                                               filename = "*@file://"
-                                                               + 
FileUtil.getCanonicalFile(finalFile);
-                                               if (logMINOR)
-                                                       Logger.minor(this, 
"Rewritten to " + filename);
-
-                                       } catch (MalformedURLException mue) {
-                                               Logger.error(this,
-                                                               
"MalformedURLException has occured : " + mue,
-                                                               mue);
-                                               return null;
-                                       } catch (FileNotFoundException e) {
-                                               Logger.error(this,
-                                                               
"FileNotFoundException has occured : " + e, e);
-                                               return null;
-                                       } catch (IOException ioe) {
-                                               System.out.println("Caught :" + 
ioe.getMessage());
-                                               ioe.printStackTrace();
-                                               return null;
-                                       } finally {
-                                               try {
-                                                       if (is != null)
-                                                               is.close();
-                                               } catch (IOException ioe) {
-                                               }
-                                       }
-                                       if (filename == null)
-                                               return null;
-                               }
-
                                BufferedReader in = null;
                                InputStream is = null;
                                if ((filename.indexOf("@") >= 0)) {
@@ -411,7 +332,7 @@
                                                String[] parts = 
filename.split("@");
                                                if (parts.length != 2) {
                                                        throw new 
PluginNotFoundException(
-                                                                       "Could 
not split at \"@\".");
+                                                       "Could not split at 
\"@\".");
                                                }
                                                realClass = parts[0];
                                                realURL = parts[1];
@@ -431,8 +352,8 @@
                                                                if (realURL == 
null)
                                                                        throw 
new PluginNotFoundException(
                                                                                
        "Initialization error: "
-                                                                               
                        + url
-                                                                               
                        + " isn't a plugin loading url!");
+                                                                               
        + url
+                                                                               
        + " isn't a plugin loading url!");
                                                                realURL = 
realURL.trim();
                                                                if (logMINOR)
                                                                        
Logger.minor(this, "Loaded new URL: "
@@ -458,7 +379,7 @@

                                                        URL url = new 
URL("jar:" + realURL + "!/");
                                                        JarURLConnection 
jarConnection = (JarURLConnection) url
-                                                                       
.openConnection();
+                                                       .openConnection();
                                                        // Java seems to cache 
even file: urls...
                                                        
jarConnection.setUseCaches(false);
                                                        JarFile jf = 
jarConnection.getJarFile();
@@ -487,8 +408,8 @@
                                                                        if 
(logMINOR)
                                                                                
Logger.minor(this,
                                                                                
                "Found plugin main class "
-                                                                               
                                + realClass
-                                                                               
                                + " from manifest");
+                                                                               
                + realClass
+                                                                               
                + " from manifest");
                                                                }
                                                        }
                                                        // 
System.err.println("Real classname: " +
@@ -517,7 +438,6 @@

                                if (cls == null)
                                        throw new 
PluginNotFoundException("Unknown error");
-
                        } catch (Exception e) {
                                Logger.normal(this, "Failed to load plugin " + 
filename + " : "
                                                + e, e);
@@ -548,7 +468,7 @@

                return (FredPlugin) o;
        }
-       
+
        Ticker getTicker() {
                return node.getTicker();
        }


Reply via email to