/* * * Copyright (c) 1999 - 2011 my-Channels Ltd * Copyright (c) 2012 - 2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors. * * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG. * */ package com.pcbsys.nirvana.nAdminAPI.apps; import com.pcbsys.foundation.utils.fEnvironment; import com.pcbsys.nirvana.client.nSessionAttributes; import com.pcbsys.nirvana.nAdminAPI.xml.tools.ImportExportManager; import com.pcbsys.nirvana.nAdminAPI.xml.tools.ImportExportParametersBuilder; import com.pcbsys.nirvana.nAdminAPI.nClusterNode; import com.pcbsys.nirvana.nAdminAPI.nRealmNode; import com.pcbsys.nirvana.nAdminAPI.xml.NirvanaRealm; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Date; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; /** * This application can be used to export the objects within a realm into an xml format. *

* The program will search through the realm and output the acl entries and permissions, configuration, * for all channels, queues, interfaces and scheduler information */ public class nExportRealmXML { private String realmUrl = null; private String fileName = null; private ImportExportParametersBuilder exportParameters = null; /** * Constructor for the class, takes the string realm url */ public nExportRealmXML(String[] args) { nRealmNode node = null; Writer writer = null; try { processArgs(args); report("Connecting to " + realmUrl); // construct the session attributes for the realm nSessionAttributes attr = new nSessionAttributes(realmUrl); // construct the realm node node = new nRealmNode(attr); node.waitForEntireNameSpace(); NirvanaRealm realm = ImportExportManager.exportConfiguration(node, exportParameters); // create a JAXBContext JAXBContext jc = JAXBContext.newInstance("com.pcbsys.nirvana.nAdminAPI.xml"); // create a Marshaller and marshal to System.out Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); writer = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"); m.marshal(realm, writer); // close the node, which closes all sessions made to the namespace } catch (Exception ex) { ex.printStackTrace(); } finally { closeNode(node); } if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Main program thread, takes a realm URL as the parameter to start the search */ public static void main(String[] args) { //Process Environment Variables processEnvironmentVariable("RNAME"); processEnvironmentVariable("LOGLEVEL"); processEnvironmentVariable("HPROXY"); processEnvironmentVariable("HAUTH"); processEnvironmentVariable("CKEYSTORE"); processEnvironmentVariable("CKEYSTOREPASSWD"); processEnvironmentVariable("CAKEYSTORE"); processEnvironmentVariable("CAKEYSTOREPASSWD"); // Install any proxy server settings fEnvironment.setProxyEnvironments(); // Install JSSE SSL Environement settings fEnvironment.setSSLEnvironments(); new nExportRealmXML(args); } private void processArgs(String[] args) { switch (args.length) { case 0: Usage(); UsageEnv(); System.exit(1); case 1: if (args[0].equals("-?")) { UsageEnv(); } System.setProperty("FILE", args[0]); getOptions(null); break; default: System.setProperty("FILE", args[0]); getOptions(args); } } private static void processEnvironmentVariable(String variable) { String laxVAR = System.getProperty("lax.nl.env." + variable); if (laxVAR != null) { System.setProperty(variable, laxVAR); } } /** * Set the program variables and flags based on command line args */ public void getOptions(String[] args) { realmUrl = System.getProperty("RNAME", null); if (realmUrl == null) { Usage(); System.exit(1); } fileName = System.getProperty("FILE"); if (fileName == null) { Usage(); System.exit(1); } if (args == null) { Usage(); System.exit(1); } exportParameters = ImportExportParametersBuilder.createParametersBuilder(); // set what will be exported based on command line arguments for (int x = 0; x < args.length; x++) { if (args[x].equals("-all")) { exportParameters.enableAll(); } else if (args[x].equals("-realmacl")) { exportParameters.setRealmAclProcessing(true); } else if (args[x].equals("-realms")) { exportParameters.setRealmsProcessing(true); } else if (args[x].equals("-cluster")) { exportParameters.setClusterProcessing(true); } else if (args[x].equals("-realmcfg")) { exportParameters.setRealmCfgProcessing(true); } else if (args[x].equals("-channels")) { exportParameters.setChannelsProcessing(true); } else if (args[x].startsWith("-channelfilter")) { exportParameters.setChannelfilter(getFilter(args[x])); } else if (args[x].equals("-channelacls")) { exportParameters.setChannelAclsProcessing(true); } else if (args[x].equals("-jndi")) { exportParameters.setJndiProcessing(true); } else if (args[x].equals("-joins")) { exportParameters.setJoinsProcessing(true); } else if (args[x].equals("-durables")) { exportParameters.setDurablesProcessing(true); } else if (args[x].equals("-queues")) { exportParameters.setQueuesProcessing(true); } else if (args[x].startsWith("-queuefilter")) { exportParameters.setQueuefilter(getFilter(args[x])); } else if (args[x].equals("-queueacls")) { exportParameters.setQueueAclsProcessing(true); } else if (args[x].equals("-interfaces")) { exportParameters.setInterfacesProcessing(true); } else if (args[x].equals("-plugins")) { exportParameters.setPluginsProcessing(true); } else if (args[x].equals("-via")) { exportParameters.setViaProcessing(true); } else if (args[x].equals("-scheduler")) { exportParameters.setSchedulerProcessing(true); } else if (args[x].equals("-datagroups")) { exportParameters.setDatagroupsProcessing(true); } else if (args[x].startsWith("-datagroupfilter")) { exportParameters.setDatagroupfilter(getFilter(args[x])); } } } private String getFilter(String arg) { String[] parts = arg.split("="); if(parts.length != 2) { throw new IllegalArgumentException("Wrong filter: " + arg + " Use '='!"); } return parts[1]; } /** * Prints the usage message for this class */ private static void Usage() { System.out.println("Usage ...\n"); System.out.println("nexportrealmxml [export_file_location]\n"); System.out.println( " -all -realms -cluster -realmacl -realmcfg -channels -jndi -channelfilter= -channeacls -datagroups -datagroupfilter= -joins -durables -queues -queuefilter= -queueacls -interfaces -plugins -via\n"); System.out.println( "\n values are a comma separated list of java regular expressions that can filter channel, queue or datagroup names respectively."); System.out.println( "\nExample 1: nexportrealmxml test.xml -channels -channelfilter=/perf.+ \n will export only channels whose absolute path starts with /perf"); System.out.println( "\nExample 2: nexportrealmxml test.xml -channels -channelfilter=.+test \n will export only channels whose absolute path ends in test"); System.out.println( "\nExample 3: nexportrealmxml test.xml -channels -channelfilter=.*test.* \n will export only channels whose absolute path contains test"); System.out.println( "\nExample 4: nexportrealmxml test.xml -all -channelfilter=.*test.*,/perf.* \n will export everything with channels being restricted to those with absolute path containing test or starting with /perf"); System.out.println("\n\nNote: -? provides help on environment variables \n"); } private static void UsageEnv() { System.out.println("\n\n(Environment Variables) \n"); System.out.println("(RNAME) - One or more RNAME entries in the form protocol://host:port"); System.out.println(" protocol - Can be one of nsp, nhp, nsps, or nhps, where:"); System.out.println(" nsp - Specifies Nirvana Socket Protocol (nsp)"); System.out.println(" nhp - Specifies Nirvana HTTP Protocol (nhp)"); System.out.println(" nsps - Specifies Nirvana Socket Protocol Secure (nsps), i.e. using SSL/TLS"); System.out.println(" nhps - Specifies Nirvana HTTP Protocol Secure (nhps), i.e. using SSL/TLS"); System.out.println(" port - The port number of the server"); System.out.println( "\nHint: - For multiple RNAME entries, use comma separated values which will be attempted in connection weight order\n"); System.out.println( "(LOGLEVEL) - This determines how much information the nirvana api will output 0 = verbose 7 = quiet\n"); System.out.println("(CKEYSTORE) - If using SSL, the location of the keystore containing the client cert\n"); System.out.println("(CKEYSTOREPASSWD) - If using SSL, the password for the keystore containing the client cert\n"); System.out.println("(CAKEYSTORE) - If using SSL, the location of the ca truststore\n"); System.out.println("(CAKEYSTOREPASSWD) - If using SSL, the password for the ca truststore\n"); System.out.println("(HPROXY) - HTTP Proxy details in the form proxyhost:proxyport, where:"); System.out.println(" proxyhost - The HTTP proxy host"); System.out.println(" proxyport - The HTTP proxy port\n"); System.out.println("(HAUTH) - HTTP Proxy authentication details in the form user:pass, where:"); System.out.println(" user - The HTTP proxy authentication username"); System.out.println(" pass - The HTTP proxy authentication password\n"); System.exit(1); } private void closeNode(nRealmNode node) { if (node != null) { nClusterNode nClusterNode = node.getCluster(); if (nClusterNode != null) { nClusterNode.close(); } else { node.close(); } } } public static void report(String line) { Date dt = new Date(); System.out.println(dt.toString() + " > " + line); } }