package com.jqmobile.core.server.rmi2; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.UndeclaredThrowableException; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import com.jqmobile.core.server.rmi.IRmiRemote; import com.jqmobile.core.server.rmi.RmiException; import com.jqmobile.core.server.rmi.RmiRemote; import com.jqmobile.core.server.service.ServiceFactory; class RmiRemoteImpl2 extends UnicastRemoteObject implements IRmiRemote{ public RmiRemoteImpl2() throws RemoteException { super(); } /** * */ private static final long serialVersionUID = 7911662448169271887L; @Override public Object invoke(RmiRemote rmi) throws RemoteException { String session = null; if(rmi.getManagerClass().equals(com.jqmobile.core.server.rmi2.IRmiLogin.class.getName()) && rmi.getMethod().equals("login")){ System.out.println("rmi login"); }else{ session = rmi.getSession(); RmiSession.activateSessionConn(session); } try { String mc = rmi.getManagerClass(); Class<?> c = Class.forName(mc, false, Thread.currentThread().getContextClassLoader()); RMI ann = c.getAnnotation(RMI.class); if(null == ann){ throw new RmiException(RmiException.RMI_Interface_NotAuth); } Object i = ServiceFactory.instance(c); Method[] ms = i.getClass().getDeclaredMethods(); for(Method m : ms){ if(m.getName().equals(rmi.getMethod())){ if(m.getParameterTypes().length == rmi.getArgs().length){ try { Method intfM = null; try { intfM = c.getMethod(m.getName(), m.getParameterTypes()); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } if(null == intfM || null == intfM.getAnnotation(RMI.class)){ continue; } return m.invoke(i, rmi.getArgs()); } catch (IllegalArgumentException e) { continue; } catch (IllegalAccessException e) { continue; } catch (InvocationTargetException e) { Throwable e1 = e.getCause(); while(e1 instanceof UndeclaredThrowableException || e1 instanceof InvocationTargetException){ e1 = e1.getCause(); } if(e1 instanceof RmiException){ throw (RmiException)e1; }else if(e1 instanceof RemoteException){ throw (RemoteException)e1; }else{ throw new RemoteException("内部方法调用异常", e1); } } } } } } catch (ClassNotFoundException e) { e.printStackTrace(); }finally{ if(null != session) RmiSession.returnSessionConn(session); } return null; } public static Method getMethod(Class<?> implClass,String methodName,Class<?>...params) throws SecurityException, NoSuchMethodException{ if(null == params || 0 == params.length){ return implClass.getMethod(methodName); } //20130813 Method cacheMethod = null; try { cacheMethod = implClass.getDeclaredMethod(methodName, params); } catch (Exception e) { } if(null != cacheMethod){ return cacheMethod; } // Method[] ms = implClass.getMethods(); L1:for(Method m : ms){ if(!m.getName().equals(methodName)){ continue; } Class<?>[] types = m.getParameterTypes(); if(null == types || 0==types.length || types.length != params.length){ continue; } for(int i=0; i<types.length; i++){ Class<?> p = params[i]; Class<?> t = types[i]; if(!equals(p,t)){ continue L1; } } return m; } //20130813 L1:for(Method m : ms){ if(!m.getName().equals(methodName)){ continue; } Class<?>[] types = m.getParameterTypes(); if(null == types || 0==types.length || types.length != params.length){ continue; } for(int i=0; i<types.length; i++){ Class<?> p = params[i]; Class<?> t = types[i]; if(!equals1(p,t)){ continue L1; } } return m; } // return null; } private static boolean equals1(Class<?> p, Class<?> t) { if(p.equals(t)){ return true; }else if(p.equals(Integer.class)){ return t.equals(int.class); }else if(p.equals(Long.class)){ return t.equals(long.class); }else if(p.equals(Float.class)){ return t.equals(float.class); }else if(p.equals(Double.class)){ return t.equals(double.class); }else if(p.equals(Byte.class)){ return t.equals(byte.class); }else if(p.equals(Short.class)){ return t.equals(short.class); }else if(p.equals(Character.class)){ return t.equals(char.class); }else if(p.equals(Boolean.class)){ return t.equals(boolean.class); }else{ return t.isAssignableFrom(p); } // return false; } private static boolean equals(Class<?> p, Class<?> t) { if(p.equals(t)){ return true; }else if(p.equals(Integer.class)){ return t.equals(int.class); }else if(p.equals(Long.class)){ return t.equals(long.class); }else if(p.equals(Float.class)){ return t.equals(float.class); }else if(p.equals(Double.class)){ return t.equals(double.class); }else if(p.equals(Byte.class)){ return t.equals(byte.class); }else if(p.equals(Short.class)){ return t.equals(short.class); }else if(p.equals(Character.class)){ return t.equals(char.class); }else if(p.equals(Boolean.class)){ return t.equals(boolean.class); } return false; } }