Undeploy non default, retired or inactive soa suite composites using java

When building a lot of soa suite composites, your environments end up with many different versions. So we might end up with 3 LocationService composites with versions[1.0.0.0], [1.0.1.0] and [1.0.2.0] which is the default. This takes up resources and clutters the EM. So you might want to have a way to remove these easily with having to go to the EM and manually uninstalling all of them with the risk of mis-clicking. In this blog I will show an easy way to undeploy old soa composites using java.

At first I looked at WLST and there is a sca_undeployComposite but we don’t want to specify each composite seperate. We just want to look through al composites and see which ones aren’t default and then undeploy them. There is also a sca_listDeployedComposites but I couldn’t find a way to pipe the output of this to an array to be able to work with it. So I just did what I know best and write it in Java. It basically does the same. Just connect through JMX, look up all the composites and check if they are:

  • non-default
  • retired
  • inactive
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;

/**
 *
 * @author hugohendriks
 */
public class UndeployServices {

    private static final Logger LOGGER = Logger.getLogger(UndeployServices.class.getName());

    /**
     * Initialize the connection with the server
     *
     * @param hostname the host
     * @param port the port
     * @param username the username
     * @param password the password
     * @return a JMXConnector
     * @throws IOException
     * @throws MalformedURLException
     */
    public static JMXConnector initConnection(String hostname, int port, String username, String password) throws IOException, MalformedURLException {
        JMXServiceURL serviceURL = new JMXServiceURL("t3", hostname, port, "/jndi/" + DomainRuntimeServiceMBean.MBEANSERVER_JNDI_NAME);
        Hashtable<String, String> h = new Hashtable<String, String>();
        h.put(Context.SECURITY_PRINCIPAL, username);
        h.put(Context.SECURITY_CREDENTIALS, password);
        h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
        return JMXConnectorFactory.connect(serviceURL, h);
    }

    /**
     * List all the SOA services
     *
     * @throws Exception
     */
    public static void uninstallObsoleteServices(String host,int port, String username, String password) throws Exception {
        
        String dn = null;
        Boolean isDefault = null;
        String state = null;
        String mode = null;
        String name = null;
        String versie = null;
        
        JMXConnector connector = initConnection(host, port, username, password);
        // get mbean connection
        MBeanServerConnection mbconn = connector.getMBeanServerConnection();

        ObjectName queryObject = new ObjectName("oracle.soa.config:Location=soa_ms01,name=soa-infra,j2eeType=CompositeLifecycleConfig,Application=soa-infra,*");
        Set queryObjectName = mbconn.queryNames(queryObject, null);
        Iterator iterator = queryObjectName.iterator();

        while (iterator.hasNext()) {
            ObjectName compositeObjectName = (ObjectName) iterator.next();

            CompositeData[] composites = (CompositeData[]) mbconn.getAttribute(compositeObjectName, "DeployedComposites");
            for (CompositeData composite : composites) {
                dn = (String) composite.get("DN");
                isDefault = (Boolean) composite.get("isDefault");
                state = (String) composite.get("state");
                mode = (String) composite.get("mode");
                name = dn.substring(dn.indexOf('/') + 1, dn.indexOf('!'));
                versie = dn.substring(dn.indexOf('!') + 1, dn.indexOf('*'));
                LOGGER.info("----Checking " + name + "[" + versie + "]");
                //if (!isDefault || mode.equalsIgnoreCase("retired") || state.equalsIgnoreCase("off")) {
                if (!isDefault) {
                    LOGGER.info("-----------Undeploying " + name + "[" + versie + "] from " + host + " as it is NOT a default composite");
                    mbconn.invoke(compositeObjectName, "removeCompositeForLabel", new Object[]{dn},new String[]{"java.lang.String"});
                }
            }
        }
        connector.close();
    }


    /**
     * Undeploy all the non-default instances
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String omgeving = null;
        String username = null;
        String password = null;
        
        //inladen parameters
        if (args.length > 0) {
            omgeving = args[0];
            username = args[1];
            password = args[2];
        }
        
        Properties prop = new Properties();
        InputStream input = null;
        input = new FileInputStream(new File("src//main//resources//omgeving.properties"));
        prop.load(input);
        
        //zoek altijd op basis van de omgeving de juiste server en port op
        String host = prop.getProperty(omgeving + ".host");
        String port = prop.getProperty(omgeving + ".port");
        //als de username en ww niet meegegeven zijn, check dan de omgevings file. Hier staan alleen zaken in voor dev en tst
        if(username == null || password == null){
            username = prop.getProperty(omgeving + ".username");
            password = prop.getProperty(omgeving + ".password");
        }
        
        if (username == null || password == null || username.equals("") || password.equals("")) {
            throw new Exception("Te weinig input parameters voor omgeving " + omgeving + ". Syntax = -omgeving -username -password");
            
        } else {
            uninstallObsoleteServices(host, Integer.parseInt(port), username, password);
        }
    }
    
}

Dependency wise it looks like my previous post here.

The output wil look something like this:
output

Leave a Reply

Your email address will not be published. Required fields are marked *

*