|
| 1 | +/* |
| 2 | + * Copyright (C) 2012 Red Hat, Inc. and/or its affiliates. |
| 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 | + |
| 17 | +package org.jboss.errai.ioc.support.bus.rebind; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.jboss.errai.bus.client.ErraiBus; |
| 24 | +import org.jboss.errai.bus.client.api.ClientMessageBus; |
| 25 | +import org.jboss.errai.bus.client.api.base.MessageBuilder; |
| 26 | +import org.jboss.errai.bus.client.api.messaging.Message; |
| 27 | +import org.jboss.errai.bus.client.api.messaging.MessageCallback; |
| 28 | +import org.jboss.errai.bus.server.annotations.Remote; |
| 29 | +import org.jboss.errai.bus.server.annotations.ShadowService; |
| 30 | +import org.jboss.errai.codegen.Parameter; |
| 31 | +import org.jboss.errai.codegen.Statement; |
| 32 | +import org.jboss.errai.codegen.VariableReference; |
| 33 | +import org.jboss.errai.codegen.builder.AnonymousClassStructureBuilder; |
| 34 | +import org.jboss.errai.codegen.builder.BlockBuilder; |
| 35 | +import org.jboss.errai.codegen.builder.ElseBlockBuilder; |
| 36 | +import org.jboss.errai.codegen.builder.impl.ObjectBuilder; |
| 37 | +import org.jboss.errai.codegen.util.EmptyStatement; |
| 38 | +import org.jboss.errai.codegen.util.If; |
| 39 | +import org.jboss.errai.codegen.util.ProxyUtil; |
| 40 | +import org.jboss.errai.codegen.util.Refs; |
| 41 | +import org.jboss.errai.codegen.util.Stmt; |
| 42 | +import org.jboss.errai.ioc.client.api.CodeDecorator; |
| 43 | +import org.jboss.errai.ioc.rebind.ioc.extension.IOCDecoratorExtension; |
| 44 | +import org.jboss.errai.ioc.rebind.ioc.injector.api.Decorable; |
| 45 | +import org.jboss.errai.ioc.rebind.ioc.injector.api.FactoryController; |
| 46 | + |
| 47 | +/** |
| 48 | + * Generates logic to register client-side shadow services for Errai's message |
| 49 | + * bus. Shadow services are used when: |
| 50 | + * <ul> |
| 51 | + * <li>Remote communication is turned off |
| 52 | + * <li>Errai's message bus is not in connected state |
| 53 | + * <li>A remote endpoint for the service doesn't exist |
| 54 | + * </ul> |
| 55 | + * |
| 56 | + * @author Mike Brock |
| 57 | + * @author Christian Sadilek <[email protected]> |
| 58 | + */ |
| 59 | +@CodeDecorator |
| 60 | +public class ShadowServiceDecorator extends IOCDecoratorExtension<ShadowService> { |
| 61 | + public ShadowServiceDecorator(Class<ShadowService> decoratesWith) { |
| 62 | + super(decoratesWith); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void generateDecorator(final Decorable decorable, final FactoryController controller) { |
| 67 | + final ShadowService shadowService = (ShadowService) decorable.getAnnotation(); |
| 68 | + String serviceName = null; |
| 69 | + |
| 70 | + Statement subscribeShadowStatement = null; |
| 71 | + final Class<?> javaClass = decorable.getType().asClass(); |
| 72 | + for (final Class<?> intf : javaClass.getInterfaces()) { |
| 73 | + if (intf.isAnnotationPresent(Remote.class)) { |
| 74 | + serviceName = intf.getName() + ":RPC"; |
| 75 | + |
| 76 | + final AnonymousClassStructureBuilder builder = generateMethodDelegates(intf, decorable, controller); |
| 77 | + subscribeShadowStatement = Stmt.castTo(ClientMessageBus.class, Stmt.invokeStatic(ErraiBus.class, "get")) |
| 78 | + .invoke("subscribeShadow", serviceName, builder.finish()); |
| 79 | + } |
| 80 | + |
| 81 | + if (serviceName == null) { |
| 82 | + if (shadowService.value().equals("")) { |
| 83 | + serviceName = decorable.getName(); |
| 84 | + } |
| 85 | + else { |
| 86 | + serviceName = shadowService.value(); |
| 87 | + } |
| 88 | + |
| 89 | + subscribeShadowStatement = Stmt.castTo(ClientMessageBus.class, Stmt.invokeStatic(ErraiBus.class, "get")) |
| 90 | + .invoke("subscribeShadow", serviceName, controller.contextGetInstanceStmt()); |
| 91 | + } |
| 92 | + |
| 93 | + controller.addFactoryInitializationStatements(Collections.singletonList(subscribeShadowStatement)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private AnonymousClassStructureBuilder generateMethodDelegates(final Class<?> intf, final Decorable decorable, final FactoryController controller) { |
| 98 | + |
| 99 | + final BlockBuilder<AnonymousClassStructureBuilder> builder = ObjectBuilder.newInstanceOf(MessageCallback.class) |
| 100 | + .extend().publicOverridesMethod("callback", Parameter.of(Message.class, "message")) |
| 101 | + .append(Stmt.declareVariable("commandType", String.class, |
| 102 | + Stmt.loadVariable("message").invoke("getCommandType"))) |
| 103 | + .append(Stmt.declareVariable("methodParms", List.class, |
| 104 | + Stmt.loadVariable("message").invoke("get", List.class, Stmt.loadLiteral("MethodParms")))); |
| 105 | + |
| 106 | + for (final Method method : intf.getMethods()) { |
| 107 | + if (ProxyUtil.isMethodInInterface(intf, method)) { |
| 108 | + final Class<?>[] parameterTypes = method.getParameterTypes(); |
| 109 | + final VariableReference[] objects = new VariableReference[parameterTypes.length]; |
| 110 | + final BlockBuilder<ElseBlockBuilder> blockBuilder = If |
| 111 | + .cond(Stmt.loadLiteral(ProxyUtil.createCallSignature(intf, method)).invoke("equals", |
| 112 | + Stmt.loadVariable("commandType"))); |
| 113 | + |
| 114 | + for (int i = 0; i < parameterTypes.length; i++) { |
| 115 | + final Class<?> parameterType = parameterTypes[i]; |
| 116 | + blockBuilder.append(Stmt.declareVariable("var" + i, parameterType, |
| 117 | + Stmt.castTo(parameterType, Stmt.loadVariable("methodParms").invoke("get", i)))); |
| 118 | + objects[i] = Refs.get("var" + i); |
| 119 | + } |
| 120 | + |
| 121 | + final boolean hasReturnType = !method.getReturnType().equals(void.class); |
| 122 | + blockBuilder.append(Stmt.declareFinalVariable("instance", intf, controller.contextGetInstanceStmt())); |
| 123 | + final Statement methodInvocation = Stmt.nestedCall(Stmt.loadVariable("instance")).invoke(method.getName(), (Object[]) objects); |
| 124 | + blockBuilder.append(Stmt.try_() |
| 125 | + .append((hasReturnType) ? Stmt.declareFinalVariable("ret", method.getReturnType(), methodInvocation) : methodInvocation) |
| 126 | + .append((decorable.isEnclosingTypeDependent()) ? Stmt.loadVariable("context").invoke("destroyInstance", Refs.get("instance")) |
| 127 | + : EmptyStatement.INSTANCE) |
| 128 | + .append((hasReturnType) ? Stmt.invokeStatic(MessageBuilder.class, "createConversation", Stmt.loadVariable("message")) |
| 129 | + .invoke("subjectProvided").invoke("with", "MethodReply", Refs.get("ret")) |
| 130 | + .invoke("noErrorHandling").invoke("sendNowWith", Stmt.invokeStatic(ErraiBus.class, "get")) |
| 131 | + : EmptyStatement.INSTANCE) |
| 132 | + .finish().catch_(Throwable.class, "throwable") |
| 133 | + .append(Stmt.throw_(RuntimeException.class, Stmt.loadVariable("throwable"))).finish()); |
| 134 | + builder.append(blockBuilder.finish()); |
| 135 | + } |
| 136 | + } |
| 137 | + return builder.finish(); |
| 138 | + } |
| 139 | +} |
0 commit comments