Thursday, November 8, 2012

OSB Remote Archive with FTP protocol

Sometimes while using FTP protocol in OSB we want to place the archive the files to the remote directory in the FTP server. In such situations enable the remote streaming option in the FTP Transport Configuration. Give a valid directory name(which exists in the FTP server) for the Archive Directory field


Wednesday, November 7, 2012

Read file content. Write content to file

Simple utility methods for reading from and writing to a file


public static String readFile(File file) throws Exception
{
 BufferedReader br = null;
 java.io.FileReader fr = null;
 try {
 fr = new java.io.FileReader(file);
 br = new BufferedReader(fr);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
throw new FileNotFoundException(e.getMessage());
}
StringBuffer sb = new StringBuffer();
String str= null;
try
{
while((str=br.readLine()) != null)
{
 sb.append(str);
 sb.append("\n");
}

}
    catch(Exception e)
{
e.printStackTrace();
throw new Exception(e.getMessage());
}
    finally
    {
    try {
    fr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    return sb.toString();
  }
public static void writeFile(File file,String content) throws Exception
{
BufferedWriter out=null;
     try{
       // Create file
       FileWriter fstream = new FileWriter(file);
           out = new BufferedWriter(fstream);
       out.write(content);
       //Close the output stream
       out.close();
       }catch (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
         throw new Exception(e.getMessage());
       }
       finally
       {
        if(out != null)
        {
        try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        }
       }
}

Remove namespace from xml

Sometime we might need to remove all the namespaces from an xml document.

Here is piece of java code which does that job !!


public static String removeNamespaces(String s)
 {
     Transformer transformer = null;
     try
     {
         transformer = templates.newTransformer();
     }
     catch(TransformerConfigurationException transformerconfigurationexception)
     {
         System.err.println("Error transforming XML");
         transformerconfigurationexception.printStackTrace();
         return null;
     }
     StringReader stringreader = new StringReader(s);
     StringWriter stringwriter = new StringWriter();
     try
     {
         transformer.transform(new StreamSource(stringreader), new StreamResult(stringwriter));
         return stringwriter.toString();
     }
     catch(TransformerException transformerexception)
     {
         System.err.println("Error transforming XML");
         transformerexception.printStackTrace();
         return null;
     }
 }

wlst script for stopping the composites

Often it will be handy if we have a wlst script to stop and start the deployed composites. This will be useful if we have many composites to be switched off and on frequently

Keep the server details and the list of composites to be stopped in a properties file in a local directory - deploydirectory

CompositeList.properties
==================================



url=t3://localhost:7001
host=localhost
port=8001
username=weblogic
password=weblogic1


COMPOSITES=default/SampleCursorRef/1.0,default/AgToAGPollingTest/1.0,default/ReloadProcess/1.0


=================== end of compositeList.properties==============

Create a wlst script as follows

ShutdownComposite.py
====================

from java.io import FileInputStream

propInputStream = FileInputStream("D:\\tech\\wlst\\compositeList.properties")
configProps = Properties()
configProps.load(propInputStream)

url = configProps.get("url")
print 'url=', url
host = configProps.get("host")
print 'host=', host
port = configProps.get("port")
print 'port=', port
username = configProps.get("username")
print 'username=', username
password = configProps.get("password")
print 'password = ', password


print '========================'

COMPOSITES = configProps.get("COMPOSITES")
print 'COMPOSITES = ', COMPOSITES

compositeList=COMPOSITES.split(',')
print compositeList[1]
count = 0
for i in compositeList:
   compositeProps = compositeList[count].split("/")
   partition = compositeProps[0]
   compoisteName = compositeProps[1]
   version = compositeProps[2]
   print 'partition ..',partition
   print 'compoisteName ..',compoisteName
   print 'version ..',version
   print '========================'

   print 'trying to stop ',compoisteName
   try:
sca_stopComposite(host, port, username, password, compoisteName, version)
   except NameError, e:
print 'EXCEPTION OCCURED'
print "Please check there is: ", sys.exc_info()[0], sys.exc_info()[1]
   else:
print 'Stopped ',compoisteName
   count = count+1



============== end of StopComposites.py===============

open command prompt and start the wlst as follows


D:\Oracle\Middleware\Oracle_SOA1\common\bin>wlst.cmd

Run the script from wlst prompt as follows
execfile('D:\\tech\wlst\\ShutdownComposite.py')


Result
==============

========================
trying to stop  ReloadProcess
host = localhost
port = 8001
user = weblogic
partition = default
compositeName = ReloadProcess
revision = 1.0
label =  None
compositeDN =default/ReloadProcess!1.0
Connecting to: service:jmx:t3://localhost:8001/jndi/weblogic.management.mbeanservers.runtime
Composite (default/ReloadProcess!1.0) is successfully stopped.
Stopped  ReloadProcess





OSB deployment using scripts

We can deploy(import) osb project to sbconsole using the wlst script. Exporting of the project from sbconsole and from eclipse workspace also can be done using scripts. Here are the steps

Download import.py and export.py to a local directory(deploy directory)

Create a build properties file in the deploy directory as below.

build.properties
=============

fmw.home=D:/Oracle/Middleware

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

# properties for workspace export
#config.project="OSB Configuration"
#config.jar=D:/temp/sbconfig.jar
#config.subprojects="ASynchronous"
#config.includeDependencies=true
#workspace.dir=C:/eclipse/user/workspace

# export properties

# specify a project or use None
#export.project    = EJB
export.project    = None
export.jar        = D:/temp/sbconfig.jar
#export.customFile = c:/temp/cust.xml
export.customFile = None

# import properties

# specify a project or use None
import.project    = None
#import.project    = EJB
import.jar        = D:/temp/sbconfig.jar
import.customFile = None
import.customFile = D:/temp/ALSBCustomizationFile.xml

==================end of build.properties==============
Create a build.xml file as below in the deploy.directory

build.xml
===============

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

<property file="./build.properties"/>
<property name="bea.home"               value="${fmw.home}"/>
<property name="build"                  value="build"/>
<property name="dist"                   value="dist"/>
<property name="domain.export.script"   value="export.py"/>
<property name="domain.import.script"   value="import.py"/>
<property name="eclipse.home" value="${fmw.home}/oepe_11gR1PS4"/>
<property name="export.config.file"     value="export.properties"/>
<property name="import.config.file"     value="import.properties"/>
<property name="metadata.dir" value="${workspace.dir}/.metadata"/>
<property name="osb.home" value="${fmw.home}/Oracle_OSB1"/>
<property name="weblogic.home" value="${fmw.home}/wlserver_10.3"/>

  <path id="class.path">
  <pathelement path="${bea.home}/wlserver_10.3/server/lib/weblogic.jar"/>
  <pathelement path="${bea.home}/Oracle_OSB1/lib/sb-kernel-api.jar"/>
  <pathelement path="${bea.home}/Oracle_OSB1/lib/sb-kernel-impl.jar"/>
        <pathelement path="${bea.home}/Oracle_OSB1/modules/com.bea.common.configfwk_1.6.0.0.jar"/>
  </path>


  <taskdef classpath="${bea.home}/wlserver_10.3/server/lib/weblogic.jar" name="wlst" classname="weblogic.ant.taskdefs.management.WLSTTask"/>


   <target name="exportFromWorkspace">
      <!--delete failonerror="false" includeemptydirs="true"
              dir="${metadata.dir}"/-->
      <java dir="${eclipse.home}"
            jar="${eclipse.home}/plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.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"/>
      </java>
   </target>

  <target name="export">
    <echo message="exportscript: ${domain.export.script}"/>
    <java classname="weblogic.WLST" fork="true">
        <arg line="${domain.export.script} ${export.config.file}"/>
        <classpath refid="class.path"/>
    </java>
  </target>

  <target name="import">
    <echo message="importscript: ${domain.import.script}"/>
    <java classname="weblogic.WLST" fork="true">
        <arg line="${domain.import.script} ${import.config.file}"/>
        <classpath refid="class.path"/>
    </java>
  </target>

  <target name="clean">
    <delete dir="${dist}"/>
    <delete dir="${build}"/>
    <mkdir dir="${dist}"/>
    <mkdir dir="${build}"/>
  </target>

 
</project>

=========================== end of build.xml==============

Create an import.properties as below in the deploy directory

import.porperties
==============================

##################################################################
# OSB Admin Security Configuration                              #
##################################################################
adminUrl=t3://localhost:7001
importUser=weblogic
importPassword=weblogic1

##################################################################
# OSB Jar to be exported, optional customization file           #
##################################################################
importJar=D:/temp/sbconfig.jar
customizationFile=D:/temp/ALSBCustomizationFile.xml

##################################################################
# Optional passphrase and project name                           #
##################################################################
#passphrase=osb
#project=default
==================end of import.properties==============

Create a batch file to set the class path

deploy.bat
=============

set FMW_HOME=D:\Oracle\Middleware
set ANT_HOME=%FMW_HOME%\modules\org.apache.ant_1.7.1
set PATH=%ANT_HOME%\bin;%PATH%
set JAVA_HOME=C:\jdk1.7.0
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

============== end of deploy.bat====================



Open command prompt and navigate to the deploy directory
Run the deploy.bat
To deploy/import run the command :  ant -verbose import
To export from workspace run the command :  ant -verbose exportFromWorkspace


export.py
============
from java.io import FileInputStream
from java.io import FileOutputStream
from java.util import ArrayList
from java.util import Collections
from com.bea.wli.sb.util import EnvValueTypes
from com.bea.wli.config.env import EnvValueQuery;
from com.bea.wli.config import Ref
from com.bea.wli.config.customization import Customization
from com.bea.wli.config.customization import FindAndReplaceCustomization
import sys
#====================================================================
# Utility function to load properties from a config file
#=========================================================================
def exportAll(exportConfigFile):
try:
print "Loading export config from :", exportConfigFile
exportConfigProp = loadProps(exportConfigFile)
adminUrl = exportConfigProp.get("adminUrl")
exportUser = exportConfigProp.get("exportUser")
exportPasswd = exportConfigProp.get("exportPassword")
exportJar = exportConfigProp.get("exportJar")
customFile = exportConfigProp.get("customizationFile")
passphrase = exportConfigProp.get("passphrase")
project = exportConfigProp.get("project")
connectToServer(exportUser, exportPasswd, adminUrl)
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
query = EnvValueQuery(None,
Collections.singleton(EnvValueTypes.WORK_MANAGER), collection, false, None, false)
customEnv = FindAndReplaceCustomization('Set the right Work Manager', query, 'Production System Work Manager')
print 'EnvValueCustomization created'
customList = ArrayList()
customList.add(customEnv)
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
#====================================================================
# Utility function to load properties from a config file
#=========================================================================
def loadProps(configPropFile):
propInputStream = FileInputStream(configPropFile)
configProps = Properties()
configProps.load(propInputStream)
return configProps
#====================================================================
# Connect to the Admin Server
#=========================================================================
def connectToServer(username, password, url):
connect(username, password, url)
domainRuntime()
# EXPORT script init
try:
exportAll(sys.argv[1])
except:
print "Unexpected error: ", sys.exc_info()[0]
dumpStack()
raise

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(importConfigFile):
    try:
        SessionMBean = None
        print '============Loading Deployment config from :==========', importConfigFile
        exportConfigProp = loadProps(importConfigFile)
        adminUrl = exportConfigProp.get("adminUrl")
        importUser = exportConfigProp.get("importUser")
        importPassword = exportConfigProp.get("importPassword")

        importJar = exportConfigProp.get("importJar")
        customFile = exportConfigProp.get("customizationFile")

        passphrase = exportConfigProp.get("passphrase")
        project = exportConfigProp.get("project")

        connectToServer(importUser, importPassword, adminUrl)

        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 test import with 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:
                    #keep the list of created resources
                    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 test 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 load properties from a config file
#=======================================================================================

def loadProps(configPropFile):
    propInputStream = FileInputStream(configPropFile)
    configProps = Properties()
    configProps.load(propInputStream)
    return configProps

#=======================================================================================
# Connect to the Admin Server
#=======================================================================================

def connectToServer(username, password, url):
    connect(username, password, url)
    domainRuntime()

#=======================================================================================
# 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(sys.argv[1])

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


Sunday, October 14, 2012

Sending message to weblogic queue

A java class for sending text message to a weblogic queue
===============================


package test;

import java.util.Hashtable;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QSendTest {
public static void main(String[] args)
throws Exception {
 QueueConnectionFactory qconFactory = null;
 QueueConnection qcon = null;
 QueueSession qsession = null;
 Queue queue = null;
 TextMessage msg = null;

 InitialContext ctx = getInitialContext();
 qconFactory = (QueueConnectionFactory)
 ctx.lookup("jms/MyTestConnectionFactory");
 qcon = qconFactory.createQueueConnection();
 qsession = qcon.createQueueSession(false,
  QueueSession.AUTO_ACKNOWLEDGE);

 queue = (Queue) ctx.lookup("jms/MyTestQueue");

//Posting message to Queue
 QueueSender qsender = qsession.createSender(queue);
 msg = qsession.createTextMessage();
 msg.setStringProperty("MSG_ID", "12345678947");
 msg.setText("Hello at " + System.currentTimeMillis());
 qsender.send(msg);
 System.out.println("Sent");
 qsender.close();

}

private static InitialContext getInitialContext()
throws NamingException {
 Hashtable<String, String> env = new Hashtable<String, String>();
 env.put(Context.INITIAL_CONTEXT_FACTORY,
   "weblogic.jndi.WLInitialContextFactory");
 env.put(Context.PROVIDER_URL, "t3://localhost:8001/");
 return new InitialContext(env);
}
}

Extract all your service end points using a java utility

Often we may find a need for publishing the endpoints of all your service to a new client. Instead of getting the end points for each and every service from em console a java class would do that job quickly.



Code
================

package test;

import java.util.Hashtable;
import java.util.UUID;
import java.util.List;

import javax.naming.Context;

import oracle.soa.management.facade.Locator;
import oracle.soa.management.facade.LocatorFactory;
import oracle.soa.management.facade.Composite;
import oracle.soa.management.facade.Service;
import oracle.soa.management.facade.CompositeInstance;
import oracle.soa.management.facade.ComponentInstance;


import oracle.fabric.common.NormalizedMessage;
import oracle.fabric.common.NormalizedMessageImpl;

import oracle.soa.management.util.CompositeInstanceFilter;
import oracle.soa.management.util.ComponentInstanceFilter;


import java.util.Map;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.Element;
import java.io.*;

import oracle.soa.management.CompositeDN;
import oracle.soa.management.facade.Component;


public class EndpointLister {
    public EndpointLister() {
        super();

        Hashtable jndiProps = new Hashtable();
        jndiProps.put(Context.PROVIDER_URL, "t3://localhost:8001/soa-infra");
        jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
        jndiProps.put(Context.SECURITY_PRINCIPAL, "weblogic");
        jndiProps.put(Context.SECURITY_CREDENTIALS, "weblogic1");
        jndiProps.put("dedicated.connection", "true");
     
 

        Locator locator = null;
        try {
            // connect to the soa server
            locator = LocatorFactory.createLocator(jndiProps);

            // find composite
            List composite = locator.listComposites();
         
            for(int i=0;i<composite.size();i++){
           
              CompositeDN lCompositeDN = (CompositeDN)composite.get(i);
              Composite lComposite = locator.lookupComposite(lCompositeDN);
              List lServices = lComposite.getServices();
           
              for(int j=0;j<lServices.size();j++){
              Service lService =(Service)lServices.get(j);
              System.out.println(lService.getWSDLURL());
              }
            }
        }
            catch( Exception E){
              E.printStackTrace();;
            }
              }



    public static void main(String[] args) {
        EndpointLister endpointLister = new EndpointLister();
    }
}

Dependent Jars
===================
servers\soa_server1\tmp\_WL_user\soa-infra\ztorh\soa-infra-mgmt.jar
oracle_common\modules\oracle.fabriccommon_11.1.1\fabric-common.jar
wlserver_10.3\server\lib\weblogic.jar
oracle_common\modules\oracle.jrf_11.1.1\jrf-api.jar