1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #include <ServantLocatorI.h>
6 #ifdef ICE_AMD_TEST
7 #   include <TestAMD.h>
8 #else
9 #   include <Test.h>
10 #endif
11 #include <TestHelper.h>
12 
13 #include <stdexcept>
14 
15 using namespace std;
16 using namespace Ice;
17 using namespace Test;
18 
ServantLocatorI(const string & category)19 ServantLocatorI::ServantLocatorI(const string& category) :
20     _category(category),
21     _deactivated(false),
22     _requestId(-1)
23 {
24 }
25 
~ServantLocatorI()26 ServantLocatorI::~ServantLocatorI()
27 {
28     test(_deactivated);
29 }
30 
31 Ice::ObjectPtr
32 #ifdef ICE_CPP11_MAPPING
locate(const Ice::Current & current,shared_ptr<void> & cookie)33 ServantLocatorI::locate(const Ice::Current& current, shared_ptr<void>& cookie)
34 #else
35 ServantLocatorI::locate(const Ice::Current& current, Ice::LocalObjectPtr& cookie)
36 #endif
37 {
38     test(!_deactivated);
39     test(current.id.category == _category || _category.empty());
40 
41     if(current.id.name == "unknown")
42     {
43         return 0;
44     }
45 
46     test(current.id.name == "locate" || current.id.name == "finished");
47     if(current.id.name == "locate")
48     {
49         exception(current);
50     }
51 
52     //
53     // Ensure locate() is only called once per request.
54     //
55     test(_requestId == -1);
56     _requestId = current.requestId;
57 
58     return newServantAndCookie(cookie);
59 }
60 
61 void
62 #ifdef ICE_CPP11_MAPPING
finished(const Ice::Current & current,const Ice::ObjectPtr &,const shared_ptr<void> & cookie)63 ServantLocatorI::finished(const Ice::Current& current, const Ice::ObjectPtr&,
64                           const shared_ptr<void>& cookie)
65 #else
66 ServantLocatorI::finished(const Ice::Current& current, const Ice::ObjectPtr&,
67                           const Ice::LocalObjectPtr& cookie)
68 #endif
69 {
70     test(!_deactivated);
71 
72     //
73     // Ensure finished() is only called once per request.
74     //
75     test(_requestId == current.requestId);
76     _requestId = -1;
77 
78     test(current.id.category == _category  || _category.empty());
79     test(current.id.name == "locate" || current.id.name == "finished");
80 
81     if(current.id.name == "finished")
82     {
83         exception(current);
84     }
85 
86     checkCookie(cookie);
87 }
88 
89 void
deactivate(const string &)90 ServantLocatorI::deactivate(const string&)
91 {
92     test(!_deactivated);
93 
94     _deactivated = true;
95 }
96 
97 void
exception(const Ice::Current & current)98 ServantLocatorI::exception(const Ice::Current& current)
99 {
100     if(current.operation == "ice_ids")
101     {
102         throw Test::TestIntfUserException();
103     }
104     else if(current.operation == "requestFailedException")
105     {
106         throw Ice::ObjectNotExistException(__FILE__, __LINE__);
107     }
108     else if(current.operation == "unknownUserException")
109     {
110         throw UnknownUserException(__FILE__, __LINE__, "reason");
111     }
112     else if(current.operation == "unknownLocalException")
113     {
114         throw UnknownLocalException(__FILE__, __LINE__, "reason");
115     }
116     else if(current.operation == "unknownException")
117     {
118         throw UnknownException(__FILE__, __LINE__, "reason");
119     }
120     else if(current.operation == "userException")
121     {
122         throwTestIntfUserException();
123     }
124     else if(current.operation == "localException")
125     {
126         throw Ice::SocketException(__FILE__, __LINE__, 0);
127     }
128     else if(current.operation == "stdException")
129     {
130         throw std::runtime_error("Hello");
131     }
132     else if(current.operation == "cppException")
133     {
134         throw 5;
135     }
136     else if(current.operation == "unknownExceptionWithServantException")
137     {
138         throw UnknownException(__FILE__, __LINE__, "reason");
139     }
140     else if(current.operation == "impossibleException")
141     {
142         throw TestIntfUserException(); // Yes, it really is meant to be TestIntfUserException.
143     }
144     else if(current.operation == "intfUserException")
145     {
146         throw TestImpossibleException(); // Yes, it really is meant to be TestImpossibleException.
147     }
148     else if(current.operation == "asyncResponse")
149     {
150         throw TestImpossibleException();
151     }
152     else if(current.operation == "asyncException")
153     {
154         throw TestImpossibleException();
155     }
156 }
157