Wednesday, 18 March 2015

Ant scripts for building and deploying osb projects



Today I am going to explain about ant scripts which we can use for build and deploy OSB Projects in the servers.After gone through plenty number of blogs finally I have succeeded to build the new script to build the Jar from the OSB resources, import resources to the server and export resources from the server.

Before going for process, copy and save all below files local to your system.

OSB.bat

set FMW_HOME=D:\OFM\Oracle\Middleware

set ANT_HOME=%FMW_HOME%\modules\org.apache.ant_1.7.1
set PATH=%ANT_HOME%\bin;%PATH%
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_71
set CLASSPATH=%FMW_HOME%/wlserver_10.3/server/lib/weblogic.jar;%FMW_HOME%/Oracle_OSB1/lib/alsb.jar;%FMW_HOME%/Oracle_OSB1/modules/com.bea.common.configfwk_1.6.0.0.jar  



build.properties

fmw.home=D:/OFM/Oracle/Middleware

weblogic.home=${fmw.home}/wlserver_10.3
eclipse.home=${weblogic.home}/OEPE
osb.home=${fmw.home}/Oracle_OSB1

wls.username      = weblogic
wls.password      = welcome1
wls.server        = t3://localhost:7001

# properties for workspace export
config.project="OSBPracticeAPP"
config.jar=D:/OSB_Workspace/Jars/cc.jar
config.subprojects="MessgaeEnrichment,HelloWorldProject,PassThroughProject"
config.includeDependencies=true
workspace.dir=D:/OSB_Workspace

# export properties from server

# specify a project or use None
export.project    = "OSBPracticeAPP"
export.jar        = D:/OSB_Workspace/Jars/aaada.jar
#export.customFile = c:/temp/cust.xml
export.customFile = None

# import properties to server

# specify a project or use None
import.project    = None
import.jar        = D:/OSB_Workspace/Jars/zzz.jar
import.customFile = None
#import.customFile = D:/temp/ALSBCustomizationFile.xml 


build.xml

<?xml version="1.0" encoding="windows-1252" ?>
<project name="ConfigExport">

   <property file="./build.properties"/>

   <property name="metadata.dir" value="${workspace.dir}/.metadata"/>

   <property name="domain.export.script" value="export.py" />
   <property name="domain.import.script" value="import.py" />

    <path id="library.osb">
       <fileset dir="${osb.home}/modules">
         <include name="com.bea.common.configfwk_1.7.0.0.jar"/>
       </fileset>
       <fileset dir="${weblogic.home}/server/lib">
         <include name="weblogic.jar"/>
       </fileset>
       <fileset dir="${osb.home}/lib">
         <include name="alsb.jar"/>
       </fileset>
    </path>

   <taskdef name="wlst"
            classpathref="library.osb"
            classname="weblogic.ant.taskdefs.management.WLSTTask"/>  


   <target name="exportFromOSB">
        <wlst fileName="${domain.export.script}" debug="true" failOnError="false"  classpathref="library.osb"
              arguments="${wls.username} ${wls.password} ${wls.server} ${export.project} ${export.jar} ${export.customFile}"> 
         <script> 
           adminUser=sys.argv[1] 
           adminPassword=sys.argv[2] 
           adminUrl=sys.argv[3]

           exportJar = sys.argv[5]
           customFile = sys.argv[6]
           passphrase = "osb"
           project = sys.argv[4]

           connect(adminUser,adminPassword,adminUrl)
           domainRuntime()
          </script>
        </wlst>
   </target>

   <target name="importToOSB">
        <wlst fileName="${domain.import.script}" debug="true" failOnError="false" classpathref="library.osb" 
              arguments="${wls.username} ${wls.password} ${wls.server} ${import.project} ${import.jar} ${import.customFile}"> 
         <script> 
           adminUser=sys.argv[1] 
           adminPassword=sys.argv[2] 
           adminUrl=sys.argv[3]

           passphrase = "osb"

           project=sys.argv[4]
           importJar=sys.argv[5]
           customFile=sys.argv[6]

           connect(adminUser,adminPassword,adminUrl)
           domainRuntime()
          </script>
        </wlst>
   </target>

   <target name="exportFromWorkspace">
      <!--delete failonerror="false" includeemptydirs="true"
              dir="${metadata.dir}"/-->
      <java dir="${osb.home}/lib" classpathref="library.osb"
            jar="${eclipse.home}/plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar"
            fork="true" failonerror="true" maxmemory="768m">
         <jvmarg line="-XX:MaxPermSize=256m"/>  
         <arg line="-data ${workspace.dir}"/>
         <arg line="-application com.bea.alsb.core.ConfigExport"/>
         <arg line="-configProject ${config.project}"/>
         <arg line="-configJar ${config.jar}"/>
         <arg line="-configSubProjects ${config.subprojects}"/>
         <arg line="-includeDependencies ${config.includeDependencies}"/>
         <sysproperty key="weblogic.home" value="${weblogic.home}"/>
         <sysproperty key="osb.home" value="${osb.home}"/>
         <sysproperty key="osgi.bundlefile.limit" value="500"/>
         <sysproperty key="harvester.home" value="${osb.home}/harvester"/>
         <sysproperty key="osgi.nl" value="en_US"/>
         <sysproperty key="sun.lang.ClassLoader.allowArraySyntax" value="true"/>
         <sysproperty key="osgi.bundlefile.limit" value="750"/>
         <sysproperty key="middleware.home" value="${fmw.home}"/>
      </java>
   </target>
</project>

 

import.py

from java.util import HashMap
from java.util import HashSet
from java.util import ArrayList
from java.io import FileInputStream

from com.bea.wli.sb.util import Refs
from com.bea.wli.config.customization import Customization
from com.bea.wli.sb.management.importexport import ALSBImportOperation

import sys

#=======================================================================================
# Entry function to deploy project configuration and resources
#        into a ALSB domain
#=======================================================================================

def importToALSBDomain():
    try:
        SessionMBean = None

        print 'Attempting to import :', importJar, "on ALSB Admin Server listening on :", adminUrl

        theBytes = readBinaryFile(importJar)
        print 'Read file', importJar
        sessionName = createSessionName()
        print 'Created session', sessionName
        SessionMBean = getSessionManagementMBean(sessionName)
        print 'SessionMBean started session'
        ALSBConfigurationMBean = findService(String("ALSBConfiguration.").concat(sessionName), "com.bea.wli.sb.management.configuration.ALSBConfigurationMBean")
        print "ALSBConfiguration MBean found", ALSBConfigurationMBean
        ALSBConfigurationMBean.uploadJarFile(theBytes)
        print 'Jar Uploaded'

        if project == "None":
            print 'No project specified, additive deployment performed'
            alsbJarInfo = ALSBConfigurationMBean.getImportJarInfo()
            alsbImportPlan = alsbJarInfo.getDefaultImportPlan()
            alsbImportPlan.setPassphrase(passphrase)
            alsbImportPlan.setPreserveExistingEnvValues(true)
            importResult = ALSBConfigurationMBean.importUploaded(alsbImportPlan)
            SessionMBean.activateSession(sessionName, "Complete import without customization using wlst")
        else:
            print 'ALSB project', project, 'will get overlaid'
            alsbJarInfo = ALSBConfigurationMBean.getImportJarInfo()
            alsbImportPlan = alsbJarInfo.getDefaultImportPlan()
            alsbImportPlan.setPassphrase(passphrase)
            operationMap=HashMap()
            operationMap = alsbImportPlan.getOperations()
            print
            print 'Default importPlan'
            printOpMap(operationMap)
            set = operationMap.entrySet()

            alsbImportPlan.setPreserveExistingEnvValues(true)

            #boolean
            abort = false
            #list of created ref
            createdRef = ArrayList()

            for entry in set:
                ref = entry.getKey()
                op = entry.getValue()
                #set different logic based on the resource type
                type = ref.getTypeId
                if type == Refs.SERVICE_ACCOUNT_TYPE or type == Refs.SERVICE_PROVIDER_TYPE:
                    if op.getOperation() == ALSBImportOperation.Operation.Create:
                        print 'Unable to import a service account or a service provider on a target system', ref
                        abort = true
#                elif op.getOperation() == ALSBImportOperation.Operation.Create:
                else:
                    #keep the list of created resources
                    print 'ref: ',ref
                    createdRef.add(ref)

            if abort == true :
                print 'This jar must be imported manually to resolve the service account and service provider dependencies'
                SessionMBean.discardSession(sessionName)
                raise

            print
            print 'Modified importPlan'
            printOpMap(operationMap)
            importResult = ALSBConfigurationMBean.importUploaded(alsbImportPlan)

            printDiagMap(importResult.getImportDiagnostics())

            if importResult.getFailed().isEmpty() == false:
                print 'One or more resources could not be imported properly'
                raise

            #customize if a customization file is specified
            #affects only the created resources
            if customFile != "None" :
                print 'Loading customization File', customFile
                print 'Customization applied to the created resources only', createdRef
                iStream = FileInputStream(customFile)
                customizationList = Customization.fromXML(iStream)
                filteredCustomizationList = ArrayList()
                setRef = HashSet(createdRef)

                # apply a filter to all the customizations to narrow the target to the created resources
                for customization in customizationList:
                    print customization
                    newcustomization = customization.clone(setRef)
                    filteredCustomizationList.add(newcustomization)

                ALSBConfigurationMBean.customize(filteredCustomizationList)

            SessionMBean.activateSession(sessionName, "Complete import with customization using wlst")

        print "Deployment of : " + importJar + " successful"
    except:
        print "Unexpected error:", sys.exc_info()[0]
        if SessionMBean != None:
            SessionMBean.discardSession(sessionName)
        raise

#=======================================================================================
# Utility function to print the list of operations
#=======================================================================================
def printOpMap(map):
    set = map.entrySet()
    for entry in set:
        op = entry.getValue()
        print op.getOperation(),
        ref = entry.getKey()
        print ref
    print

#=======================================================================================
# Utility function to print the diagnostics
#=======================================================================================
def printDiagMap(map):
    set = map.entrySet()
    for entry in set:
        diag = entry.getValue().toString()
        print diag
    print

#=======================================================================================
# Utility function to read a binary file
#=======================================================================================
def readBinaryFile(fileName):
    file = open(fileName, 'rb')
    bytes = file.read()
    return bytes

#=======================================================================================
# Utility function to create an arbitrary session name
#=======================================================================================
def createSessionName():
    sessionName = String("SessionScript"+Long(System.currentTimeMillis()).toString())
    return sessionName

#=======================================================================================
# Utility function to load a session MBeans
#=======================================================================================
def getSessionManagementMBean(sessionName):
    SessionMBean = findService("SessionManagement", "com.bea.wli.sb.management.configuration.SessionManagementMBean")
    SessionMBean.createSession(sessionName)
    return SessionMBean

# IMPORT script init
try:
    # import the service bus configuration
    # argv[1] is the export config properties file
    importToALSBDomain()

except:
    print "Unexpected error: ", sys.exc_info()[0]
    dumpStack()

    raise


export.py 

from java.io import FileInputStream
from java.io import FileOutputStream
from java.util import ArrayList
from java.util import Collections
from java.util import HashSet

from com.bea.wli.sb.util import EnvValueTypes
from com.bea.wli.sb.util import Refs
from com.bea.wli.config.env import EnvValueQuery
from com.bea.wli.config.env import QualifiedEnvValue
from com.bea.wli.config import Ref
from com.bea.wli.config.customization import Customization
from com.bea.wli.config.customization import FindAndReplaceCustomization
from com.bea.wli.config.customization import EnvValueCustomization

import sys

#=======================================================================================
# Utility function to load properties from a config file
#=======================================================================================
def exportAll():
    try:

        ALSBConfigurationMBean = findService("ALSBConfiguration", "com.bea.wli.sb.management.configuration.ALSBConfigurationMBean")
        print "ALSBConfiguration MBean found"

        print project
        if project == "None" :
            ref = Ref.DOMAIN
            collection = Collections.singleton(ref)
            if passphrase == None :
                print "Export the config"
                theBytes = ALSBConfigurationMBean.export(collection, true, None)
            else :
                print "Export and encrypt the config"
                theBytes = ALSBConfigurationMBean.export(collection, true, passphrase)
        else :
            ref = Ref.makeProjectRef(project);
            print "Export the project", project
            collection = Collections.singleton(ref)
            theBytes = ALSBConfigurationMBean.exportProjects(collection, passphrase)

        aFile = File(exportJar)
        out = FileOutputStream(aFile)
        out.write(theBytes)
        out.close()
        print "ALSB Configuration file: "+ exportJar + " has been exported"

        if customFile != "None":
            print collection
# see com.bea.wli.sb.util.EnvValueTypes in sb-kernel-api.jar for the values

#EnvValueQuery evquery =
#     new EnvValueQuery(
#         null,        // search across all resource types
#         Collections.singleton(EnvValueTypes.URI_ENV_VALUE_TYPE), // search only the URIs
#         null,        // search across all projects and folders.
#         true,        // only search across resources that are
#                      // actually modified/imported in this session
#         "localhost", // the string we want to replace
#         false        // not a complete match of URI. any URI
#                      // that has "localhost" as substring will match
#         );

            refTypes = HashSet()
            refTypes.add(EnvValueTypes.SERVICE_URI_TABLE)
            refTypes.add(EnvValueTypes.SERVICE_URI)
            query = EnvValueQuery(Collections.singleton(Refs.BUSINESS_SERVICE_TYPE), refTypes, collection, false, "search string", false)
#           query = EnvValueQuery(None, Collections.singleton(EnvValueTypes.SERVICE_URI_TABLE), collection, false, "search string", false)
            customEnv = FindAndReplaceCustomization('new endpoint url', query, 'replace string')

#            object = QualifiedEnvValue(Refs.makeBusinessSvcRef(ref,'file'), Refs.BUSINESS_SERVICE_TYPE, "XSDvalidation/file", "aaa")
#            objects = ArrayList()
#            objects.add(object)
#            customEnv2 = EnvValueCustomization('Set the right endpoints', objects)

            print 'EnvValueCustomization created'
            customList = ArrayList()
            customList.add(customEnv)
#            customList.add(customEnv2)

            print customList
            aFile = File(customFile)
            out = FileOutputStream(aFile)
            Customization.toXML(customList, out)
            out.close()

        print "ALSB Dummy Customization file: "+ customFile + " has been created"
    except:
        raise

# EXPORT script init
try:
    exportAll()

except:
    print "Unexpected error: ", sys.exc_info()[0]
    dumpStack()
    raise

Hope you have copied and saved successfully, here we go for step by step procedure
  1. Place the all files under ant -bin location(D:\OFM\Oracle\Middleware\modules\org.apache.ant_1.7.1\bin)  
  2. Configure with proper values in OSB.bat and build.properties as per the environment.
  3. Run the OSB.bat file through command file to set the environment variables.
  4. Execute below commands to perform the respective operations as mentioned below.
  5. ·         Command to create jar file from OSB workspace/Repository
    ant -f build.xml exportFromWorkspace  > exportFromWorkspace.log
    ·         Command to export  jar file from OSB server
    ant -f build.xml exportFromOSB  > exportFromOSB.log
    ·         Command to import the Jar file in the OSB Server
    ant -f build.xml importToOSB  > importToOSB.log

    Note: You can check the log files for respective operations in below location.                         D:\OFM\Oracle\Middleware\modules\org.apache.ant_1.7.1\bin
     
Hope it will helpful for the people who are looking for OSB ant scripts.
Happy Coding, Good Luck.!!! ;)