KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > ServiceAccessController


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ServiceAccessController.java 2499 2006-02-24 21:33:16Z dblevins $
44  */

45 package org.openejb.server;
46
47 import java.io.*;
48 import java.net.*;
49 import java.util.*;
50 import org.openejb.*;
51
52 /**
53  * The Server will call the following methods.
54  *
55  * newInstance()
56  * init( port, properties)
57  * start()
58  * stop()
59  *
60  * All Daemon implementations must have a no argument
61  * constructor.
62  *
63  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
64  */

65 public class ServiceAccessController implements ServerService {
66     
67     ServerService next;
68     
69     InetAddress[] allowedHosts;
70
71     public ServiceAccessController(ServerService next){
72         this.next = next;
73     }
74
75     /**
76      * Pulls out the access log information
77      *
78      * @param props
79      *
80      * @exception ServiceException
81      */

82     public void init(Properties props) throws Exception JavaDoc{
83         // Do our stuff
84
parseAdminIPs(props);
85
86         // Then call the next guy
87
next.init(props);
88     }
89     
90     public void start() throws ServiceException{
91         // Do our stuff
92

93         // Then call the next guy
94
next.start();
95     }
96     
97     public void stop() throws ServiceException{
98         // Do our stuff
99

100         // Then call the next guy
101
next.stop();
102     }
103
104     public void service(Socket socket) throws ServiceException, IOException{
105         // Do our stuff
106
// Check authorization
107
//checkHostsAuthorization(socket.getInetAddress(), socket.getLocalAddress());
108
// Then call the next guy
109
next.service(socket);
110     }
111
112     public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
113         throw new UnsupportedOperationException JavaDoc("service(in,out)");
114     }
115
116     
117     public String JavaDoc getName(){
118         return next.getName();
119     }
120
121     /**
122      * Gets the ip number that the
123      * daemon is listening on.
124      */

125     public String JavaDoc getIP(){
126         return next.getIP();
127     }
128     
129     /**
130      * Gets the port number that the
131      * daemon is listening on.
132      */

133     public int getPort(){
134         return next.getPort();
135     }
136
137
138     public void checkHostsAuthorization(InetAddress client, InetAddress server) throws SecurityException JavaDoc {
139         // Authorization flag. This starts out as unauthorized
140
// and will stay that way unless a matching admin ip is
141
// found.
142
boolean authorized = false;
143
144         // Check the client ip against the server ip. Hosts are
145
// allowed to access themselves, so if these ips
146
// match, the following for loop will be skipped.
147
authorized = client.equals( server );
148
149         for (int i=0; i < allowedHosts.length && !authorized; i++){
150             authorized = allowedHosts[i].equals( client );
151         }
152
153         if ( !authorized ) {
154             throw new SecurityException JavaDoc("Host "+client.getHostAddress()+" is not authorized to access this service.");
155         }
156     }
157
158     private void parseAdminIPs(Properties props){
159         try{
160
161             Vector addresses = new Vector();
162
163             InetAddress[] localIps = InetAddress.getAllByName("localhost");
164             for (int i=0; i < localIps.length; i++){
165                 addresses.add( localIps[i] );
166             }
167
168             String JavaDoc ipString = props.getProperty("only_from");
169             if (ipString != null) {
170                 StringTokenizer st = new StringTokenizer(ipString, " ,");
171                 while (st.hasMoreTokens()) {
172                     String JavaDoc address = null;
173                     InetAddress ip = null;
174                     try{
175                         address = st.nextToken();
176                         ip = InetAddress.getByName(address);
177                         addresses.add( ip );
178                     } catch (Exception JavaDoc e){
179                         //logger.error("Unable to apply the address ["+address+"] to the list of valid admin hosts: "+e.getMessage());
180
}
181                 }
182             }
183
184             allowedHosts = new InetAddress[ addresses.size() ];
185             addresses.copyInto( allowedHosts );
186
187         } catch (Exception JavaDoc e){
188             //logger.error("Unable to create the list of valid hosts: "+e.getMessage());
189
}
190     }
191
192 }
193
Popular Tags