-
Notifications
You must be signed in to change notification settings - Fork 53
/
AbstractRepositoryImpl.java
executable file
·236 lines (210 loc) · 8.14 KB
/
AbstractRepositoryImpl.java
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
/*
* Copyright (c) 2007, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.module.common_impl;
import com.sun.enterprise.module.Repository;
import com.sun.enterprise.module.ModuleDefinition;
import com.sun.enterprise.module.RepositoryChangeListener;
import com.sun.enterprise.module.ManifestConstants;
import java.io.*;
import java.net.URI;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
/**
* Partial implementation of {@link Repository}
* that statically enumerates all {@link ModuleDefinition}
* upfront.
*
* @author Kohsuke Kawaguchi
* @author Jerome Dochez
* @author Sanjeeb.Sahoo@Sun.COM
*/
public abstract class AbstractRepositoryImpl implements Repository {
private final ReentrantLock lock = new ReentrantLock();
private final String name;
private final URI location;
private Map<ModuleId, ModuleDefinition> moduleDefs;
private List<URI> libraries;
protected List<RepositoryChangeListener> listeners;
public AbstractRepositoryImpl(String name, URI location) {
this.name = name;
this.location = location;
}
public String getName() {
return name;
}
public URI getLocation() {
return location;
}
public ModuleDefinition find(String name, String version) {
return moduleDefs.get(AbstractFactory.getInstance().createModuleId(name, version));
}
public List<ModuleDefinition> findAll() {
return new ArrayList<ModuleDefinition>(moduleDefs.values());
}
public List<ModuleDefinition> findAll(String name) {
List<ModuleDefinition> result = new ArrayList<ModuleDefinition>();
for (ModuleDefinition md : findAll()) {
if (name.equals(md.getName())) result.add(md);
}
return result;
}
public void initialize() throws IOException {
assert moduleDefs==null; // TODO: is it allowed to call the initialize method multiple times?
moduleDefs = new HashMap<ModuleId, ModuleDefinition>();
libraries = new ArrayList<URI>();
loadModuleDefs(moduleDefs, libraries);
}
/**
* Called from {@link #initialize()} to load all {@link ModuleDefinition}s and libraries defintions
*/
protected abstract void loadModuleDefs(Map<ModuleId, ModuleDefinition> moduleDefs,
List<URI> libraries) throws IOException;
/**
* Loads a jar file and builds a {@link ModuleDefinition}.
*
* <p>
* The system allows {@link ModuleDefinition}s to be built in any way,
* but in practice module jars need to be built in a way agnostic
* to {@link Repository} implementations (so that same module could
* be used in different {@link Repository}s), so it makes sense
* to try to stick to the "common" loading scheme.
*
* @param jar
* Either a jar file or a directory that has the same structure as a jar file.
*/
protected ModuleDefinition loadJar(File jar) throws IOException {
Jar jarFile = Jar.create(jar);
Manifest manifest = jarFile.getManifest();
if (manifest==null) {
// we cannot find a manifest file in the bundle, so we look
// if there is a manifest file residing outside of the jar
// file with the same jar file name with a .mf extension,
// so for foo.jar, we look for foo.mf and if it is there,
// we use it as the manifest file.
String simpleName = jarFile.getBaseName();
File manifestFile = new File(jar.getParentFile(), simpleName+".mf");
if (manifestFile.exists()) {
InputStream is=null;
try {
is = new BufferedInputStream(new FileInputStream(manifestFile));
manifest = new Manifest(is);
} finally {
if (is!=null) {
is.close();
}
}
}
}
if (manifest!=null) {
Attributes attr = manifest.getMainAttributes();
String bundleName = attr.getValue(ManifestConstants.BUNDLE_NAME);
if (bundleName!=null) {
return newModuleDefinition(jar, attr);
}
}
/* this has undesirable side effect of picking up jar files
referenced via class-path as modules. so just return null.
// if we are here, that mean that either there was no co-bundled or
// external manifest file for this jar file OR the manifest file did
// not contain the manifest entries for module management
// in that case, I am just adding the jar file to the repository
// with no dependency management.
return new CookedModuleDefinition(jar, null);
*/
return null;
}
protected void add(ModuleDefinition def) {
moduleDefs.put(AbstractFactory.getInstance().createModuleId(def), def);
}
protected void remove(ModuleDefinition def) {
moduleDefs.remove(AbstractFactory.getInstance().createModuleId(def));
}
protected void addLibrary(URI location) {
libraries.add(location);
}
protected void removeLibrary(URI location) {
libraries.remove(location);
}
public void shutdown() throws IOException {
// nothing to do
}
public String toString() {
StringBuffer s= new StringBuffer();
for (ModuleDefinition moduleDef : findAll()) {
s.append(moduleDef.getName()).append(":");
}
return s.toString();
}
/**
* Returns the plain jar files installed in this repository. Plain jar files
* are not modules, they do not have the module's metadata and can only be used
* when referenced from a module dependency list or when added to a class
* loader directly
*
* @return jar files location stored in this repository.
*/
public List<URI> getJarLocations() {
return Collections.unmodifiableList(libraries);
}
/**
* Add a listener to changes happening to this repository. Repository can
* change during the lifetime of an execution (files added/removed/changed)
*
* @param listener implementation listening to this repository changes
* @return true if the listener was added successfully
*/
public boolean addListener(RepositoryChangeListener listener) {
lock.lock();
try {
if (listeners==null) {
listeners = new ArrayList<RepositoryChangeListener>();
}
return listeners.add(listener);
} finally {
lock.unlock();
}
}
/**
* Removes a previously registered listener
*
* @param listener the previously registered listener
* @return true if the listener was successfully unregistered
*/
public boolean removeListener(RepositoryChangeListener listener) {
lock.lock();
try {
if (listeners==null) {
return false;
}
return listeners.remove(listener);
} finally {
lock.unlock();
}
}
/**
* Extensibility point for subclasses to create a different instance
* of {@link ModuleDefinition}.
*
* @param jar
* The module jar file for which {@link ModuleDefinition} will be created.
* Never null.
*/
protected ModuleDefinition newModuleDefinition(File jar, Attributes attr) throws IOException {
return new DefaultModuleDefinition(jar, attr);
}
}