package oracle.osb.jmx;

import com.bea.wli.config.Ref;
import com.bea.wli.sb.management.configuration.ALSBConfigurationMBean;
import com.bea.wli.sb.management.configuration.ProxyServiceConfigurationMBean;
import com.bea.wli.sb.management.configuration.SessionManagementMBean;
import com.bea.wli.sb.management.query.ProxyServiceQuery;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import weblogic.management.jmx.MBeanServerInvocationHandler;
import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;


public class ProxyServiceUtilJMX {
    private static final String HTTP_TRANSPORT_SCHEME = "http";

    private String hostName = null;
    private int port = -1;
    private String user = null;
    private String password = null;
    private String sessionName = null;

    public ProxyServiceUtilJMX() {
        super();
    }

    public ProxyServiceUtilJMX(String hostName, int port, String user, String password, String sessionName) {
        super();

        this.hostName = hostName;
        this.port = port;
        this.user = user;
        this.password = password;
        this.sessionName = sessionName;
    }

    public boolean enableProxyService(String proxyServiceName, String projectName) {
        return setStatusProxyService(proxyServiceName, projectName, true);
    }

    public boolean disableProxyService(String proxyServiceName, String projectName) {
        return setStatusProxyService(proxyServiceName, projectName, false);
    }

    public boolean setStatusProxyService(String proxyServiceName, String projectName, boolean enable) {
        boolean done = false;

        JMXConnector jmxConnector = null;
        SessionManagementMBean sessionManagementMBean = null;
        try {
            //get the jmx connector
            jmxConnector = initConnection();

            // get mbean connection
            MBeanServerConnection mbConnection = jmxConnector.getMBeanServerConnection();

            // get domain service mbean. This is the topmost mbean
            DomainRuntimeServiceMBean domainService =
                (DomainRuntimeServiceMBean)MBeanServerInvocationHandler.newProxyInstance(mbConnection, new ObjectName(DomainRuntimeServiceMBean.OBJECT_NAME));

            // obtain session management mbean to create a session.
            // This mbean instance can be used more than once to
            // create/discard/commit many sessions
            sessionManagementMBean = (SessionManagementMBean)domainService.findService(SessionManagementMBean.NAME, SessionManagementMBean.TYPE, null);

            // create a session
            sessionManagementMBean.createSession(sessionName);

            // obtain the ALSBConfigurationMBean instance that operates on the session that has
            // just been created. Notice that the name of the mbean contains the session name.
            ALSBConfigurationMBean alsbConfMB =
                (ALSBConfigurationMBean)domainService.findService(ALSBConfigurationMBean.NAME + "." + sessionName, ALSBConfigurationMBean.TYPE, null);


            ProxyServiceQuery query = new ProxyServiceQuery();

      // We look for a PS whose transport is HTTP, WSDL and qualified
            query.setTransportScheme(HTTP_TRANSPORT_SCHEME);
            query.setWSDLBasedService(true);

            if (enable)
                query.setServiceEnabled(false);
            else
                query.setServiceEnabled(true);

            // Name of the service to look for
            query.setLocalName(proxyServiceName);

            Ref proxyServiceRef = null;
            Set<Ref> refs = alsbConfMB.getRefs(query);
            System.out.println("DEBUG: Found resources that ");
            for (Ref ref : refs) {
                System.out.println(ref + " Project name: " + ref.getProjectName());

                if (ref.getProjectName().equalsIgnoreCase(projectName)) {
                    proxyServiceRef = ref;
                    break;
                }

            }

            // If we have found the  proxy service...
            if (proxyServiceRef != null) {
                System.out.println("Realizing changes in the session: " + sessionName);

                ProxyServiceConfigurationMBean proxyServiceConfigurationMBean =
                    (ProxyServiceConfigurationMBean)domainService.findService(
            ProxyServiceConfigurationMBean.NAME + "." + sessionName,
              ProxyServiceConfigurationMBean.TYPE, null)

                if (enable) {
                    proxyServiceConfigurationMBean.enableService(proxyServiceRef);
                else {
                    proxyServiceConfigurationMBean.disableService(proxyServiceRef);
                }

                // activate changes performed in the session
                sessionManagementMBean.activateSession(sessionName, "Activate changes made in the session: " + sessionName);

                done = true;
            }

        catch (Throwable t) {
            System.out.println("ERROR: " + t.getMessage());
            t.printStackTrace();
            try {
                if (sessionManagementMBean != null)
                    sessionManagementMBean.discardSession(sessionName);
            catch (Exception e) {
                System.out.println("Error when discarding the session: " + sessionName);
            }

        finally {
            try {
                if (jmxConnector != null)
                    jmxConnector.close();
            catch (IOException ioe) {
                System.out.println("Error when closing the connection.");
            }
        }

        System.out.println("End of program");
        return done;
    }

    public static void enableProxyService (String hostName, int port, String user, String password, String sessionName, String proxyServiceName, String projectNamethrows Exception {
        ProxyServiceUtilJMX util = new ProxyServiceUtilJMX(hostName, port, user, password, sessionName);
        util.enableProxyService(proxyServiceName, projectName);
    }

    public static void disableProxyService (String hostName, int port, String user, String password, String sessionName, String proxyServiceName, String projectNamethrows Exception {
        ProxyServiceUtilJMX util = new ProxyServiceUtilJMX(hostName, port, user, password, sessionName);
        util.disableProxyService(proxyServiceName, projectName);
    }

    /**
     @param args
     @throws Exception
     */
    public static void main(String[] argsthrows Exception {

        String hostName = "localhost";
        int port = 7001;
        String user = "weblogic";
        String password = "weblogic";
        String sessionName = "JMX_REMOTE_ENABLE_PROXY";

    // Modify this to match the name of the proxy service you want to turn on or off
        String proxyServiceName = "TestProxy_PS"
    
    // Modify this to match the name of the project you are working with
        String projectName = "TestProject"

        ProxyServiceUtilJMX util = new ProxyServiceUtilJMX(hostName, port, user, password, sessionName);

        util.enableProxyService(proxyServiceName, projectName);

    }

  /**
   * Get the JMXConnector to the Oracle Service Bus
   *
   */
    private JMXConnector initConnection() throws IOException, MalformedURLException {
        JMXServiceURL serviceURL = new JMXServiceURL("t3"this.hostName, this.port, "/jndi/" + DomainRuntimeServiceMBean.MBEANSERVER_JNDI_NAME);

        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.SECURITY_PRINCIPAL, this.user);
        env.put(Context.SECURITY_CREDENTIALS, this.password);
        env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");

        return JMXConnectorFactory.connect(serviceURL, env);
    }
}
Java2html