View Javadoc

1   /**********************************************************************
2    * ServerHandlerMapping.java
3    * created on 26.05.2007 by netseeker
4    * $Id: ServerHandlerMapping.java,v 1.3 2007/11/17 10:56:59 netseeker Exp $
5    * $Log: ServerHandlerMapping.java,v $
6    * Revision 1.3  2007/11/17 10:56:59  netseeker
7    * *** empty log message ***
8    *
9    * Revision 1.2  2007/05/28 12:42:37  netseeker
10   * *** empty log message ***
11   *
12   * Revision 1.1  2007/05/27 22:13:08  netseeker
13   * *** empty log message ***
14   *
15   *
16   * ====================================================================
17   *
18   *  Copyright 2005-2006 netseeker aka Michael Manske
19   *
20   *  Licensed under the Apache License, Version 2.0 (the "License");
21   *  you may not use this file except in compliance with the License.
22   *  You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   *  Unless required by applicable law or agreed to in writing, software
27   *  distributed under the License is distributed on an "AS IS" BASIS,
28   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   *  See the License for the specific language governing permissions and
30   *  limitations under the License.
31   * ====================================================================
32   *
33   * This file is part of the EJOE framework.
34   * For more information on the author, please see
35   * <http://www.manskes.de/>.
36   *
37   *********************************************************************/
38  package de.netseeker.ejoe.handler;
39  
40  import java.util.HashMap;
41  import java.util.Map;
42  
43  import de.netseeker.ejoe.request.IRequest;
44  
45  /***
46   * When using a ServerHandlerMapping with EJServer it is possible to use different
47   * ServerHandlers depending on the type of the used requests. 
48   * @author netseeker
49   * @since 0.3.9.3
50   * @see de.netseeker.ejoe.request.IRequest
51   */
52  public class ServerHandlerMapping implements ServerHandler
53  {
54      /***
55       * 
56       */
57      private static final long serialVersionUID = 1L;
58  
59      private Map           _handlerMapping = new HashMap();
60  
61      private ServerHandler _defaultHandler = null;
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see de.netseeker.ejoe.handler.ServerHandler#handle(java.lang.Object)
67       */
68      public Object handle( Object obj ) throws Exception
69      {
70          if ( obj instanceof IRequest )
71          {
72              IRequest request = (IRequest) obj;
73              String ident = request.getUniqueName();
74              ServerHandler handler = (ServerHandler) (_handlerMapping.containsKey( ident ) ? _handlerMapping.get( ident )
75                      : _defaultHandler);
76              if ( handler == null )
77              {
78                  throw new NullPointerException( "No Handler found for Request Type and no default Handler registered!" );
79              }
80  
81              return handler.handle( request );
82          }
83  
84          if ( _defaultHandler != null )
85          {
86              return _defaultHandler.handle( obj );
87          }
88          else
89          {
90              throw new NullPointerException( "No default handler registered to process the request!" );
91          }
92      }
93  
94      /***
95       * @param uniqueName
96       * @param serverHandler
97       */
98      public void addHandlerMapping( String uniqueName, ServerHandler serverHandler )
99      {
100         _handlerMapping.put( uniqueName, serverHandler );
101     }
102     
103     /***
104      * @param uniqueName
105      * @return
106      */
107     public boolean removeHandlerMapping( String uniqueName )
108     {
109         return _handlerMapping.remove( uniqueName ) != null;
110     }
111 
112     /***
113      * @param serverHandler
114      */
115     public void setDefaultHandler( ServerHandler serverHandler )
116     {
117         _defaultHandler = serverHandler;
118     }
119 
120 }