KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > remote > server > ServiceCallHandler


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * ServiceCallHandler handles remote requests
38  * for executing Service layer methods. These remote requests originate from
39  * HttpServiceProxy.
40  * <p>
41  * There are certain assumptions being made for the Service layer methods:
42  * <ul>
43  * <li>All service layer methods take ServiceContext as the first argument.
44  * </ul>
45  *
46  * @see org.jmanage.core.remote.client.HttpServiceProxy
47  * date: Jan 17, 2005
48  * @author Rakesh Kalra
49  */

50 public class ServiceCallHandler {
51
52     private static final Logger JavaDoc logger =
53             Loggers.getLogger(ServiceCallHandler.class);
54
55     public static InvocationResult execute(RemoteInvocation invocation){
56         try {
57             Object JavaDoc result = executeInternal(invocation.getClassName(),
58                     invocation.getMethodName(),
59                     invocation.getSignature(),
60                     invocation.getArgs());
61             return new InvocationResult(result);
62         } catch (InvocationTargetException JavaDoc e) {
63             Throwable JavaDoc 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 JavaDoc(e);
76         } catch(Exception JavaDoc e){
77             logger.log(Level.SEVERE, "Error while invoking: " +
78                     invocation.getClassName() +"->"+ invocation.getMethodName(),
79                     e);
80             throw new RuntimeException JavaDoc(e);
81         }
82     }
83
84     private static Object JavaDoc executeInternal(String JavaDoc className,
85                                           String JavaDoc methodName,
86                                           Class JavaDoc[] parameterTypes,
87                                           Object JavaDoc[] args)
88         throws Exception JavaDoc {
89
90         /* take the service context */
91         ServiceContextImpl serviceContext = (ServiceContextImpl)args[0];
92         try {
93             /* authenticate the request */
94             authenticate((ServiceContextImpl)args[0], className, methodName);
95             /* invoke the method */
96             Class JavaDoc serviceClass = Class.forName(className);
97             Method JavaDoc method = serviceClass.getMethod(methodName, parameterTypes);
98             Object JavaDoc serviceObject = ServiceFactory.getService(serviceClass);
99             /* invoke the method */
100             return method.invoke(serviceObject, args);
101         } finally {
102             serviceContext.releaseResources();
103         }
104     }
105
106     private static void authenticate(ServiceContextImpl context,
107                                      String JavaDoc className,
108                                      String JavaDoc methodName){
109
110         User user = context.getUser();
111         if(user == null){
112             /* only the login method in AuthService is allowed without
113                 authentication */

114             if(!className.equals(AuthService.class.getName())
115                     || !methodName.equals("login")){
116
117                 throw new RuntimeException JavaDoc("Service method called without " +
118                         "User credentials");
119             }
120             return;
121         }
122
123         /* User must have username and password specified */
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         /* validate password */
131         if(!user.getPassword().equals(completeUser.getPassword())
132             || !User.STATUS_ACTIVE.equals(completeUser.getStatus())){
133             throw new RuntimeException JavaDoc("Invalid user credentials.");
134         }
135
136         context.setUser(completeUser);
137     }
138 }
139
Popular Tags