I am trying to import configuration sbconfig.jar in OSB using java for automated tests. Before that I implemented FindAndReplace operation using Orcale help in java. But in сase importing configuration from the same Article, when I run presented code - I get an error.
What I am trying to do
- Connect to OSB(Oracle Service Bus 11.1)
- Read file
- Create session
- Import prepared file
- Activate session
On fourth item i got an error:
Cannot import to deployed configuration
Maybe someone has already implemented such a thing?
I would be grateful for help
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");
}
}