summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/ObjectAdapterFactory.cpp
blob: 0de58ac0ee6bf383c4b9d0a87e59a1c477b1f781 (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
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <Ice/ObjectAdapterFactory.h>
#include <Ice/ObjectAdapterI.h>
#include <Ice/Functional.h>


#include <Ice/Instance.h>

using namespace std;
using namespace Ice;
using namespace IceInternal;

void IceInternal::incRef(ObjectAdapterFactory* p) { p->__incRef(); }
void IceInternal::decRef(ObjectAdapterFactory* p) { p->__decRef(); }

void
IceInternal::ObjectAdapterFactory::shutdown()
{
    JTCSyncT<JTCMutex> sync(*this);
    for_each(_adapters.begin(), _adapters.end(),
	     secondVoidMemFun<string, ObjectAdapter>(&ObjectAdapter::deactivate));
    _adapters.clear();
}

ObjectAdapterPtr
IceInternal::ObjectAdapterFactory::createObjectAdapter(const string& name, const string& endpts)
{
    JTCSyncT<JTCMutex> sync(*this);
    map<string, ObjectAdapterPtr>::iterator p = _adapters.find(name);
    if (p != _adapters.end())
    {
	return p->second;
    }

    ObjectAdapterPtr adapter = new ObjectAdapterI(_instance, name, endpts);
    _adapters.insert(make_pair(name, adapter));
    return adapter;
}

ObjectPtr
IceInternal::ObjectAdapterFactory::proxyToServant(const ObjectPrx& proxy)
{
    if (_adapters.empty())
    {
	//
	// If there are no adapters at all, no endpoint can ever be
	// local, and no object can exist locally. We indicate this by
	// returning null to the caller.
	//
	return 0;	 
    }

    bool allEndpointsLocal = true;

    for (map<string, ObjectAdapterPtr>::iterator p = _adapters.begin(); p != _adapters.end(); ++p)
    {
	try
	{
	    ObjectPtr servant = p->second->proxyToServant(proxy);
	    if (servant)
	    {
		//
		// Servant found, return it to the caller
		//
		return servant;
	    }
	}
	catch(const WrongObjectAdapterException&)
	{
	    //
	    // A WrongObjectAdapter exception indicates that there is
	    // no endpoint from the proxy that matches at least one
	    // endpoint from the object adapter. That means that we
	    // have at least one non-local endpoint in the proxy.
	    //
	    allEndpointsLocal = false;
	}
    }

    if (allEndpointsLocal)
    {
	//
	// If we didn't find a servant even though all endpoints are
	// local, we throw an ObjectNotExistException.
	//
	throw ObjectNotExistException(__FILE__, __LINE__);
    }
    else
    {
	//
	// If at least one of the endpoints from the proxy are not
	// local, it might be possible that the object exists
	// remotely. We indicate this by returning null to the caller.
	//
	return 0;
    }
}

IceInternal::ObjectAdapterFactory::ObjectAdapterFactory(const InstancePtr& instance) :
    _instance(instance)
{
}