summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/RepositoryManager/Repository_Manager.cpp
blob: b6406da05e2054d9fbc0a167f153e5eac2346e07 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// $Id$

#include "RepositoryManager_Impl.h"
#include "Update_Plan.h"
#include "ExecutionManager/ExecutionManagerC.h"
#include "Config_Handlers/DnC_Dump.h"
#include "NodeManager/NodeDaemonC.h"
#include "ace/OS_NS_stdio.h"
#include "ace/streams.h"
#include "ace/Auto_Ptr.h"
using namespace std;

const char * exec_ior = "file://exec_mgr.ior";
const char * node_daemon_ior = 0;

static void
usage (const ACE_TCHAR* program)
{
  ACE_ERROR ((LM_ERROR,
              ACE_TEXT ("Usage: %s -p <URI> -d <URI> -k <EX_IOR> \
                         -t <NODE_MANAGER_IOR> \n")
              ACE_TEXT (" <URI>: URI identifying the package\n"),
              program));
}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  // top level package URL
  char* package_url = 0;

  // deployment plan URL
  char* plan_url = 0;

  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("p:d:k:t:"));
  int c;

  while ((c = get_opt ()) != EOF)
    {
      switch (c)
        {
          case 'p':
            package_url = get_opt.opt_arg ();
            break;
          case 'd':
            plan_url = get_opt.opt_arg ();
            break;
          case 'k':
            exec_ior = get_opt.opt_arg ();
            break;
          case 't':
            node_daemon_ior = get_opt.opt_arg ();
            break;
          default:
            usage(argv[0]);
            return -1;
        }
    }

  if (package_url == 0) {
    usage(argv[0]);
    return -1;
  }

  if (plan_url == 0) {
    usage(argv[0]);
    return -1;
  }

  // Initialize the ORB so that CORBA::Any will work
  //
  CORBA::ORB_var orb =
    CORBA::ORB_init (argc,
                     argv,
                     "");

  try
    {
      xercesc::XMLPlatformUtils::Initialize();
    }

  catch (const XMLException& xml_e)
    {
      char* message = XMLString::transcode (xml_e.getMessage());
      ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);
      ACE_DEBUG ((LM_DEBUG, "Error during initialization : %s\n", message));
      return 1;
    }
  try
    {
      // get a reference to the parser.
      auto_ptr<DOMBuilder> plan_parser (
          CIAO::Config_Handler::Utils::create_parser ()
        );
        
      CIAO::Config_Handler::Config_Error_Handler handler;
      plan_parser->setErrorHandler(&handler);

      auto_ptr<DOMBuilder> tpd_parser (
          CIAO::Config_Handler::Utils::create_parser ()
        );

      CIAO::Config_Handler::Config_Error_Handler tpd_handler;
      tpd_parser->setErrorHandler(&tpd_handler);

      // use the parser to parse the deployment plan URL and create
      // a DOM document.
      DOMDocument* plan_doc = plan_parser->parseURI (plan_url);
      
      if (handler.getErrors())
        {
          return 1;
        }

      //DOMDocument* tpd_doc = tpd_parser->parseURI (package_url);

      //ACE_UNUSED_ARG (tpd_doc);

      if (tpd_handler.getErrors())
        {
          return 1;
        }

      if (plan_doc == NULL)
        {
          ACE_DEBUG ((LM_DEBUG, "Null DOM Document obtained, \
                      May be the URL is wrong!!\n"));
          throw CIAO::Null_Dom_Document ();
        }

      // free up DOMBuilder. DOMBuilder also deletes the DOMDocument memory.
      //auto_ptr<DOMBuilder> cleanup_parser (parser);

      // call the Deployment Plan handler to parse the XML descriptor.
      CIAO::Config_Handler::Plan_Handler plan_handler (
          plan_doc,
          DOMNodeFilter::SHOW_ELEMENT | DOMNodeFilter::SHOW_TEXT
        );
        
      Deployment::DeploymentPlan plan;
      plan_handler.process_plan (plan);

      // call the PackageConfiguration handler to parse the XML descriptor.
      Deployment::PackageConfiguration* pc;

      CIAO::RepositoryManager_Impl *rep_impl = 0;
      ACE_NEW_RETURN (rep_impl,
                      CIAO::RepositoryManager_Impl (),
                      -1);
      PortableServer::ServantBase_var owner_transfer (rep_impl);

      rep_impl->installPackage ("PC", package_url);
      pc = rep_impl->findPackageByName ("PC");
      //Deployment::DnC_Dump::dump (*pc);

      CIAO::REF_MAP ref_map;
      CIAO::REF_MAP primary_ref_map;

      // traverse the PackageConfiguration IDL data structure and
      // update the deployment plan IDL data structure.
      CIAO::traverse_package (pc, plan, ref_map, primary_ref_map);

      //Deployment::DnC_Dump::dump (plan);

      // Pass the parsed plan to the Execution Manager to start the
      // Deployment Process.

      CORBA::Object_var obj = orb->string_to_object (exec_ior
                                                     ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CIAO::ExecutionManagerDaemon_var exec_mgr =
        CIAO::ExecutionManagerDaemon::_narrow (obj.in ()
                                               ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (exec_mgr.in ()))
        {
          ACE_DEBUG ((LM_DEBUG,
                      "Executor: nil Execution Manager reference, \
                       narrow failed\n"));
          return 1;

        }

      CIAO::NodeDaemon_var node_mgr;
      if (node_daemon_ior != 0)
      {
        CORBA::Object_var node_mgr_obj = 
          orb->string_to_object (node_daemon_ior
                                 ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;

        node_mgr =
          CIAO::NodeDaemon::_narrow (node_mgr_obj.in ()
                                     ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;

        if (CORBA::is_nil (node_mgr.in ()))
          {
            ACE_DEBUG ((LM_DEBUG,
                        "Executor: nil Node Manager reference, \
                        narrow failed\n"));
            return 1;
          }
      }

      ACE_DEBUG ((LM_DEBUG, "Executor: Obtained Execution Manager ref \n"));
      Deployment::DomainApplicationManager_var dapp_mgr =
        exec_mgr->preparePlan (plan, 1);

      if (CORBA::is_nil (dapp_mgr.in ()))
        {
          ACE_DEBUG ((LM_DEBUG,
                      "Executor:preparePlan call failed:nil \
                       DomainApplicationManager reference\n"));
          return 1;
        }
      ACE_DEBUG ((LM_DEBUG,
                  "Executor: Obtained DomainApplication Manager ref \n"));

      // Create a dummy set of properties and start the
      // Launching of applications
      Deployment::Properties_var properties;
      ACE_NEW_RETURN (properties,
                      Deployment::Properties,
                      1);
      ACE_DEBUG ((LM_DEBUG, "Executor: start Launch application....."));
      // Start the Application immediately
      int start = 1;
      dapp_mgr->startLaunch (properties.in (), start);
      ACE_DEBUG ((LM_DEBUG, "[success]\n"));

      ACE_DEBUG ((LM_DEBUG, "Executor: finish Launch application....."));
      // Call finish Launch to complete the connections
      dapp_mgr->finishLaunch (start);
      ACE_DEBUG ((LM_DEBUG, "[success]\n"));

      ACE_DEBUG ((LM_DEBUG, "Executor: Application Deployed successfully \n"));
      ACE_DEBUG ((LM_DEBUG, "Press <Enter> to tear down application \n"));

      char dummy [256];
      cin.getline (dummy, 256);
      // Tear down the assembly

      ACE_DEBUG ((LM_DEBUG, "Executor: destroy the application....."));
      dapp_mgr->destroyApplication ();
      ACE_DEBUG ((LM_DEBUG, "[success]\n"));

      ACE_DEBUG ((LM_DEBUG, "Executor: destroy the manager....."));
      exec_mgr->destroyManager (dapp_mgr.in ());
      ACE_DEBUG ((LM_DEBUG, "[success]\n"));

      if (node_daemon_ior != 0)
        {
          ACE_DEBUG ((LM_DEBUG, "shutting down node manager \n"));
          exec_mgr->shutdown (); // shut down execution manager.
          ACE_DEBUG ((LM_DEBUG, "[success]\n"));

          ACE_DEBUG ((LM_DEBUG, "shutting down node manager \n"));
          node_mgr->shutdown (); // shut down the node manager.
          ACE_DEBUG ((LM_DEBUG, "[success]\n"));
        }

      orb->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  catch (CORBA::Exception& ex)
    {
      ACE_PRINT_EXCEPTION (ex, "Caught CORBA Exception: ");
      return -1;
    }
  catch (const DOMException& e)
    {
      const unsigned int maxChars = 2047;
      XMLCh errText[maxChars + 1];

      ACE_ERROR ((LM_ERROR, "\nException occured while parsing %s: \
                  \n",plan_url));
      ACE_ERROR ((LM_ERROR, "DOMException code: %d\n ", e.code));
      if (DOMImplementation::loadDOMExceptionMsg (e.code, errText, maxChars))
        {
          char* message = XMLString::transcode (errText);
          ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);
          ACE_ERROR ((LM_ERROR, "Message is: %s\n", message));
        }
      //ACE_PRINT_EXCEPTION ("Caught DOM Exception: ");
      ACE_ERROR ((LM_ERROR, "Caught DOM exception\n"));
      return -1;
    }
  catch (...)
    {
      ACE_ERROR ((LM_ERROR, "Caught unknown exception\n"));
      return -1;
    }

  return 0;
}