lundi 11 avril 2011


Cet article présente les fonctionnalités des modules OEPE d’Oracle pour Eclipse afin d’aider le développement sous Jython et WLST. Nous allons créer des scriptes de connexion et création de ressources sur une instance Weblogic. Vous aurez besoin d’avoir accès à une DataBase.

L’avantage de ce module est d’offrir des Wizards de création de scripte et un éditeur Eclipse apportant la correction de code, l’auto indentation et la complétion ainsi que la validation automatique et l’aide à l’exécution. Nous pourrons également exécuter les scriptes en mode debug pas à pas.

CREATION D’UN PROJET WLST


Créer un projet JAVA.

FileàNewàJava Project.

Associer le projet à Weblogic.

Click droit sur le projetàPropertiesàTargeted RuntimesàOracle Weblogic Server 11gR1 PatchSet 3àOk


 Éditer les facets du projet afin de sélectionner l’option WLST.

Click droit sur le projetàPropertiesàProject FacetsàOracle Weblogic Scripting Tools (WLST) Support

(ne pas faire attention a la ligne de commentaire ‘Futher configuration available’)

CREATION D’UN SCRIPTE DE CONNEXION


Créer un source WLST :

Placer vous sous le répertoire wlst du projet :

click droitàNewàOtheràWeblogicàWLSTàNextà


Donner un nom au fichier WLST (TestConnect.py)

Vous obtenez un fichier avec le contenu suivant :

#Conditionally import wlstModule only when script is executed with jython
if __name__ == '__main__':
    from wlstModule import *#@UnusedWildImport

print 'starting the script ....'
username = 'weblogic'
password = 'welcome1'
url='t3://localhost:7001'

connect(username,password,url)
  
edit()
startEdit()

try:
    save()
    activate(block="true")
    print "script returns SUCCESS"  
except Exception, e:
    print e
    print "Error while trying to save and/or activate!!!"
    dumpStack()
    raise

Modifier

La vue de droite donne une visibilité sur les variables déclarées.
 
Démarrons une instance Weblogic et modifions le scripte sur les variables username, password, url pour se connecter à cette instance.

Click Droit sur le scriptàRun AsàWLST Run
  
TEMPLATE DE SCRIPTE


Lors de la création des scriptes, vous avez différent template possible. On pourra ainsi créer et supprimer un data source avec le templateConfigure JDBC Data Source’ et ‘Delete JDBC Data Source

Click droit sur le répertoire wlst du projetàNewàOtheràWeblogicàWLST ScriptàNextà’donner un nom de fichier ;createDataSource.py’àOverwrite an existing fileàclique sur le bouton Template


Finish

if __name__ == '__main__':
    from wlstModule import *#@UnusedWildImport

print 'starting the script ....'
username = 'weblogic'
password = 'welcome1'
url='t3://localhost:7001'
connect(username,password,url)

edit()
startEdit()

# Change these names as necessary
dsname='myJDBCDataSource'
server='AdminServer'
cd("Servers/"+server)
target=cmo
cd("../..")

# start creation
print 'Creating JDBCSystemResource with name '+dsname
jdbcSR = create(dsname,"JDBCSystemResource")
theJDBCResource = jdbcSR.getJDBCResource()
theJDBCResource.setName("myJDBCDataSource")

connectionPoolParams = theJDBCResource.getJDBCConnectionPoolParams()
connectionPoolParams.setConnectionReserveTimeoutSeconds(25)
connectionPoolParams.setMaxCapacity(100)
connectionPoolParams.setTestTableName("SYSTABLES")

dsParams = theJDBCResource.getJDBCDataSourceParams()
dsParams.addJNDIName("ds.myJDBCDataSource")

driverParams = theJDBCResource.getJDBCDriverParams()
driverParams.setUrl("jdbc:derby://localhost:1527/examples;create=true")
driverParams.setDriverName("org.apache.derby.jdbc.ClientXADataSource")
# driverParams.setUrl("jdbc:oracle:thin:@my-oracle-server:my-oracle-server-port:my-oracle-sid")
# driverParams.setDriverName("oracle.jdbc.driver.OracleDriver")

driverParams.setPassword("examples")
# driverParams.setLoginDelaySeconds(60)
driverProperties = driverParams.getProperties()

proper = driverProperties.createProperty("user")
#proper.setName("userName")
proper.setValue("examples")

proper1 = driverProperties.createProperty("DatabaseName")
#proper1.setName("DatabaseName")
proper1.setValue("examples")

jdbcSR.addTarget(target)

save()
activate(block="true")

print 'Done configuring the data source'

Faire de même pour supprimer le datasource :

Click droit sur le répertoire wlst du projetàNewàOtheràWeblogicàWLST ScriptàNextà’donner un nom de fichier ;deleteDataSource.py’àOverwrite an existing fileàclique sur le bouton TemplateàDelete JDBC DataSource.

VARIABILISATION DES SCRIPTES


Nous allons maintenant variabiliser ces scriptes en introduisant un fichier de properties. Pour cela nous allons créer un scripte utilitaire pour y placer toutes les fonctionnalités désirées comme une méthode loadProps et créer un fichier properties regroupant tous les aspects de paramétrage.

Pour créer le fichier properties :
              
Click droit sur le répertoire WLST du projetàNewàFileàparam.properties

username=weblogic
password=weblogic1
url=t3://localhost:7001

Créer le scripte utilitaire pour y placer la méthode de chargement du fichier properties.

Click droit sur le répertoire WLST du projetàNewàPyDev ModuleàPackage=lib.wlsutility, Name=mytools.py

Coller le code suivant pour introduire la méthode loadProps

#Conditionally import wlstModule only when script is executed with jython
#if __name__ == '__main__':
#    from wlstModule import *#@UnusedWildImport
from wlstModule import *#@UnusedWildImport
#import wlstModule as wlst

from java.io import FileInputStream

def loadProps(configPropFile):

        propInputStream = FileInputStream(configPropFile)
        configProps = Properties()
        configProps.load(propInputStream)
        return configProps
 
Modifier le code du fichier TestConnect en important les méthodes utilitaires et chargeant le fichier properties et en injectant son contenu dans les variables username, password, url :

import lib.wlsutility.mytools as mytools

propertie = mytools.loadProps('param.properties')

print 'starting the script ....'
username = propertie['username']
password = propertie['password']
url= propertie['url']

Tester la modification en exécutant le scripte TestConnect.py

Click Droit sur le scriptàRun AsàWLST Run

Reporter cette modification dans les scriptes de création et suppression de DataSource en y introduisant les variables associées au paramétrage du DataSource (url, driver, nom, etc … ). Prenez les paramètres d’une base accessible lors de la création de la ressource. Dans mon cas ce sera une base Oracle. Vous devez avoir les modifications suivantes :

param.properties
username=weblogic
password=weblogic1
url=t3://localhost:7001
dsname=monDataSource
server=AdminServer
TestTableName='SELECT 1 FROM DUAL'
JNDIName=ds.monJDBCDataSource
JDBC_URL=jdbc:oracle:thin:@localhost:1521:ORACLE
JDBC_DriverName=oracle.jdbc.xa.client.OracleXADataSource
JDBC_UserName=system
JDBC_Password=password

createDataSource
if __name__ == '__main__':
    from wlstModule import *#@UnusedWildImport

import lib.wlsutility.mytools as mytools

propertie = mytools.loadProps('param.properties')

print 'starting the script ....'
username = propertie['username']
password = propertie['password']
url= propertie['url']

connect(username,password,url)

edit()
startEdit()

# Change these names as necessary
dsname=propertie['dsname']
server=propertie['server']
cd("Servers/"+server)
target=cmo
cd("../..")

# start creation
print 'Creating JDBCSystemResource with name '+dsname
jdbcSR = create(dsname,"JDBCSystemResource")
theJDBCResource = jdbcSR.getJDBCResource()
theJDBCResource.setName(dsname)

connectionPoolParams = theJDBCResource.getJDBCConnectionPoolParams()
connectionPoolParams.setConnectionReserveTimeoutSeconds(25)
connectionPoolParams.setMaxCapacity(100)
connectionPoolParams.setTestTableName(propertie['TestTableName'])

dsParams = theJDBCResource.getJDBCDataSourceParams()
dsParams.addJNDIName(propertie['JNDIName'])

driverParams = theJDBCResource.getJDBCDriverParams()
driverParams.setUrl(propertie['JDBC_URL'])
driverParams.setDriverName(propertie['JDBC_DriverName'])
# driverParams.setUrl("jdbc:oracle:thin:@my-oracle-server:my-oracle-server-port:my-oracle-sid")
# driverParams.setDriverName("oracle.jdbc.driver.OracleDriver")

driverParams.setPassword(propertie['JDBC_Password'])
# driverParams.setLoginDelaySeconds(60)
driverProperties = driverParams.getProperties()

proper = driverProperties.createProperty("user")
#proper.setName("userName")
proper.setValue(propertie['JDBC_UserName'])

proper1 = driverProperties.createProperty("DatabaseName")
#proper1.setName("DatabaseName")
proper1.setValue("examples")

jdbcSR.addTarget(target)

save()
activate(block="true")

print 'Done configuring the data source'

deleteDataSource.py

if __name__ == '__main__':
    from wlstModule import *#@UnusedWildImport

import lib.wlsutility.mytools as mytools

propertie = mytools.loadProps('param.properties')

print 'starting the script ....'
username = propertie['username']
password = propertie['password']
url= propertie['url']

connect(username,password,url)

edit()
startEdit()

delete(propertie['dsname'],'JDBCSystemResource')

save()
activate(block="true")

Vous pouvez tester ces scriptes et vérifier la création et la suppression de la ressource dans la console Weblogic.

ConsoleàServicesàDataSources

Avant Création
 
Exécution du scripte de création :

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

starting the script ....
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'dev_domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)
You already have an edit session in progress and hence WLST will
continue with your edit session.

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
Creating JDBCSystemResource with name monDataSource
MBean type JDBCSystemResource with name monDataSource has been created successfully.
Saving all your changes ...
Saved all your changes successfully.
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed
Done configuring the data source

Après création


 Exécution du scripte de suppression :

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

starting the script ....
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'dev_domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)
You already have an edit session in progress and hence WLST will
continue with your edit session.

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
MBean type JDBCSystemResource with name monDataSource has been deleted successfully.
Saving all your changes ...
Saved all your changes successfully.
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

Rafraichissement de la console :

On pourra améliorer ces scriptes en introduisant pas exemple des tests pour savoir si la ressource n’est pas déjà créée lors de sa création, supprimer les ressources en cas d’exception dans un try/catch ou tester si la session console n’est pas déjà prise par quelqu'un et la fermer avant d’en rouvrir une autre.

AUTEUR

Ma photo
Carrières Sur Sein, Yvelines, France
Consultant Oracle (Ancien consultant BEA depuis 2001), je m’occupe des expertises sur les produits Oracle : SOCLE (Weblogic, Coherence, JRockit) SOA (Service Bus, SOA Suite, BPM)
MON CV

LABEL 3D

Blogumulus by Roy Tanck and Amanda Fazani

LABEL CLOUD

MAP

Locations of visitors to this page

AUTRES BLOG

LIVRES

MEMBRES