summaryrefslogtreecommitdiff
path: root/cpp/src/IcePack/AdapterFactory.cpp
blob: e08fd404e0c6d9722a8255045b806ea14d26364f (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
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <Ice/Ice.h>
#include <IceUtil/UUID.h>
#include <Freeze/DB.h>
#include <Freeze/Evictor.h>
#include <Freeze/Initialize.h>
#include <IcePack/AdapterFactory.h>
#include <IcePack/AdapterI.h>
#include <IcePack/TraceLevels.h>

using namespace std;
using namespace IcePack;

IcePack::AdapterFactory::AdapterFactory(const Ice::ObjectAdapterPtr& adapter, 
					const TraceLevelsPtr& traceLevels, 
					const string& envName) :
    _adapter(adapter),
    _traceLevels(traceLevels)
{
    //
    // Create and install the freeze evictor for standalone adapter objects.
    //
    _evictor = Freeze::createEvictor(_adapter->getCommunicator(), envName, "adapter");
    _evictor->setSize(1000);

    //
    // Install the server object factory.
    // 
    _adapter->getCommunicator()->addObjectFactory(this, "::IcePack::StandaloneAdapter");

    //
    // Install the evictors.
    //
    _adapter->addServantLocator(_evictor, "IcePackStandaloneAdapter");
}

//
// Ice::ObjectFactory::create method implementation
//
Ice::ObjectPtr
IcePack::AdapterFactory::create(const string& type)
{
    if(type == "::IcePack::StandaloneAdapter")
    {
	return new StandaloneAdapterI(this);
    }
    else
    {
	assert(false);
	return 0; // Keep the compiler happy.
    }
}

//
// Ice::ObjectFactory::destroy method implementation
//
void 
IcePack::AdapterFactory::destroy()
{
    _adapter = 0;
    _evictor = 0;
    _traceLevels = 0;
}

//
// Create a new adapter servant with the given name and add
// it the evictor database.
//
AdapterPrx
IcePack::AdapterFactory::createStandaloneAdapter(const string& name)
{
    StandaloneAdapterPtr adapterI = new StandaloneAdapterI(this);

    Ice::Identity id;
    id.category = "IcePackStandaloneAdapter";
    id.name = name + "-" + IceUtil::generateUUID();

    _evictor->createObject(id, adapterI);

    return AdapterPrx::uncheckedCast(_adapter->createProxy(id));
}

void
IcePack::AdapterFactory::destroy(const Ice::Identity& id)
{
    try
    {
	_evictor->destroyObject(id);
    }
    catch(const Freeze::DBException& ex)
    {
	ostringstream os;
	os << "couldn't destroy standalone adapter:\n" << ex;
	_traceLevels->logger->warning(os.str());
    }
    catch(const Freeze::EvictorDeactivatedException&)
    {
	assert(false);
    }
}