moved GlobalSite up to the main "svc" package; added AcceptGlobalSite interface

and new background-task system to GlobalSiteImpl
This commit is contained in:
Eric J. Bowersox 2002-08-20 05:15:57 +00:00
parent e40c1e6a58
commit c6add9371b
15 changed files with 200 additions and 13 deletions

View File

@ -26,7 +26,7 @@ import com.silverwrist.venice.db.*;
import com.silverwrist.venice.except.*; import com.silverwrist.venice.except.*;
import com.silverwrist.venice.security.AuditRecord; import com.silverwrist.venice.security.AuditRecord;
import com.silverwrist.venice.security.Role; import com.silverwrist.venice.security.Role;
import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.svc.GlobalSite;
class AdminOperationsImpl implements AdminOperations class AdminOperationsImpl implements AdminOperations
{ {

View File

@ -30,7 +30,6 @@ import com.silverwrist.venice.except.*;
import com.silverwrist.venice.security.AuditRecord; import com.silverwrist.venice.security.AuditRecord;
import com.silverwrist.venice.security.SecurityMonitor; import com.silverwrist.venice.security.SecurityMonitor;
import com.silverwrist.venice.svc.*; import com.silverwrist.venice.svc.*;
import com.silverwrist.venice.svc.internal.GlobalSite;
import com.silverwrist.venice.util.PropertyProvider; import com.silverwrist.venice.util.PropertyProvider;
class CommunityCoreData implements CommunityData, PropertyProvider class CommunityCoreData implements CommunityData, PropertyProvider

View File

@ -27,7 +27,7 @@ import com.silverwrist.venice.core.internals.*;
import com.silverwrist.venice.db.*; import com.silverwrist.venice.db.*;
import com.silverwrist.venice.except.*; import com.silverwrist.venice.except.*;
import com.silverwrist.venice.security.AuditRecord; import com.silverwrist.venice.security.AuditRecord;
import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.svc.GlobalSite;
class ConferenceCoreData implements ConferenceData class ConferenceCoreData implements ConferenceData
{ {

View File

@ -19,7 +19,7 @@ package com.silverwrist.venice.core.impl;
import com.silverwrist.util.*; import com.silverwrist.util.*;
import com.silverwrist.venice.except.*; 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.MailSend;
import com.silverwrist.venice.util.PropertyProvider; import com.silverwrist.venice.util.PropertyProvider;

View File

@ -27,8 +27,8 @@ import com.silverwrist.venice.core.internals.*;
import com.silverwrist.venice.db.*; import com.silverwrist.venice.db.*;
import com.silverwrist.venice.except.*; import com.silverwrist.venice.except.*;
import com.silverwrist.venice.security.*; import com.silverwrist.venice.security.*;
import com.silverwrist.venice.svc.GlobalSite;
import com.silverwrist.venice.svc.ServiceControl; import com.silverwrist.venice.svc.ServiceControl;
import com.silverwrist.venice.svc.internal.GlobalSite;
import com.silverwrist.venice.util.*; import com.silverwrist.venice.util.*;
class UserContextImpl implements UserContext, ServiceProvider, PropertyProvider class UserContextImpl implements UserContext, ServiceProvider, PropertyProvider

View File

@ -31,7 +31,6 @@ import com.silverwrist.venice.except.*;
import com.silverwrist.venice.htmlcheck.*; import com.silverwrist.venice.htmlcheck.*;
import com.silverwrist.venice.security.*; import com.silverwrist.venice.security.*;
import com.silverwrist.venice.svc.*; import com.silverwrist.venice.svc.*;
import com.silverwrist.venice.svc.internal.GlobalSite;
import com.silverwrist.venice.util.MailSend; import com.silverwrist.venice.util.MailSend;
import com.silverwrist.venice.util.ServiceProvider; import com.silverwrist.venice.util.ServiceProvider;
import com.silverwrist.venice.util.XMLLoader; import com.silverwrist.venice.util.XMLLoader;

View File

@ -22,7 +22,7 @@ import javax.mail.*;
import javax.mail.internet.*; import javax.mail.internet.*;
import com.silverwrist.util.*; import com.silverwrist.util.*;
import com.silverwrist.venice.except.*; 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.MailSend;
class BaseMailSend implements MailSend class BaseMailSend implements MailSend

View File

@ -26,8 +26,8 @@ import com.silverwrist.venice.db.*;
import com.silverwrist.venice.except.AccessError; import com.silverwrist.venice.except.AccessError;
import com.silverwrist.venice.htmlcheck.HTMLChecker; import com.silverwrist.venice.htmlcheck.HTMLChecker;
import com.silverwrist.venice.security.*; import com.silverwrist.venice.security.*;
import com.silverwrist.venice.svc.GlobalSite;
import com.silverwrist.venice.svc.ServiceControl; import com.silverwrist.venice.svc.ServiceControl;
import com.silverwrist.venice.svc.internal.GlobalSite;
import com.silverwrist.venice.util.MailSend; import com.silverwrist.venice.util.MailSend;
public class EnvEngine public class EnvEngine

View File

@ -30,13 +30,56 @@ import com.silverwrist.venice.htmlcheck.dict.*;
import com.silverwrist.venice.htmlcheck.filters.*; import com.silverwrist.venice.htmlcheck.filters.*;
import com.silverwrist.venice.security.*; import com.silverwrist.venice.security.*;
import com.silverwrist.venice.svc.*; import com.silverwrist.venice.svc.*;
import com.silverwrist.venice.svc.internal.GlobalSite;
import com.silverwrist.venice.util.ServiceProvider; import com.silverwrist.venice.util.ServiceProvider;
import com.silverwrist.venice.util.MailSend; import com.silverwrist.venice.util.MailSend;
import com.silverwrist.venice.util.XMLLoader; import com.silverwrist.venice.util.XMLLoader;
public class GlobalSiteImpl implements GlobalSite 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 * Static data members
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
@ -44,6 +87,8 @@ public class GlobalSiteImpl implements GlobalSite
private static Category logger = Category.getInstance(GlobalSiteImpl.class); private static Category logger = Category.getInstance(GlobalSiteImpl.class);
private static final int NUM_TASK_THREADS = 4; // TODO: make this configurable
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
* Attributes * Attributes
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
@ -56,6 +101,10 @@ public class GlobalSiteImpl implements GlobalSite
private StockMessages stock_messages = null; // stock messages holder private StockMessages stock_messages = null; // stock messages holder
private HashMap html_checkers = new HashMap(); // HTML checkers private HashMap html_checkers = new HashMap(); // HTML checkers
private SecurityMonitorEnvironment sm_env; // security monitor environment 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 * Constructor
@ -155,6 +204,20 @@ public class GlobalSiteImpl implements GlobalSite
} // end for } // end for
// Initialize the task queues.
task_queues = new LinkedList[TASK_PRIO_MAX + 1];
for (i=0; i<task_queues.length; i++)
task_queues[i] = new LinkedList();
// Initialize the threads.
task_threads = new Thread[NUM_TASK_THREADS];
for (i=0; i<task_threads.length; i++)
{ // create and kick off the background threads
task_threads[i] = new BackgroundThread(i);
task_threads[i].start();
} // end for
} // end class GlobalSiteImpl } // end class GlobalSiteImpl
/*-------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------
@ -374,6 +437,48 @@ public class GlobalSiteImpl implements GlobalSite
} // end createHTMLChecker } // end createHTMLChecker
private final Runnable getNextTaskToRun(boolean quitting)
{
int i;
synchronized (task_semaphore)
{ // acquire the task semaphore
if (quitting)
{ // just scan the queues and return the first available task
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());
} // 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 * Implementations from interface ServiceProvider
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
@ -461,6 +566,35 @@ public class GlobalSiteImpl implements GlobalSite
} // end getStockMessage } // end getStockMessage
public void queueTask(Runnable task, int priority)
{
if ((priority<TASK_PRIO_MIN) || (priority>TASK_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 * External operations
*-------------------------------------------------------------------------------- *--------------------------------------------------------------------------------
@ -468,6 +602,28 @@ public class GlobalSiteImpl implements GlobalSite
public void shutdown() 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<task_threads.length)
{ // wait for all background threads to finally die
try
{ // wait for a thread to die
task_threads[i].join();
i++;
} // end try
catch (InterruptedException e)
{ // do nothing, but try this join again
} // end catch
} // end while
engine_svc = null; engine_svc = null;
if (datapool!=null) if (datapool!=null)
datapool.closeAllConnections(); datapool.closeAllConnections();

View File

@ -23,7 +23,7 @@ import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices; import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData; import com.silverwrist.venice.htmlcheck.MarkupData;
import com.silverwrist.venice.except.ValidationException; import com.silverwrist.venice.except.ValidationException;
import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.svc.GlobalSite;
import com.silverwrist.venice.util.IDUtils; import com.silverwrist.venice.util.IDUtils;
public class PostLinkRewriter implements Rewriter public class PostLinkRewriter implements Rewriter

View File

@ -22,7 +22,7 @@ import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.htmlcheck.Rewriter; import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices; import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData; import com.silverwrist.venice.htmlcheck.MarkupData;
import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.svc.GlobalSite;
import com.silverwrist.venice.util.IDUtils; import com.silverwrist.venice.util.IDUtils;
public class UserNameRewriter implements Rewriter public class UserNameRewriter implements Rewriter

View File

@ -24,7 +24,7 @@ import com.silverwrist.venice.db.SQLUtil;
import com.silverwrist.venice.core.AuditData; import com.silverwrist.venice.core.AuditData;
import com.silverwrist.venice.except.DataException; import com.silverwrist.venice.except.DataException;
import com.silverwrist.venice.except.InternalStateError; import com.silverwrist.venice.except.InternalStateError;
import com.silverwrist.venice.svc.internal.GlobalSite; import com.silverwrist.venice.svc.GlobalSite;
public class AuditRecord implements AuditData public class AuditRecord implements AuditData
{ {

View File

@ -0,0 +1,24 @@
/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* 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 <erbo@silcom.com>,
* 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

View File

@ -15,7 +15,7 @@
* *
* Contributor(s): * Contributor(s):
*/ */
package com.silverwrist.venice.svc.internal; package com.silverwrist.venice.svc;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@ -23,8 +23,16 @@ import com.silverwrist.venice.util.ServiceProvider;
public interface GlobalSite extends 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 Connection getConnection(String db_selector) throws SQLException;
public abstract String getStockMessage(String key); public abstract String getStockMessage(String key);
public abstract void queueTask(Runnable task, int priority);
public abstract void queueTask(Runnable task);
} // end interface GlobalSite } // end interface GlobalSite

View File

@ -21,6 +21,7 @@ import org.w3c.dom.Element;
import com.silverwrist.venice.core.ServiceToken; import com.silverwrist.venice.core.ServiceToken;
import com.silverwrist.venice.except.ConfigException; import com.silverwrist.venice.except.ConfigException;
import com.silverwrist.venice.security.SecurityMonitor; import com.silverwrist.venice.security.SecurityMonitor;
import com.silverwrist.venice.svc.GlobalSite;
public interface CommServiceInternal extends ServiceInternal public interface CommServiceInternal extends ServiceInternal
{ {