001/**
002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
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 */
016package org.talend.sdk.component.runtime.manager.xbean;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.xbean.finder.filter.Filter;
022
023import lombok.Getter;
024
025public class KnownClassesFilter implements Filter { // one easy and efficient solution for fatjars
026
027    public static final Filter INSTANCE = new KnownClassesFilter();
028
029    @Getter
030    private final Filter delegateSkip;
031
032    private KnownClassesFilter() {
033        final Set<String> excluded = new HashSet<>();
034        excluded.add("avro.shaded");
035        excluded.add("com.codehale.metrics");
036        excluded.add("com.ctc.wstx");
037        excluded.add("com.datastax.driver");
038        excluded.add("com.fasterxml.jackson");
039        excluded.add("com.google.common");
040        excluded.add("com.google.thirdparty");
041        excluded.add("com.ibm.wsdl");
042        excluded.add("com.jcraft.jsch");
043        excluded.add("com.kenai");
044        excluded.add("com.sun.istack");
045        excluded.add("com.sun.xml");
046        excluded.add("com.talend.shaded");
047        excluded.add("com.thoughtworks");
048        excluded.add("io.jsonwebtoken");
049        excluded.add("io.netty");
050        excluded.add("io.swagger");
051        excluded.add("javax");
052        excluded.add("jnr");
053        excluded.add("junit");
054        excluded.add("net.sf.ehcache");
055        excluded.add("net.shibboleth");
056        excluded.add("org.aeonbits.owner");
057        excluded.add("org.apache");
058        excluded.add("org.bouncycastle");
059        excluded.add("org.codehaus");
060        excluded.add("org.cryptacular");
061        excluded.add("org.eclipse");
062        excluded.add("org.fusesource");
063        excluded.add("org.h2");
064        excluded.add("org.hamcrest");
065        excluded.add("org.hsqldb");
066        excluded.add("org.jasypt");
067        excluded.add("org.jboss");
068        excluded.add("org.joda");
069        excluded.add("org.jose4j");
070        excluded.add("org.junit");
071        excluded.add("org.jvnet");
072        excluded.add("org.metatype");
073        excluded.add("org.objectweb");
074        excluded.add("org.openejb");
075        excluded.add("org.opensaml");
076        excluded.add("org.slf4j");
077        excluded.add("org.swizzle");
078        excluded.add("org.terracotta");
079        excluded.add("org.tukaani");
080        excluded.add("org.yaml");
081        excluded.add("serp");
082
083        delegateSkip = new OptimizedExclusionFilter(excluded);
084    }
085
086    @Override
087    public boolean accept(final String name) {
088        return !delegateSkip.accept(name);
089
090    }
091
092    public static class OptimizedExclusionFilter implements Filter {
093
094        @Getter
095        private final Set<String> included;
096
097        private OptimizedExclusionFilter(final Set<String> exclusions) {
098            included = exclusions;
099        }
100
101        @Override
102        public boolean accept(final String name) {
103            int dot = name.indexOf('.');
104            while (dot > 0) {
105                if (included.contains(name.substring(0, dot))) {
106                    return true;
107                }
108                dot = name.indexOf('.', dot + 1);
109            }
110            return included.contains(name);
111        }
112    }
113}