File: ManifestResource.java

package info (click to toggle)
tomcat9 9.0.115-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,140 kB
  • sloc: java: 383,595; xml: 71,225; jsp: 4,682; sh: 1,228; perl: 324; makefile: 18; ansic: 14
file content (221 lines) | stat: -rw-r--r-- 7,595 bytes parent folder | download
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.util;

import java.util.ArrayList;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

/**
 * Representation of a Manifest file and its available extensions and required extensions.
 */
public class ManifestResource {

    // ------------------------------------------------------------- Properties

    // These are the resource types for determining effect error messages
    public static final int SYSTEM = 1;
    public static final int WAR = 2;
    public static final int APPLICATION = 3;

    private ArrayList<Extension> availableExtensions = null;
    private ArrayList<Extension> requiredExtensions = null;

    private final String resourceName;
    private final int resourceType;

    public ManifestResource(String resourceName, Manifest manifest, int resourceType) {
        this.resourceName = resourceName;
        this.resourceType = resourceType;
        processManifest(manifest);
    }

    /**
     * Gets the name of the resource
     *
     * @return The name of the resource
     */
    public String getResourceName() {
        return resourceName;
    }

    /**
     * Gets the list of available extensions
     *
     * @return List of available extensions
     */
    public ArrayList<Extension> getAvailableExtensions() {
        return availableExtensions;
    }

    /**
     * Gets the list of required extensions
     *
     * @return List of required extensions
     */
    public ArrayList<Extension> getRequiredExtensions() {
        return requiredExtensions;
    }

    // --------------------------------------------------------- Public Methods

    /**
     * Gets the number of available extensions
     *
     * @return The number of available extensions
     */
    public int getAvailableExtensionCount() {
        return (availableExtensions != null) ? availableExtensions.size() : 0;
    }

    /**
     * Gets the number of required extensions
     *
     * @return The number of required extensions
     */
    public int getRequiredExtensionCount() {
        return (requiredExtensions != null) ? requiredExtensions.size() : 0;
    }

    /**
     * Returns <code>true</code> if all required extension dependencies have been meet for this
     * <code>ManifestResource</code> object.
     *
     * @return boolean true if all extension dependencies have been satisfied
     */
    public boolean isFulfilled() {
        if (requiredExtensions == null) {
            return true;
        }
        for (Extension ext : requiredExtensions) {
            if (!ext.isFulfilled()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("ManifestResource[");
        sb.append(resourceName);

        sb.append(", isFulfilled=");
        sb.append(isFulfilled() + "");
        sb.append(", requiredExtensionCount =");
        sb.append(getRequiredExtensionCount());
        sb.append(", availableExtensionCount=");
        sb.append(getAvailableExtensionCount());
        switch (resourceType) {
            case SYSTEM:
                sb.append(", resourceType=SYSTEM");
                break;
            case WAR:
                sb.append(", resourceType=WAR");
                break;
            case APPLICATION:
                sb.append(", resourceType=APPLICATION");
                break;
        }
        sb.append(']');
        return sb.toString();
    }


    // -------------------------------------------------------- Private Methods

    private void processManifest(Manifest manifest) {
        availableExtensions = getAvailableExtensions(manifest);
        requiredExtensions = getRequiredExtensions(manifest);
    }

    /**
     * Return the set of <code>Extension</code> objects representing optional packages that are required by the
     * application associated with the specified <code>Manifest</code>.
     *
     * @param manifest Manifest to be parsed
     *
     * @return List of required extensions, or null if the application does not require any extensions
     */
    private ArrayList<Extension> getRequiredExtensions(Manifest manifest) {

        Attributes attributes = manifest.getMainAttributes();
        String names = attributes.getValue("Extension-List");
        if (names == null) {
            return null;
        }

        ArrayList<Extension> extensionList = new ArrayList<>();
        names += " ";

        while (true) {

            int space = names.indexOf(' ');
            if (space < 0) {
                break;
            }
            String name = names.substring(0, space).trim();
            names = names.substring(space + 1);

            String value = attributes.getValue(name + "-Extension-Name");
            if (value == null) {
                continue;
            }
            Extension extension = new Extension();
            extension.setExtensionName(value);
            extension.setImplementationURL(attributes.getValue(name + "-Implementation-URL"));
            extension.setImplementationVendorId(attributes.getValue(name + "-Implementation-Vendor-Id"));
            String version = attributes.getValue(name + "-Implementation-Version");
            extension.setImplementationVersion(version);
            extension.setSpecificationVersion(attributes.getValue(name + "-Specification-Version"));
            extensionList.add(extension);
        }
        return extensionList;
    }

    /**
     * Return the set of <code>Extension</code> objects representing optional packages that are bundled with the
     * application associated with the specified <code>Manifest</code>.
     *
     * @param manifest Manifest to be parsed
     *
     * @return List of available extensions, or null if the web application does not bundle any extensions
     */
    private ArrayList<Extension> getAvailableExtensions(Manifest manifest) {

        Attributes attributes = manifest.getMainAttributes();
        String name = attributes.getValue("Extension-Name");
        if (name == null) {
            return null;
        }

        ArrayList<Extension> extensionList = new ArrayList<>();

        Extension extension = new Extension();
        extension.setExtensionName(name);
        extension.setImplementationURL(attributes.getValue("Implementation-URL"));
        extension.setImplementationVendor(attributes.getValue("Implementation-Vendor"));
        extension.setImplementationVendorId(attributes.getValue("Implementation-Vendor-Id"));
        extension.setImplementationVersion(attributes.getValue("Implementation-Version"));
        extension.setSpecificationVersion(attributes.getValue("Specification-Version"));

        extensionList.add(extension);

        return extensionList;
    }

}