1 16 package org.jmanage.core.remote.server; 17 18 import org.jmanage.core.auth.User; 19 import org.jmanage.core.auth.UserManager; 20 import org.jmanage.core.auth.UnAuthorizedAccessException; 21 import org.jmanage.core.remote.InvocationResult; 22 import org.jmanage.core.remote.RemoteInvocation; 23 import org.jmanage.core.services.AuthService; 24 import org.jmanage.core.services.ServiceContextImpl; 25 import org.jmanage.core.services.ServiceException; 26 import org.jmanage.core.services.ServiceFactory; 27 import org.jmanage.core.util.Loggers; 28 import org.jmanage.core.util.ErrorCodes; 29 import org.jmanage.core.management.ConnectionFailedException; 30 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Method ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 36 50 public class ServiceCallHandler { 51 52 private static final Logger logger = 53 Loggers.getLogger(ServiceCallHandler.class); 54 55 public static InvocationResult execute(RemoteInvocation invocation){ 56 try { 57 Object result = executeInternal(invocation.getClassName(), 58 invocation.getMethodName(), 59 invocation.getSignature(), 60 invocation.getArgs()); 61 return new InvocationResult(result); 62 } catch (InvocationTargetException e) { 63 Throwable t = e.getCause(); 64 if(t != null){ 65 if(t instanceof ServiceException || t instanceof UnAuthorizedAccessException){ 66 return new InvocationResult(t); 67 }else if(t instanceof ConnectionFailedException){ 68 return new InvocationResult( 69 new ServiceException(ErrorCodes.CONNECTION_FAILED)); 70 } 71 } 72 logger.log(Level.SEVERE, "Error while invoking: " + 73 invocation.getClassName() +"->"+ invocation.getMethodName(), 74 e); 75 throw new RuntimeException (e); 76 } catch(Exception e){ 77 logger.log(Level.SEVERE, "Error while invoking: " + 78 invocation.getClassName() +"->"+ invocation.getMethodName(), 79 e); 80 throw new RuntimeException (e); 81 } 82 } 83 84 private static Object executeInternal(String className, 85 String methodName, 86 Class [] parameterTypes, 87 Object [] args) 88 throws Exception { 89 90 91 ServiceContextImpl serviceContext = (ServiceContextImpl)args[0]; 92 try { 93 94 authenticate((ServiceContextImpl)args[0], className, methodName); 95 96 Class serviceClass = Class.forName(className); 97 Method method = serviceClass.getMethod(methodName, parameterTypes); 98 Object serviceObject = ServiceFactory.getService(serviceClass); 99 100 return method.invoke(serviceObject, args); 101 } finally { 102 serviceContext.releaseResources(); 103 } 104 } 105 106 private static void authenticate(ServiceContextImpl context, 107 String className, 108 String methodName){ 109 110 User user = context.getUser(); 111 if(user == null){ 112 114 if(!className.equals(AuthService.class.getName()) 115 || !methodName.equals("login")){ 116 117 throw new RuntimeException ("Service method called without " + 118 "User credentials"); 119 } 120 return; 121 } 122 123 124 assert user.getUsername() != null; 125 assert user.getPassword() != null; 126 127 UserManager userManager = UserManager.getInstance(); 128 User completeUser = 129 userManager.getUser(user.getUsername()); 130 131 if(!user.getPassword().equals(completeUser.getPassword()) 132 || !User.STATUS_ACTIVE.equals(completeUser.getStatus())){ 133 throw new RuntimeException ("Invalid user credentials."); 134 } 135 136 context.setUser(completeUser); 137 } 138 } 139 | Popular Tags |