# HG changeset patch # User xitiomet # Date 1311136619 14400 # Node ID e167c089f5ac2751f6511c2a023c3086ac12bb8b # Parent c64d895c3331457482cb15d667693b51fc227358 Fixed some errors diff -r c64d895c3331457482cb15d667693b51fc227358 -r e167c089f5ac2751f6511c2a023c3086ac12bb8b Makefile --- a/Makefile Tue May 10 16:20:15 2011 -0400 +++ b/Makefile Wed Jul 20 00:36:59 2011 -0400 @@ -73,7 +73,7 @@ build/org/openstatic/util/JTextAreaOutputStream.class: src/org/openstatic/util/JTextAreaOutputStream.java $(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $< -build/org/openstatic/util/LiquidLogger.class: src/org/openstatic/util/LiquidLogger.java +build/org/openstatic/util/PlaceboLogger.class: src/org/openstatic/util/PlaceboLogger.java $(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $< build/org/openstatic/util/JSONUtil.class: src/org/openstatic/util/JSONUtil.java build/org/json/JSONObject.class build/org/json/JSONArray.class @@ -106,7 +106,7 @@ # Main Builds # ------------------------------------------------------------------------------- -placebohttp.jar: build/org/openstatic/http/PlaceboHttpServer.class build/org/openstatic/http/PlaceboSession.class build/org/openstatic/http/HttpRequest.class build/org/openstatic/http/HttpRequestThread.class build/org/openstatic/http/HttpResponse.class build/PlaceboBase.class build/org/openstatic/placebo/ControlPanel.class build/org/openstatic/placebo/CoreServer.class build/org/openstatic/util/JTextAreaOutputStream.class build/org/openstatic/util/JSONUtil.class build/org/openstatic/util/LiquidLogger.class build/org/openstatic/placebo/PlaceboPlugin.class build/org/openstatic/placebo/plugins/Clock.class build/org/openstatic/placebo/plugins/KeyStore.class +placebohttp.jar: build/org/openstatic/http/PlaceboHttpServer.class build/org/openstatic/http/PlaceboSession.class build/org/openstatic/http/HttpRequest.class build/org/openstatic/http/HttpRequestThread.class build/org/openstatic/http/HttpResponse.class build/PlaceboBase.class build/org/openstatic/placebo/ControlPanel.class build/org/openstatic/placebo/CoreServer.class build/org/openstatic/util/JTextAreaOutputStream.class build/org/openstatic/util/JSONUtil.class build/org/openstatic/util/PlaceboLogger.class build/org/openstatic/placebo/PlaceboPlugin.class build/org/openstatic/placebo/plugins/Clock.class build/org/openstatic/placebo/plugins/KeyStore.class $(JAR) -cvmf res/manifest.mf $@ -C build org -C build PlaceboBase.class www LICENSE clean: diff -r c64d895c3331457482cb15d667693b51fc227358 -r e167c089f5ac2751f6511c2a023c3086ac12bb8b src/org/openstatic/placebo/ControlPanel.java --- a/src/org/openstatic/placebo/ControlPanel.java Tue May 10 16:20:15 2011 -0400 +++ b/src/org/openstatic/placebo/ControlPanel.java Wed Jul 20 00:36:59 2011 -0400 @@ -389,7 +389,7 @@ core.setWebRoot(root_field.getText()); core.setDebug(debug_field.isSelected()); core.setShowData(show_data_field.isSelected()); - core.getLiquidLogger().setDisplay(new PrintStream(log_output, true)); + core.getPlaceboLogger().setDisplay(new PrintStream(log_output, true)); if (debug_field.isSelected()) { diff -r c64d895c3331457482cb15d667693b51fc227358 -r e167c089f5ac2751f6511c2a023c3086ac12bb8b src/org/openstatic/placebo/CoreServer.java --- a/src/org/openstatic/placebo/CoreServer.java Tue May 10 16:20:15 2011 -0400 +++ b/src/org/openstatic/placebo/CoreServer.java Wed Jul 20 00:36:59 2011 -0400 @@ -22,7 +22,7 @@ import org.openstatic.http.HttpRequest; import org.openstatic.http.PlaceboSession; import org.openstatic.util.JSONUtil; -import org.openstatic.util.LiquidLogger; +import org.openstatic.util.PlaceboLogger; import java.io.File; import java.io.InputStream; @@ -53,7 +53,7 @@ private Hashtable plugins; private Properties global_settings; private ControlPanel control_panel; - private LiquidLogger myLogger; + private PlaceboLogger myLogger; public CoreServer() { @@ -69,7 +69,7 @@ { this.http_port = 80; this.web_root = "."; - this.myLogger = new LiquidLogger(System.err); + this.myLogger = new PlaceboLogger(System.err); this.keep_running = true; this.debug = false; this.show_data = false; @@ -87,10 +87,20 @@ addPlugin(key, plugin_list.get(key)); } } + + /** This allows plugins to check if it is safe to use swing elements */ + public boolean isSwingOk() + { + if (this.control_panel != null) + return true; + else + return false; + } + /** This allows plugins to add a tab to the control panel screen */ public boolean addPanel(String panel_caption, Component panel) { - if (this.control_panel != null) + if (this.isSwingOk()) { this.control_panel.addPanel(panel_caption, panel); return true; @@ -113,6 +123,7 @@ return new_path; } + /** This is to mount a plugin at a specific path */ public void addPlugin(String path, String class_name) { String pl_path = path; @@ -139,6 +150,7 @@ } } + /** This will unmount a plugin by its path */ public void removePlugin(String path) { this.plugins.get(path).shutdown(); @@ -151,26 +163,31 @@ // Stub for control panel } + /** Returns a Hashtable with the mount points and their plugins */ public Hashtable getPlugins() { return this.plugins; } + /** Returns a Hashtable of session keys and their session objects */ public Hashtable getSessions() { return this.server.getSessions(); } + /** Returns the Properties object containing the global settings */ public Properties getSettings() { return this.global_settings; } - public LiquidLogger getLiquidLogger() + /** Return the logger object for this CoreServer */ + public PlaceboLogger getPlaceboLogger() { return this.myLogger; } + /** Locate the entire plugin path by its start */ private String findPluginPath(String path) { for (Enumeration e = this.plugins.keys(); e.hasMoreElements(); ) @@ -184,6 +201,7 @@ return null; } + /** Locate a plugin object by its mount point */ private PlaceboPlugin findPlugin(String path) { for (Enumeration e = this.plugins.keys(); e.hasMoreElements(); ) @@ -198,21 +216,25 @@ return null; } + /** Set the TCP port to listen on */ public void setPort(int value) { this.http_port = value; } + /** Turn debugging on or off */ public void setDebug(boolean value) { this.debug = value; } + /** Should data recieved in POST requests be shown as well as outbound document/data */ public void setShowData(boolean value) { this.show_data = value; } + /** Set the default path to serve files from, this can also be a zip file or ini file */ public void setWebRoot(String value) { this.web_root = value; @@ -236,6 +258,7 @@ this.interrupt(); } + /** A Function designed to be overridden for extending a CoreServer object */ public void onRequest(HttpRequest request) { // stub diff -r c64d895c3331457482cb15d667693b51fc227358 -r e167c089f5ac2751f6511c2a023c3086ac12bb8b src/org/openstatic/placebo/PlaceboPlugin.java --- a/src/org/openstatic/placebo/PlaceboPlugin.java Tue May 10 16:20:15 2011 -0400 +++ b/src/org/openstatic/placebo/PlaceboPlugin.java Wed Jul 20 00:36:59 2011 -0400 @@ -22,7 +22,12 @@ public interface PlaceboPlugin { + /** This method is called when the plugin is added to a CoreServer, the CoreServer and mount point are passed. core.getSettings() should be called to check for command line variables. */ public boolean startup(CoreServer core, String mount_location); + + /** This method is called every time an http request comes to this plugin's mount point */ public void onRequest(HttpRequest request) throws Exception; + + /** This method is called when the CoreServer shuts down or the plugin is unmounted. All cleanup efforts should be made here */ public boolean shutdown(); } diff -r c64d895c3331457482cb15d667693b51fc227358 -r e167c089f5ac2751f6511c2a023c3086ac12bb8b src/org/openstatic/placebo/plugins/KeyStore.java --- a/src/org/openstatic/placebo/plugins/KeyStore.java Tue May 10 16:20:15 2011 -0400 +++ b/src/org/openstatic/placebo/plugins/KeyStore.java Wed Jul 20 00:36:59 2011 -0400 @@ -38,26 +38,33 @@ { private JSONObject store; private JTextPane json_box; + private boolean swing_mode; public boolean startup(CoreServer core, String mount_location) { System.err.println("KeyStore: Initializing Keystore"); store = new JSONObject(); - json_box = new JTextPane(); - json_box.setContentType("text/html"); - Font font = new Font("Monospaced", Font.BOLD, 12); - json_box.setFont(font); - JScrollPane scroller = new JScrollPane(json_box); - scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); - core.addPanel("Keystore [" + mount_location + "]", scroller); - + if (core.isSwingOk()) + { + json_box = new JTextPane(); + json_box.setContentType("text/html"); + Font font = new Font("Monospaced", Font.BOLD, 12); + json_box.setFont(font); + JScrollPane scroller = new JScrollPane(json_box); + scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + core.addPanel("Keystore [" + mount_location + "]", scroller); + this.swing_mode = true; + } else { + this.swing_mode = false; + } return true; } public void updateBox(JSONObject data) throws Exception { - json_box.setText("" + JSONUtil.json2table(store) + ""); + if (this.swing_mode) + json_box.setText("" + JSONUtil.json2table(store) + ""); } public void onRequest(HttpRequest request) throws Exception diff -r c64d895c3331457482cb15d667693b51fc227358 -r e167c089f5ac2751f6511c2a023c3086ac12bb8b src/org/openstatic/util/LiquidLogger.java --- a/src/org/openstatic/util/LiquidLogger.java Tue May 10 16:20:15 2011 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,119 +0,0 @@ -package org.openstatic.util; - -import java.io.PrintStream; -import java.io.ByteArrayOutputStream; -import java.util.Date; -import java.util.StringTokenizer; -import java.util.Hashtable; -import java.util.Enumeration; -import java.text.SimpleDateFormat; - -public class LiquidLogger -{ - private Hashtable inbound; - private PrintStream outbound; - private PrintStream inbound_ps; - - public LiquidLogger(PrintStream display) - { - this.inbound = new Hashtable(); - this.inbound_ps = this.getPrintStream("debug"); - this.outbound = display; - Thread t = new Thread() - { - public void run() - { - while(true) - { - for (Enumeration keys = LiquidLogger.this.inbound.keys(); keys.hasMoreElements(); ) - { - String caption = keys.nextElement(); - ByteArrayOutputStream baos = LiquidLogger.this.inbound.get(caption); - if (baos.size() > 0 && LiquidLogger.this.outbound != null) - { - StringTokenizer st = new StringTokenizer(baos.toString(), "\n"); - baos.reset(); - while (st.hasMoreTokens()) - { - SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"); - String date_time = df.format(new Date()); - String msg = st.nextToken(); - String temp_caption = caption; - if (msg.startsWith("[") && msg.contains("]")) - { - temp_caption = msg.substring(msg.indexOf("[")+1, msg.indexOf("]")); - msg = msg.substring(msg.indexOf("]")+1); - } - LiquidLogger.this.outbound.println("<" + setSizedString(date_time,23) + "> [ " + setSizedString(temp_caption, 16) + " ] " + msg.replaceAll("\r","")); - } - } - } - try - { - Thread.sleep(10); - } catch (Exception thread_sleep) {} - } - } - }; - t.start(); - } - - public void setDisplay(PrintStream display) - { - this.outbound = display; - } - - public PrintStream getPrintStream() - { - return this.inbound_ps; - } - - public PrintStream getPrintStream(String caption) - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream z = new PrintStream(baos, true); - this.inbound.put(caption, baos); - return z; - } - - public void println(String text) - { - this.inbound_ps.println(text); - } - - public void logln(String text) - { - this.inbound_ps.println(text); - } - - public void logln(String caption, String text) - { - this.inbound_ps.println("[" + caption + "]" + text); - } - - public static String setSizedString(String value, int size) - { - if (value == null) - { - return getPaddingSpace(size); - } else if (value.length() == size) { - return value; - } else if (value.length() > size) { - return value.substring(0, size); - } else if (value.length() < size) { - return value + getPaddingSpace(size - value.length()); - } else { - return null; - } - } - - public static String getPaddingSpace(int value) - { - StringBuffer x = new StringBuffer(""); - for (int n = 0; n < value; n++) - { - x.append(" "); - } - return x.toString(); - } -} diff -r c64d895c3331457482cb15d667693b51fc227358 -r e167c089f5ac2751f6511c2a023c3086ac12bb8b src/org/openstatic/util/PlaceboLogger.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/openstatic/util/PlaceboLogger.java Wed Jul 20 00:36:59 2011 -0400 @@ -0,0 +1,136 @@ +/* + Copyright (C) 2010 Brian Dunigan + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package org.openstatic.util; + +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; +import java.util.Date; +import java.util.StringTokenizer; +import java.util.Hashtable; +import java.util.Enumeration; +import java.text.SimpleDateFormat; + +public class PlaceboLogger +{ + private Hashtable inbound; + private PrintStream outbound; + private PrintStream inbound_ps; + + public PlaceboLogger(PrintStream display) + { + this.inbound = new Hashtable(); + this.inbound_ps = this.getPrintStream("debug"); + this.outbound = display; + Thread t = new Thread() + { + public void run() + { + while(true) + { + for (Enumeration keys = PlaceboLogger.this.inbound.keys(); keys.hasMoreElements(); ) + { + String caption = keys.nextElement(); + ByteArrayOutputStream baos = PlaceboLogger.this.inbound.get(caption); + if (baos.size() > 0 && PlaceboLogger.this.outbound != null) + { + StringTokenizer st = new StringTokenizer(baos.toString(), "\n"); + baos.reset(); + while (st.hasMoreTokens()) + { + SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"); + String date_time = df.format(new Date()); + String msg = st.nextToken(); + String temp_caption = caption; + if (msg.startsWith("[") && msg.contains("]")) + { + temp_caption = msg.substring(msg.indexOf("[")+1, msg.indexOf("]")); + msg = msg.substring(msg.indexOf("]")+1); + } + PlaceboLogger.this.outbound.println("<" + setSizedString(date_time,23) + "> [ " + setSizedString(temp_caption, 16) + " ] " + msg.replaceAll("\r","")); + } + } + } + try + { + Thread.sleep(10); + } catch (Exception thread_sleep) {} + } + } + }; + t.start(); + } + + public void setDisplay(PrintStream display) + { + this.outbound = display; + } + + public PrintStream getPrintStream() + { + return this.inbound_ps; + } + + public PrintStream getPrintStream(String caption) + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream z = new PrintStream(baos, true); + this.inbound.put(caption, baos); + return z; + } + + public void println(String text) + { + this.inbound_ps.println(text); + } + + public void logln(String text) + { + this.inbound_ps.println(text); + } + + public void logln(String caption, String text) + { + this.inbound_ps.println("[" + caption + "]" + text); + } + + public static String setSizedString(String value, int size) + { + if (value == null) + { + return getPaddingSpace(size); + } else if (value.length() == size) { + return value; + } else if (value.length() > size) { + return value.substring(0, size); + } else if (value.length() < size) { + return value + getPaddingSpace(size - value.length()); + } else { + return null; + } + } + + public static String getPaddingSpace(int value) + { + StringBuffer x = new StringBuffer(""); + for (int n = 0; n < value; n++) + { + x.append(" "); + } + return x.toString(); + } +}