aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/xml/BundleInstantiationSpecificationBuilder.java
blob: 1323506eaeb3de14200ec2cc51710815223c0381 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.xml;

import com.yahoo.config.model.builder.xml.XmlHelper;
import com.yahoo.container.bundle.BundleInstantiationSpecification;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.vespa.model.container.PlatformBundles;
import org.w3c.dom.Element;

import java.util.List;

import static com.yahoo.vespa.model.container.component.chain.ProcessingHandler.PROCESSING_HANDLER_CLASS;

/**
 * This object builds a bundle instantiation spec from an XML element.
 *
 * @author gjoranv
 */
public class BundleInstantiationSpecificationBuilder {

    public static BundleInstantiationSpecification build(Element spec) {
        ComponentSpecification id = XmlHelper.getIdRef(spec);
        ComponentSpecification classId = getComponentSpecification(spec, "class");
        ComponentSpecification bundle = getComponentSpecification(spec, "bundle");

        BundleInstantiationSpecification instSpec = new BundleInstantiationSpecification(id, classId, bundle);
        validate(instSpec);

        return bundle == null ? setBundleForComponent(instSpec) : instSpec;
    }

    private static BundleInstantiationSpecification setBundleForComponent(BundleInstantiationSpecification spec) {
        if (PlatformBundles.isSearchAndDocprocClass(spec.getClassName()))
            return spec.inBundle(PlatformBundles.SEARCH_AND_DOCPROC_BUNDLE);
        else if (PlatformBundles.isModelIntegrationClass(spec.getClassName()))
            return spec.inBundle(PlatformBundles.MODEL_INTEGRATION_BUNDLE);
        else
            return spec;
    }

    private static void validate(BundleInstantiationSpecification instSpec) {
        List<String> forbiddenClasses = List.of(SearchHandler.HANDLER_CLASSNAME, PROCESSING_HANDLER_CLASS);

        for (String forbiddenClass: forbiddenClasses) {
            if (forbiddenClass.equals(instSpec.getClassName())) {
                throw new IllegalArgumentException("Setting up " + forbiddenClass + " manually is not supported");
            }
        }
    }

    // null if missing
    private static ComponentSpecification getComponentSpecification(Element spec, String attributeName) {
        return (spec.hasAttribute(attributeName)) ?
                new ComponentSpecification(spec.getAttribute(attributeName)) :
                null;
    }

}