View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: ConnectionInvocationHandler.java 1011 2007-02-23 06:25:07Z vlads $
21   */
22  package org.microemu.microedition.io;
23  
24  import java.lang.reflect.InvocationHandler;
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  import java.security.AccessControlContext;
28  import java.security.AccessController;
29  import java.security.PrivilegedActionException;
30  import java.security.PrivilegedExceptionAction;
31  
32  import javax.microedition.io.Connection;
33  
34  import org.microemu.log.Logger;
35  
36  /**
37   * Dynamic proxy class for GCF Connections returend to MIDlet
38   * Used to debug excetions thrown to MIDlet
39   * Makes PrivilegedCalls when rinning in Webstart
40   * 
41   * @author vlads
42   */
43  public class ConnectionInvocationHandler implements InvocationHandler {
44  
45  	private Connection originalConnection; 
46  	
47  	/* The context to be used when connecting to network */
48      private AccessControlContext acc;
49      
50  	static {
51  		Logger.addLogOrigin(ConnectionInvocationHandler.class);
52  	}
53  	
54  	public ConnectionInvocationHandler(Connection con, boolean needPrivilegedCalls) {
55  		this.originalConnection = con;
56  		if (needPrivilegedCalls) {
57  			this.acc = AccessController.getContext();
58  		}
59  	}
60  	
61  	/* (non-Javadoc)
62  	 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
63  	 */
64  	public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
65  		if (ConnectorImpl.debugConnectionInvocations) {
66  			Logger.debug("invoke", method);
67  		}
68  		try {
69  			if (this.acc != null) {
70  			return AccessController.doPrivileged(new PrivilegedExceptionAction() {
71  				public Object run() throws InvocationTargetException, IllegalAccessException {
72  					return method.invoke(originalConnection, args);
73  				}
74  			}, acc);
75  			} else {
76  				return method.invoke(this.originalConnection, args);
77  			}
78  		} catch (PrivilegedActionException e) {
79  			if (e.getCause() instanceof InvocationTargetException) {
80  				if (ConnectorImpl.debugConnectionInvocations) {
81  	        		Logger.error("Connection." + method.getName(), e.getCause().getCause());
82  	        	}
83  				throw e.getCause().getCause();
84  			} else {
85  				if (ConnectorImpl.debugConnectionInvocations) {
86  	        		Logger.error("Connection." + method.getName(), e.getCause());
87  	        	}
88  				throw e.getCause();
89  			}
90          } catch (InvocationTargetException e) {
91          	if (ConnectorImpl.debugConnectionInvocations) {
92          		Logger.error("Connection." + method.getName(), e.getCause());
93          	}
94              throw e.getCause();
95          }
96  	}
97  
98  }