forked from wildfly/wildfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POARegistry.java
276 lines (237 loc) · 10.5 KB
/
POARegistry.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.ejb3.iiop;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.omg.CORBA.Policy;
import org.omg.CORBA.SetOverrideType;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.IdUniquenessPolicyValue;
import org.omg.PortableServer.LifespanPolicyValue;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.RequestProcessingPolicyValue;
import org.omg.PortableServer.Servant;
import org.omg.PortableServer.ServantRetentionPolicyValue;
/**
* Registry that maintains 2 different servant registries
* <p/>
* <ul>
* <li>a <code>ServantRegistry</code> with a transient POA per servant;</li>
* <li>a <code>ServantRegistry</code> with persistent POA per servant.</li>
* </ul>
* <p/>
* CORBA servants registered with any of these
* <code>ServantRegistry</code> instances will receive IIOP invocations.
*
* @author <a href="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
*/
public class POARegistry implements Service<POARegistry> {
public static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("ejb", "iiop", "POARegistry");
public static final byte[] EMPTY_BYTES = {};
/**
* The root POA. *
*/
private final InjectedValue<POA> rootPOA = new InjectedValue<POA>();
/**
* A ServantRegistry with a transient POA per servant.
*/
private ServantRegistry registryWithTransientPOAPerServant;
/**
* The transient POA map used by the ServantRegistry above.
*/
private Map<String, POA> transientPoaMap;
/**
* POA policies used by the ServantRegistry above.
*/
private Policy[] transientPoaPolicies;
/**
* A ServantRegistry with a persistent POA per servant.
*/
private ServantRegistry registryWithPersistentPOAPerServant;
/**
* The persistent POA map used by the ServantRegistry above.
*/
private Map<String, POA> persistentPoaMap;
/**
* POA policies used by the ServantRegistry above.
*/
private Policy[] persistentPoaPolicies;
public synchronized void start(final StartContext startContext) throws StartException {
transientPoaMap = Collections.synchronizedMap(new HashMap<String, POA>());
persistentPoaMap = Collections.synchronizedMap(new HashMap<String, POA>());
final POA rootPOA = this.rootPOA.getValue();
// Policies for per-servant transient POAs
transientPoaPolicies = new Policy[]{rootPOA.create_lifespan_policy(
LifespanPolicyValue.TRANSIENT),
rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.SYSTEM_ID),
rootPOA.create_servant_retention_policy(ServantRetentionPolicyValue.NON_RETAIN),
rootPOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_DEFAULT_SERVANT),
rootPOA.create_id_uniqueness_policy( IdUniquenessPolicyValue.MULTIPLE_ID),
};
// Policies for per-servant persistent POAs
persistentPoaPolicies = new Policy[]{
rootPOA.create_lifespan_policy(
LifespanPolicyValue.PERSISTENT),
rootPOA.create_id_assignment_policy(
IdAssignmentPolicyValue.USER_ID),
rootPOA.create_servant_retention_policy(
ServantRetentionPolicyValue.NON_RETAIN),
rootPOA.create_request_processing_policy(
RequestProcessingPolicyValue.USE_DEFAULT_SERVANT),
rootPOA.create_id_uniqueness_policy(
IdUniquenessPolicyValue.MULTIPLE_ID),
};
// Create this POARegistry's ServantRegistry implementations
registryWithTransientPOAPerServant = new ServantRegistryWithTransientPOAPerServant();
registryWithPersistentPOAPerServant = new ServantRegistryWithPersistentPOAPerServant();
}
public synchronized void stop(final StopContext context) {
transientPoaMap = null;
persistentPoaMap = null;
transientPoaPolicies = null;
persistentPoaPolicies = null;
}
private static Policy[] concatPolicies(Policy[] policies1, Policy[] policies2) {
Policy[] policies = new Policy[policies1.length + policies2.length];
int j = 0;
for (int i = 0; i < policies1.length; i++, j++) {
policies[j] = policies1[i];
}
for (int i = 0; i < policies2.length; i++, j++) {
policies[j] = policies2[i];
}
return policies;
}
@Override
public synchronized POARegistry getValue() throws IllegalStateException, IllegalArgumentException {
return this;
}
static class PoaReferenceFactory implements ReferenceFactory {
private final POA poa;
private final String servantName;
private final Policy[] policies;
PoaReferenceFactory(final POA poa, final String servantName, final Policy[] policies) {
this.poa = poa;
this.servantName = servantName;
this.policies = policies;
}
PoaReferenceFactory(final POA poa, final String servantName) {
this( poa, servantName, null);
}
PoaReferenceFactory(final POA poa) {
this(poa, null, null);
}
public org.omg.CORBA.Object createReference(final String interfId) throws Exception {
final org.omg.CORBA.Object corbaRef = poa.create_reference_with_id(EMPTY_BYTES, interfId);
if (policies != null) {
return corbaRef._set_policy_override(policies, SetOverrideType.ADD_OVERRIDE);
} else {
return corbaRef;
}
}
public org.omg.CORBA.Object createReferenceWithId(final byte[] id, final String interfId) throws Exception {
final org.omg.CORBA.Object corbaRef = poa.create_reference_with_id(id, interfId);
if (policies != null) {
return corbaRef._set_policy_override(policies, SetOverrideType.ADD_OVERRIDE);
} else {
return corbaRef;
}
}
public POA getPOA() {
return poa;
}
}
/**
* ServantRegistry with a transient POA per servant
*/
class ServantRegistryWithTransientPOAPerServant implements ServantRegistry {
public ReferenceFactory bind(final String name, final Servant servant, final Policy[] policies) throws Exception {
final Policy[] poaPolicies = concatPolicies(transientPoaPolicies, policies);
final POA poa = rootPOA.getValue().create_POA(name, null, poaPolicies);
transientPoaMap.put(name, poa);
poa.set_servant(servant);
poa.the_POAManager().activate();
return new PoaReferenceFactory(poa); // no servantName: in this case
// name is the POA name
}
public ReferenceFactory bind(final String name, final Servant servant) throws Exception {
final POA poa = rootPOA.getValue().create_POA(name, null, transientPoaPolicies);
transientPoaMap.put(name, poa);
poa.set_servant(servant);
poa.the_POAManager().activate();
return new PoaReferenceFactory(poa); // no servantName: in this case
// name is the POA name
}
public void unbind(final String name) throws Exception {
final POA poa = transientPoaMap.remove(name);
if (poa != null) {
poa.the_POAManager().deactivate(false, true);
poa.destroy(false, false);
}
}
}
/**
* ServantRegistry with a persistent POA per servant
*/
class ServantRegistryWithPersistentPOAPerServant implements ServantRegistry {
public ReferenceFactory bind(final String name, final Servant servant, final Policy[] policies) throws Exception {
final Policy[] poaPolicies = concatPolicies(persistentPoaPolicies, policies);
final POA poa = rootPOA.getValue().create_POA(name, null, poaPolicies);
persistentPoaMap.put(name, poa);
poa.set_servant(servant);
poa.the_POAManager().activate();
return new PoaReferenceFactory(poa); // no servantName: in this case
// name is the POA name
}
public ReferenceFactory bind(final String name, final Servant servant) throws Exception {
final POA poa = rootPOA.getValue().create_POA(name, null, persistentPoaPolicies);
persistentPoaMap.put(name, poa);
poa.set_servant(servant);
poa.the_POAManager().activate();
return new PoaReferenceFactory(poa); // no servantName: in this case
// name is the POA name
}
public void unbind(final String name) throws Exception {
final POA poa = persistentPoaMap.remove(name);
if (poa != null) {
poa.the_POAManager().deactivate(false, true);
poa.destroy(false, false);
}
}
}
public ServantRegistry getRegistryWithTransientPOAPerServant() {
return registryWithTransientPOAPerServant;
}
public ServantRegistry getRegistryWithPersistentPOAPerServant() {
return registryWithPersistentPOAPerServant;
}
public InjectedValue<POA> getRootPOA() {
return rootPOA;
}
}