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







Datasrc

print

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

Purpose of the wls category :
Easy to customize. This category use in background the wlst command in jython mode, to drive WebLogic Server configuration.
You can use it to manage the whole WebLogic architecture.

Purpose of this datasrc plugin : Creates a DataSource

The following shows the main Source Code File for the Action : wls.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




from actions.wls.tools import *
from weblogic.management import NoAccessRuntimeException


class Datasrc(wlsAction):
    
    DATASRC=\
        {
            'db2': {
                'notxa':        'COM.ibm.db2.jdbc.app.DB2Driver',
                'xa':           'COM.ibm.db2.jdbc.DB2XADataSource)',
                'url':          'jdbc:db2:DATABASE',
                'testreq':      'SQL SELECT COUNT(*) FROM SYSIBM.SYSTABLES\r\n',
                'properties':   ('driverType', 'currentSchema')
            },

            'oracle': {
                'notxa':        'oracle.jdbc.OracleDriver',
                'xa':           'oracle.jdbc.xa.client.OracleXADataSource',
                'url':          'jdbc:oracle:thin:@HOST:PORT:DATABASE',
                'testreq':      'SQL SELECT 1 FROM DUAL\r\n',
                'properties':   tuple()
            },
            
            'sqlserver': {
                'notxa':        'com.microsoft.sqlserver.jdbc.SQLServerDriver',
                'xa':           'com.microsoft.sqlserver.jdbc.SQLServerXADataSource',
                'url':          'jdbc:sqlserver://HOST:PORT',
                'testreq':      'SQL SELECT 1\r\n',
                'properties':   tuple()
            }
        }

    CONN_POOL_PROPS={'max_capacity': 'MaxCapacity', 'capacity_increment': 'CapacityIncrement' , 'initial_capacity': 'InitialCapacity'}
    
    def extract(self, scope_attrs, name=None, prefix=None, doRaise=True, **keywords):
        self_funct='extract'
        self.verbose(scope_attrs, name=name, prefix=prefix)    
        scope_cp, scope_attrs, scope=self.getScope(scope_attrs=scope_attrs, indent=self.getIndent())
        indent=self.getIndent() + 3*' '
        
        verbose('DataSource retreiving.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
        cmo=cd('/JDBCSystemResources')
        dsrces=cmo.getJDBCSystemResources()
    
        for ds in dsrces:
            dname=ds.getName()        
            
            if not (prefix!=None and name!=None and dname==prefix + '_' + name \
            or prefix==None and name!=None and name==dname \
            or prefix!=None and name==None and dname.startswith(prefix) \
            or prefix==None and name==None):continue            
            
            if not checkTarget(ds, scope):continue
            verbose('DataSource:' + dname + ' retreived.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())

            # - finds dbtype, database, host and port
            # -- samples UrlsS
            # jdbc:db2:DATABASE   
            # jdbc:oracle:thin:@HOST:PORT:DATABASE
            # jdbc:sqlserver://HOST:PORT
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDriverParams/' + dname)
            spl=cmo.getUrl().split(':')
            dbtype=spl[1]
            if not dbtype in Datasrc.DATASRC.keys():
                verbose('DataSource:' + dname + ', not managed Database Type: ' + dbtype + '. Skipped !', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())
                continue
            
            if dbtype=='db2':
                database=spl[-1]
                
                cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDriverParams/' + dname + '/Properties/' + dname + '/Properties/host')
                host=cmo.getValue()
                cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDriverParams/' + dname + '/Properties/' + dname + '/Properties/portNumber')
                port=cmo.getValue()
                
            elif dbtype=='oracle':
                database=spl[-1]
                port=spl[-2]
                host=spl[-3].split('@')[-1]
                
            else:
                port=spl[-1]
                host=spl[-2].split('//')[-1]

            verbose('DataSource:' + dname + ' with type:' + dbtype + ' retreiving.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())

            # - get description
            cmo=cd('/JDBCSystemResources/' + dname)
            desc=cmo.getNotes()
            if desc==DESIGN_NOTICE:desc=None
        
            # - get jndiName
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDataSourceParams/' + dname)
            jndi_name=cmo.getJNDINames()[0]

            # - get transaction
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDataSourceParams/' + dname)
            if cmo.getGlobalTransactionsProtocol()=='TwoPhaseCommit':xa='true'
            else:xa='false'
            
            # - get statementCacheSize
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCConnectionPoolParams/' + dname)
            statement_cache_size=cmo.getStatementCacheSize()
            
            # - get Database
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDriverParams/' + dname + '/Properties/' + dname + '/Properties/databaseName')
            if cmo==None:verbose('Property: datatabase not found Skipped !', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())
            else:datatabase=cmo.getSysPropValue()
            
            # - gets user
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDriverParams/' + dname + '/Properties/' + dname + '/Properties/user')
            user=cmo.getValue()
            
            # - gets Password
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCDriverParams/' + dname)            
            try:
                password=cmo.getPassword()
            except NoAccessRuntimeException:
                password='???'
                verbose('Passwords retreiving is not allowed because "clearTextCredentialAccessEnabled" attribute is not set in SecurityConfigurationMBean. Password retreiving is skipped !', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())

            datasrc_node=self.newTop() #-- because in extract mode, each call returns a new blank top node.
            _name=dname
            if prefix!=None:_name=dname.split(prefix + '_')[1]
            
            datasrc_node.setAttrs(name=_name, desc=desc, jndi_name=jndi_name, dbtype=dbtype, xa=xa, statement_cache_size=statement_cache_size, database=database, host=host, port=port, user=user, password=password, prefix=prefix)

            mkNodeScope(datasrc_node, scope_attrs, isUnique=True) #-- creates a scope node.

            # - gets Connection Pool
            connection_pool_node=datasrc_node.newNode('connection_pool')        # Tag: connection_pool
            verbose('Connection Pool retreiving.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
            cmo=cd('/JDBCSystemResources/' + dname + '/JDBCResource/' + dname + '/JDBCConnectionPoolParams/' + dname)
            for tag in Datasrc.CONN_POOL_PROPS.keys():
                cpname=Datasrc.CONN_POOL_PROPS[tag]
                
                verbose('Connection Property:' + cpname + ' retreiving.', level=self.getVerbose(), ifLevel=4, indent=indent + 3*' ', logFile=self.getLogFile())
                value=getattr(cmo, 'get' + cpname)()
                connection_pool_node.newNode(tag, value=value)
                verbose('Connection Property:' + cpname + ' retreived.', level=self.getVerbose(), ifLevel=4, indent=indent + 3*' ', logFile=self.getLogFile())
            verbose('Connection Pool retreived.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())
            
            # - gets properties
            properties_node=datasrc_node.newNode('properties')
            self.extProps(properties_node, dname, dbtype, indent=indent)

    def remove(self, no_name, no_name_no_prefix):
        datasrc_node=self.getTop()        
        datasrc_attrs = datasrc_node.getAttrs()
        scope_cp, scope_attrs, scope=self.getScope(parent_node=datasrc_node, indent=self.getIndent()) #-- Retreives scope
        self.verbose(scope_attrs, name=datasrc_attrs.name, prefix=datasrc_attrs.prefix)
        indent=self.getIndent() + 3*' '

        # no_name/no_name_no_prefix
        prefix=datasrc_attrs.prefix
        name=datasrc_attrs.name
        if no_name or no_name_no_prefix:name=None
        if no_name_no_prefix:prefix=None
        
        # remove all at prefix if present.
        self.rmvDatasrc(name=name, prefix=prefix, indent=indent)
    
    def inject(self):
        self_funct='inject'
        datasrc_node = self.getTop()        
        datasrc_attrs = datasrc_node.getAttrs()
        scope_cp, scope_attrs, scope=self.getScope(parent_node=datasrc_node, indent=self.getIndent()) #-- Retreives scope
        indent=self.getIndent() + 3*' '
        
        self.rmvDatasrc(name=datasrc_attrs.name, prefix=datasrc_attrs.prefix, indent=indent) #-- Destroy all first

        #-- name
        if datasrc_attrs.prefix==None:name=datasrc_attrs.name
        else:name=datasrc_attrs.prefix + '_' + datasrc_attrs.name
        self.verbose(scope_attrs, name=name, prefix=datasrc_attrs.prefix)
        
        if datasrc_attrs.xa=='true':isxa='xa'
        else:isxa='notxa'
        driver=Datasrc.DATASRC[datasrc_attrs.dbtype][isxa]
        url=Datasrc.DATASRC[datasrc_attrs.dbtype]['url']
        testreq=Datasrc.DATASRC[datasrc_attrs.dbtype]['testreq']
        
        
        # - creates DataSource
        verbose('DataSource:' + datasrc_attrs.name + ' creating.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())        
        
        cmo=cd('/')
        dsmb=cmo.createJDBCSystemResource(name)
        
        # - sets target
        cmo=cd('/')
        tg=getMBean(scope_cp)
        cmo=cd('/JDBCSystemResources/' + name)
        # dsmb.addTarget(getMBean(scope_cp)) <=>
        cmo.setTargets([tg])
        
        # - ds name
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name)
        cmo.setName(name)
        
        # - sets description
        cmo=cd('/JDBCSystemResources/' + name)
        if datasrc_attrs.desc!=None:desc=datasrc_attrs.desc
        else:desc=DESIGN_NOTICE
        cmo.setNotes(desc)

        # - sets jndiName
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDataSourceParams/' + name)
        cmo.setJNDINames([datasrc_attrs.jndi_name])

        # - sets Driver
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name)
        cmo.setDriverName(driver)

        # - sets Database
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name + '/Properties/' + name)
        cmo.createProperty('databaseName')
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name + '/Properties/' + name + '/Properties/databaseName')
        cmo.setValue(datasrc_attrs.database)

        # - adaptes url
        if datasrc_attrs.dbtype=='db2':
            url.replace('DATABASE', datasrc_attrs.database)
            # -- As for db2 host and port are not in url, creates properties
            cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name + '/Properties/' + name)
            cmo.createProperty('host')
            cmo.createProperty('portNumber')
            cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name + '/Properties/' + name + '/Properties/host')
            cmo.setValue(datasrc_attrs.host)
            cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name + '/Properties/' + name + '/Properties/portNumber')
            cmo.setValue(datasrc_attrs.port)
        elif datasrc_attrs.dbtype=='oracle':
            url.replace('DATABASE', datasrc_attrs.database)
            url.replace('HOST', datasrc_attrs.host)
            url.replace('PORT', datasrc_attrs.port)
        else:
            url.replace('HOST', datasrc_attrs.host)
            url.replace('PORT', datasrc_attrs.port)

        # - sets url
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name)
        cmo.setUrl(url)
        
        # - sets user
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name + '/Properties/' + name)
        cmo.createProperty('user')
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name + '/Properties/' + name + '/Properties/user')
        cmo.setValue(datasrc_attrs.user)
        
        # - sets Password
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDriverParams/' + name)
        cmo.setPasswordEncrypted(encrypt(datasrc_attrs.password))
        
        # - sets transaction
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCDataSourceParams/' + name)
        if isxa=='xa':cmo.setGlobalTransactionsProtocol('TwoPhaseCommit')
        else:cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

        # - sets testreq
        cmo=cd('/JDBCSystemResources/' + name + '/JDBCResource/' + name + '/JDBCConnectionPoolParams/' + name)
        cmo.setTestTableName(testreq)

        # - sets Connection Pool
        cmo.setStatementCacheSize(datasrc_attrs.statement_cache_size)
        if datasrc_node.hasNode('connection_pool'):
            cnodes=datasrc_node.getNode('connection_pool')[0].getNodes()
            for cnode in cnodes:
                cnode_attrs=cnode.getAttrs()
                tag=cnode.getTag()
                if tag not in Datasrc.CONN_POOL_PROPS.keys():raise xception.kikonfActionSystemException(self, self_funct, 'Unknown Ponnection Pool tag:' + tag + ', known Connection pool tags are:' + str(Datasrc.CONN_POOL_PROPS.keys())[1:-1] + ' !' )
                cpname=Datasrc.CONN_POOL_PROPS[tag]
                getattr(cmo, 'set' + cpname)(cnode_attrs.value)
                    
        verbose('DataSource:' + datasrc_attrs.name + ' created.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())
                 
        if datasrc_node.hasNode('properties'):self.inProps(datasrc_node.getNode('properties')[0], name, indent=indent + 3*' ')
 
    def rmvDatasrc(self, name=None, prefix=None, indent=None):
        scope_cp, scope_attrs, scope=self.getScope()
        verbose('DataSource retreiving.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
        cmo=cd('/JDBCSystemResources')
        dsrces=cmo.getJDBCSystemResources()
    
        for ds in dsrces:
            dname=ds.getName()
            
            if prefix!=None and dname.startswith(prefix) \
            or prefix==None and name!=None and name==dname \
            or prefix==None and name==None:
            
                if not checkTarget(ds, scope):continue
                verbose('DataSource:' + dname + ' removing.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
                cmo=cd('/')
                cmo.destroyJDBCSystemResource(ds)
                verbose('DataSource:' + dname + ' removed.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())
        
    def rmvProps(self, datasrc, upds, indent=None):
        if upds==None:return
        
        verbose('DataSource Properties retreiving.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
        cmo=cd('/JDBCSystemResources/' + datasrc + '/JDBCResource/' + datasrc + '/JDBCDriverParams/' + datasrc + '/Properties/' + datasrc)
        props=cmo.getProperties()

        for prop in props:
            pname=prop.getName()
            if pname in upds:
                
                verbose('DataSource Property:' + pname + ' removing.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
                cmo.removeProperty(prop)
                verbose('DataSource Property:' + pname + ' removed.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())        

    def inProps(self, node, datasrc, indent=None):
        props={}
        
        for prop_node in node.getNodes():
            dct=prop_node.getdAttrs()
            props[dct['name']]=dct['value']
 
        if len(props)!=0:
            self.rmvProps(datasrc, upds=props.keys(), indent=indent) #-- destroy first
    
            for prop in props.keys():
                verbose('DataSource Property' + prop + ' creating.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
                cmo=cd('/JDBCSystemResources/' + datasrc+ '/JDBCResource/' + datasrc + '/JDBCDriverParams/' + datasrc + '/Properties/' + datasrc)
                cmo.createProperty(prop)
                cmo=cd('/JDBCSystemResources/' + datasrc + '/JDBCResource/' + datasrc + '/JDBCDriverParams/' + datasrc + '/Properties/' + datasrc + '/Properties/' + prop)
                cmo.setSysPropValue(props[prop])
                
                verbose('DataSource Property:' + prop + ' created.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())

    def extProps(self, properties_node, datasrc, dbtype, indent=None):
        props=driver=Datasrc.DATASRC[dbtype]['properties']
        
        verbose('DataSource Properties retreiving.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
        cmo=cd('/JDBCSystemResources/' + datasrc + '/JDBCResource/' + datasrc + '/JDBCDriverParams/' + datasrc + '/Properties/' + datasrc)

        for pname in props:
            verbose('DataSource Property:' + pname + ' retreiving.', level=self.getVerbose(), ifLevel=4, indent=indent, logFile=self.getLogFile())
            p=cmo.lookupProperty(pname)
            if p==None:continue
            properties_node.newNode('property', name=pname, value=p.getSysPropValue())
            verbose('DataSource Property:' + pname + ' retreived.', level=self.getVerbose(), ifLevel=3, indent=indent, logFile=self.getLogFile())

    def verbose(self, scope_attrs, name=None, prefix=None):
        scope=str(scope_attrs).replace("'", '')[1:-1]
        
        if name!=None and prefix!=None:verbose('DataSource:' + name + ', under prefix:' + prefix+ ', at scope:' + scope + '.', level=self.getVerbose(), ifLevel=2, indent=self.getIndent())
        elif name!=None:verbose('DataSource:' + name + ', at scope:' + scope + '.', level=self.getVerbose(), ifLevel=2, indent=self.getIndent())
        elif prefix!=None:verbose('DataSources under prefix:' + prefix + ', at scope:' + scope + '.', level=self.getVerbose(), ifLevel=2, indent=self.getIndent())
        else:verbose('DataSources at scope:' + scope + '.', level=self.getVerbose(), ifLevel=2, indent=self.getIndent())
							
(Source: <KIKONF_INSTALLATION_DIR>/plugins/actions/wls/datasrc/by/kikonf/datasrc.py)


  • 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 :
  • "Db2" is a registred trademark of International Business Machines Corporation.
  • "oracle" is a registred trademark of Oracle and/or its affiliates.
  • "oracle", "Weblogic", "Weblogic Server", "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