aboutsummaryrefslogtreecommitdiffstats
path: root/config-application-package/src/main/java/com/yahoo/config/model/application/provider/Bundle.java
blob: c4fb7c29e7fbc1dbd725b84f79a7238af2afb5e8 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.application.provider;

import com.yahoo.collections.Tuple2;
import com.yahoo.vespa.config.util.ConfigUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;

/**
 * A Bundle represents an OSGi bundle inside the model, and provides utilities
 * for accessing resources within that bundle.
 *
 * @author Tony Vaagenes, Ulf Lilleengen
 * @since 5.1
 */
public class Bundle {
    private static final Logger log = Logger.getLogger(Bundle.class.getName());
    private static final String DEFPATH = "configdefinitions/"; // path inside jar file
    private final File bundleFile;
    private final JarFile jarFile;
    private final List<DefEntry> defEntries;

    public Bundle(JarFile jarFile, File bundleFile) {
        this.jarFile = jarFile;
        this.bundleFile = bundleFile;
        defEntries = findDefEntries();
    }

    public static List<Bundle> getBundles(File bundleDir) {
        try {
            List<Bundle> bundles =  new ArrayList<>();
            for (File bundleFile : getBundleFiles(bundleDir)) {
                JarFile jarFile;
                try {
                    jarFile = new JarFile(bundleFile);
                } catch (ZipException e) {
                    throw new IllegalArgumentException("Error opening jar file '" + bundleFile.getName() +
                            "'. Please check that this is a valid jar file");
                }
                bundles.add(new Bundle(jarFile, bundleFile));
            }
            return bundles;
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }

    private static List<File> getBundleFiles(File bundleDir) {
        if (!bundleDir.isDirectory()) {
            return new ArrayList<>();
        }
        return Arrays.asList(bundleDir.listFiles((dir, name) -> name.endsWith(".jar")));
    }

    public List<DefEntry> getDefEntries() {
        return Collections.unmodifiableList(defEntries);
    }

    /**
     * Returns a list of all .def-file entries in this Component.
     * @return  A list of .def-file entries.
     */
    private List<DefEntry> findDefEntries() {
        List<DefEntry> defEntries = new ArrayList<>();

        ZipEntry defDir = jarFile.getEntry(DEFPATH);

        if ((defDir == null) || !defDir.isDirectory())
            return defEntries;

        for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
            JarEntry entry = entries.nextElement();
            String name = entry.getName();

            if (name.endsWith(".def")) {
                if (name.matches("^" + DEFPATH + ".*\\.def$")) {
                    defEntries.add(new DefEntry(this, entry));
                } else {
                    log.info("Config definition file '" + name + "' in component '" + jarFile.getName() +
                            "' will not be used. Files must reside in the '" + DEFPATH +
                            "' directory in the .jar file");

                }
            }
        }
        return defEntries;
    }

    public JarFile getJarFile() {
        return jarFile;
    }

    public File getFile() {
        return bundleFile;
    }

    /**
     * Represents a def-file inside a Component. Immutable.
     */
    public static class DefEntry {

        private final Bundle bundle;
        private final ZipEntry zipEntry;
        public final String defName;  // Without version number and suffix.
        public final String defNamespace;
        public final String contents;

        /**
         * @param bundle      The bundle this def entry belongs to.
         * @param zipEntry    The ZipEntry representing the def-file.
         */
        public DefEntry(Bundle bundle, ZipEntry zipEntry) {
            this.bundle = bundle;
            this.zipEntry = zipEntry;

            String entryName = zipEntry.getName();
            Tuple2<String, String> nameAndNamespace = ConfigUtils.getNameAndNamespaceFromString(entryName.substring(DEFPATH.length(), entryName.indexOf(".def")));

            defName = nameAndNamespace.first;
            defNamespace = getNamespace();
            if (defNamespace.isEmpty())
                throw new IllegalArgumentException("Config definition '" + defName + "' is missing a package (or namespace)");
            contents = getContents();
        }

        /**
         * Returns the namespace of the .def-file, as given by the "namespace=" statement inside the given entry.
         * @return  The namespace string, or "" (empty string) if no namespace exists
         */
        private String getNamespace() {
            return ConfigUtils.getDefNamespace(getReader());
        }

        private String getContents() {
            StringBuilder ret = new StringBuilder("");
            BufferedReader reader = new BufferedReader(getReader());
            try {
                String str = reader.readLine();
                while (str != null){
                    ret.append(str);
                    str = reader.readLine();
                    if (str != null) {
                        ret.append("\n");
                    }
                }
                reader.close();
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed reading contents of def-file '" + defName +
                        ".def in component " + bundle.jarFile.getName(),e);
            }
            return ret.toString();
        }

        public Reader getReader() {
            if (zipEntry == null) {
                return new StringReader("");
            }
            try {
                return new InputStreamReader(bundle.jarFile.getInputStream(zipEntry), StandardCharsets.UTF_8);
            }  catch (IOException e) {
                throw new IllegalArgumentException("IOException", e);
            }
        }

    }
}