• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aspectran / aspectran / #3986

15 Jan 2025 04:27AM CUT coverage: 35.056% (+0.05%) from 35.004%
#3986

push

github

topframe
Update

7 of 40 new or added lines in 6 files covered. (17.5%)

2 existing lines in 2 files now uncovered.

14194 of 40489 relevant lines covered (35.06%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.7
/core/src/main/java/com/aspectran/core/context/resource/ResourceManager.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.core.context.resource;
17

18
import com.aspectran.utils.PathUtils;
19
import com.aspectran.utils.ResourceUtils;
20
import com.aspectran.utils.StringUtils;
21
import com.aspectran.utils.annotation.jsr305.NonNull;
22

23
import java.io.File;
24
import java.io.IOException;
25
import java.net.URL;
26
import java.util.Arrays;
27
import java.util.Collections;
28
import java.util.Enumeration;
29
import java.util.Iterator;
30
import java.util.NoSuchElementException;
31
import java.util.Objects;
32
import java.util.jar.JarEntry;
33

34
import static com.aspectran.utils.ClassUtils.CLASS_FILE_SUFFIX;
35
import static com.aspectran.utils.ClassUtils.PACKAGE_SEPARATOR_CHAR;
36
import static com.aspectran.utils.PathUtils.REGULAR_FILE_SEPARATOR_CHAR;
37
import static com.aspectran.utils.ResourceUtils.CLASSPATH_URL_PREFIX;
38
import static com.aspectran.utils.ResourceUtils.FILE_URL_PREFIX;
39

40
/**
41
 * The Class ResourceManager.
42
 *
43
 * <p>Created: 2014. 12. 18 PM 5:51:13</p>
44
 */
45
public class ResourceManager {
46

47
    private final ResourceEntries resourceEntries = new ResourceEntries();
1✔
48

49
    ResourceManager() {
1✔
50
    }
1✔
51

52
    public URL getResource(String name) {
53
        return resourceEntries.get(name);
1✔
54
    }
55

56
    protected Iterator<URL> getResources() {
NEW
57
        return resourceEntries.values().iterator();
×
58
    }
59

60
    public int getNumberOfResources() {
NEW
61
        return resourceEntries.size();
×
62
    }
63

64
    protected void putResource(String resourceName, File file) throws InvalidResourceException {
NEW
65
        resourceEntries.putResource(resourceName, file);
×
NEW
66
    }
×
67

68
    protected void putResource(File file, JarEntry entry) throws InvalidResourceException {
NEW
69
        resourceEntries.putResource(file, entry);
×
NEW
70
    }
×
71

72
    public void reset() throws InvalidResourceException {
NEW
73
        release();
×
NEW
74
    }
×
75

76
    public void release() {
NEW
77
        resourceEntries.clear();
×
NEW
78
    }
×
79

80
    @NonNull
81
    public static Enumeration<URL> findResources(final Iterator<SiblingClassLoader> owners) {
82
        return new Enumeration<>() {
×
83
            private Iterator<URL> iter;
84
            private URL next;
85

86
            private boolean hasNext() {
87
                while (true) {
NEW
88
                    if (iter == null) {
×
89
                        if (!owners.hasNext()) {
×
90
                            return false;
×
91
                        }
NEW
92
                        iter = owners.next().getResourceManager().getResources();
×
93
                    }
NEW
94
                    if (iter.hasNext()) {
×
NEW
95
                        next = iter.next();
×
UNCOV
96
                        return true;
×
97
                    }
NEW
98
                    iter = null;
×
99
                }
100
            }
101

102
            @Override
103
            public synchronized boolean hasMoreElements() {
104
                return (next != null || hasNext());
×
105
            }
106

107
            @Override
108
            public synchronized URL nextElement() {
109
                if (next == null) {
×
110
                    if (!hasNext()) {
×
111
                        throw new NoSuchElementException();
×
112
                    }
113
                }
114
                URL current = next;
×
115
                next = null;
×
116
                return current;
×
117
            }
118
        };
119
    }
120

121
    @NonNull
122
    public static Enumeration<URL> findResources(String name, final Iterator<SiblingClassLoader> owners) {
123
        return findResources(name, owners, null);
1✔
124
    }
125

126
    @NonNull
127
    public static Enumeration<URL> findResources(
128
            String name, final Iterator<SiblingClassLoader> owners, final Enumeration<URL> parentResources) {
129
        if (owners == null || name == null) {
1✔
130
            return Collections.emptyEnumeration();
×
131
        }
132

133
        if (StringUtils.endsWith(name, REGULAR_FILE_SEPARATOR_CHAR)) {
1✔
134
            name = name.substring(0, name.length() - 1);
1✔
135
        }
136

137
        final String nameToSearch = name;
1✔
138

139
        return new Enumeration<>() {
1✔
140
            private URL next;
141
            private boolean noMore; //for parent
142

143
            private boolean hasNext() {
144
                do {
145
                    if (owners.hasNext()) {
1✔
146
                        next = owners.next().getResourceManager().getResource(nameToSearch);
1✔
147
                    } else {
148
                        return false;
1✔
149
                    }
150
                } while (next == null);
1✔
151
                return true;
×
152
            }
153

154
            @Override
155
            public boolean hasMoreElements() {
156
                if (!noMore) {
1✔
157
                    if (parentResources != null && parentResources.hasMoreElements()) {
1✔
158
                        return true;
1✔
159
                    } else {
160
                        noMore = true;
1✔
161
                    }
162
                }
163
                return (next != null || hasNext());
1✔
164
            }
165

166
            @Override
167
            public URL nextElement() {
168
                if (!noMore) {
1✔
169
                    if (parentResources != null && parentResources.hasMoreElements()) {
1✔
170
                        return parentResources.nextElement();
1✔
171
                    }
172
                }
173
                if (next == null) {
×
174
                    if (!hasNext()) {
×
175
                        throw new NoSuchElementException();
×
176
                    }
177
                }
178
                URL current = next;
×
179
                next = null;
×
180
                return current;
×
181
            }
182
        };
183
    }
184

185
    @NonNull
186
    public static String resourceNameToClassName(@NonNull String resourceName) {
187
        String className = resourceName.substring(0, resourceName.length() - CLASS_FILE_SUFFIX.length());
×
188
        className = className.replace(REGULAR_FILE_SEPARATOR_CHAR, PACKAGE_SEPARATOR_CHAR);
×
189
        return className;
×
190
    }
191

192
    @NonNull
193
    public static String classNameToResourceName(@NonNull String className) {
194
        return className.replace(PACKAGE_SEPARATOR_CHAR, REGULAR_FILE_SEPARATOR_CHAR)
1✔
195
                + CLASS_FILE_SUFFIX;
196
    }
197

198
    public static String packageNameToResourceName(@NonNull String packageName) {
199
        String resourceName = packageName.replace(PACKAGE_SEPARATOR_CHAR, REGULAR_FILE_SEPARATOR_CHAR);
×
200
        if (StringUtils.endsWith(resourceName, REGULAR_FILE_SEPARATOR_CHAR)) {
×
201
            resourceName = resourceName.substring(0, resourceName.length() - 1);
×
202
        }
203
        return resourceName;
×
204
    }
205

206
    public static String[] checkResourceLocations(String[] resourceLocations, String basePath)
207
            throws InvalidResourceException {
208
        if (resourceLocations == null || resourceLocations.length == 0) {
1✔
209
            return null;
×
210
        }
211

212
        String[] resourceLocationsToUse = resourceLocations.clone();
1✔
213
        for (int i = 0; i < resourceLocationsToUse.length; i++) {
1✔
214
            String tempLocation = resourceLocationsToUse[i];
1✔
215
            if (tempLocation != null) {
1✔
216
                tempLocation = PathUtils.cleanPath(tempLocation);
1✔
217
                if (StringUtils.endsWith(tempLocation, REGULAR_FILE_SEPARATOR_CHAR)) {
1✔
218
                    tempLocation = tempLocation.substring(0, tempLocation.length() - 1);
1✔
219
                }
220
                if (tempLocation.startsWith(CLASSPATH_URL_PREFIX)) {
1✔
221
                    String path = tempLocation.substring(CLASSPATH_URL_PREFIX.length());
1✔
222
                    try {
223
                        URL url = ResourceUtils.getResource(path);
1✔
224
                        tempLocation = url.getPath();
1✔
225
                    } catch (IOException e) {
×
226
                        throw new InvalidResourceException("Class path resource [" + tempLocation +
×
227
                                "] cannot be resolved to URL", e);
228
                    }
1✔
229
                } else if (tempLocation.startsWith(FILE_URL_PREFIX)) {
1✔
230
                    try {
231
                        URL url = ResourceUtils.toURL(tempLocation);
1✔
232
                        tempLocation = url.getFile();
1✔
233
                    } catch (Exception e) {
×
234
                        throw new InvalidResourceException("Resource location [" + tempLocation +
×
235
                                "] is neither a URL not a well-formed file path", e);
236
                    }
1✔
237
                } else {
238
                    try {
239
                        File f;
240
                        if (StringUtils.hasText(basePath)) {
1✔
241
                            f = new File(basePath, tempLocation);
1✔
242
                            if (!f.exists()) {
1✔
243
                                File f2 = new File(tempLocation);
1✔
244
                                if (f2.exists()) {
1✔
245
                                    f = f2;
×
246
                                }
247
                            }
1✔
248
                        } else {
249
                            f = new File(tempLocation);
×
250
                        }
251
                        tempLocation = f.getCanonicalPath();
1✔
252
                    } catch (IOException e) {
×
253
                        throw new InvalidResourceException("Invalid resource location: " + tempLocation, e);
×
254
                    }
1✔
255
                }
256
                resourceLocationsToUse[i] = tempLocation;
1✔
257
            }
258
        }
259

260
        for (int i = 0; i < resourceLocationsToUse.length - 1; i++) {
1✔
261
            String tempLocation1 = resourceLocationsToUse[i];
1✔
262
            if (tempLocation1 != null) {
1✔
263
                for (int j = i + 1; j < resourceLocationsToUse.length; j++) {
1✔
264
                    String tempLocation2 = resourceLocationsToUse[j];
1✔
265
                    if (tempLocation2 != null) {
1✔
266
                        if (tempLocation1.equals(tempLocation2)) {
1✔
267
                            resourceLocationsToUse[j] = null;
1✔
268
                        }
269
                    }
270
                }
271
            }
272
        }
273

274
        return Arrays.stream(resourceLocationsToUse).filter(Objects::nonNull).toArray(String[]::new);
1✔
275
    }
276

277
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc