Sunday, July 8, 2012

Changing the reference endpoint of a Composite through JAVA – Oracle SOA Suite


Changing the reference endpoint of a Composite through JAVA – Oracle SOA Suite

Sometimes we may need to change the endpoint value of a Composite reference, the same can be done through the EM console by following the below blog.

Another way to accomplish this is to use the Java API; this blog will explain how to use the Java API to change the reference endpoint of a Oracle SOA Suite composite. This approach will work in both Oracle SOA Suite 11g and Oracle SOA Suite 12c.

This update is not a cluster-aware; remember that you must repeat the same process for each managed server of your environment.


UpdateEndPoint .java

 import java.util.*;
import javax.management.*;
import javax.management.openmbean.*;
import javax.management.remote.*;
import javax.naming.Context;

public class UpdateEndPoint {

    public static MBeanServerConnection getMbeanServerConnection(String host,int port,String userName,String password) throws Exception {
        String jndiroot = "/jndi/";
        MBeanServerConnection m_connection = null;
        try {
            Hashtable jndiProps = new Hashtable();
            jndiProps.put(Context.SECURITY_PRINCIPAL, userName);
            jndiProps.put(Context.SECURITY_CREDENTIALS, password);
            jndiProps.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");

            JMXServiceURL serviceURL =new JMXServiceURL("t3", host, port, jndiroot +"weblogic.management.mbeanservers.runtime");
            JMXConnector m_connector =JMXConnectorFactory.newJMXConnector(serviceURL, jndiProps);
            m_connector.connect();
            m_connection = m_connector.getMBeanServerConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return m_connection;
    }

    private static CompositeDataSupport UpdateEndPointData(CompositeDataSupport cds,String[] keys,String[] newValues) throws Exception {
        Map<String, Object> items =new HashMap<String, Object>(cds.getCompositeType().keySet().size());

        for (String key : cds.getCompositeType().keySet()) {
            boolean matched = false;
            int i = 0;

            for (; i < keys.length; i++) {
                String searchKey = keys[i];

                if (searchKey.equalsIgnoreCase(key)) {
                    items.put(key, newValues[i]);
                    matched = true;
                    break;
                }
            }

                if (!matched) {                   
                         items.put(key, cds.get(key));                 
                 }
             }

        return new CompositeDataSupport(cds.getCompositeType(), items);
    }

    public static void updateEndPoint(String host, String port,String userName, String password,String compositeName,String revision, String referenceName, String endpoint) {

        String mBeanName =
            "oracle.soa.config:SCAComposite=\""+compositeName+"\""+",revision="+revision+",port=*,label=*,Application=*,name=WSBinding,SCAComposite.SCAReference="+referenceName+",j2eeType=SCAComposite.SCAReference.SCABinding,*";

        MBeanServerConnection mbsc;
        try {
            mbsc =getMbeanServerConnection(host, Integer.parseInt(port), userName, password);
            Set<ObjectName> mbeans =mbsc.queryNames(new ObjectName(mBeanName), null);
            ObjectName mbean = (ObjectName)mbeans.iterator().next();
            javax.management.openmbean.CompositeData[] properties =(javax.management.openmbean.CompositeData[])mbsc.getAttribute(mbean, "Properties");
            boolean endpointFound = false;
            for (int i = 0; i < properties.length; i++) {
                CompositeDataSupport cds = (CompositeDataSupport)properties[i];        

                if ("endpointURI".equalsIgnoreCase((String)cds.get("name"))) {

                    properties[i] =UpdateEndPointData((CompositeDataSupport)properties[i],new String[] { "value" },new String[] { endpoint });
                    mbsc.setAttribute(mbean,new Attribute("Properties", properties));
                    endpointFound = true;
                    break;
                }
            }

            // Property not found, insert it
            if (!endpointFound) {
                CompositeDataSupport cds =UpdateEndPointData((CompositeDataSupport)properties[0],new String[] { "name", "value" },new String[] { "endpointURI",endpoint });
                CompositeData[] newPropertyArray =AppendEntry(properties, cds);
                mbsc.setAttribute(mbean,new Attribute("Properties", newPropertyArray));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static CompositeData[] AppendEntry(CompositeData[] properties,CompositeDataSupport endpointProperty) {
        CompositeData[] newArray =new CompositeData[properties.length + 1];
        for (int x = 0; x < properties.length; x++) {
            newArray[x] = properties[x];
        }
        newArray[properties.length] = endpointProperty;
        return newArray;
    }

    public static void main(String[] args) {        //updatePreference(host,post,username,password,compositeName,revision,,referenceParnerlink,enpoint);
  updateEndPoint("localhost", "8000", "weblogic", "password","InvokeServlet", "1.0","InvokeServletService","http://localhost:8080/EmployeeServlet/employeeservlet");
    }
}

Add the weblogic.jar file to the class.
Before executing the program, change the server details, Composite name, revision, reference name and the endpoint location accordingly.

O/P



3 comments:

  1. Hey I see the Endpoint URL getting reset to the old value when I restart the server. is there a way to avoid that?

    ReplyDelete
  2. It seems the end point changes are not persisted.
    Execute mbsc.invoke(mbean, "save", null, null); after changing the endpoint.
    The save operation should be performed even if we are updating the endpoint from EM console(The save operation can be invoked form EM console Mbean browser)

    Regards
    Albin I

    ReplyDelete
  3. DreamHost is ultimately one of the best hosting company for any hosting plans you might require.

    ReplyDelete