/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.bootstrap.api.as.server;
import org.jboss.bootstrap.api.as.config.JBossASServerConfig;
import org.jboss.bootstrap.api.as.config.JBossASServerConfigFactory;
import org.jboss.bootstrap.api.factory.ServerFactory;
import org.jboss.bootstrap.api.server.Server;
/**
* JBossASServerFactory
*
* Factory of {@link JBossASServer} implementations
*
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
* @version $Revision: $
*/
public class JBossASServerFactory
{
//-------------------------------------------------------------------------------------||
// Class Members ----------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* The FQN of the default server implementation class to create
*/
public static final String DEFAULT_AS_SERVER_IMPL_CLASS_NAME = "org.jboss.bootstrap.impl.as.server.JBossASServerImpl";
//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* Constructor, should not be used
*/
private JBossASServerFactory()
{
// No external access
}
//-------------------------------------------------------------------------------------||
// Factory Methods --------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* Creates a JBossASServer from the default implementation class name
* {@link JBossASServerFactory#DEFAULT_AS_SERVER_IMPL_CLASS_NAME}, using the
* Thread Context ClassLoader. The returned server will additionally be created using
* the TCCL.
*
* @return The newly-created Server
*/
public static JBossASServer createServer()
{
return createServer(SecurityActions.getTccl());
}
/**
* Creates a JBossASServer from the default implementation class name
* {@link JBossASServerFactory#DEFAULT_AS_SERVER_IMPL_CLASS_NAME}, using the
* specified ClassLoader. The returned server will additionally be created using
* the ClassLoader denoted.
*
* @throws IllegalArgumentException If the ClassLoader is null
* @return The newly-created Server
*/
public static JBossASServer createServer(final ClassLoader cl) throws IllegalArgumentException
{
try
{
return createServer(DEFAULT_AS_SERVER_IMPL_CLASS_NAME, cl);
}
catch (Exception e)
{
// An exception here is likely our fault, so throw a RuntimeException
throw new RuntimeException("Error in creating the Server", e);
}
}
/**
* Creates a JBossASServer from the specified implementation class name
* using the specified ClassLoader. The returned server will additionally
* be created using the ClassLoader denoted.
*
* @param implClassName The FQN of the class used to create the new server instance
* @param cl The ClassLoader used to create the new server instance
* @throws IllegalArgumentException If the ClassLoader or server implementation class is null
* @throws Exception If there was an error in creating the Server
* @return The newly-created Server
*/
public static JBossASServer createServer(final String implClassName, final ClassLoader cl)
throws IllegalArgumentException, Exception
{
return createServer(implClassName, cl, JBossASServer.class);
}
/**
* Creates a JBossASServer from the specified implementation class name
* using the specified ClassLoader. The returned server will additionally
* be created using the ClassLoader denoted.
*
* @param implClassName The FQN of the class used to create the new server instance
* @param cl The ClassLoader used to create the new server instance
* @param expectedType The expected runtime type of the new server instance
* @param <T> Expected runtime type of the newly-created server instance
* @throws IllegalArgumentException If the ClassLoader or server implementation class is null
* @throws Exception If there was an error in creating the Server
* @return The newly-created Server
*/
public static <T extends JBossASBasedServer<T, JBossASServerConfig>> T createServer(final String implClassName,
final ClassLoader cl, final Class<T> expectedType) throws IllegalArgumentException, InstantiationException
{
// Precondition check
if (cl == null)
{
throw new IllegalArgumentException(ClassLoader.class.getSimpleName() + " is required.");
}
if (implClassName == null || implClassName.length() == 0)
{
throw new IllegalArgumentException("Implementation class name must be specified");
}
// Get the server
final Server<?, ?> server = ServerFactory.createServer(implClassName, cl);
// Cast
final T asServer;
try
{
asServer = expectedType.cast(server);
}
catch (final ClassCastException cce)
{
// Catch, explain giving some context, throw new
throw new ClassCastException("Specified server implementation class, " + implClassName
+ " must be assignable to " + expectedType.getName());
}
// Set a default config
final JBossASServerConfig config = JBossASServerConfigFactory.createServerConfig(cl);
asServer.setConfiguration(config);
// Return
return asServer;
}
}