-
Notifications
You must be signed in to change notification settings - Fork 53
/
OSGiObrModuleImpl.java
executable file
·176 lines (152 loc) · 4.94 KB
/
OSGiObrModuleImpl.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
/*
* Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020Payara Services Ltd.
*
* 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 org.jvnet.hk2.osgiadapter;
import com.sun.enterprise.module.HK2Module;
import com.sun.enterprise.module.ModuleDefinition;
import com.sun.enterprise.module.ModuleState;
import com.sun.enterprise.module.ResolveError;
import com.sun.enterprise.module.bootstrap.BootException;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.PopulatorPostProcessor;
import org.glassfish.hk2.api.ServiceLocator;
import org.osgi.framework.Bundle;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author Sanjeeb.Sahoo@Sun.COM
*/
public class OSGiObrModuleImpl extends OSGiModuleImpl {
private final ReentrantLock lock = new ReentrantLock();
public OSGiObrModuleImpl(OSGiObrModulesRegistryImpl registry, File file) throws IOException {
this(registry, new OSGiModuleDefinition(file));
}
public OSGiObrModuleImpl(OSGiObrModulesRegistryImpl registry, ModuleDefinition moduleDef) {
this(registry, null, moduleDef);
}
public OSGiObrModuleImpl(OSGiObrModulesRegistryImpl registry, Bundle bundle, ModuleDefinition moduleDef) {
super(registry, bundle, moduleDef);
}
private boolean isUninitialized() {
lock.lock();
try {
return getBundle() == null;
} finally {
lock.unlock();
}
}
private void init() {
lock.lock();
try {
if (isUninitialized()) {
final ModuleDefinition moduleDefinition = getModuleDefinition();
Bundle bundle = getRegistry().getObrHandler().deploy(moduleDefinition.getName(), moduleDefinition.getVersion());
if (bundle != null) {
setBundle(bundle);
} else {
throw new RuntimeException("Unable to install module [ "
+ this
+ "] due to unsatisfied dependencies. See previous log messages.");
}
}
} finally {
lock.unlock();
}
}
@Override
public OSGiObrModulesRegistryImpl getRegistry() {
return (OSGiObrModulesRegistryImpl) super.getRegistry();
}
@Override
public ModuleState getState() {
if (isUninitialized()) {
return ModuleState.NEW;
}
return super.getState();
}
@Override
public void resolve() throws ResolveError {
init();
super.resolve();
}
@Override
public void start() throws ResolveError {
init();
super.start();
}
@Override
public boolean stop() {
if (isUninitialized()) {
return false;
}
return super.stop();
}
@Override
public void detach() {
if (isUninitialized()) {
return;
}
super.detach();
}
@Override
public void uninstall() {
if (isUninitialized()) {
return;
}
super.uninstall();
}
@Override
public void refresh() {
if (isUninitialized()) {
return;
}
super.refresh();
}
@Override
public void dumpState(PrintStream writer) {
writer.print(toString());
}
@Override
public ClassLoader getClassLoader() {
init();
return super.getClassLoader();
}
@Override
public List<HK2Module> getImports() {
if (isUninitialized()) {
return Collections.emptyList();
}
return super.getImports();
}
@Override
List<ActiveDescriptor> parseInhabitants(String name, ServiceLocator serviceLocator, List<PopulatorPostProcessor> populatorPostProcessors) throws IOException, BootException {
init();
return super.parseInhabitants(name, serviceLocator, populatorPostProcessors);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("OSGiObrModuleImpl::");
if (isUninitialized()) {
sb.append("Name: [").append(getName()).append("], State: [").append(getState()).append("]");
return sb.toString();
}
return sb.append(super.toString()).toString();
}
}