All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gh.bmd.jrt.proxy.builder.AbstractProxyBuilder Maven / Gradle / Ivy

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gh.bmd.jrt.proxy.builder;

import com.gh.bmd.jrt.builder.ProxyConfiguration;
import com.gh.bmd.jrt.builder.RoutineConfiguration;
import com.gh.bmd.jrt.builder.RoutineConfiguration.OrderType;
import com.gh.bmd.jrt.common.ClassToken;
import com.gh.bmd.jrt.common.WeakIdentityHashMap;
import com.gh.bmd.jrt.log.Logger;
import com.gh.bmd.jrt.time.TimeDuration;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;

import javax.annotation.Nonnull;

/**
 * Abstract implementation of a builder of async proxy objects.
 * 

* Created by davide-maestroni on 2/26/15. * * @param the interface type. */ public abstract class AbstractProxyBuilder implements ProxyBuilder, RoutineConfiguration.Configurable>, ProxyConfiguration.Configurable> { private static final WeakIdentityHashMap> sClassMap = new WeakIdentityHashMap>(); private ProxyConfiguration mProxyConfiguration = ProxyConfiguration.DEFAULT_CONFIGURATION; private RoutineConfiguration mRoutineConfiguration = RoutineConfiguration.DEFAULT_CONFIGURATION; @Nonnull public TYPE buildProxy() { synchronized (sClassMap) { final Object target = getTarget(); final WeakIdentityHashMap> classMap = sClassMap; HashMap classes = classMap.get(target); if (classes == null) { classes = new HashMap(); classMap.put(target, classes); } final RoutineConfiguration routineConfiguration = mRoutineConfiguration; final ProxyConfiguration proxyConfiguration = mProxyConfiguration; final ClassToken token = getInterfaceToken(); final ClassInfo classInfo = new ClassInfo(token, routineConfiguration, proxyConfiguration); final Object instance = classes.get(classInfo); if (instance != null) { return token.cast(instance); } warn(routineConfiguration); try { final TYPE newInstance = newProxy(routineConfiguration, proxyConfiguration); classes.put(classInfo, newInstance); return newInstance; } catch (final Throwable t) { throw new IllegalArgumentException(t); } } } @Nonnull public RoutineConfiguration.Builder> withRoutine() { return new RoutineConfiguration.Builder>(this, mRoutineConfiguration); } @Nonnull @SuppressWarnings("ConstantConditions") public ProxyBuilder setConfiguration(@Nonnull final RoutineConfiguration configuration) { if (configuration == null) { throw new NullPointerException("the configuration must not be null"); } mRoutineConfiguration = configuration; return this; } @Nonnull @SuppressWarnings("ConstantConditions") public ProxyBuilder setConfiguration(@Nonnull final ProxyConfiguration configuration) { if (configuration == null) { throw new NullPointerException("the proxy configuration must not be null"); } mProxyConfiguration = configuration; return this; } @Nonnull public ProxyConfiguration.Builder> withProxy() { return new ProxyConfiguration.Builder>(this, mProxyConfiguration); } /** * Returns the builder proxy class token. * * @return the proxy class token. */ @Nonnull protected abstract ClassToken getInterfaceToken(); /** * Returns the builder target object. * * @return the target object. */ @Nonnull protected abstract Object getTarget(); /** * Creates and return a new proxy instance. * * @param routineConfiguration the routine configuration. * @param proxyConfiguration the proxy configuration. * @return the proxy instance. */ @Nonnull protected abstract TYPE newProxy(@Nonnull final RoutineConfiguration routineConfiguration, @Nonnull final ProxyConfiguration proxyConfiguration); /** * Logs any warning related to ignored options in the specified configuration. * * @param configuration the routine configuration. */ private void warn(@Nonnull final RoutineConfiguration configuration) { Logger logger = null; final Object[] args = configuration.getFactoryArgsOr(null); if (args != null) { logger = configuration.newLogger(this); logger.wrn("the specified factory arguments will be ignored: %s", Arrays.toString(args)); } final OrderType inputOrderType = configuration.getInputOrderTypeOr(null); if (inputOrderType != null) { if (logger == null) { logger = configuration.newLogger(this); } logger.wrn("the specified input order type will be ignored: %s", inputOrderType); } final int inputSize = configuration.getInputMaxSizeOr(RoutineConfiguration.DEFAULT); if (inputSize != RoutineConfiguration.DEFAULT) { if (logger == null) { logger = configuration.newLogger(this); } logger.wrn("the specified maximum input size will be ignored: %d", inputSize); } final TimeDuration inputTimeout = configuration.getInputTimeoutOr(null); if (inputTimeout != null) { if (logger == null) { logger = configuration.newLogger(this); } logger.wrn("the specified input timeout will be ignored: %s", inputTimeout); } final OrderType outputOrderType = configuration.getOutputOrderTypeOr(null); if (outputOrderType != null) { if (logger == null) { logger = configuration.newLogger(this); } logger.wrn("the specified output order type will be ignored: %s", outputOrderType); } final int outputSize = configuration.getOutputMaxSizeOr(RoutineConfiguration.DEFAULT); if (outputSize != RoutineConfiguration.DEFAULT) { if (logger == null) { logger = configuration.newLogger(this); } logger.wrn("the specified maximum output size will be ignored: %d", outputSize); } final TimeDuration outputTimeout = configuration.getOutputTimeoutOr(null); if (outputTimeout != null) { if (logger == null) { logger = configuration.newLogger(this); } logger.wrn("the specified output timeout will be ignored: %s", outputTimeout); } } /** * Class used as key to identify a specific proxy instance. */ private static class ClassInfo { private final ProxyConfiguration mProxyConfiguration; private final RoutineConfiguration mRoutineConfiguration; private final Type mType; /** * Constructor. * * @param token the proxy interface token. * @param routineConfiguration the routine configuration. * @param proxyConfiguration the proxy configuration. */ private ClassInfo(@Nonnull final ClassToken token, @Nonnull final RoutineConfiguration routineConfiguration, @Nonnull final ProxyConfiguration proxyConfiguration) { mType = token.getRawClass(); mRoutineConfiguration = routineConfiguration; mProxyConfiguration = proxyConfiguration; } @Override public int hashCode() { // auto-generated code int result = mProxyConfiguration.hashCode(); result = 31 * result + mRoutineConfiguration.hashCode(); result = 31 * result + mType.hashCode(); return result; } @Override public boolean equals(final Object o) { // auto-generated code if (this == o) { return true; } if (!(o instanceof ClassInfo)) { return false; } final ClassInfo classInfo = (ClassInfo) o; return mProxyConfiguration.equals(classInfo.mProxyConfiguration) && mRoutineConfiguration.equals(classInfo.mRoutineConfiguration) && mType.equals(classInfo.mType); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy