Home Help Center

How to determine whether an adapter name has been used?

sarlanorisarlanori Member Alex ChouOrganization: PersonalProject: Just Interested in Ice
Hi all,
is it possible to determine whether an adapter name has been used?I use Communicator.createAdapterObject to create a new object adapter,but if the adapter name has been used,I will get a AlreadyRegisteredException error.So,before I invoke createAdapterObject,I need to know if the adapter name has been used.Unfortunately,I have checked the docs many times without finding a method to do this.I use ice 3.4.2.Any suggestions?Thanks.

Comments

  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    Hi,

    You can use the AlreadyRegisteredException as the indicator that an adapter name is already in use. Another possibility would be to maintain your own list of adapter names.

    However, I have to wonder why you need to keep track of adapter names. Normally a program needs only one adapter, and its name is well known. Perhaps we could provide more help if you explained your use case in more detail.

    Mark
  • sarlanorisarlanori Member Alex ChouOrganization: PersonalProject: Just Interested in Ice
    mes wrote: »
    Hi,

    You can use the AlreadyRegisteredException as the indicator that an adapter name is already in use. Another possibility would be to maintain your own list of adapter names.

    However, I have to wonder why you need to keep track of adapter names. Normally a program needs only one adapter, and its name is well known. Perhaps we could provide more help if you explained your use case in more detail.

    Mark

    Hi mes,
    thanks for your reply.Actually,I'm developing an IceStorm utility to help other programmers in our team to quickly publish or subscribe a topic.The following code is a snippet of my utility class:
    public static void Subscribe(string topicName, string adapterName, Ice.Object cbServant, Communicator comm)
    {
        var manager = TopicManagerPrxHelper.checkedCast(comm.propertyToProxy("TopicManager.Proxy"));
    
        TopicPrx topic = null;
        try
        {
            topic = manager.retrieve(topicName);
        }
        catch (NoSuchTopic)
        {
            try
            {
                topic = manager.create(topicName);
            }
            catch (TopicExists)
            {
                // Intentionally leave blank.
            }
        }
    
        // In fact,this method does not exist,but it's what I need.
        ObjectAdapter adapter = comm.findObjectAdapter(adapterName); 
        if (null == adapter)
        {
            adapter = comm.createObjectAdapter(adapterName);
        }
    
        var subscriber = adapter.addWithUUID(cbServant);
    
        try
        {
            topic.subscribeAndGetPublisher(null, subscriber);
        }
        catch (AlreadySubscribed)
        {
            // Intentionally leave blank.
        }
    
        // Here I also need a method to determine if the adapter is activated.
        if (adapter.needActivate())
        {
            adapter.activate();
        }
    }
    
    As you can see,when I wrote this method,I didn't know the adapter name or if the adapter had been created.So,I need a method like comm.createObjectAdapter(adapterName) to determine if the adapter has been created.
    // In fact,this method does not exist,but it's what I need.
    ObjectAdapter adapter = comm.findObjectAdapter(adapterName);
    if (null == adapter)
    {
        adapter = comm.createObjectAdapter(adapterName);
    }
    
    Moreover,I need a method like adapter.needActivate() to determine if the adapter has been activated.
    // Here I also need a method to determine if the adapter is activated.
    if (adapter.needActivate())
    {
        adapter.activate();
    }
    
    Any advice would be appreciated.Thanks :-)
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    I suggest changing your Subscribe helper to take an ObjectAdapter argument instead of the adapter name. There are several reasons for this:
    • The communicator doesn't have an equivalent of findObjectAdapter, so your current strategy cannot work
    • It's not difficult for the caller to create its own object adapter
    • It's important when using IceStorm that the object adapter be activated before the subscriber is registered. Your Subscribe helper can require that the given adapter has already been activated.

    Mark
Sign In or Register to comment.