/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AuditFactory
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.message.Message
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.GoidUpgradeMapper
 */
package com.l7tech.server.service.resolution;

import com.l7tech.gateway.common.audit.AuditFactory;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.message.Message;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.service.resolution.Result;
import com.l7tech.server.service.resolution.ServiceResolutionException;
import com.l7tech.server.service.resolution.ServiceResolver;
import com.l7tech.util.GoidUpgradeMapper;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public abstract class NameValueServiceResolver<T>
extends ServiceResolver<T> {
    protected final Map<T, Map<Goid, PublishedService>> _valueToServiceMapMap = new HashMap<T, Map<Goid, PublishedService>>();
    protected final Map<Serializable, List<T>> serviceIdToValueListMap = new HashMap<Serializable, List<T>>();
    protected final ReadWriteLock _rwlock = new ReentrantReadWriteLock(false);

    public NameValueServiceResolver(AuditFactory auditFactory) {
        super(auditFactory);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void serviceDeleted(PublishedService publishedService) {
        Goid goid = publishedService.getGoid();
        this._rwlock.writeLock().lock();
        try {
            this.serviceIdToValueListMap.remove(goid);
            if (GoidUpgradeMapper.prefixMatches((EntityType)EntityType.SERVICE, (long)goid.getHi())) {
                this.serviceIdToValueListMap.remove(goid.getLow());
            }
            for (Map<Goid, PublishedService> map : this._valueToServiceMapMap.values()) {
                map.remove(goid);
            }
        }
        finally {
            this._rwlock.writeLock().unlock();
        }
    }

    @Override
    public void serviceUpdated(PublishedService publishedService) throws ServiceResolutionException {
        this._rwlock.writeLock().lock();
        try {
            this.serviceDeleted(publishedService);
            this.serviceCreated(publishedService);
        }
        finally {
            this._rwlock.writeLock().unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    protected void updateServiceValues(PublishedService publishedService, List<T> list) {
        Goid goid = publishedService.getGoid();
        this._rwlock.writeLock().lock();
        try {
            this.serviceIdToValueListMap.put((Serializable)goid, list);
            if (GoidUpgradeMapper.prefixMatches((EntityType)EntityType.SERVICE, (long)goid.getHi())) {
                this.serviceIdToValueListMap.put(Long.valueOf(goid.getLow()), list);
            }
            for (T t2 : list) {
                Map<Goid, PublishedService> map = this.getServiceMap(t2);
                map.put(goid, publishedService);
            }
        }
        finally {
            this._rwlock.writeLock().unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected List<T> getTargetValues(PublishedService publishedService) throws ServiceResolutionException {
        List<T> list;
        try {
            if (Goid.isDefault((Goid)publishedService.getGoid())) {
                return this.buildTargetValues(publishedService);
            }
        }
        catch (ServiceResolutionException serviceResolutionException) {
            throw NameValueServiceResolver.a(serviceResolutionException);
        }
        Goid goid = publishedService.getGoid();
        Lock lock = this._rwlock.readLock();
        lock.lock();
        try {
            List<T> list2 = this.serviceIdToValueListMap.get(goid);
            if (list2 == null) {
                list2 = this.buildTargetValues(publishedService);
                lock.unlock();
                lock = null;
                this._rwlock.writeLock().lock();
                try {
                    this.serviceIdToValueListMap.put((Serializable)goid, list2);
                    if (GoidUpgradeMapper.prefixMatches((EntityType)EntityType.SERVICE, (long)goid.getHi())) {
                        this.serviceIdToValueListMap.put(Long.valueOf(goid.getLow()), list2);
                    }
                }
                finally {
                    this._rwlock.writeLock().unlock();
                }
            }
            list = list2;
        }
        catch (Throwable throwable) {
            try {
                if (lock != null) {
                    lock.unlock();
                }
            }
            catch (ServiceResolutionException serviceResolutionException) {
                throw NameValueServiceResolver.a(serviceResolutionException);
            }
            throw throwable;
        }
        try {
            if (lock != null) {
                lock.unlock();
            }
        }
        catch (ServiceResolutionException serviceResolutionException) {
            throw NameValueServiceResolver.a(serviceResolutionException);
        }
        return list;
    }

    protected abstract T getRequestValue(Message var1) throws ServiceResolutionException;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected Map<Goid, PublishedService> getServiceMap(T t2) {
        Lock lock = this._rwlock.readLock();
        lock.lock();
        try {
            Map<Goid, PublishedService> map = this._valueToServiceMapMap.get(t2);
            if (map == null) {
                map = new HashMap<Goid, PublishedService>();
                lock.unlock();
                lock = null;
                this._rwlock.writeLock().lock();
                try {
                    this._valueToServiceMapMap.put(t2, map);
                }
                finally {
                    this._rwlock.writeLock().unlock();
                }
            }
            Map<Goid, PublishedService> map2 = map;
            return map2;
        }
        finally {
            if (lock != null) {
                lock.unlock();
            }
        }
    }

    @Override
    public void populateResolutionParameters(Message message, Map<String, Object> map) throws ServiceResolutionException {
        if (this.isApplicableToMessage(message)) {
            T t2 = this.getRequestValue(message);
            map.put(this.PROP_VALUE, t2);
        }
    }

    @Override
    public Collection<Map<String, Object>> generateResolutionParameters(PublishedService publishedService, Collection<Map<String, Object>> collection) throws ServiceResolutionException {
        try {
            if (!this.isApplicableToConflicts()) {
                return collection;
            }
        }
        catch (ServiceResolutionException serviceResolutionException) {
            throw NameValueServiceResolver.a(serviceResolutionException);
        }
        return super.generateResolutionParameters(publishedService, collection);
    }

    @Override
    public final Result resolve(Map<String, Object> map, Collection<PublishedService> collection) throws ServiceResolutionException {
        try {
            if (!map.containsKey(this.PROP_VALUE)) {
                return Result.NOT_APPLICABLE;
            }
        }
        catch (ServiceResolutionException serviceResolutionException) {
            throw NameValueServiceResolver.a(serviceResolutionException);
        }
        Object object = map.get(this.PROP_VALUE);
        return this.resolve(object, collection);
    }

    protected Result resolve(T t2, Collection collection) throws ServiceResolutionException {
        Map<Goid, PublishedService> map;
        block14: {
            map = this.getServiceMap(t2);
            try {
                try {
                    if (map != null && !map.isEmpty()) break block14;
                }
                catch (ServiceResolutionException serviceResolutionException) {
                    throw NameValueServiceResolver.a(serviceResolutionException);
                }
                return Result.NO_MATCH;
            }
            catch (ServiceResolutionException serviceResolutionException) {
                throw NameValueServiceResolver.a(serviceResolutionException);
            }
        }
        HashSet<PublishedService> hashSet = new HashSet<PublishedService>();
        for (PublishedService publishedService : map.values()) {
            if (!collection.contains(publishedService)) continue;
            List<T> list = this.getTargetValues(publishedService);
            for (T t3 : list) {
                try {
                    block15: {
                        try {
                            try {
                                try {
                                    if (t3 != null && t3.equals(t2)) break block15;
                                }
                                catch (ServiceResolutionException serviceResolutionException) {
                                    throw NameValueServiceResolver.a(serviceResolutionException);
                                }
                                if (t3 != null) continue;
                            }
                            catch (ServiceResolutionException serviceResolutionException) {
                                throw NameValueServiceResolver.a(serviceResolutionException);
                            }
                            if (t2 != null) continue;
                        }
                        catch (ServiceResolutionException serviceResolutionException) {
                            throw NameValueServiceResolver.a(serviceResolutionException);
                        }
                    }
                    hashSet.add(publishedService);
                }
                catch (ServiceResolutionException serviceResolutionException) {
                    throw NameValueServiceResolver.a(serviceResolutionException);
                }
            }
        }
        return new Result(hashSet);
    }

    protected boolean isApplicableToConflicts() {
        return false;
    }

    protected abstract boolean isApplicableToMessage(Message var1) throws ServiceResolutionException;

    private static ServiceResolutionException a(ServiceResolutionException serviceResolutionException) {
        return serviceResolutionException;
    }
}

