Exemplo n.º 1
0
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  CORBA::ORB_var orb;
  CORBA::Object_var obj;
  PortableServer::POA_var root_poa;
  PortableServer::POAManagerFactory_var poa_manager_factory;

  if (parse_args (argc, argv) != 0)
    return 1;

  const ACE_TCHAR *e1_format= ACE_TEXT ("iiop://%s:%d");
  const ACE_TCHAR *e2_format= ACE_TEXT ("iiop://%s:%d/hostname_in_ior=unreachable");
  ACE_TCHAR hostname[256];
  int num_extra = 4;

  switch (host_form)
    {
    case from_hostname:
      ACE_OS::hostname (hostname, sizeof(hostname) / sizeof (ACE_TCHAR));
      break;

    case use_localhost:
      ACE_OS::strcpy (hostname, ACE_TEXT ("localhost"));
      break;

    case use_defaulted:
      hostname[0] = ACE_TEXT ('\0');
      break;

    case multi_protocol:
      hostname[0] = ACE_TEXT ('\0');
      e2_format = ACE_TEXT ("diop://");
      num_extra = 6;
      break;
    }

  size_t hostname_len = ACE_OS::strlen (hostname);
  size_t e1_len = ACE_OS::strlen (e1_format) + 5; // 5 for the port#
  size_t e2_len = ACE_OS::strlen (e2_format) + 5;
  ACE_TCHAR **extra = 0;
  ACE_NEW_RETURN (extra, ACE_TCHAR *[num_extra], -1);

  extra[0] = ACE::strnew (ACE_TEXT ("-ORBEndpoint"));
  ACE_NEW_RETURN (extra[1],
                  ACE_TCHAR[e1_len + hostname_len + 1],
                  -1);
  ACE_OS::sprintf (extra[1], e1_format, hostname, endpoint_port);
  extra[2] = ACE::strnew (ACE_TEXT ("-ORBEndpoint"));
  ACE_NEW_RETURN (extra[3],
                  ACE_TCHAR[e2_len + hostname_len + 1],
                  -1);
  ACE_OS::sprintf (extra[3], e2_format, hostname, endpoint_port+1);
  if (host_form == multi_protocol)
    {
      extra[4] = ACE::strnew (ACE_TEXT ("-ORBSvcConf"));
      extra[5] = ACE::strnew (svc_conf_file);
    }

  ACE_TCHAR **largv = new ACE_TCHAR *[argc+num_extra];
  int i = 0;
  for (i = 0; i < argc; i++)
    largv[i] = argv[i];

  ACE_DEBUG ((LM_DEBUG,"server adding args: "));
  for (i = 0; i < num_extra; i++)
    {
      ACE_DEBUG ((LM_DEBUG, "%s ", extra[i]));
      largv[argc+i] = extra[i];
    }
  ACE_DEBUG ((LM_DEBUG, "\n"));

  argc += num_extra;

  try
    {
      orb =
        CORBA::ORB_init (argc, largv, "EndpointPolicy");

      obj =
        orb->resolve_initial_references("RootPOA");

      root_poa =
        PortableServer::POA::_narrow (obj.in ());

      if (CORBA::is_nil (root_poa.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Panic: nil RootPOA\n"),
                          1);

      poa_manager_factory
        = root_poa->the_POAManagerFactory ();
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception("initialization error ");
      return 1;
    }

  for (i = 0; i < num_extra; i++)
    ACE::strdelete (extra[i]);
  delete [] extra;
  delete [] largv;

  //-----------------------------------------------------------------------

  // Create two valid endpoint policies. One to match each of the generated
  // endpoint arguments supplied to ORB_init().
  PortableServer::POAManager_var good_pm;
  PortableServer::POAManager_var bad_pm;
  CORBA::PolicyList policies;
  policies.length (1);

  EndpointPolicy::EndpointList list;
  list.length (1);
  list[0] = new IIOPEndpointValue_i (ACE_TEXT_ALWAYS_CHAR (hostname), endpoint_port);

  try
    {
      CORBA::Any policy_value;
      policy_value <<= list;
      policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
                                        policy_value);
      good_pm = poa_manager_factory->create_POAManager ("goodPOAManager",
                                                        policies);

      if (host_form == use_defaulted)
        {
          ACE_ERROR_RETURN ((LM_DEBUG,
                             "ERROR: Expected to catch policy error with "
                             "defaulted hostname, but didn't.\n"),1);
        }
    }
  catch (CORBA::PolicyError &)
    {
      if (host_form == use_defaulted)
        {
          ACE_ERROR_RETURN ((LM_DEBUG,
                             "Defaulted hostname properly rejected\n"), 0);
        }
      ACE_ERROR_RETURN ((LM_DEBUG,
                         "ERROR: Unexpectedly caught PolicyError "
                         "exception host_form = %s\n", form_arg), 1);
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("Failed to create reachable POA manager");
      return 1;
    }

  list[0] = new IIOPEndpointValue_i("unreachable", endpoint_port+1);
  try
    {
      CORBA::Any policy_value;
      policy_value <<= list;
      policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
                                        policy_value);
      bad_pm = poa_manager_factory->create_POAManager ("badPOAManager",
                                                        policies);
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("Failed to create unreachable POA manager");
      return 1;
    }

  try
    {

      Hello *hello_impl;
      ACE_NEW_RETURN (hello_impl,
                      Hello (orb.in ()),
                      1);
      PortableServer::ServantBase_var owner_transfer(hello_impl);

      // Create poas assiciated with the each the good poa manager and the
      // bad poa manager.
      policies.length(0);
      PortableServer::POA_var good_poa =
        root_poa->create_POA ("goodPOA",
                              good_pm.in (),
                              policies);

      int result = 0;
      result = make_ior (orb.in(), root_poa.in(), hello_impl, root_ior_file);
      if (result != 0)
        return result;

      result = make_ior (orb.in(), good_poa.in(), hello_impl, good_ior_file);
      if (result != 0)
        return result;

      good_pm->activate ();

      PortableServer::POA_var bad_poa;

      if (host_form != multi_protocol)
        {
          bad_poa =
          root_poa->create_POA ("badPOA",
                                bad_pm.in (),
                                policies);
          result = make_ior (orb.in(), bad_poa.in(), hello_impl, bad_ior_file);
          if (result != 0)
            return result;

          bad_pm->activate ();
        }

      if (host_form == from_hostname || host_form == use_localhost)
        {
          orb->run ();
          ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
        }
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("cannot run server");
    }
  try
    {
      root_poa->destroy (1, 1);

      orb->destroy ();
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("CORBA exception during shutdown");
    }
  return 0;
}
Exemplo n.º 2
0
int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  CORBA::ORB_var orb;
  CORBA::Object_var obj;
  PortableServer::POA_var root_poa;
  PortableServer::POAManagerFactory_var poa_manager_factory;

  if (parse_args (argc, argv) != 0)
    return 1;
  ACE_TCHAR *extra[4];
#ifdef ACE_USES_WCHAR
  extra[0] = CORBA::wstring_dup (ACE_TEXT ("-ORBEndpoint"));
  extra[1] =CORBA::wstring_alloc (100);
#else
  extra[0] = CORBA::string_dup ("-ORBEndpoint");
  extra[1] = CORBA::string_alloc (100);
#endif
  ACE_OS::sprintf (extra[1],
                   ACE_TEXT ("iiop://localhost:%d/ssl_port=%d"),
                   endpoint_port, endpoint_port+1);

#ifdef ACE_USES_WCHAR
  extra[2] = CORBA::wstring_dup (ACE_TEXT ("-ORBEndpoint"));
  extra[3] = CORBA::wstring_alloc (100);
#else
  extra[2] = CORBA::string_dup ("-ORBEndpoint");
  extra[3] = CORBA::string_alloc (100);
#endif
  ACE_OS::sprintf (extra[3],
                   ACE_TEXT ("iiop://localhost:%d/ssl_port=%d"),
                   endpoint_port+10, endpoint_port+11);

  ACE_TCHAR **largv = new ACE_TCHAR *[argc+4];
  int i = 0;
  for (i = 0; i < argc; i++)
    largv[i] = argv[i];
  for (i = 0; i < 4; i++)
    largv[argc+i] = extra[i];
  argc += 4;

  try
    {
      orb =
        CORBA::ORB_init (argc, largv, "EndpointPolicy");

      obj =
        orb->resolve_initial_references("RootPOA");

      root_poa =
        PortableServer::POA::_narrow (obj.in ());

      if (CORBA::is_nil (root_poa.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Panic: nil RootPOA\n"),
                          1);

      poa_manager_factory
        = root_poa->the_POAManagerFactory ();
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception("initialization error ");
      return 1;
    }

  for (i = 0; i < 4; i++)
#ifdef ACE_USES_WCHAR
    CORBA::wstring_free (extra[i]);
#else
    CORBA::string_free (extra[i]);
#endif

  delete [] largv;


  //-----------------------------------------------------------------------


  // Create two valid endpoint policies. One to match each of the generated
  // endpoint arguments supplied to ORB_init().
  PortableServer::POAManager_var good_pm;
  PortableServer::POAManager_var bad_pm;
  CORBA::PolicyList policies;
  policies.length (1);

  EndpointPolicy::EndpointList list;
  list.length (1);
  list[0] = new IIOPEndpointValue_i("localhost", endpoint_port);

  try
    {
      CORBA::Any policy_value;
      policy_value <<= list;
      policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
                                        policy_value);
      good_pm = poa_manager_factory->create_POAManager ("goodPOAManager",
                                                        policies);
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("Failed to create reachable POA manager");
      return 1;
    }

  try
    {
      PortableServer::ObjectId_var oid;
      CORBA::Object_var o = CORBA::Object::_nil();
      FILE *output_file= 0;
      // Create poas assiciated with the each the good poa manager and the
      // bad poa manager.
      policies.length(0);
      PortableServer::POA_var good_poa =
        root_poa->create_POA ("goodPOA",
                              good_pm.in (),
                              policies);

      ACE_DEBUG ((LM_DEBUG, "Creating IOR from good poa\n"));

      o = good_poa->create_reference ("IDL:JustATest:1.0");

      CORBA::String_var good_ior =
        orb->object_to_string (o.in ());

      output_file= ACE_OS::fopen (ior_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s",
                           ior_file),
                          1);
      ACE_OS::fprintf (output_file, "%s", good_ior.in ());
      ACE_OS::fclose (output_file);
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("cannot run server");
    }
  root_poa->destroy (1, 1);

  orb->destroy ();

  return 0;
}
Exemplo n.º 3
0
void
Lorica::Proxy::configure(Config & config, 
			 const std::string &def_ior_file )
	throw (InitError)
{
	try {
		// This should be OK even if multiple copies of Proxy
		// get created as they all create the same ORB instance
		// and therefore the single ORB instance will get shutdown.
		Lorica::Proxy::this_ = this;

		std::auto_ptr<ACE_ARGV> arguments(config.get_orb_options());

		// Create proxy ORB.
		int argc = arguments->argc();
		if (Lorica_debug_level > 2) {
			ACE_DEBUG((LM_DEBUG,
				   ACE_TEXT("(%T) %N:%l - passing %d args to ORB_init:\n"), 
				   argc));

			for (int i = 0; i < argc; i++)
				ACE_DEBUG((LM_DEBUG, ACE_TEXT("%N:%l - %s\n"), arguments->argv()[i]));
		}
		orb_ = CORBA::ORB_init(argc, arguments->argv());

		// test if we have any security functionality
		ACE_Service_Repository * repo = 
			orb_->orb_core()->configuration()->
			current_service_repository();
		config.secure_available(repo->find("SSLIOP_Factory") == 0);

		int attempts = 3;
		CORBA::Object_var obj = CORBA::Object::_nil();
		resolve_again:
		try {
			obj = orb_->resolve_initial_references("RootPOA");
		}
		catch (CORBA::Exception & ex) {
			if (attempts--) {
				ACE_DEBUG((LM_INFO, ACE_TEXT("(%T) %N:%l - Exception trying to resolve initial references\n")));
				ACE_OS::sleep(10);
				goto resolve_again;
			}
			ACE_DEBUG((LM_ERROR, ACE_TEXT("(%T) %N:%l - %s\n"), ex._info().c_str()));
			throw InitError();
		}

		root_poa_ = PortableServer::POA::_narrow(obj.in());
		if (CORBA::is_nil(root_poa_.in())) {
			ACE_ERROR((LM_ERROR,
				   "(%T) %N:%l - could not get root POA\n"));
			throw InitError();
		}

		pmf_ = root_poa_->the_POAManagerFactory();
		if (CORBA::is_nil(pmf_.in())) {
			ACE_ERROR((LM_ERROR,
				   "(%T) %N:%l - could not get PMF\n"));
			throw InitError();
		}

		obj = orb_->resolve_initial_references("IORTable");
		iorTable_ = IORTable::Table::_narrow(obj.in());
		if (CORBA::is_nil(iorTable_.in())) {
			ACE_ERROR((LM_ERROR,
				   "(%T) %N:%l - could not get IORTable\n"));
			throw InitError();
		}

		CORBA::PolicyList policies;
		EndpointPolicy::EndpointList list;
		CORBA::Any policy_value;

		// Create external POA manager
		Config::Endpoints ex_points = config.get_endpoints(true);
		policies.length(1);
		list.length(ex_points.size());
		if (Lorica_debug_level > 2) {
			ACE_DEBUG((LM_DEBUG,
				   ACE_TEXT("(%T) %N:%l - setting up External POA manager with %d endpoints\n"),
				   ex_points.size()));
		}
		for (size_t count = 0; count < ex_points.size(); count++) {
			std::string inter = ex_points[count].hostname_;
			if (!ex_points[count].alias_.empty())
				inter = ex_points[count].alias_;

			list[count] = new IIOPEndpointValue_i(inter.c_str(),
							      ex_points[count].port_);
		}
		policy_value <<= list;
		policies[0] = orb_->create_policy(EndpointPolicy::ENDPOINT_POLICY_TYPE,
						  policy_value);
		outside_pm_ = pmf_->create_POAManager("OutsidePOAManager",
						      policies);

		// Create internal POA manager.
		Config::Endpoints in_points = config.get_endpoints(false);
		policies.length(1);
		list.length(in_points.size());
		if (Lorica_debug_level > 2) {
			ACE_DEBUG((LM_DEBUG,
				   ACE_TEXT("(%T) %N:%l - setting up Internal POA manager with %d endpoints\n"),
				   in_points.size()));
		}
		for (size_t count = 0; count < in_points.size(); count++) {
			std::string inter = in_points[count].hostname_;
			if (!in_points[count].alias_.empty())
				inter = in_points[count].alias_;

			list[count] = new IIOPEndpointValue_i(inter.c_str(),
							      in_points[count].port_);
		}
		policy_value <<= list;
		policies[0] = orb_->create_policy(EndpointPolicy::ENDPOINT_POLICY_TYPE,
						  policy_value);
		inside_pm_ = pmf_->create_POAManager("InsidePOAManager",
						     policies);

		if (Lorica_debug_level > 2) {
			ACE_DEBUG((LM_DEBUG,
				   ACE_TEXT("(%T) %N:%l - creating admin POA with internal POA manager\n")));
		}
		policies.length(0);
		admin_poa_ = root_poa_->create_POA("adminPOA",
						   inside_pm_.in(),
						   policies);

		try {
			ReferenceMapper_i *refMapper = new ReferenceMapper_i(orb_.in(),
									     iorTable_.in(),
									     config.secure_available());
			PortableServer::ServantBase_var refMapperServant = refMapper;

			PortableServer::ObjectId_var oid = admin_poa_->activate_object(refMapperServant.in());
			obj = admin_poa_->id_to_reference(oid.in());

			// refMapper->allow_insecure_access (obj.in());
		}
		catch (CORBA::Exception & ex) {
			ACE_DEBUG((LM_ERROR, ACE_TEXT("(%T) %N:%l - %s\n"), ex._info().c_str()));
			throw InitError();
		}

		ReferenceMapper_var refMapper_obj = ReferenceMapper::_narrow(obj.in());

		CORBA::String_var ior = 
			orb_->object_to_string(refMapper_obj.in());
		iorTable_->bind(Lorica::ReferenceMapper::IOR_TABLE_KEY,
				ior.in());

		this->ior_file_ = config.get_value("IOR_FILE", def_ior_file);

		FILE *output_file =
			ACE_OS::fopen (ACE_TEXT_CHAR_TO_TCHAR (this->ior_file_.c_str()), ACE_TEXT("w"));
		if (!output_file) {
			ACE_ERROR((LM_ERROR,
				   "(%T) %N:%l - cannot open output file for writing IOR: %s\n",
				   this->ior_file_.c_str()));
			throw InitError();
		}
		ACE_OS::fprintf(output_file, "%s", ior.in());
		ACE_OS::fclose(output_file);

		if (!setup_shutdown_handler()) {
			ACE_ERROR ((LM_ERROR,
				    "(%T) %N:%l - could not set up shutdown handler\n"));
			throw InitError();
		}

		// Initialize the mapper registry
		Lorica_MapperRegistry *mreg = 
			ACE_Dynamic_Service<Lorica_MapperRegistry>::instance
			(this->orb_->orb_core()->configuration(),"MapperRegistry");

		std::string ne_ids = config.null_eval_type_ids();
		if (!ne_ids.empty()) {
			if (Lorica_debug_level > 2) {
				ACE_DEBUG((LM_DEBUG,
					   ACE_TEXT("(%T) %N:%l - adding type ids for null ")
					   ACE_TEXT("evaluator: %s\n"),
					   ne_ids.c_str()));
			}

			size_t space = ne_ids.find(' ');
			size_t start = 0;
			while (space != std::string::npos) {
				mreg->add_null_mapper_type(ne_ids.substr(start,space - start));
				start = space+1;
				space = ne_ids.find(' ',start);
			}
			mreg->add_null_mapper_type(ne_ids.substr(start));
		} else if (config.null_eval_any()) {
			if (Lorica_debug_level > 2)
				ACE_DEBUG((LM_DEBUG,
					   ACE_TEXT("(%T) %N:%l - creating default null mapper\n")));
			mreg->create_default_null_mapper();
		}
		if (config.generic_evaluator()) {
			if (Lorica_debug_level > 0) {
				ACE_DEBUG((LM_DEBUG,
					   ACE_TEXT("(%T) %N:%l - loading generic evaluator\n")));
			}
#ifdef ACE_WIN32
			this->orb_->orb_core()->configuration()->process_directive
				(ACE_TEXT_ALWAYS_CHAR
				 (ACE_DYNAMIC_SERVICE_DIRECTIVE("Lorica_GenericLoader",
								"lorica_GenericEvaluator",
								"_make_Lorica_GenericLoader",
								"")
					 )
					);
#else
			Lorica::GenericMapper *gen_eval =
				new Lorica::GenericMapper(debug_, *mreg);
			mreg->add_proxy_mapper(gen_eval);
#endif
		}

		// add generic or other mappers...
		mreg->init_mappers(this->outside_pm_,
				   this->inside_pm_,
				   this->orb_,
				   config.secure_available());



		// last thing to do, put both POAs in the active state.
		outside_pm_->activate();
		inside_pm_->activate();

	}
	catch (CORBA::Exception & ex) {
		ACE_DEBUG((LM_ERROR, ACE_TEXT("(%T) %N:%l - %s\n"), ex._info().c_str()));
		throw InitError();
	}
	catch (...) {
		ACE_ERROR((LM_ERROR, "%N:%l - Caught an otherwise unknown exception\n"));
		throw InitError();
	}
}