aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/main/java/com/yahoo/application/Application.java
blob: 92ce73087bde40f99c65b50e25b81c368832662f (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application;

import ai.vespa.rankingexpression.importer.configmodelview.MlModelImporter;
import ai.vespa.rankingexpression.importer.lightgbm.LightGBMImporter;
import ai.vespa.rankingexpression.importer.onnx.OnnxImporter;
import ai.vespa.rankingexpression.importer.tensorflow.TensorFlowImporter;
import ai.vespa.rankingexpression.importer.vespa.VespaImporter;
import ai.vespa.rankingexpression.importer.xgboost.XGBoostImporter;
import com.yahoo.api.annotations.Beta;
import com.yahoo.application.container.JDisc;
import com.yahoo.application.container.impl.StandaloneContainerRunner;
import com.yahoo.application.content.ContentCluster;
import com.yahoo.config.ConfigInstance;
import com.yahoo.config.InnerNode;
import com.yahoo.config.InnerNodeVector;
import com.yahoo.config.LeafNode;
import com.yahoo.config.LeafNodeVector;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.NullConfigModelRegistry;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.docproc.DocumentProcessor;
import com.yahoo.io.IOUtils;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.service.ClientProvider;
import com.yahoo.jdisc.service.ServerProvider;
import com.yahoo.search.Searcher;
import com.yahoo.search.query.profile.compiled.CompiledQueryProfileRegistry;
import com.yahoo.search.query.profile.config.QueryProfileXMLReader;
import com.yahoo.search.rendering.Renderer;
import com.yahoo.text.StringUtilities;
import com.yahoo.text.Utf8;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.yolean.Exceptions;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.net.BindException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;

/**
 * Contains one or more containers built from services.xml.
 * Other services present in the services.xml file might be mocked in future versions.
 *
 * Currently, only a single top level JDisc Container is allowed. Other clusters are ignored.
 *
 * @author Tony Vaagenes
 */
@Beta
public final class Application implements AutoCloseable {

    /**
     * This system property is set to "true" upon creation of an Application.
     * This is useful for components which are created by dependency injection which needs to modify
     * their behavior to function without reliance on any processes outside the JVM.
     */
    public static final String vespaLocalProperty = "vespa.local";

    private final JDisc container;
    private final List<ContentCluster> contentClusters;
    private final Path path;
    private final boolean deletePathWhenClosing;
    private final CompiledQueryProfileRegistry compiledQueryProfileRegistry;

    // For internal use only
    Application(Path path, Networking networking, boolean deletePathWhenClosing) {
        System.setProperty(vespaLocalProperty, "true");
        this.path = path;
        this.deletePathWhenClosing = deletePathWhenClosing;
        contentClusters = ContentCluster.fromPath(path);
        container = JDisc.fromPath(path, networking, createVespaModel().configModelRepo());
        compiledQueryProfileRegistry = readQueryProfilesFromApplicationPackage(path);
    }

    @Beta
    public static Application fromBuilder(Builder builder) throws Exception {
        return builder.build();
    }

    /**
     * Factory method to create an Application from an XML String. Note that any components that are referenced in
     * the XML must be present on the classpath. To deploy OSGi bundles in memory,
     * use {@link Application#fromApplicationPackage(Path, Networking)}.
     *
     * @param xml the XML configuration to use
     * @return a new JDisc instance
     */
    public static Application fromServicesXml(String xml, Networking networking) {
        Path applicationDir = StandaloneContainerRunner.createApplicationPackage(xml);
        return new Application(applicationDir, networking, true);
    }

    /**
     * Factory method to create an Application from an application package.
     * This method allows deploying OSGi bundles(contained in the components subdirectory).
     * All the OSGi bundles will share the same class loader.
     *
     * @param path the reference to the application package to use
     * @return a new JDisc instance
     */
    public static Application fromApplicationPackage(Path path, Networking networking) {
        return new Application(path, networking, false);
    }

    /**
     * Factory method to create an Application from an application package.
     * This method allows deploying OSGi bundles(contained in the components subdirectory).
     * All the OSGi bundles will share the same class loader.
     *
     * @param file the reference to the application package to use
     * @return a new JDisc instance
     */
    public static Application fromApplicationPackage(File file, Networking networking) {
        return fromApplicationPackage(file.toPath(), networking);
    }

    private CompiledQueryProfileRegistry readQueryProfilesFromApplicationPackage(Path path) {
        String queryProfilePath = path + "/search/query-profiles";
        QueryProfileXMLReader queryProfileXMLReader = new QueryProfileXMLReader();

        File f = new File(queryProfilePath);
        if(f.exists() && f.isDirectory()) {
            return queryProfileXMLReader.read(queryProfilePath).compile();
        }
        return CompiledQueryProfileRegistry.empty;
    }

    private VespaModel createVespaModel() {
        try {
            List<MlModelImporter> modelImporters = List.of(new VespaImporter(),
                                                           new TensorFlowImporter(),
                                                           new OnnxImporter(),
                                                           new XGBoostImporter(),
                                                           new LightGBMImporter());
            DeployState deployState = new DeployState.Builder()
                    .applicationPackage(FilesApplicationPackage.fromFile(path.toFile(), true))
                    .modelImporters(modelImporters)
                    .deployLogger((level, s) -> { })
                    .accessLoggingEnabledByDefault(false)
                    .build();
            return new VespaModel(new NullConfigModelRegistry(), deployState);
        } catch (IOException | SAXException e) {
            throw new IllegalArgumentException("Error creating application from '" + path + "'", e);
        }
    }

    /**
     * @param id from the jdisc element in services xml. Default id in services.xml is "jdisc"
     */
    public JDisc getJDisc(String id) {
        return container;
    }

    public CompiledQueryProfileRegistry getCompiledQueryProfileRegistry() {
        return compiledQueryProfileRegistry;
    }

    /**
     * Shuts down all services.
     */
    @Override
    public void close() {
        container.close();
        IOUtils.recursiveDeleteDir(new File(path.toFile(), "models.generated"));
        if (deletePathWhenClosing)
            IOUtils.recursiveDeleteDir(path.toFile());
    }

    /**
     * A wrapper around ApplicationBuilder that generates a services.xml
     */
    @Beta
    public static class Builder {

        private static final ThreadLocal<Random> random = new ThreadLocal<>();
        private static final String DEFAULT_CHAIN = "default";

        private final Map<String, Container> containers = new LinkedHashMap<>();
        private final Path path;
        private Networking networking = Networking.disable;

        public Builder() throws IOException {
            this.path = makeTempDir("app", "standalone").toPath();
        }

        public Builder container(String id, Container container) {
            if (containers.size() > 0) {
                throw new RuntimeException("Only a single JDisc container is currently supported.");
            }
            containers.put(id, container);
            return this;
        }

        /**
         * Create a temporary directory using @{link File.createTempFile()}, but creating
         * a directory instead of a file.
         *
         * @param prefix directory prefix
         * @param suffix directory suffix
         * @return The created directory
         * @throws IOException if the temporary directory could not be created
         */
        private static File makeTempDir(String prefix, String suffix) throws IOException {
            File tmpDir = File.createTempFile(prefix, suffix, getTempDir());
            if (!tmpDir.delete()) {
                throw new RuntimeException("Could not delete temp directory: " + tmpDir);
            }
            if (!tmpDir.mkdirs()) {
                throw new RuntimeException("Could not create temp directory: " + tmpDir);
            }
            return tmpDir;
        }

        /**
         * Get the temporary directory
         *
         * @return The temporary directory File object
         */
        private static File getTempDir() {
            String rootPath = getResourceFile("/");
            String tmpPath = rootPath + "/tmp/";
            File tmpDir = new File(tmpPath);
            if (!tmpDir.exists() && !tmpDir.mkdirs()) {
                if (!tmpDir.exists()) { // possible race condition may cause mkdirs() to fail, check a second time before failing
                    throw new RuntimeException("Could not create temp dir: " + tmpDir.getAbsolutePath());
                }
            }
            if (!tmpDir.isDirectory()) {
                throw new RuntimeException("Temp dir path is not a directory: " + tmpDir.getAbsolutePath());
            }
            return tmpDir;
        }

        /**
         * Get the file name (path) of a resource or fail if it can not be found
         *
         * @param resource Name of desired resource
         * @return Path of resource
         */
        private static String getResourceFile(String resource) {
            URL resourceUrl = Application.class.getResource(resource);
            if (resourceUrl == null || resourceUrl.getFile() == null || resourceUrl.getFile().isEmpty()) {
                throw new RuntimeException("Could not access resource: " + resource);
            }

            return resourceUrl.getFile();
        }

        // copy from com.yahoo.application.ApplicationBuilder
        private void createFile(Path path, String content) throws IOException {
            Files.createDirectories(path.getParent());
            Files.write(path, Utf8.toBytes(content));
        }

        /**
         * @return a random number between 2000 and 62000
         */
        private static int getRandomPort() {
            Random r = random.get();
            if (r == null) {
                r = new Random(System.currentTimeMillis());
                random.set(r);
            }
            return r.nextInt(60000) + 2000;
        }

        /**
         * @param name             name of document type (search definition)
         * @param schema add this search definition to the application
         * @throws java.io.IOException e.g.if file not found
         */
        public Builder documentType(String name, String schema) throws IOException {
            Path path = nestedResource(ApplicationPackage.SCHEMAS_DIR, name, ApplicationPackage.SD_NAME_SUFFIX);
            createFile(path, schema);
            return this;
        }

        public Builder expressionInclude(String name, String schema) throws IOException {
            Path path = nestedResource(ApplicationPackage.SCHEMAS_DIR, name, ApplicationPackage.RANKEXPRESSION_NAME_SUFFIX);
            createFile(path, schema);
            return this;
        }

        /**
         * @param name                  name of ranking expression
         * @param rankingExpressionContent add this ranking expression to the application
         * @throws java.io.IOException e.g.if file not found
         */
        public Builder rankExpression(String name, String rankingExpressionContent) throws IOException {
            Path path = nestedResource(ApplicationPackage.SCHEMAS_DIR, name, ApplicationPackage.RANKEXPRESSION_NAME_SUFFIX);
            createFile(path, rankingExpressionContent);
            return this;
        }

        /**
         * @param name         name of query profile
         * @param queryProfile add this query profile to the application
         * @return builder
         * @throws java.io.IOException e.g.if file not found
         */
        public Builder queryProfile(String name, String queryProfile) throws IOException {
            Path path = nestedResource(ApplicationPackage.QUERY_PROFILES_DIR, name, ".xml");
            createFile(path, queryProfile);
            return this;
        }

        /**
         * @param name             name of query profile type
         * @param queryProfileType add this query profile type to the application
         * @return builder
         * @throws java.io.IOException e.g.if file not found
         */
        public Builder queryProfileType(String name, String queryProfileType) throws IOException {
            Path path = nestedResource(ApplicationPackage.QUERY_PROFILE_TYPES_DIR, name, ".xml");
            createFile(path, queryProfileType);
            return this;
        }

        // copy from com.yahoo.application.ApplicationBuilder
        private Path nestedResource(com.yahoo.path.Path nestedPath, String name, String fileType) {
            String nameWithoutSuffix = StringUtilities.stripSuffix(name, fileType);
            return path.resolve(nestedPath.getRelative()).resolve(nameWithoutSuffix + fileType);
        }

        /**
         * @param networking enable or disable networking (disabled by default)
         * @return builder
         */
        public Builder networking(Networking networking) {
            this.networking = networking;
            return this;
        }

        // generate the services xml and load the container
        public Application build() throws Exception {
            Application app = null;
            Exception exception = null;

            // if we get a bind exception, then retry a few times (may conflict with parallel test runs)
            for (int i = 0; i < 5; i++) {
                try {
                    generateXml();
                    app = new Application(path, networking, true);
                    break;
                } catch (Error e) { // the container thinks this is really serious, in this case is it not in the cause is a BindException
                    // catch bind error and reset container
                    Optional<BindException> bindException = Exceptions.findCause(e, BindException.class);
                    if (bindException.isPresent()) {
                        exception = bindException.get();
                        com.yahoo.container.Container.resetInstance(); // this is needed to be able to recreate the container from config again
                    } else {
                        throw new Exception(e.getCause());
                    }
                }
            }

            if (app == null) {
                throw exception;
            }
            return app;
        }

        private void generateXml() throws Exception {
            try (PrintWriter xml = new PrintWriter(Files.newOutputStream(path.resolve("services.xml")))) {
                xml.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                for (Map.Entry<String, Container> entry : containers.entrySet()) {
                    entry.getValue().build(xml, entry.getKey(), (networking == Networking.enable ? getRandomPort() : -1));
                }
            }
        }

        public static class Container {

            private final Map<String, List<ComponentItem<? extends DocumentProcessor>>> docprocs = new LinkedHashMap<>();
            private final Map<String, List<ComponentItem<? extends Searcher>>> searchers = new LinkedHashMap<>();
            private final List<ComponentItem<? extends Renderer>> renderers = new ArrayList<>();
            private final List<ComponentItem<? extends RequestHandler>> handlers = new ArrayList<>();
            private final List<ComponentItem<? extends ClientProvider>> clients = new ArrayList<>();
            private final List<ComponentItem<? extends ServerProvider>> servers = new ArrayList<>();
            private final List<ComponentItem<?>> components = new ArrayList<>();
            private final List<ConfigInstance> configs = new ArrayList<>();
            private boolean enableSearch = false;

            private static class ComponentItem<T> {
                private String id;
                private Class<? extends T> component;
                private List<ConfigInstance> configs = new ArrayList<>();

                public ComponentItem(String id, Class<? extends T> component, ConfigInstance... configs) {
                    this.id = id;
                    this.component = component;
                    if (configs != null) {
                        Collections.addAll(this.configs, configs);
                    }
                }
            }

            /**
             * @param docproc add this docproc to the default document processing chain
             * @return builder
             */
            public Container documentProcessor(Class<? extends DocumentProcessor> docproc) {
                return documentProcessor(DEFAULT_CHAIN, docproc);
            }

            /**
             * @param chainName chain name to add docproc
             * @param docproc   add this docproc to the document processing chain
             * @param configs   local docproc configs
             * @return builder
             */
            public Container documentProcessor(String chainName, Class<? extends DocumentProcessor> docproc, ConfigInstance... configs) {
                return documentProcessor(docproc.getName(), chainName, docproc, configs);
            }

            /**
             * @param id        component id
             * @param chainName chain name to add docproc
             * @param docproc   add this docproc to the document processing chain
             * @param configs   local docproc configs
             * @return builder
             */
            public Container documentProcessor(String id, String chainName, Class<? extends DocumentProcessor> docproc, ConfigInstance... configs) {
                List<ComponentItem<? extends DocumentProcessor>> chain = docprocs.get(chainName);
                if (chain == null) {
                    chain = new ArrayList<>();
                    docprocs.put(chainName, chain);
                }
                chain.add(new ComponentItem<>(id, docproc, configs));
                return this;
            }

            /**
             * @param enableSearch if true, enable search even without any searchers defined
             * @return builder
             */
            public Container search(boolean enableSearch) {
                this.enableSearch = enableSearch;
                return this;
            }

            /**
             * @param searcher add this searcher to the default search chain
             * @return builder
             */
            public Container searcher(Class<? extends Searcher> searcher) {
                return searcher(DEFAULT_CHAIN, searcher);
            }

            /**
             * @param chainName chain name to add searcher
             * @param searcher  add this searcher to the search chain
             * @param configs   local searcher configs
             * @return builder
             */
            public Container searcher(String chainName, Class<? extends Searcher> searcher, ConfigInstance... configs) {
                return searcher(searcher.getName(), chainName, searcher, configs);
            }

            /**
             * @param id        component id
             * @param chainName chain name to add searcher
             * @param searcher  add this searcher to the search chain
             * @param configs   local searcher configs
             * @return builder
             */
            public Container searcher(String id, String chainName, Class<? extends Searcher> searcher, ConfigInstance... configs) {
                List<ComponentItem<? extends Searcher>> chain = searchers.get(chainName);
                if (chain == null) {
                    chain = new ArrayList<>();
                    searchers.put(chainName, chain);
                }
                chain.add(new ComponentItem<>(id, searcher, configs));
                return this;
            }

            /**
             * @param id       component id, enable template with ?format=id or ?presentation.format=id
             * @param renderer add this renderer
             * @param configs  local renderer configs
             * @return builder
             */
            public Container renderer(String id, Class<? extends Renderer> renderer, ConfigInstance... configs) {
                renderers.add(new ComponentItem<>(id, renderer, configs));
                return this;
            }

            /**
             * @param binding binding string
             * @param handler the handler class
             * @return builder
             */
            public Container handler(String binding, Class<? extends RequestHandler> handler) {
                handlers.add(new ComponentItem<>(binding, handler));
                return this;
            }

            /**
             * @param binding binding string
             * @param client  the client class
             * @return builder
             */
            public Container client(String binding, Class<? extends ClientProvider> client) {
                clients.add(new ComponentItem<>(binding, client));
                return this;
            }

            /**
             * @param id     server compoent id
             * @param server the server class
             * @return builder
             */
            public Container server(String id, Class<? extends ServerProvider> server) {
                servers.add(new ComponentItem<>(id, server));
                return this;
            }

            /**
             * @param component make this component available to the container
             * @return builder
             */
            public Container component(Class<?> component) {
                return component(component.getName(), component, (ConfigInstance) null);
            }

            /**
             * @param component make this component available to the container
             * @return builder
             */
            public Container component(String id, Class<?> component, ConfigInstance... configs) {
                components.add(new ComponentItem<>(id, component, configs));
                return this;
            }

            /**
             * @param config add this config to the application
             * @return builder
             */
            public Container config(final ConfigInstance config) {
                configs.add(config);
                return this;
            }

            // generate services.xml based on this builder
            private void build(PrintWriter xml, String id, int port) throws Exception {
                xml.println("<container version=\"1.0\" id=\"" + id + "\">");

                if (port > 0) {
                    xml.println("<http>");
                    xml.println("<server id=\"http\" port=\"" + port + "\" />");
                    xml.println("</http>");
                }

                for (ComponentItem<? extends RequestHandler> entry : handlers) {
                    xml.println("<handler id=\"" + entry.component.getName() + "\">");
                    xml.println("<binding>" + entry.id + "</binding>");
                    xml.println("</handler>");
                }

                for (ComponentItem<? extends ClientProvider> entry : clients) {
                    xml.println("<client id=\"" + entry.component.getName() + "\">");
                    xml.println("<binding>" + entry.id + "</binding>");
                    xml.println("</client>");
                }

                for (ComponentItem<? extends ServerProvider> server : servers) {
                    generateComponent(xml, server, "server");
                }

                // container scoped configs
                for (ConfigInstance config : configs) {
                    generateConfig(xml, config);
                }

                for (ComponentItem<?> component : components) {
                    generateComponent(xml, component, "component");
                }

                if (!docprocs.isEmpty()) {
                    xml.println("<document-processing>");
                    for (Map.Entry<String, List<ComponentItem<? extends DocumentProcessor>>> entry : docprocs.entrySet()) {
                        xml.println("<chain id=\"" + entry.getKey() + "\">");
                        for (ComponentItem<? extends DocumentProcessor> docproc : entry.getValue()) {
                            generateComponent(xml, docproc, "documentprocessor");
                        }
                        xml.println("</chain>");
                    }
                    xml.println("</document-processing>");
                }

                if (enableSearch || !searchers.isEmpty() || !renderers.isEmpty()) {
                    xml.println("<search>");
                    for (Map.Entry<String, List<ComponentItem<? extends Searcher>>> entry : searchers.entrySet()) {
                        xml.println("<chain id=\"" + entry.getKey() + "\">");
                        for (ComponentItem<? extends Searcher> searcher : entry.getValue()) {
                            generateComponent(xml, searcher, "searcher");
                        }
                        xml.println("</chain>");
                    }
                    for (ComponentItem<? extends Renderer> renderer : renderers) {
                        generateComponent(xml, renderer, "renderer");
                    }
                    xml.println("</search>");
                }

                xml.println("<accesslog type=\"disabled\" />");
                xml.println("</container>");
            }

            private void generateComponent(PrintWriter xml, ComponentItem<?> componentItem, String elementName) throws Exception {
                xml.print("<" + elementName + " id=\"" + componentItem.id + "\" class=\"" + componentItem.component.getName() + "\"");
                if (componentItem.configs.isEmpty() || (!componentItem.configs.isEmpty() && componentItem.configs.get(0) == null)) {
                    xml.println(" />");
                } else {
                    xml.println(">");
                    for (ConfigInstance config : componentItem.configs) {
                        generateConfig(xml, config);
                    }
                    xml.println("</" + elementName + ">");
                }
            }

            // uses reflection to generate XML from a config object
            private void generateConfig(PrintWriter xml, ConfigInstance config) throws Exception {
                Field nameField = config.getClass().getField("CONFIG_DEF_NAME");
                String name = (String) nameField.get(config);
                Field namespaceField = config.getClass().getField("CONFIG_DEF_NAMESPACE");
                String namespace = (String) namespaceField.get(config);

                xml.println("<config name=\"" + namespace + "." + name + "\">");
                generateConfigNode(xml, config);
                xml.println("</config>");
            }

            private void generateConfigNode(PrintWriter xml, InnerNode node) throws Exception {
                // print all leaf nodes as config values
                Field[] fields = node.getClass().getDeclaredFields();
                for (Field field : fields) {
                    generateConfigField(xml, node, field);
                }
            }

            private void generateConfigField(PrintWriter xml, InnerNode node, Field field) throws Exception {
                field.setAccessible(true);
                if (LeafNode.class.isAssignableFrom(field.getType())) {
                    LeafNode<?> value = (LeafNode<?>) field.get(node);
                    if (value.value() != null) {
                        xml.print("<" + field.getName());
                        String v = value.getValue();
                        if (v.isEmpty()) {
                            xml.println(" />");
                        } else {
                            xml.println(">" + v + "</" + field.getName() + ">");
                        }
                    }
                } else if (InnerNode.class.isAssignableFrom(field.getType())) {
                    xml.println("<" + field.getName() + ">");
                    generateConfigNode(xml, (InnerNode) field.get(node));
                    xml.println("</" + field.getName() + ">");
                } else if (Map.class.isAssignableFrom(field.getType())) {
                    Map<?, ?> map = (Map<?, ?>) field.get(node);
                    if (!map.isEmpty()) {
                        xml.println("<" + field.getName() + ">");
                        for (Map.Entry<?, ?> entry : map.entrySet()) {
                            if (entry.getValue() instanceof InnerNode) {
                                xml.println("<item key=\"" + entry.getKey() + "\">");
                                generateConfigNode(xml, (InnerNode) entry.getValue());
                                xml.println("</item>");
                            } else if (entry.getValue() instanceof LeafNode) {
                                xml.println("<item key=\"" + entry.getKey() + "\">" + ((LeafNode<?>) entry.getValue()).getValue() + "</item>");
                            }
                        }
                        xml.println("</" + field.getName() + ">");
                    }
                } else if (InnerNodeVector.class.isAssignableFrom(field.getType())) {
                    InnerNodeVector<? extends InnerNode> vector = (InnerNodeVector<? extends InnerNode>) field.get(node);
                    if (!vector.isEmpty()) {
                        xml.println("<" + field.getName() + ">");
                        for (InnerNode innerNode : vector) {
                            xml.println("<item>");
                            generateConfigNode(xml, innerNode);
                            xml.println("</item>");
                        }
                        xml.println("</" + field.getName() + ">");
                    }
                } else if (LeafNodeVector.class.isAssignableFrom(field.getType())) {
                    LeafNodeVector<?, ? extends LeafNode<?>> vector = (LeafNodeVector<?, ? extends LeafNode<?>>) field.get(node);
                    if (!vector.isEmpty()) {
                        xml.println("<" + field.getName() + ">");
                        for (LeafNode<?> item : vector) {
                            xml.println("<item>" + item.getValue() + "</item>");
                        }
                        xml.println("</" + field.getName() + ">");
                    }
                }
            }
        }
    }

}