1

我正在尝试使用 java 在 OSB 中导入配置 sbconfig.jar 进行自动化测试。在此之前,我使用 Java 中的Orcale 帮助实现了 FindAndReplace 操作。但是如果从同一篇文章中导入配置,当我运行提供的代码时 - 我得到一个错误。

我想要做什么

  1. 连接到 OSB(Oracle 服务总线 11.1)
  2. 读取文件
  3. 创建会话
  4. 导入准备好的文件
  5. 激活会话

在第四项我得到一个错误:

Cannot import to deployed configuration

也许有人已经实施了这样的事情?

我将不胜感激

package update.configuration.osb;

import com.bea.wli.config.Ref;
import com.bea.wli.config.customization.FindAndReplaceCustomization;
import com.bea.wli.config.env.EnvValueQuery;
import com.bea.wli.config.importexport.ImportResult;
import com.bea.wli.config.resource.Diagnostics;
import com.bea.wli.sb.management.configuration.*;
import com.bea.wli.sb.util.EnvValueTypes;
import com.bea.wli.config.customization.Customization;

import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

import javax.management.*;
import javax.management.remote.*;
import javax.naming.Context;

import weblogic.management.jmx.MBeanServerInvocationHandler;
import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
import com.bea.wli.sb.management.importexport.ALSBJarInfo;
import com.bea.wli.sb.management.importexport.ALSBImportPlan;

import java.util.Map;


public class OSBConfigUpdateNew {
    private 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);
    }

    static private void simpleImportExport(String importFileName, String host, int port,
                                           String username, String password) {
        JMXConnector conn = null;
        SessionManagementMBean sm = null;

        File importFile = new File(importFileName);
        byte[] bytes = readBytes(importFile);

        String sessionName = "newsession";
        String statusmsg = "";
        try {
            conn = initConnection(host, port, username, password);
            MBeanServerConnection mbconn = conn.getMBeanServerConnection();
            DomainRuntimeServiceMBean domainService = (DomainRuntimeServiceMBean) MBeanServerInvocationHandler
                    .newProxyInstance(mbconn, new ObjectName(
                            DomainRuntimeServiceMBean.OBJECT_NAME));
            sm = (SessionManagementMBean) domainService.findService(
                    SessionManagementMBean.NAME, SessionManagementMBean.TYPE,
                    null);
            sm.createSession(sessionName);
            ALSBConfigurationMBean alsbSession = getConfigMBean(sessionName, conn);
//            ALSBConfigurationMBean alsbSession = (ALSBConfigurationMBean) domainService.findService(ALSBConfigurationMBean.NAME + "." + "newsession", ALSBConfigurationMBean.TYPE, null);

        try {
            alsbSession.uploadJarFile(bytes);

            ALSBJarInfo jarInfo = alsbSession.getImportJarInfo();
            ALSBImportPlan importPlan = jarInfo.getDefaultImportPlan();

            ImportResult result = alsbSession.importUploaded(importPlan);

            if (result.getImported().size() > 0) {
                System.out.println("The following resources have been successfully imported.");
                for (Ref ref : result.getImported()) {
                    System.out.println("\t" + ref);
                }
            }

            if (result.getFailed().size() > 0) {
                System.out.println("The following resources have failed to be imported.");
                for (Map.Entry<Ref, Diagnostics> e : result.getFailed().entrySet()) {
                    Ref ref = e.getKey();
                    Diagnostics d = e.getValue();
                    System.out.println("\t" + ref + ". reason: " + d);
                }
                System.out.println("Discarding the session.");
                sm.discardSession(sessionName);
                System.exit(1);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
        sm.activateSession(sessionName, "description");

        ALSBConfigurationMBean alsbcore = getConfigMBean(null, conn);
            byte[] contentsProj = alsbcore.exportProjects(Collections.singleton(Ref.makeProjectRef("Project")), null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        } catch (Exception ex) {
            if (null != sm) {
                try {
                    sm.discardSession(sessionName);
                } catch (Exception e) {
                    System.out.println("Able to discard the session");
                }
            }
            statusmsg = "Not able to perform the operation";
            ex.printStackTrace();
        } finally {
            if (null != conn)
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    }

    private static ALSBConfigurationMBean getConfigMBean(String sessionName, JMXConnector conn) throws Exception {
        MBeanServerConnection mbconn = conn.getMBeanServerConnection();
        DomainRuntimeServiceMBean domainService =
                (DomainRuntimeServiceMBean) MBeanServerInvocationHandler.newProxyInstance(
                        mbconn, new ObjectName(DomainRuntimeServiceMBean.OBJECT_NAME));
        return (ALSBConfigurationMBean) domainService.findService(
                ALSBConfigurationMBean.NAME,
                ALSBConfigurationMBean.TYPE, null);
    }

    private static byte[] readBytes(File importFile) {
        ByteArrayOutputStream ous = null;
        InputStream ios = null;
        try {
            byte[] buffer = new byte[4096];
            ous = new ByteArrayOutputStream();
            ios = new FileInputStream(importFile);
            int read = 0;
            while ((read = ios.read(buffer)) != -1)
                ous.write(buffer, 0, read);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ous != null)
                    ous.close();
            } catch (IOException e) {
            }
            try {
                if (ios != null)
                    ios.close();
            } catch (IOException e) {
            }
        }
        return ous.toByteArray();
    }    

//  private static Ref constructRef(String refType, String serviceuri) {
//      Ref ref = null;
//      String[] uriData = serviceuri.split("/");
//      ref = new Ref(refType, uriData);
//      return ref;
//  }

    public static void main(String[] args) {
        simpleImportExport("C:\\sbconfig.jar", "127.0.0.1",
                7001, "user", "password");
    }
}
4

0 回答 0