001/*
002 * Copyright 2008 Google Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016
017package org.opencms.gwt.rebind.rpc;
018
019import java.util.Map;
020
021import com.google.gwt.core.ext.typeinfo.JClassType;
022import com.google.gwt.core.ext.typeinfo.JMethod;
023import com.google.gwt.core.ext.typeinfo.TypeOracle;
024import com.google.gwt.user.client.rpc.SynchronizedRpcRequest;
025import com.google.gwt.user.rebind.SourceWriter;
026import com.google.gwt.user.rebind.rpc.ProxyCreator;
027import com.google.gwt.user.rebind.rpc.SerializableTypeOracle;
028
029/**
030 * Creates proxies supporting optionally synchronized RPC methods
031 * using the {@link SynchronizedRpcRequest} annotation.<p>
032 */
033public class CmsRpcProxyCreator extends ProxyCreator {
034
035    /**
036     * Constructor.<p>
037     *
038     * @param serviceIntf the service interface
039     */
040    public CmsRpcProxyCreator(@SuppressWarnings("hiding") JClassType serviceIntf) {
041
042        super(serviceIntf);
043    }
044
045    /**
046     * @see com.google.gwt.user.rebind.rpc.ProxyCreator#generateProxyMethods(com.google.gwt.user.rebind.SourceWriter, com.google.gwt.user.rebind.rpc.SerializableTypeOracle, com.google.gwt.core.ext.typeinfo.TypeOracle, java.util.Map)
047     */
048    @Override
049    protected void generateProxyMethods(
050        SourceWriter w,
051        SerializableTypeOracle serializableTypeOracle,
052        TypeOracle typeOracle,
053        Map<JMethod, JMethod> syncMethToAsyncMethMap) {
054
055        super.generateProxyMethods(w, serializableTypeOracle, typeOracle, syncMethToAsyncMethMap);
056        generateSyncOverride(w, syncMethToAsyncMethMap);
057    }
058
059    /**
060     * Generates a method to check if a given RPC method has to be synchronized.<p>
061     *
062     * @param srcWriter the source write to generate the code with
063     * @param syncMethToAsyncMethMap the method map
064     */
065    protected void generateSyncOverride(SourceWriter srcWriter, Map<JMethod, JMethod> syncMethToAsyncMethMap) {
066
067        srcWriter.println("@Override");
068        srcWriter.println("public boolean isSync(String methodName) {");
069
070        JMethod[] syncMethods = serviceIntf.getOverridableMethods();
071        for (JMethod syncMethod : syncMethods) {
072            JMethod asyncMethod = syncMethToAsyncMethMap.get(syncMethod);
073            if (!asyncMethod.isAnnotationPresent(SynchronizedRpcRequest.class)) {
074                continue;
075            }
076            srcWriter.indentln(
077                "if (methodName.equals(\"" + getProxySimpleName() + "." + syncMethod.getName() + "\")) {");
078            srcWriter.indentln("return true;");
079            srcWriter.indentln("}");
080        }
081        srcWriter.indentln("return false;");
082        srcWriter.println("}");
083    }
084}