View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.mitosis.service.protocol.handler;
21  
22  
23  import org.apache.directory.mitosis.configuration.ReplicationConfiguration;
24  import org.apache.directory.mitosis.service.DefaultReplicationContext;
25  import org.apache.directory.mitosis.service.ReplicationContext;
26  import org.apache.directory.mitosis.service.ReplicationInterceptor;
27  import org.apache.directory.server.core.DirectoryService;
28  import org.apache.mina.common.IdleStatus;
29  import org.apache.mina.common.IoHandler;
30  import org.apache.mina.common.IoSession;
31  
32  /**
33   * A MINA {@link IoHandler} that wraps a {@link ReplicationContextHandler}
34   * and provides a default implementation of {@link ReplicationContext}.
35   *  
36   * @author The Apache Directory Project (dev@directory.apache.org)
37   * @version $Rev$, $Date$
38   */
39  public class ReplicationProtocolHandler implements IoHandler
40  {
41      private static final String CONTEXT = "context";
42  
43      private final ReplicationInterceptor interceptor;
44      private final ReplicationConfiguration configuration;
45      private final DirectoryService directoryService;
46      private final ReplicationContextHandler contextHandler;
47  
48  
49      public ReplicationProtocolHandler( ReplicationInterceptor interceptor, ReplicationContextHandler contextHandler )
50      {
51          assert interceptor != null;
52          assert contextHandler != null;
53  
54          this.interceptor = interceptor;
55          this.configuration = interceptor.getConfiguration();
56          this.directoryService = interceptor.getDirectoryService();
57          this.contextHandler = contextHandler;
58      }
59  
60  
61      public ReplicationContext getContext( IoSession session )
62      {
63          return ( ReplicationContext ) session.getAttribute( CONTEXT );
64      }
65      
66      public ReplicationContextHandler getContextHandler() {
67          return contextHandler;
68      }
69  
70  
71      public void sessionCreated( IoSession session ) throws Exception
72      {
73          session.setAttribute( CONTEXT,
74                  new DefaultReplicationContext( interceptor, directoryService, configuration, session ) );
75      }
76  
77  
78      public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
79      {
80          contextHandler.exceptionCaught( getContext( session ), cause );
81      }
82  
83  
84      public void messageReceived( IoSession session, Object message ) throws Exception
85      {
86          contextHandler.messageReceived( getContext( session ), message );
87      }
88  
89  
90      public void messageSent( IoSession session, Object message ) throws Exception
91      {
92          contextHandler.messageSent( getContext( session ), message );
93      }
94  
95  
96      public void sessionClosed( IoSession session ) throws Exception
97      {
98          ReplicationContext ctx = getContext( session );
99          contextHandler.contextEnd( ctx );
100         ctx.cancelAllExpirations();
101     }
102 
103 
104     public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
105     {
106         contextHandler.contextIdle( getContext( session ), status );
107     }
108 
109 
110     public void sessionOpened( IoSession session ) throws Exception
111     {
112         contextHandler.contextBegin( getContext( session ) );
113     }
114 }