/** * Attempts to ascertain the remote service's relative path by retrieving * the {@link RemoteServiceRelativePath} annotation value from the base * interface. This method can take either the base interface or the Async * interface class. * * @since 0.5 * @param serviceIntf * either the base or Async interface class * @return */ protected static <ServiceIntf> String getRemoteServiceRelativePathFromAnnotation( Class<ServiceIntf> serviceIntf) { Class<?> baseServiceIntf = serviceIntf; if (serviceIntf.getName().endsWith(ASYNC_POSTFIX)) { // Try determine remoteServiceRelativePath from the 'sync' version // of the Async one String className = serviceIntf.getName(); try { baseServiceIntf = Class.forName(className.substring(0, className.length() - ASYNC_POSTFIX.length())); } catch (ClassNotFoundException e) { throw new SyncProxyException(baseServiceIntf, InfoType.SERVICE_BASE, e); } } if (baseServiceIntf.getAnnotation(RemoteServiceRelativePath.class) == null) { if (isSuppressRelativePathWarning()) { logger.info("Suppressed warning for lack of RemoteServiceRelativePath annotation on service: " + baseServiceIntf); return ""; } throw new SyncProxyException(baseServiceIntf, InfoType.REMOTE_SERVICE_RELATIVE_PATH); } return baseServiceIntf.getAnnotation(RemoteServiceRelativePath.class) .value(); }
/** * * @deprecated since 0.5, {@link #createProxy(Class, ProxySettings)} with * {@link ProxySettings#setCookieManager(CookieManager)} and * {@link ProxySettings#setWaitForInvocation(boolean)} */ @Deprecated public static <ServiceIntf> ServiceIntf newProxyInstance( Class<ServiceIntf> serviceIntf, String moduleBaseURL, CookieManager cookieManager, boolean waitForInvocation) { RemoteServiceRelativePath relativePathAnn = serviceIntf .getAnnotation(RemoteServiceRelativePath.class); if (serviceIntf.getName().endsWith("Async")) { // Try determine remoteServiceRelativePath from the 'sync' version // of the Async one String className = serviceIntf.getName(); try { Class<?> clazz = Class.forName(className.substring(0, className.length() - 5)); relativePathAnn = clazz .getAnnotation(RemoteServiceRelativePath.class); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } if (relativePathAnn == null) { throw new RuntimeException(serviceIntf + " does not has a RemoteServiceRelativePath annotation"); } String remoteServiceRelativePath = relativePathAnn.value(); return newProxyInstance(serviceIntf, moduleBaseURL, remoteServiceRelativePath, POLICY_MAP.get(serviceIntf.getName()), cookieManager, waitForInvocation); }
/** * Scans the application context and its parents for service beans that * implement the {@link GWTRequestMapping} * * @param appContext * Application context */ private void scanForAnnotatedBeans(final ApplicationContext appContext) { if (appContext == null) { return; } for (String beanName : appContext .getBeanNamesForType(RemoteService.class)) { Object service = appContext.getBean(beanName); if (service == null) continue; final Class<?> beanClass = service.getClass(); final RemoteServiceRelativePath requestMapping = ReflectionUtils .findAnnotation(beanClass, RemoteServiceRelativePath.class); if (requestMapping == null) { continue; } // Create serviceExporter to bind to String mapping = requestMapping.value(); if (mapping.contains("/")){ mapping = mapping.substring(mapping.lastIndexOf("/")); } if (getMappings().containsKey(mapping)) logger.warn("Bean '" + mapping + "' already in mapping, skipping."); else getMappings().put(mapping, service); } if (scanParentApplicationContext) scanForAnnotatedBeans(appContext.getParent()); }
private void scanForGwtRpcBeans(ApplicationContext appContext, Map<String, Object> rpcServiceMap, String[] remoteServiceBeans) { //Iterate through the beans from the application context and scan for beans with RemoteServiceRelativePath for (String beanName : remoteServiceBeans) { Object service = appContext.getBean(beanName); if (service != null) { // Get the RemoteServiceRelativePath annotation on the class final RemoteServiceRelativePath servicePathAnnotation = ReflectionUtils.findAnnotation(service.getClass(), RemoteServiceRelativePath.class); if (servicePathAnnotation != null) { // This class is annotated with RemoteServiceRelativePath so treat it as a GWT RPC service bean // and add corresponding entry to the map addRpcServiceBeanMapping(rpcServiceMap, service, servicePathAnnotation); } } } if (scanParentApplicationContext) { // Call this method recursively with parent application context if parent // application context needs to be scanned. scanForGwtRpcBeans(appContext.getParent(), rpcServiceMap, remoteServiceBeans); } }
private void addRpcServiceBeanMapping(Map<String, Object> rpcServiceMap, Object service, RemoteServiceRelativePath servicePathAnnotation) { //Get the service's relative path value from the annotation String servicePath = servicePathAnnotation.value(); Class<?> serviceClass = service.getClass(); if (servicePath == null) { // If no value is specified with the RemoteServiceRelativePath annotation then warn LOG.warn("RemoteServiceRelativePath value for [{}] is null. This service will be mapped to handle GWT RPC" + " calls with null servicePath. Are you sure you want to do that?", serviceClass); } if ("".equals(servicePath)) { // If an empty string is specified with the RemoteServiceRelativePath annotation then warn LOG.warn("RemoteServiceRelativePath value for [{}] is an empty string. This service will be mapped to " + "handle GWT RPC calls with empty servicePath. Are you sure you want to do that?", serviceClass); } // Check to see if an entry for the current servicePath already exists if (rpcServiceMap.containsKey(servicePath)) { // If mapping already exists for the servicePath then skip the entry. Object existingServiceBean = rpcServiceMap.get(servicePath); LOG.warn( "Duplicate [{}] mappings found for the GWT RPC service. Ignoring [{}]. The [{}] will be used for path" + " [{}].", servicePath, serviceClass, (existingServiceBean == null ? "null" : existingServiceBean.getClass()), servicePath); } else { // Add an entry of the servicePath vs service to the GWT-RPC service map rpcServiceMap.put(servicePath, service); } }