001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of 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,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.impl;
020
021import java.io.Serializable;
022import java.lang.reflect.Proxy;
023import java.util.ArrayList;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Set;
027
028import org.nuxeo.ecm.automation.TypeAdapter;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class AdapterKeyedRegistry extends SuperKeyedRegistry<TypeAdapterKey, TypeAdapter> {
034
035    protected final Set<Class<?>> blacklist;
036
037    public AdapterKeyedRegistry() {
038        blacklist = new HashSet<>();
039        blacklist.add(Serializable.class);
040        blacklist.add(Cloneable.class);
041        blacklist.add(Comparable.class);
042    }
043
044    @Override
045    protected boolean isRoot(TypeAdapterKey key) {
046        return key.input == Object.class;
047    }
048
049    @Override
050    protected List<TypeAdapterKey> getSuperKeys(TypeAdapterKey key) {
051        List<TypeAdapterKey> result = new ArrayList<>();
052        Class<?> cl = key.input.getSuperclass();
053        if (cl != null) {
054            result.add(new TypeAdapterKey(cl, key.output));
055        }
056        for (Class<?> itf : key.input.getInterfaces()) {
057            if (!blacklist.contains(itf)) {
058                result.add(new TypeAdapterKey(itf, key.output));
059            }
060        }
061        return result;
062    }
063
064    @Override
065    protected boolean isCachingEnabled(TypeAdapterKey key) {
066        return !Proxy.isProxyClass(key.input);
067    }
068
069}