/*********************************************************************
* ServerHandlerMapping.java
* created on 26.05.2007 by netseeker
* $Id: ServerHandlerMapping.java,v 1.3 2007/11/17 10:56:59 netseeker Exp $
* $Log: ServerHandlerMapping.java,v $
* Revision 1.3 2007/11/17 10:56:59 netseeker
* *** empty log message ***
*
* Revision 1.2 2007/05/28 12:42:37 netseeker
* *** empty log message ***
*
* Revision 1.1 2007/05/27 22:13:08 netseeker
* *** empty log message ***
*
*
* ====================================================================
*
* Copyright 2005-2006 netseeker aka Michael Manske
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.handler;
import java.util.HashMap;
import java.util.Map;
import de.netseeker.ejoe.request.IRequest;
/**
* When using a ServerHandlerMapping with EJServer it is possible to use different
* ServerHandlers depending on the type of the used requests.
* @author netseeker
* @since 0.3.9.3
* @see de.netseeker.ejoe.request.IRequest
*/
public class ServerHandlerMapping implements ServerHandler
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Map _handlerMapping = new HashMap();
private ServerHandler _defaultHandler = null;
/*
* (non-Javadoc)
*
* @see de.netseeker.ejoe.handler.ServerHandler#handle(java.lang.Object)
*/
public Object handle( Object obj ) throws Exception
{
if ( obj instanceof IRequest )
{
IRequest request = (IRequest) obj;
String ident = request.getUniqueName();
ServerHandler handler = (ServerHandler) (_handlerMapping.containsKey( ident ) ? _handlerMapping.get( ident )
: _defaultHandler);
if ( handler == null )
{
throw new NullPointerException( "No Handler found for Request Type and no default Handler registered!" );
}
return handler.handle( request );
}
if ( _defaultHandler != null )
{
return _defaultHandler.handle( obj );
}
else
{
throw new NullPointerException( "No default handler registered to process the request!" );
}
}
/**
* @param uniqueName
* @param serverHandler
*/
public void addHandlerMapping( String uniqueName, ServerHandler serverHandler )
{
_handlerMapping.put( uniqueName, serverHandler );
}
/**
* @param uniqueName
* @return
*/
public boolean removeHandlerMapping( String uniqueName )
{
return _handlerMapping.remove( uniqueName ) != null;
}
/**
* @param serverHandler
*/
public void setDefaultHandler( ServerHandler serverHandler )
{
_defaultHandler = serverHandler;
}
}