/*
    loads and configures components
    in a SysContext
    @param doc the DOMDocument
    @return SysContext the system context where all components are loaded into
*/
RefCountedPtr<SysContext>
SysComponentLoader::createComponentContext(
    const DOMDocument* doc )
{
    RefCountedPtr<SysContext> resContext(new SysContext);

    std::vector<DOMProcessingInstruction*> pis;
    DomUtils::findProcessingInstructions( doc, TARGET_NAME, pis );

    // map of components to their configuration
    std::map< RefCountedPtr<iSysComponent>, DOMElement* > luckyComponents;

    for ( std::vector<DOMProcessingInstruction*>::const_iterator it = pis.begin();
            it != pis.end(); it++ )
    {
        // check to see if the processing instruction has the
        // following attributes:
        // "component-name" - name of the class to create
        // "component-config" - path of the config tree for the component
        //
        Mapping attrs;
        if ( DomUtils::getAttributes( (*it), attrs ) )
        {
            String cmpName;
            attrs.getIfAvailable(CMP_NAME_ATTR, cmpName);

            RefCountedPtr<iSysComponent> cmp( SysComponentFactory::createObject( cmpName ) );
            if ( cmp != NULL )
            {
                // locate it's configuration and call it to initialize
                DOMElement* xmlNode = NULL;

                String xmlIsland;
                attrs.getIfAvailable(CMP_CFG_ATTR, xmlIsland);

                if ( xmlIsland.length() > 0 )
                {
                    const DOMNode* theNode = (const DOMNode*)doc->getDocumentElement();
                    DomUtils::selectSingleNode( theNode, xmlIsland, (DOMNode**)&xmlNode );
                }

                int initRes = cmp->init( xmlNode, resContext );
                if ( initRes == 0 )
                {
                    resContext->addComponent( cmp );
                    luckyComponents[ cmp ] = xmlNode;
                }
            }
        }
    }

    // now post initialize the components that were successfully initialized
    for ( std::map< RefCountedPtr<iSysComponent>, DOMElement* >::iterator lit = luckyComponents.begin();
            lit != luckyComponents.end(); lit++ )
    {
        (*lit).first->postInit( (*lit).second, resContext );
    }

    return resContext;
}
void
SysComponentLoader::updateComponentContext(
    RefCountedPtr<SysContext> &ctx,
    const DOMDocument* doc )
{

    std::vector<DOMProcessingInstruction*> pis;
    DomUtils::findProcessingInstructions( doc, TARGET_NAME, pis );

    for ( std::vector<DOMProcessingInstruction*>::const_iterator it = pis.begin();
            it != pis.end(); it++ )
    {
        // check to see if the processing instruction has the
        // following attributes:
        // "component-name" - name of the class to create
        // "component-config" - path of the config tree for the component
        //
        Mapping attrs;
        if ( DomUtils::getAttributes( (*it), attrs ) )
        {
            String cmpName;
            attrs.getIfAvailable(CMP_NAME_ATTR, cmpName);

            RefCountedPtr<iSysComponent> cmp = ctx->getComponent( cmpName );
            if ( cmp != NULL )
            {
                // locate it's configuration and call it to initialize
                String xmlIsland;
                attrs.getIfAvailable(CMP_CFG_ATTR, xmlIsland);

                if ( xmlIsland.length() > 0 )
                {
                    const DOMNode* theNode = (const DOMNode*)doc->getDocumentElement();

                    DOMElement* xmlNode = NULL;
                    DomUtils::selectSingleNode( theNode, xmlIsland, (DOMNode**)&xmlNode );

                    cmp->update( xmlNode );
                }
            }
        }
    }
}
Esempio n. 3
0
int 
SysPathMgr::init( 
            const DOMNode* config, 
            RefCountedPtr<SysContext>& ctx )
{
    int res = -1;

    REFCOUNTED_CAST(iSysComponent, StdLogger, ctx->getComponent( StdLogger::getRegistryName()), _logger);

	// I expect the config node to be an element node
    if ( ( config != NULL ) && ( config->getNodeType() == DOMNode::ELEMENT_NODE ) )
    {
        // set the root to be the current directory by default
        String root(NTEXT("."));

        Mapping rootAttrs;
        if ( DomUtils::getNodeValue( (const DOMElement*)config, NULL, &rootAttrs ) )
        {
            rootAttrs.getIfAvailable(SYS_PATH_ROOTATTR, root);
        }

        // resolve whatever we have in the root and then verify a file does not exist
        // in place of the intended directory
        _root = FileUtils::resolve( root );

        FileAttributes fattrs;
        if ( !FileUtils::getAttributes( _root, fattrs ) )
        {
            if (!FileUtils::mkdirs( _root ))
            {
                CBLOGERR(_logger, NTEXT("SysPathMgr::init: could not create directory '") + _root + NTEXT("'"));
                return -1;
            }

            // update the file attributes with another call to get info in the directory
            FileUtils::getAttributes( _root, fattrs );
        }

        if (!fattrs.isDirectory())
        {
            CBLOGERR(_logger, NTEXT("SysPathMgr::init: found a file where a directory '") + _root + NTEXT("' was expected"));
            return -1;
        }

        // put a special key with the root attribute in the map
    	_paths[ SYS_PATH_IND + SYS_PATH_ROOTATTR ] = _root;

        // ok to this point, no errors
        res = 0;

        // iterate through the list of child elements whose tag is SYS_PATH_ENTRY
        // and get the name attribute and value. If all is found properly, then
        // construct a counter whose value is retrieved from the config node
        DOMNodeList* children = DomUtils::getNodeList( (const DOMElement*)config, SYS_PATH_ENTRY );
        if ( children != NULL )
        {
            for ( XMLSize_t i = 0, sz = children->getLength() ;
                  i < sz; i++ )
            {
                DOMNode* child = children->item(i);
                if ( (child != NULL) && (child->getNodeType() == DOMNode::ELEMENT_NODE) )
                {
                    String value;
                    Mapping attrs;

                    bool found = DomUtils::getNodeValue( (const DOMElement*)child, &value, &attrs );
                    if ( found )
                    {
                        res = 0;

                        String tagName;
                        attrs.getIfAvailable(SYS_PATH_TAGATTR, tagName);

                        if ( tagName.length() > 0 )
                        {
                            add( tagName, value );
                        }
                    }
                }
            }
        }
    }

    return res;
}