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
|
package com.c2kernel.entity;
import java.util.Map;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAManager;
import org.omg.PortableServer.Servant;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import com.c2kernel.common.CannotManageException;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.agent.ActiveEntity;
import com.c2kernel.entity.agent.ActiveLocator;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.EntityPath;
import com.c2kernel.lookup.InvalidEntityPathException;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.SoftCache;
/**************************************************************************
*
* $Revision: 1.8 $
* $Date: 2005/10/13 08:13:44 $
*
* Copyright (C) 2003 CERN - European Organization for Nuclear Research
* All rights reserved.
**************************************************************************/
public class CorbaServer {
private final Map<EntityPath, Servant> mEntityCache;
private POA mRootPOA;
private POA mItemPOA;
private POA mAgentPOA;
private POAManager mPOAManager;
public CorbaServer() throws InvalidDataException {
mEntityCache = new SoftCache<EntityPath, Servant>(50);
// init POA
try {
setupPOA();
mPOAManager.activate();
} catch (Exception ex) {
Logger.error(ex);
throw new InvalidDataException("Error initialising POA", "");
}
new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("ORB Invoker");
Gateway.getORB().run();
}
}).start();
}
public void close() {
try {
mPOAManager.deactivate(true, true);
} catch (AdapterInactive ex) {
Logger.error(ex);
}
}
/**************************************************************************
* Initialises the C2KRootPOA with policies which are suitable for Factory objects
**************************************************************************/
public void setupPOA() throws Exception {
//Initialise the RootPOA
mRootPOA = org.omg.PortableServer.POAHelper.narrow(
Gateway.getORB().resolve_initial_references("RootPOA"));
//Initilaise the default POAManager
mPOAManager = mRootPOA.the_POAManager();
// Create POA for use by the entities
org.omg.CORBA.Policy[] policies = new org.omg.CORBA.Policy[6];
policies[0] = mRootPOA.create_id_assignment_policy(
org.omg.PortableServer.IdAssignmentPolicyValue.USER_ID);
policies[1] = mRootPOA.create_lifespan_policy(
org.omg.PortableServer.LifespanPolicyValue.PERSISTENT);
policies[2] = mRootPOA.create_servant_retention_policy(
org.omg.PortableServer.ServantRetentionPolicyValue.NON_RETAIN);
policies[3] = mRootPOA.create_id_uniqueness_policy(
org.omg.PortableServer.IdUniquenessPolicyValue.UNIQUE_ID);
policies[4] = mRootPOA.create_request_processing_policy(
org.omg.PortableServer.RequestProcessingPolicyValue.
USE_SERVANT_MANAGER);
policies[5] = mRootPOA.create_implicit_activation_policy(
org.omg.PortableServer.ImplicitActivationPolicyValue.
NO_IMPLICIT_ACTIVATION);
mItemPOA = mRootPOA.create_POA( "Item",
mRootPOA.the_POAManager(),
policies );
mAgentPOA = mRootPOA.create_POA( "Agent",
mRootPOA.the_POAManager(),
policies );
//Create the locators
TraceableLocator itemLocator = new TraceableLocator( mItemPOA );
mItemPOA.set_servant_manager( itemLocator._this( Gateway.getORB() ) );
ActiveLocator agentLocator = new ActiveLocator( mAgentPOA );
mAgentPOA.set_servant_manager( agentLocator._this( Gateway.getORB() ) );
}
/**************************************************************************
* Returns a CORBA servant for a pre-existing entity
**************************************************************************/
private Servant getEntity(int sysKey, org.omg.PortableServer.POA poa) throws ObjectNotFoundException {
try {
EntityPath entityPath = new EntityPath(sysKey);
Servant entity = null;
synchronized (mEntityCache) {
entity = mEntityCache.get(entityPath);
if (entity == null) {
Logger.msg(7, "Creating new servant for "+sysKey);
Class<?> entityClass = Gateway.getLDAPLookup().getEntityClass(entityPath);
if (entityClass == TraceableEntity.class) {
if (poa == null) poa = mItemPOA;
entity = new TraceableEntity(sysKey, poa);
}
else if (entityClass == ActiveEntity.class) {
if (poa == null) poa = mAgentPOA;
entity = new ActiveEntity(sysKey, poa);
}
mEntityCache.put(entityPath, entity);
}
}
return entity;
} catch (InvalidEntityPathException ex) {
throw new ObjectNotFoundException("Invalid Entity Key", "");
}
}
/**************************************************************************
* Wrapper for fetching Items
**************************************************************************/
public TraceableEntity getItem(int sysKey) throws ObjectNotFoundException {
return (TraceableEntity)getEntity(sysKey, mItemPOA);
}
/**************************************************************************
* Wrapper for fetching Agents
**************************************************************************/
public ActiveEntity getAgent(int sysKey) throws ObjectNotFoundException {
return (ActiveEntity)getEntity(sysKey, mAgentPOA);
}
/**
* @param entityPath
* @return
*/
public Servant createEntity(EntityPath entityPath) throws CannotManageException, ObjectAlreadyExistsException {
try {
if (entityPath == null)
entityPath = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey();
} catch (Exception ex) {
Logger.error(ex);
throw new CannotManageException("Cannot generate next entity key");
}
boolean isAgent = entityPath instanceof AgentPath;
POA myPOA = isAgent?mAgentPOA:mItemPOA;
org.omg.CORBA.Object obj = myPOA.create_reference_with_id(entityPath.getOID(), isAgent?AgentHelper.id():ItemHelper.id());
entityPath.setIOR(obj);
Servant entity;
if (isAgent)
entity = new ActiveEntity(entityPath.getSysKey(), myPOA);
else
entity = new TraceableEntity(entityPath.getSysKey(), myPOA);
synchronized (mEntityCache) {
mEntityCache.put(entityPath, entity);
}
return entity;
}
}
|