summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/xml/BundleInstantiationSpecificationBuilder.java
blob: 544f4f93c694f85ef31e61dcbac65c9c1d6f9f23 (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 2016 Yahoo Inc. 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.processing.handler.ProcessingHandler;
import com.yahoo.search.handler.SearchHandler;
import org.w3c.dom.Element;

import java.util.*;

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

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

        BundleInstantiationSpecification instSpec = new BundleInstantiationSpecification(id, classId, bundle);
        if ( ! legacyMode) // TODO: Remove?
            validate(instSpec);

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

    private static BundleInstantiationSpecification setBundleForKnownClass(BundleInstantiationSpecification spec) {
        return BundleMapper.getBundle(spec.getClassName()).
                map(spec::inBundle).
                orElse(spec);
    }


    private static void validate(BundleInstantiationSpecification instSpec) {
        List<String> forbiddenClasses = Arrays.asList(
                "com.yahoo.search.handler.SearchHandler",
                "com.yahoo.processing.handler.ProcessingHandler");

        for (String forbiddenClass: forbiddenClasses) {
            if (forbiddenClass.equals(instSpec.getClassName())) {
                throw new RuntimeException("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;
    }

}