KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > proxy > model > LdapProxy


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.ldapstudio.proxy.model;
21
22
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28
29 /**
30  * This class implements a LDAP Proxy
31  *
32  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
33  * @version $Rev$, $Date$
34  */

35 public class LdapProxy
36 {
37     /** Default timeout in milli seconds */
38     public static final int DEFAULT_TIMEOUT = 30000;
39
40     /** The listeners list */
41     private List JavaDoc<LdapProxyListener> listeners;
42
43     /** The received LDAP Messages */
44     private List JavaDoc<LdapMessageWithPDU> receivedLdapMessages;
45
46     private LdapProxyThread proxyThread;
47
48
49     /**
50      * Creates a new instance of LdapProxy.
51      *
52      * @param localPort
53      * the local port
54      * @param remoteHost
55      * the host name of the LDAP Server
56      * @param remotePort
57      * the port of the LDAP Server
58      */

59     public LdapProxy( int localPort, String JavaDoc remoteHost, int remotePort )
60     {
61         listeners = new ArrayList JavaDoc<LdapProxyListener>();
62         receivedLdapMessages = new ArrayList JavaDoc<LdapMessageWithPDU>();
63         proxyThread = new LdapProxyThread( this, localPort, remoteHost, remotePort, DEFAULT_TIMEOUT );
64     }
65
66
67     /**
68      * Connects the LDAP Proxy.
69      *
70      * @throws IOException
71      */

72     public void connect() throws IOException JavaDoc
73     {
74         proxyThread.start();
75     }
76
77
78     /**
79      * Disconnects the LDAP Proxy.
80      */

81     public void disconnect()
82     {
83         proxyThread.interrupt();
84     }
85
86
87     /**
88      * Adds a LDAP Proxy Listener.
89      *
90      * @param listener
91      * the listener to add
92      */

93     public void addListener( LdapProxyListener listener )
94     {
95         listeners.add( listener );
96     }
97
98
99     /**
100      * Removes the LDAP Proxy Listener.
101      *
102      * @param listener
103      * the listener to remove
104      */

105     public void removeListener( LdapProxyListener listener )
106     {
107         listeners.remove( listener );
108     }
109
110
111     public boolean addReceivedLdapMessage( LdapMessageWithPDU ldapMessage )
112     {
113         boolean bool = receivedLdapMessages.add( ldapMessage );
114         notifyListeners( ldapMessage );
115         return bool;
116     }
117
118
119     /**
120      * Notifies all listeners that a new LDAP Message has arrived.
121      *
122      * @param ldapMessage
123      * the LDAP Message
124      */

125     private void notifyListeners( LdapMessageWithPDU ldapMessage )
126     {
127         for ( Iterator JavaDoc iter = listeners.iterator(); iter.hasNext(); )
128         {
129             LdapProxyListener proxyListener = ( LdapProxyListener ) iter.next();
130             proxyListener.ldapMessageReceived( ldapMessage );
131         }
132     }
133
134
135     public List JavaDoc<LdapMessageWithPDU> getReceivedLdapMessages()
136     {
137         return receivedLdapMessages;
138     }
139 }
140
Popular Tags