//----------------------------BEGIN LICENSE----------------------------
/*
* Willow : the Open Source WorkFlow Project
* Distributable under GNU LGPL license by gun.org
*
* Copyright (C) 2004-2010 huihoo.org
* Copyright (C) 2004-2010 ZosaTapo <dertyang@hotmail.com>
*
* ====================================================================
* Project Homepage : http://www.huihoo.org/willow
* Source Forge : http://sourceforge.net/projects/huihoo
* Mailing list : willow@lists.sourceforge.net
*/
//----------------------------END LICENSE-----------------------------
package org.huihoo.workflow.client.serial.factory;
import java.util.Hashtable;
import javax.naming.NamingException;
import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.client.ClientNotFoundException;
import org.huihoo.workflow.client.SerialClient;
import org.huihoo.workflow.client.ObjectClient;
import org.huihoo.workflow.client.object.factory.VersionHelper;
/**
* @author reic
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class SerialClientManager
{
/*
* Disallow anyone from creating one of these.
* Made package private so that DirectoryManager can subclass.
*/
SerialClientManager()
{
}
// should be protected and package private
static final VersionHelper helper = VersionHelper.getVersionHelper();
//------------ Initial LiveClient Factory Stuff
private static SerialClientFactoryBuilder initctx_factory_builder = null;
/**
* Use this method for accessing initctx_factory_builder while
* inside an unsynchronized method.
*/
private static synchronized SerialClientFactoryBuilder getInitialWfiClientFactoryBuilder()
{
return initctx_factory_builder;
}
/**
* Creates an initial context using the specified environment
* properties.
*<p>
* If an LiveClientFactoryBuilder has been installed,
* it is used to create the factory for creating the initial context.
* Otherwise, the class specified in the
* <tt>WorkflowClient.INITIAL_CONTEXT_FACTORY</tt> environment property is used.
* Note that an initial wfiClient factory (an object that implements the
* LiveClientFactory interface) must be public and must have a
* public constructor that accepts no arguments.
*
* @param env The possibly null environment properties used when
* creating the context.
* @return A non-null initial context.
* @exception ClientNotFoundException If the
* <tt>WorkflowClient.INITIAL_CONTEXT_FACTORY</tt> property
* is not found or names a nonexistent
* class or a class that cannot be instantiated,
* or if the initial context could not be created for some other
* reason.
* @exception NamingException If some other naming exception was encountered.
*/
public static SerialClient getInitialWfiClient(Hashtable env)
throws WorkflowException, NamingException
{
SerialClientFactory factory;
SerialClientFactoryBuilder builder = getInitialWfiClientFactoryBuilder();
if (builder == null)
{
// No factory installed, use property
// Get initial wfiClient factory class name
String className =
env != null ? (String) env.get(ObjectClient.INITIAL_CLIENT_FACTORY) : null;
if (className == null)
{
ClientNotFoundException ne =
new ClientNotFoundException(
"Need to specify class name in environment or system "
+ "property, or as an applet parameter, or in an "
+ "application resource file: "
+ ObjectClient.INITIAL_CLIENT_FACTORY);
throw ne;
}
try
{
factory = (SerialClientFactory) helper.loadClass(className).newInstance();
}
catch (Exception e)
{
ClientNotFoundException ne =
new ClientNotFoundException("Cannot instantiate class: " + className);
ne.initCause(e);
throw ne;
}
}
else
{
factory = builder.createInitialWfiClientFactory(env);
}
return factory.getInitialWfiClient(env);
}
/**
* Sets the LiveClientFactory builder to be builder.
*
*<p>
* The builder can only be installed if the executing thread is allowed by
* the security manager to do so. Once installed, the builder cannot
* be replaced.
* @param builder The initial wfiClient factory builder to install. If null,
* no builder is set.
* @exception SecurityException builder cannot be installed for security
* reasons.
* @exception NamingException builder cannot be installed for
* a non-security-related reason.
* @exception IllegalStateException If a builder was previous installed.
*/
public static synchronized void setInitialWfiClientFactoryBuilder(SerialClientFactoryBuilder builder)
throws NamingException
{
if (initctx_factory_builder != null)
{
throw new IllegalStateException("LiveClientFactoryBuilder already set");
}
SecurityManager security = System.getSecurityManager();
if (security != null)
{
security.checkSetFactory();
}
initctx_factory_builder = builder;
}
/**
* Determines whether an initial wfiClient factory builder has
* been set.
* @return true if an initial wfiClient factory builder has
* been set; false otherwise.
*/
public static boolean hasInitialWfiClientFactoryBuilder()
{
return (getInitialWfiClientFactoryBuilder() != null);
}
}