From c6add9371b6ea0938fcd29c7a4ea5814cc152c15 Mon Sep 17 00:00:00 2001 From: "Eric J. Bowersox" Date: Tue, 20 Aug 2002 05:15:57 +0000 Subject: [PATCH] moved GlobalSite up to the main "svc" package; added AcceptGlobalSite interface and new background-task system to GlobalSiteImpl --- .../venice/core/impl/AdminOperationsImpl.java | 2 +- .../venice/core/impl/CommunityCoreData.java | 1 - .../venice/core/impl/ConferenceCoreData.java | 2 +- .../venice/core/impl/PersonalMailSend.java | 2 +- .../venice/core/impl/UserContextImpl.java | 2 +- .../venice/core/impl/VeniceEngineImpl.java | 1 - .../venice/core/internals/BaseMailSend.java | 2 +- .../venice/core/internals/EnvEngine.java | 2 +- .../venice/core/internals/GlobalSiteImpl.java | 158 +++++++++++++++++- .../venice/db/PostLinkRewriter.java | 2 +- .../venice/db/UserNameRewriter.java | 2 +- .../venice/security/AuditRecord.java | 2 +- .../venice/svc/AcceptGlobalSite.java | 24 +++ .../svc/{internals => }/GlobalSite.java | 10 +- .../svc/internals/CommServiceInternal.java | 1 + 15 files changed, 200 insertions(+), 13 deletions(-) create mode 100644 src/com/silverwrist/venice/svc/AcceptGlobalSite.java rename src/com/silverwrist/venice/svc/{internals => }/GlobalSite.java (80%) diff --git a/src/com/silverwrist/venice/core/impl/AdminOperationsImpl.java b/src/com/silverwrist/venice/core/impl/AdminOperationsImpl.java index bf13577..e8f882f 100644 --- a/src/com/silverwrist/venice/core/impl/AdminOperationsImpl.java +++ b/src/com/silverwrist/venice/core/impl/AdminOperationsImpl.java @@ -26,7 +26,7 @@ import com.silverwrist.venice.db.*; import com.silverwrist.venice.except.*; import com.silverwrist.venice.security.AuditRecord; import com.silverwrist.venice.security.Role; -import com.silverwrist.venice.svc.internal.GlobalSite; +import com.silverwrist.venice.svc.GlobalSite; class AdminOperationsImpl implements AdminOperations { diff --git a/src/com/silverwrist/venice/core/impl/CommunityCoreData.java b/src/com/silverwrist/venice/core/impl/CommunityCoreData.java index d889deb..275a1b8 100644 --- a/src/com/silverwrist/venice/core/impl/CommunityCoreData.java +++ b/src/com/silverwrist/venice/core/impl/CommunityCoreData.java @@ -30,7 +30,6 @@ import com.silverwrist.venice.except.*; import com.silverwrist.venice.security.AuditRecord; import com.silverwrist.venice.security.SecurityMonitor; import com.silverwrist.venice.svc.*; -import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.util.PropertyProvider; class CommunityCoreData implements CommunityData, PropertyProvider diff --git a/src/com/silverwrist/venice/core/impl/ConferenceCoreData.java b/src/com/silverwrist/venice/core/impl/ConferenceCoreData.java index b4cf9aa..3445e5a 100644 --- a/src/com/silverwrist/venice/core/impl/ConferenceCoreData.java +++ b/src/com/silverwrist/venice/core/impl/ConferenceCoreData.java @@ -27,7 +27,7 @@ import com.silverwrist.venice.core.internals.*; import com.silverwrist.venice.db.*; import com.silverwrist.venice.except.*; import com.silverwrist.venice.security.AuditRecord; -import com.silverwrist.venice.svc.internal.GlobalSite; +import com.silverwrist.venice.svc.GlobalSite; class ConferenceCoreData implements ConferenceData { diff --git a/src/com/silverwrist/venice/core/impl/PersonalMailSend.java b/src/com/silverwrist/venice/core/impl/PersonalMailSend.java index 85ef2c6..edfc179 100644 --- a/src/com/silverwrist/venice/core/impl/PersonalMailSend.java +++ b/src/com/silverwrist/venice/core/impl/PersonalMailSend.java @@ -19,7 +19,7 @@ package com.silverwrist.venice.core.impl; import com.silverwrist.util.*; import com.silverwrist.venice.except.*; -import com.silverwrist.venice.svc.internal.GlobalSite; +import com.silverwrist.venice.svc.GlobalSite; import com.silverwrist.venice.util.MailSend; import com.silverwrist.venice.util.PropertyProvider; diff --git a/src/com/silverwrist/venice/core/impl/UserContextImpl.java b/src/com/silverwrist/venice/core/impl/UserContextImpl.java index 7f7b102..06e2466 100644 --- a/src/com/silverwrist/venice/core/impl/UserContextImpl.java +++ b/src/com/silverwrist/venice/core/impl/UserContextImpl.java @@ -27,8 +27,8 @@ import com.silverwrist.venice.core.internals.*; import com.silverwrist.venice.db.*; import com.silverwrist.venice.except.*; import com.silverwrist.venice.security.*; +import com.silverwrist.venice.svc.GlobalSite; import com.silverwrist.venice.svc.ServiceControl; -import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.util.*; class UserContextImpl implements UserContext, ServiceProvider, PropertyProvider diff --git a/src/com/silverwrist/venice/core/impl/VeniceEngineImpl.java b/src/com/silverwrist/venice/core/impl/VeniceEngineImpl.java index 63a8d51..b56fbdc 100644 --- a/src/com/silverwrist/venice/core/impl/VeniceEngineImpl.java +++ b/src/com/silverwrist/venice/core/impl/VeniceEngineImpl.java @@ -31,7 +31,6 @@ import com.silverwrist.venice.except.*; import com.silverwrist.venice.htmlcheck.*; import com.silverwrist.venice.security.*; import com.silverwrist.venice.svc.*; -import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.util.MailSend; import com.silverwrist.venice.util.ServiceProvider; import com.silverwrist.venice.util.XMLLoader; diff --git a/src/com/silverwrist/venice/core/internals/BaseMailSend.java b/src/com/silverwrist/venice/core/internals/BaseMailSend.java index a3a1c3c..92a5374 100644 --- a/src/com/silverwrist/venice/core/internals/BaseMailSend.java +++ b/src/com/silverwrist/venice/core/internals/BaseMailSend.java @@ -22,7 +22,7 @@ import javax.mail.*; import javax.mail.internet.*; import com.silverwrist.util.*; import com.silverwrist.venice.except.*; -import com.silverwrist.venice.svc.internal.GlobalSite; +import com.silverwrist.venice.svc.GlobalSite; import com.silverwrist.venice.util.MailSend; class BaseMailSend implements MailSend diff --git a/src/com/silverwrist/venice/core/internals/EnvEngine.java b/src/com/silverwrist/venice/core/internals/EnvEngine.java index 8e08021..c79db1b 100644 --- a/src/com/silverwrist/venice/core/internals/EnvEngine.java +++ b/src/com/silverwrist/venice/core/internals/EnvEngine.java @@ -26,8 +26,8 @@ import com.silverwrist.venice.db.*; import com.silverwrist.venice.except.AccessError; import com.silverwrist.venice.htmlcheck.HTMLChecker; import com.silverwrist.venice.security.*; +import com.silverwrist.venice.svc.GlobalSite; import com.silverwrist.venice.svc.ServiceControl; -import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.util.MailSend; public class EnvEngine diff --git a/src/com/silverwrist/venice/core/internals/GlobalSiteImpl.java b/src/com/silverwrist/venice/core/internals/GlobalSiteImpl.java index 543439c..f43a5fb 100644 --- a/src/com/silverwrist/venice/core/internals/GlobalSiteImpl.java +++ b/src/com/silverwrist/venice/core/internals/GlobalSiteImpl.java @@ -30,13 +30,56 @@ import com.silverwrist.venice.htmlcheck.dict.*; import com.silverwrist.venice.htmlcheck.filters.*; import com.silverwrist.venice.security.*; import com.silverwrist.venice.svc.*; -import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.util.ServiceProvider; import com.silverwrist.venice.util.MailSend; import com.silverwrist.venice.util.XMLLoader; public class GlobalSiteImpl implements GlobalSite { + /*-------------------------------------------------------------------------------- + * Internal background thread class + *-------------------------------------------------------------------------------- + */ + + class BackgroundThread extends Thread + { + BackgroundThread(int index) + { + super("GlobalSide_bkgd_" + String.valueOf(index + 1)); + + } // end constructor + + private void doRunLoop(boolean quitting) + { + Runnable task = getNextTaskToRun(quitting); + while (task!=null) + { // null task indicates we are shutting down + try + { // run the background task + task.run(); + + } // end try + catch (Exception e) + { // log the exception and continue + logger.error("Background thread caught exception",e); + + } // end catch + + task = getNextTaskToRun(quitting); + + } // end while + + } // end doRunLoop + + public void run() + { + doRunLoop(false); + doRunLoop(true); + + } // end run + + } // end class BackgroundThread + /*-------------------------------------------------------------------------------- * Static data members *-------------------------------------------------------------------------------- @@ -44,6 +87,8 @@ public class GlobalSiteImpl implements GlobalSite private static Category logger = Category.getInstance(GlobalSiteImpl.class); + private static final int NUM_TASK_THREADS = 4; // TODO: make this configurable + /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- @@ -56,6 +101,10 @@ public class GlobalSiteImpl implements GlobalSite private StockMessages stock_messages = null; // stock messages holder private HashMap html_checkers = new HashMap(); // HTML checkers private SecurityMonitorEnvironment sm_env; // security monitor environment + private volatile boolean task_running = true; // running tasks flag + private Object task_semaphore = new Object(); // semaphore used for tasks + private LinkedList[] task_queues; // the task queues + private Thread[] task_threads; // the task threads /*-------------------------------------------------------------------------------- * Constructor @@ -155,6 +204,20 @@ public class GlobalSiteImpl implements GlobalSite } // end for + // Initialize the task queues. + task_queues = new LinkedList[TASK_PRIO_MAX + 1]; + for (i=0; i=TASK_PRIO_MIN; i--) + if (!(task_queues[i].isEmpty())) // select a task from highest possible priority queue + return (Runnable)(task_queues[i].removeFirst()); + + } // end if + else + { // work it normally + while (task_running) + { // look for a task we can do + for (i=TASK_PRIO_MAX; i>=TASK_PRIO_MIN; i--) + if (!(task_queues[i].isEmpty())) // select a task from highest possible priority queue + return (Runnable)(task_queues[i].removeFirst()); + + try + { // wait for something to happen + task_semaphore.wait(); + + } // end try + catch (InterruptedException e) + { // do nothing + } // end catch + + // then go back around and look for a task + + } // end while + + } // end else + + } // end synchronized block + + return null; // if get here, task_running is false and we are quitting + + } // end getNextTaskToRun + /*-------------------------------------------------------------------------------- * Implementations from interface ServiceProvider *-------------------------------------------------------------------------------- @@ -461,6 +566,35 @@ public class GlobalSiteImpl implements GlobalSite } // end getStockMessage + public void queueTask(Runnable task, int priority) + { + if ((priorityTASK_PRIO_MAX)) + throw new IllegalArgumentException("task priority out of range"); + if (task==null) + throw new NullPointerException("task null"); + if (task instanceof AcceptGlobalSite) + ((AcceptGlobalSite)task).setGlobalSite(this); + + synchronized (task_semaphore) + { // make sure we are running + // If the running flag is off, only the background tasks themselves may queue new tasks. + if (task_running || Thread.currentThread().getName().startsWith("GlobalSite_bkgd")); + { // add the task to a queue and notify a thread to take it + task_queues[priority].addLast(task); + task_semaphore.notify(); + + } // end if + + } // end synchronized block + + } // end queueTask + + public void queueTask(Runnable task) + { + this.queueTask(task,TASK_PRIO_NORMAL); + + } // end queueTask + /*-------------------------------------------------------------------------------- * External operations *-------------------------------------------------------------------------------- @@ -468,6 +602,28 @@ public class GlobalSiteImpl implements GlobalSite public void shutdown() { + synchronized (task_semaphore) + { // notify the task threads that we're quitting + task_running = false; + task_semaphore.notifyAll(); + + } // end synchronized block + + int i = 0; + while (i. + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.venice.svc; + +public interface AcceptGlobalSite +{ + public abstract void setGlobalSite(GlobalSite site); + +} // end interface AcceptGlobalSite diff --git a/src/com/silverwrist/venice/svc/internals/GlobalSite.java b/src/com/silverwrist/venice/svc/GlobalSite.java similarity index 80% rename from src/com/silverwrist/venice/svc/internals/GlobalSite.java rename to src/com/silverwrist/venice/svc/GlobalSite.java index 22aa24b..2829d83 100644 --- a/src/com/silverwrist/venice/svc/internals/GlobalSite.java +++ b/src/com/silverwrist/venice/svc/GlobalSite.java @@ -15,7 +15,7 @@ * * Contributor(s): */ -package com.silverwrist.venice.svc.internal; +package com.silverwrist.venice.svc; import java.sql.Connection; import java.sql.SQLException; @@ -23,8 +23,16 @@ import com.silverwrist.venice.util.ServiceProvider; public interface GlobalSite extends ServiceProvider { + public static final int TASK_PRIO_MIN = 0; + public static final int TASK_PRIO_NORMAL = 3; + public static final int TASK_PRIO_MAX = 7; + public abstract Connection getConnection(String db_selector) throws SQLException; public abstract String getStockMessage(String key); + public abstract void queueTask(Runnable task, int priority); + + public abstract void queueTask(Runnable task); + } // end interface GlobalSite diff --git a/src/com/silverwrist/venice/svc/internals/CommServiceInternal.java b/src/com/silverwrist/venice/svc/internals/CommServiceInternal.java index 6a66360..05a6e7d 100644 --- a/src/com/silverwrist/venice/svc/internals/CommServiceInternal.java +++ b/src/com/silverwrist/venice/svc/internals/CommServiceInternal.java @@ -21,6 +21,7 @@ import org.w3c.dom.Element; import com.silverwrist.venice.core.ServiceToken; import com.silverwrist.venice.except.ConfigException; import com.silverwrist.venice.security.SecurityMonitor; +import com.silverwrist.venice.svc.GlobalSite; public interface CommServiceInternal extends ServiceInternal {