cle_clock
Home > wikikonf >  tom/actions/by kikonf > datasrc







Datasrc

print

Action : tom.datasrc   (Category : tom, Name : datasrc, By : kikonf)
Complete Name : tom.datasrc.by.kikonf
Version : 4.0        License : Modified BSD License

Purpose of the tom category :
Easy to customize. This category use in background the Kikonf Repoz tools to create and configure Apache Tomcat ® root installs and Catalina bases at any level (server.xml, hosts or defaults).
You can use it to manage the whole Tomcat ® Architecture.

Purpose of this datasrc plugin : This Action configures a JDBC Datasource (JNDI).

The following shows the main Source Code File for the Action : tom.datasrc

Toggle lines
##  Copyright (c) 2008, Patrick Germain Placidoux
##  All rights reserved.
##
##  This file is part of Kikonf Public Software.
##
##  Kikonf Public Software is released under the modified BSD License,
##  which should accompany it or any part of it in the file "COPYING".
##  If you do not have this file you can access the license 
##  through the WWW at http://www.kikonf.org/license/bsd/license.txt.
## 
##  Home: http://www.kikonf.org
##  Contact: kikonf@gmx.com




DATABASES={
    'derby':{
        'class_name' : 'org.apache.derby.jdbc.EmbeddedDriver',
        'url_pattern' : 'jdbc:derby:Databases/DBNAME'
    },
    'hsql':{
        'class_name' : 'org.hsql.jdbcDriver',
        'url_pattern' : 'jdbc:HypersonicSQL:DBNAME'
    },
    'mysql':{
        'class_name' : 'com.mysql.jdbc.Driver',
        'url_pattern' : 'jdbc:mysql://HOST:PORT/DBNAME'
    },
    'psql':{
        'class_name' : 'org.postgresql.Driver',
        'url_pattern' : 'jdbc:postgresql://HOST:PORT/DBNAME'
    },
    'oracle':{
        'class_name' : 'oracle.jdbc.OracleDriver',
        'url_pattern' : 'jdbc:oracle:thin:@HOST:PORT:DBNAME'
    },
    'db2':{
        'class_name' : 'com.ibm.db2.jcc.DB2Driver',
        'url_pattern' : 'jdbc:db2://HOST:PORT/DBNAME'
    }
}



#-------------------#
# Utility functions #
#-------------------#


def getDriverClassNameFromDbType(node_attrs, dbtype):
    if dbtype not in DATABASES:raise xception.kikonfActionSystemException(self_action, 'inject', 'Unsupported dbtype:' + dbtype + ', Supported dbtype are:' + str(DATABASES.keys())[1:-1] + ' !')
    return DATABASES[dbtype]['class_name']

def getDriverUrlFromDbType(node_attrs, dbtype, host, port, dbname):
    if dbtype not in DATABASES:raise xception.kikonfActionSystemException(self_action, 'inject', 'Unsupported dbtype:' + dbtype + ', Supported dbtype are:' + str(DATABASES.keys())[1:-1] + ' !')
    TEMPLATE=DATABASES[dbtype]['url_pattern']
    
    dbname=node_attrs.dbname
    host=node_attrs.host
    port=node_attrs.port
    
    if (host==None or port==None) and dbtype in ('mysql', 'psql', 'oracle', 'db2'):raise xception.kikonfActionSystemException(self_action, 'inject', 'With resource@dbtype: '+  dbtype + ' resource@host and resource@port are requiered !')
    url=TEMPLATE.replace('HOST', host)
    url=url.replace('PORT', port)
    url=url.replace('DBNAME', dbname)
    
    return url

def rmvLink(name, level=None, indent=None, logFile=None):
    verbose('DataSource ResourceLink:' + name + ' Removing.', level=level, ifLevel=4, indent=indent, logFile=logFile)
    var name = name
    %delete ResourceLink where global = ${name}
    
    if ro!=None:n=len(ro)
    else:n=0    
    if n>0:verbose('DataSource ResourceLink:' + name + ' Removed:' + str(n) + ' configuration Entry.', level=level, ifLevel=3, indent=indent, logFile=logFile)

def rmvDataSrc(name, level=None, indent=None, logFile=None):
    verbose('DataSource Resource:' + name + ' Removing.', level=level, ifLevel=4, indent=indent, logFile=logFile)
    var name = name
    %delete Resource where  name = ${name}
    
    if ro!=None:n=len(ro)
    else:n=0    
    if n>0:verbose('DataSource Resource:' + name + ' Removed:' + str(n) + ' configuration Entry.', level=level, ifLevel=3, indent=indent, logFile=logFile)

def injLink(top_node, target_node, level=0, indent=None, logFile=None):
    """ Creates a ResourceLink """
    datarsc_attrs=top_node.getAttrs()
    verbose('DataSource ResourceLink:' + datarsc_attrs.name + ' Creating.', level=level, ifLevel=4, indent=indent, logFile=logFile)
    
    # link node
    link_attrs=top_node.getNode('link')[0].getAttrs()
    
    attrs={'global': datarsc_attrs.name, 'name': link_attrs.name, 'type':'javax.sql.DataSource'}
    
    # creates
    target_node.newNode('ResourceLink', **attrs)
    verbose('DataSource ResourceLink:' + datarsc_attrs.name + ' Created.', level=level, ifLevel=3, indent=indent, logFile=logFile)

def injRsc(top_node, target_node, level=0, indent=None, logFile=None):
    """ Creates a DataSource Resource """
    datarsc_attrs=top_node.getAttrs()
    verbose('DataSource Resource:' + datarsc_attrs.name + ' Creating.', level=level, ifLevel=4, indent=indent, logFile=logFile)
    attrs={}
    
    # resource node
    resource_node=top_node.getNode('resource')[0]
    resource_attrs=resource_node.getAttrs()
    
    #- guess driverClassName
    if resource_attrs.driverClassName==None:driverClassName=getDriverClassNameFromDbType(resource_attrs, resource_attrs.dbtype)
    else:driverClassName=resource_attrs.driverClassName
    
    #- guess auth
    if resource_attrs.auth=='true':auth='Container'
    else:auth='Application'
    
    attrs.update({
        'name' : datarsc_attrs.name,
        'driverClassName' : driverClassName,
        'url' : getDriverUrlFromDbType(resource_attrs, resource_attrs.dbtype, resource_attrs.host, resource_attrs.port, resource_attrs.dbname),
        'username' : resource_attrs.username,
        'password' : resource_attrs.password,
        'auth' : auth
    })
    
    attrs['type']='javax.sql.DataSource'
    
    # extended node
    if resource_node.hasNode('extended'):
        extended_node=resource_node.getNode('extended')[0]
        
        #- pool node
        if extended_node.hasNode('pool'):
            pool_attrs=extended_node.getNode('pool')[0].getdAttrs()
            
            attrs.update(pool_attrs)
            
        #- validation_query node
        if extended_node.hasNode('validation_query'):
            validation_query_attrs=extended_node.getNode('validation_query')[0].getdAttrs()
            attrs.update(validation_query_attrs)
            
        #- evictor_thread node
        if extended_node.hasNode('evictor_thread'):
            evictor_thread_attrs=extended_node.getNode('evictor_thread')[0].getdAttrs()
            attrs.update(evictor_thread_attrs)
            
        #- stale_connections node
        if extended_node.hasNode('stale_connections'):
            stale_connections_attrs=extended_node.getNode('stale_connections')[0].getdAttrs()
            attrs.update(stale_connections_attrs)
            
        #- others node
        if extended_node.hasNode('others'):
            others_attrs=extended_node.getNode('others')[0].getdAttrs()
            attrs.update(others_attrs)
        
    # creates
    target_node.newNode('Resource', **attrs)
    verbose('DataSource Resource:' + datarsc_attrs.name + ' Created.', level=level, ifLevel=3, indent=indent, logFile=logFile)



#------#
# Main #
#------#


# Stores the Action File's Processor Alias
var source_action_alias=ro['alias']

# Stores the Action File's First Node <=> self_action.getTop()
:ls
source_action_node=ro[0]


try:

    ## Retreives the Scope
    print
    print '## Retreives the Scope'
    scope_name, scope_attrs, scope=self_action.getScope(parent_node=self_action.getTop(), indent=self_action.getIndent(), context_template=None, global_template='resources.global')
    self_action.verbose(scope_name=scope_name, scope_attrs=scope_attrs)    
    indent=self_action.getIndent() + 3*' '

    ## Stores the target Scope Processor Alias and mount it
    print '## Stores the target Scope Processor Alias and mount it'
    var target_scope_alias=scope.pc_ro['alias']
    :mount $target_scope_alias
    :ls
    target_node=ro[0]
    
    # Integrity check
    if self_action.getTop().hasNode('link') and not scope.type.startswith('context'):
        raise xception.kikonfActionSystemException(self_action, 'inject', 'Bad Scope:' + scope.type + ', When using Tag "link" A Scope "context" is required for this Action !')
    if scope.type!='resources.global' and not scope.type.startswith('context'):
        raise xception.kikonfActionSystemException(self_action, 'inject', 'Bad Scope:' + scope.type + ',Either a Scope "context" or "resource.global" is required for this Action !')
    if not scope_attrs['resources.global']=='true' and self_action.getTop().hasNode('link'):
        raise xception.kikonfActionSystemException(self_action, 'inject', 'Bad Scope:' + scope.type + ', When an entry link is used, "resources.global" equal true is required for the Scope !')
    datarsc_attrs=self_action.getTop().getAttrs()
        
    ## Creates the ResourceLink
    print
    print '## Creates the DataSource ResourceLink'

    if self_action.getTop().hasNode('link'):
        rmvLink(datarsc_attrs.name, level=self_action.getVerbose(), indent=indent, logFile=self_action.getLogFile())
        injLink(self_action.getTop(), target_node, level=self_action.getVerbose(), indent=indent, logFile=self_action.getLogFile())
    
    ## Creates the DataSource Resource
    if self_action.getTop().hasNode('resource'):
        print
        print '## Creates the DataSource Resource'
##        print 'resources.global:::', scope_attrs['resources.global']
##        print 'start.with.context:::', scope.type.startswith('context')
##        print 'scope.typ:::', scope.type
        
        if scope_attrs['resources.global']=='true' and scope.type.startswith('context')  and scope.type!= 'context.global': # Changes the target scope to the global resource scope
            print '# Changes the target scope to the global resource scope'
            var target_root_scope_alias=scope.root_pc_ro['alias']
            :mount $target_root_scope_alias
            :cd /
            :ls
            target_node=ro[0]
            
            print 'PASSSSE0'
            
            # Creates GlobalNamingResources if not exists
            if target_node.hasNode('GlobalNamingResources'):target_node=target_node.getNode('GlobalNamingResources')[0]
            else:target_node=target_node.newNode('GlobalNamingResources')
            :cd GlobalNamingResources
            
            rmvDataSrc(datarsc_attrs.name, level=self_action.getVerbose(), indent=indent, logFile=self_action.getLogFile())
            injRsc(self_action.getTop(), target_node, level=self_action.getVerbose(), indent=indent, logFile=self_action.getLogFile())
            
        else: # target_scope is the rigth target for resource
            if scope_attrs['resources.global']=='true':
                :cd /
            :ls
            target_node=ro[0]
            
            # Creates GlobalNamingResources if not exists
            if scope_attrs['resources.global']=='true':
                if target_node.hasNode('GlobalNamingResources'):target_node=target_node.getNode('GlobalNamingResources')[0]
                else:target_node=target_node.newNode('GlobalNamingResources')
                :cd GlobalNamingResources
            
            rmvDataSrc(datarsc_attrs.name, level=self_action.getVerbose(), indent=indent, logFile=self_action.getLogFile())
            injRsc(self_action.getTop(), target_node, level=self_action.getVerbose(), indent=indent, logFile=self_action.getLogFile())

except Exception, e:
    
    _e=xception.kikonfActionSystemException(self_action, 'inject', 'An exception occures ! SubException is:' + str(e) + ' !')
    _e.setSubException(e)
    REPOZ.recordSessionException(self_action.getName(), _e)
    if self_action.getVerbose()>=10:raise
    # Dont forget to skipt a strict empty line at the end to let the Interpretor to interprete the End of the bloc !

							
(Source: <KIKONF_INSTALLATION_DIR>/plugins/actions/tom/datasrc/by/kikonf/datasrc.inject.repoz)


  • Line 1 to 3 say something , say something , say something
  • Line 10 to 17 say something , say something , say something , say something , say something
  • Line 20 to 25 say something



Trademarks :
  • "IBM", "WebSphere MQ", "Db2" and "AIX" are registred trademarks of International Business Machines Corporation.
  • "Oracle", "MySql", "Java" and "JVM" are a registred trademarks of Oracle and/or its affiliates.
  • "Linux" is a trademark registred to Linus Torvalds
  • "MS SQL Server" is a registred trademark of Microsoft Corporation.
  • "Apache", "Apache Tomcat" and "Tomcat" are trademarks of the Apache Software Foundation.
  • "Java" and "JVM" are a registred trademarks of Oracle and/or its affiliates.
  • Other names may be trademarks of their respective owners.

Copyright © 2011 - Patrick Placidoux, Hélène Malamoud