Java Code Examples for org.springframework.remoting.rmi.RmiInvocationHandler

Following code examples demonstrate how to use org.springframework.remoting.rmi.RmiInvocationHandlerfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.remoting.rmi.RmiInvocationHandlerand various code implementation of this class.

	public RemoteClient createRemoteClient() {
		ProxyFactory pf = null;
		if (getServiceInterface().isAssignableFrom(remoteService.getClass())) {
			pf = new ProxyFactory(remoteService);
		}
		else {
			pf = new ProxyFactory(getServiceInterface(), 
					new RmiServiceInterceptor((RmiInvocationHandler) remoteService));
		}
		
		pf.addInterface(RemoteClient.class);
		pf.addAdvisor(new RemoteClientAdvisor(this));
		
		return (RemoteClient) pf.getProxy();
	} 


	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client/context.xml");
		
		Object obj;
		obj = context.getBean("accountService");
		
		System.out.println("obj.getClass() = " + obj.getClass().getCanonicalName());
		
		System.out.println("obj.getClass() = " + obj.getClass().getCanonicalName());
		
		System.out.println("implemented interfaces:");
		for (Class<?> c : obj.getClass().getInterfaces()) {
			System.out.println("> " + c.getCanonicalName());
		}
		
		if (obj instanceof AccountService) {
			AccountService service = (AccountService) obj;
			service.insertAccount(new Account("Mueller"));
			List<Account> accounts = service.getAccounts("Mueller");
			System.out.println(accounts.size() + " accounts returned");
		}
		
		if(obj instanceof RmiInvocationHandler) {
		  RmiInvocationHandler rih = (RmiInvocationHandler) obj;
		  RemoteInvocation invocation = new RemoteInvocation("insertAccount", new Class<?>[]{Account.class}, new Object[]{new Account("Müller")});
		  rih.invoke(invocation);
		  invocation = new RemoteInvocation("getAccounts", new Class<?>[]{String.class}, new Object[]{"Müller"});
		  List<Account> accounts = (List<Account>) rih.invoke(invocation);
		  System.out.println(accounts.size() + " accounts returned");
		}
		
		context.close();
	} 

    protected Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler) throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        String requestId = RmiThreadLocal.get();
        RemoteInvocation remoteInvocation = createRemoteInvocation(methodInvocation);
        remoteInvocation.addAttribute("requestId",requestId);
        return invocationHandler.invoke(remoteInvocation);
    } 

	public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		System.out.println(obj.getClass());
		RmiInvocationHandler handler = (RmiInvocationHandler) obj;
		RemoteInvocation invocation = new RemoteInvocation();
		invocation.setMethodName("insertAccount");
		invocation.setParameterTypes(new Class<?>[]{ Account.class });
		invocation.setArguments(new Object[]{ new Account("XXX") });
		handler.invoke(invocation);
		RemoteInvocation invocation2 = new RemoteInvocation();
		invocation2.setMethodName("getAccounts");
		invocation2.setParameterTypes(new Class<?>[]{ String.class });
		invocation2.setArguments(new Object[]{ "Mueller" });
		Object result = handler.invoke(invocation2);
		@SuppressWarnings("unchecked")
		List<Account> list = (List<Account>)result;
		System.out.println("Count mueller: " + list.size());
	} 

    protected Remote getObjectToExport() {
        Remote exportedObject = super.getObjectToExport();

        if (getService() instanceof Remote && (
                getServiceInterface() == null || exportedObject.getClass().isAssignableFrom(getServiceInterface()))) {
            this.remoteService = exportedObject;
        }
        else {
            RmiInvocationHandler wrapper = (RmiInvocationHandler) exportedObject;
            ProxyFactory factory = new ProxyFactory(new Class[] { getServiceInterface(), Remote.class });
            factory.addAdvice(new RmiExporterServiceInterceptor(wrapper));

            this.remoteService = (Remote) factory.getProxy();
        }

        return exportedObject;

    } 

	public void executeAttack(RMIEndpoint ep, DeserPayload payload, String cmd) throws BaRMIeException {
		RmiInvocationHandler rmih;
		RemoteInvocation ri;
		Registry reg;
		String objName;
		
		System.out.println("[~] Finding object to target...");
		objName = ep.findObjectWithClass("org.springframework.remoting.rmi.RmiInvocationHandler");
		
		try {
			System.out.println("[~] Getting proxied " + objName + " object...");
			rmih = (RmiInvocationHandler)this.getProxiedObject(ep, objName, payload.getBytes(cmd, 8));
			
			System.out.println("[+] Retrieved, invoking invoke() with payload...");
			ri = new RemoteInvocation();
			ri.arguments = new Object[] { this.DEFAULT_MARKER_OBJECT };
			rmih.invoke(ri);
		} catch(Exception ex) {
			this.checkDeserException(ex);
		}
	} 

            public Result invoke(Invocation invocation) throws RpcException {
                RpcResult result = new RpcResult();
                try {
                    RemoteInvocation i = new RemoteInvocation();
                    i.setMethodName(invocation.getMethodName());
                    i.setParameterTypes(invocation.getParameterTypes());
                    i.setArguments(invocation.getArguments());
                    result.setValue(((RmiInvocationHandler) remote).invoke(i));
                } catch (InvocationTargetException e) {
                    result.setException(e.getTargetException());
                } catch (Exception e) {
                    throw new RpcException(StringUtils.toString(e), e);
                }
                return result;
            } 

	protected Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler) throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
			return "RMI invoker proxy for service URL [" + getServiceUrl() + "]";
		}

		RemoteInvocation ri = createRemoteInvocation(methodInvocation);
		ri.addAttribute("callpk", "123");

		return invocationHandler.invoke(ri);
	} 

	public Remote getObjectToExport() {
		Remote exportedObject = super.getObjectToExport();

		if (getService() instanceof Remote && (
				getServiceInterface() == null || exportedObject.getClass().isAssignableFrom(getServiceInterface()))) {
			this.remoteService = exportedObject;
		}
		else {
			ProxyFactory factory = new ProxyFactory(getServiceInterface(), 
					new RmiServiceInterceptor((RmiInvocationHandler) exportedObject, remoteServiceName));

			this.remoteService = factory.getProxy();
		}

		return exportedObject;
	} 

Advertisement
Javadoc
Interface for RMI invocation handlers instances on the server, wrapping exported services. A client

uses a stub implementing this interface to access such a service.

This is an SPI interface, not to be used directly by applications. @author Juergen Hoeller @since 14.05.2003

Read More
Advertisement