summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/entity/CorbaServer.java
blob: 22b8e13a1fd888c6eb7810441bb01a7e1dae3b26 (plain)
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
/**
 * This file is part of the CRISTAL-iSE kernel.
 * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
 *
 * This library 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 3 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; with out 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 library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * http://www.fsf.org/licensing/licenses/lgpl.html
 */
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.InvalidAgentPathException;
import com.c2kernel.lookup.ItemPath;
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<ItemPath, Servant>         mItemCache;
    private POA         mRootPOA;
    private POA         mItemPOA;
    private POA         mAgentPOA;
    private POAManager  mPOAManager;

    public CorbaServer() throws InvalidDataException {
        mItemCache   = new SoftCache<ItemPath, 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.set_servant_manager( itemLocator._this( Gateway.getORB() ) );

        ActiveLocator agentLocator = new ActiveLocator();
        mAgentPOA.set_servant_manager( agentLocator._this( Gateway.getORB() ) );

    }


    /**************************************************************************
     * Returns a CORBA servant for a pre-existing entity
     * @throws ObjectNotFoundException 
     **************************************************************************/
    public TraceableEntity getItem(ItemPath itemPath) throws ObjectNotFoundException {
        Servant item = null;
        if (!itemPath.exists()) throw new ObjectNotFoundException(itemPath+" does not exist");
        synchronized (mItemCache) {
            item = mItemCache.get(itemPath);
            if (item == null) {
                Logger.msg(7, "Creating new servant for "+itemPath);
                item = new TraceableEntity(itemPath, mItemPOA);
                mItemCache.put(itemPath, item);
            }
        }
        return (TraceableEntity)item;
    }

    /**************************************************************************
     * Returns a CORBA servant for a pre-existing entity
     **************************************************************************/
    public ActiveEntity getAgent(AgentPath agentPath) throws InvalidAgentPathException, ObjectNotFoundException {
        Servant agent = null;
        if (!agentPath.exists()) throw new ObjectNotFoundException(agentPath+" does not exist");
        synchronized (mItemCache) {
        	agent = mItemCache.get(agentPath);
            if (agent == null) {
                Logger.msg(7, "Creating new servant for "+agentPath);
                agent = new ActiveEntity(agentPath, mAgentPOA);
                mItemCache.put(agentPath, agent);
            }
            else if (!(agent instanceof ActiveEntity))
            	throw new InvalidAgentPathException("Item "+agentPath+" was not an agent");
        }
        return (ActiveEntity)agent;
    }

    /**
     * @param itemPath
     * @return
     */
    public TraceableEntity createItem(ItemPath itemPath) throws CannotManageException, ObjectAlreadyExistsException {

    	if (itemPath.exists()) throw new ObjectAlreadyExistsException();
        org.omg.CORBA.Object obj = mItemPOA.create_reference_with_id(itemPath.getOID(), ItemHelper.id());
        itemPath.setIOR(obj);
        TraceableEntity item = new TraceableEntity(itemPath, mItemPOA);
        synchronized (mItemCache) {
            mItemCache.put(itemPath, item);
        }
        return item;
    }
    
    public ActiveEntity createAgent(AgentPath agentPath) throws CannotManageException, ObjectAlreadyExistsException {
    	if (agentPath.exists()) throw new ObjectAlreadyExistsException();
        org.omg.CORBA.Object obj = mAgentPOA.create_reference_with_id(agentPath.getOID(), AgentHelper.id());
        agentPath.setIOR(obj);
        ActiveEntity agent;
		agent = new ActiveEntity(agentPath, mAgentPOA);
        synchronized (mItemCache) {
            mItemCache.put(agentPath, agent);
        }
        return agent;
    	
    }
}