|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2020 Michael Zhang <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 7 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 10 | + * Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 14 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 15 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +package net.devh.boot.grpc.client.stubfactory; |
| 19 | + |
| 20 | +import java.lang.reflect.Constructor; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.lang.reflect.Modifier; |
| 23 | +import java.lang.reflect.Parameter; |
| 24 | + |
| 25 | +import org.springframework.beans.BeanInstantiationException; |
| 26 | + |
| 27 | +import io.grpc.Channel; |
| 28 | +import io.grpc.stub.AbstractStub; |
| 29 | + |
| 30 | +/** |
| 31 | + * The StubFactory which tries to find a suitable factory method or constructor as a last resort. This factory will |
| 32 | + * always be the last one that is attempted. |
| 33 | + * |
| 34 | + * @author Daniel Theuke ([email protected]) |
| 35 | + */ |
| 36 | +public final class FallbackStubFactory implements StubFactory { |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean isApplicable(final Class<? extends AbstractStub<?>> stubType) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public AbstractStub<?> createStub(final Class<? extends AbstractStub<?>> stubType, final Channel channel) { |
| 45 | + try { |
| 46 | + // Search for public static *Grpc#new*Stub(Channel) |
| 47 | + final Class<?> declaringClass = stubType.getDeclaringClass(); |
| 48 | + if (declaringClass != null) { |
| 49 | + for (final Method method : declaringClass.getMethods()) { |
| 50 | + final String name = method.getName(); |
| 51 | + final int modifiers = method.getModifiers(); |
| 52 | + final Parameter[] parameters = method.getParameters(); |
| 53 | + if (name.startsWith("new") && name.endsWith("Stub") |
| 54 | + && Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers) |
| 55 | + && method.getReturnType().isAssignableFrom(stubType) |
| 56 | + && parameters.length == 1 |
| 57 | + && Channel.class.equals(parameters[0].getType())) { |
| 58 | + return AbstractStub.class.cast(method.invoke(null, channel)); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Search for a public constructor *Stub(Channel) |
| 64 | + final Constructor<? extends AbstractStub<?>> constructor = stubType.getConstructor(Channel.class); |
| 65 | + return constructor.newInstance(channel); |
| 66 | + |
| 67 | + } catch (final Exception e) { |
| 68 | + throw new BeanInstantiationException(stubType, "Failed to create gRPC client via FallbackStubFactory", e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments