Cross-Platform C++

ot::net
class URLStreamHandlerFactory

#include "ot/net/URLStreamHandlerFactory.h"

ot::ManagedObject Factory class for creating instances of URLStreamHandler. OpenTop is supplied with URLStreamHandlers that can understand a range of different protocols. The URLStreamHandlerFactory base class is aware of the supplied protocols, and will return an appropriate URLStreamHandler instance for every protocol that it recognizes.

Applications can extend OpenTop by supplementing their own protocols. To achieve this the application must create a new factory class derived from URLStreamHandlerFactory and set an instance of the derived class as the global URLStreamHandlerFactory by calling SetInstance().

An example of creating a custom URLStreamHandlerFactory is shown below. In this example, the application has implemented classes for dealing with URLs for the "mailto" protocol. The MailtoUrlConnection class is not shown but its sister class, MailtoURLStreamHandler, is a complete (but trivial) implementation of a URLStreamHandler.

    class MailtoURLStreamHandler : public URLStreamHandler {
    public:
        virtual RefPtr<URLConnection> openConnection(const URL& url) const
        {
            return new MailtoURLConnection(url);
        }

        virtual int getDefaultPort() const
        {
            return 25;  // SMTP port
        }
    };

    class MyURLStreamHandlerFactory : public URLStreamHandlerFactory {
    public:
        RefPtr<URLStreamHandler> createURLStreamHandler(const String& protocol) const
        {
            if(StringUtils::CompareNoCase(protocol, OT_T("mailto")) == 0)
                return new MailtoURLStreamHandler;
            else
                return URLStreamHandlerFactory::createURLStreamHandler(protocol);
        }
    };

    int main(int argc, char* argv[])
    {
        // create a SystemMonitor instance to ensure clean termination
        SystemMonitor monitor;
        
        // register our custom URLStreamHandlerFactory
        URLStreamHandlerFactory::SetInstance(new MyURLStreamHandlerFactory);

        // 
        // Manipulate URLs, including our mailto: URL...
        //

        return (0);
    }

See also:
URLStreamHandler



Method Summary
 RefPtr< URLStreamHandler > createURLStreamHandler(const String& protocol) const
         Returns an instance of a URLStreamHandler that can understand URLs for the given protocol.
static RefPtr< URLStreamHandlerFactory > GetInstance()
         Returns the global URLStreamHandlerFactory.
static void SetInstance(URLStreamHandlerFactory* pFactory)
         Sets the global URLStreamHandlerFactory instance.

Methods inherited from class ot::ManagedObject
addRef, getRefCount, onFinalRelease, operator=, release

Method Detail

createURLStreamHandler

RefPtr< URLStreamHandlercreateURLStreamHandler(const String& protocol) const
Returns an instance of a URLStreamHandler that can understand URLs for the given protocol.

Parameters:
protocol - the name of the protocol
Multi-threaded considerations:
Can safely be called from multiple concurrent threads.

GetInstance

static RefPtr< URLStreamHandlerFactory > GetInstance()
Returns the global URLStreamHandlerFactory. If a factory has not been registered by the application, a default URLStreamHandlerFactory is created by the system and returned.

See also:
SetInstance()
Multi-threaded considerations:
Can safely be called from multiple concurrent threads.

SetInstance

static void SetInstance(URLStreamHandlerFactory* pFactory)
Sets the global URLStreamHandlerFactory instance. This global factory is used by the URL class to obtain a URLStreamHandler that can parse and understand URLs for a given protocol.

Like other OpenTop global objects, URLStreamHandlerFactory is a ManagedObject. This enables the passed object to be registered with the system's ObjectManager, thereby freeing the application from having to manage the lifetime of the factory object. In other words, the application does not need to maintain the object's reference-count once it has been registered.

A typical application may do the following:-

   URLStreamHandlerFactory::SetInstance(new MyURLStreamHandlerFactory);

See also:
GetInstance()
Multi-threaded considerations:
Can safely be called from multiple concurrent threads.


Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements