View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: UserChannelManager.java,v 1.4 2005/08/29 17:41:32 mat007 Exp $
29   */
30  
31  package jtge.engine.security;
32  
33  import java.util.Iterator;
34  import java.util.Vector;
35  import jtge.engine.handler.IConsumer;
36  import jtge.engine.io.IChannelObserver;
37  import jtge.engine.io.IInput;
38  import jtge.engine.io.IOutput;
39  import jtge.engine.user.IUser;
40  
41  /***
42   * Maintains an association between a channel and a user.
43   *
44   * @author Mathieu Champlon
45   * @version $Revision: 1.4 $ $Date: 2005/08/29 17:41:32 $
46   */
47  public class UserChannelManager implements IUserManager, IChannelObserver
48  {
49      private final Vector inputs;
50      private final Vector outputs;
51      private final Vector users;
52      private final Vector observers;
53  
54      /***
55       * Create a user channel manager.
56       */
57      public UserChannelManager()
58      {
59          inputs = new Vector();
60          outputs = new Vector();
61          users = new Vector();
62          observers = new Vector();
63      }
64  
65      /***
66       * Register a login observer.
67       *
68       * @param observer the observer to register
69       */
70      public final void register( final ILoginObserver observer )
71      {
72          if( observer != null )
73              observers.add( observer );
74      }
75  
76      private void login( final IUser user, final IConsumer consumer )
77      {
78          final Iterator iterator = observers.iterator();
79          while( iterator.hasNext() )
80              ((ILoginObserver)iterator.next()).login( user, consumer );
81      }
82  
83      private void logout( final IUser user )
84      {
85          final Iterator iterator = observers.iterator();
86          while( iterator.hasNext() )
87              ((ILoginObserver)iterator.next()).logout( user );
88      }
89  
90      /***
91       * {@inheritDoc}
92       */
93      public final void register( final IInput input, final IUser user )
94      {
95          final int index = inputs.indexOf( input );
96          if( index != -1 )
97          {
98              users.set( index, user ); // TODO handle previous registered user
99              login( user, (IOutput)outputs.elementAt( index ) );
100         }
101     }
102 
103     /***
104      * {@inheritDoc}
105      */
106     public final void unregister( final IUser user )
107     {
108         throw new UnsupportedOperationException(); // TODO : usefull to kick a user
109     }
110 
111     /***
112      * {@inheritDoc}
113      */
114     public final void connected( final IInput input, final IOutput output )
115     {
116         inputs.add( input );
117         outputs.add( output );
118         users.add( null );
119     }
120 
121     /***
122      * {@inheritDoc}
123      */
124     public final void disconnected( final IInput input, final IOutput output )
125     {
126         final int index = inputs.indexOf( input );
127         if( index != -1 )
128         {
129             inputs.remove( index );
130             outputs.remove( index );
131             final IUser user = (IUser)users.remove( index );
132             if( user != null )
133                 logout( user );
134         }
135     }
136 }