⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 raconfig.java

📁 一个类似于openJMS分布在ObjectWeb之下的JMS消息中间件。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * JORAM: Java(TM) Open Reliable Asynchronous Messaging
 * Copyright (C) 2005 - Bull SA
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA.
 *
 * Initial developer(s): Nicolas Tachker (ScalAgent)
 * Contributor(s):
 */
package org.objectweb.joram.client.connector.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.LineNumberReader;
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipInputStream;
import java.util.jar.JarFile;

import org.objectweb.joram.client.connector.AdapterTracing;
import fr.dyade.aaa.agent.conf.A3CML;
import fr.dyade.aaa.agent.conf.A3CMLConfig;
import fr.dyade.aaa.agent.conf.A3CMLServer;
import fr.dyade.aaa.agent.conf.A3CMLService;

/**
 *
 */
public class RAConfig {

  private static final String RA_XML = "META-INF/ra.xml";
  private static final String JORAM_CONFIG_JAR = "joram-config.jar";
  private static final String A3SERVERS_XML = "a3servers.xml";
  private static final String A3DEBUG_CFG = "a3debug.cfg";
  private static final String RA_PROPERTIES = "ra.properties";
  private static final String JORAMADMIN_CFG = "joram-admin.cfg";
  private static final String JORAMADMIN_XML = "joramAdmin.xml";
  private static final int BUFFER_SIZE = 2048;
  private static boolean debug = false;
  private static boolean verbose = false;
  private static String confDir = null;
  private static String tmpDir = null;


  private RAConfig() {
  }

  public static void main(String [] args) throws Exception {
    RAConfig raconfig = new RAConfig();

    debug = new Boolean(System.getProperty("debug","false")).booleanValue();

    String rarName = null;
    String jarName = null;
    String raProperties = null;
    String extractFile = null;
    String hostName = null;
    String port = null;
    String serverId = null;
    String path = null;
    String newFileName = null;
    String oldFileName = null;

    int command = -1;

    try {
      int i = 0;
      while (i < args.length) {
        if (args[i].equals("-rar")) {
          rarName = args[i+1];
          i = i + 2;
        } else if (args[i].equals("-jar")) {
          jarName = args[i+1];
          i = i + 2;
        } else if (args[i].equals("-c")) {
          command = 1;
          i++;
        } else if (args[i].equals("-u")) {
          command = 2;
          raProperties = args[i+1];
          i = i + 2;
        } else if (args[i].equals("-x")) {
          command = 3;
          extractFile = args[i+1];
          i = i + 2;
        } else if (args[i].equals("-uhp")) {
          command = 4;
          hostName = args[i+1];
          port = args[i+2];
          serverId = args[i+3];
          i = i + 3;
        } else if (args[i].equals("-ua3")) {
          command = 5;
          hostName = args[i+1];
          port = args[i+2];
          serverId = args[i+3];
          i = i + 3;
        } else if (args[i].equals("-uz")) {
          command = 6;
          path = args[i+1];
          newFileName = args[i+2];
          oldFileName = args[i+3];
          i = i + 3;
        } else if (args[i].equals("-conf")) {
          confDir = args[i+1];
          i = i + 2;
        } else if (args[i].equals("-v")) {
          verbose = true;
          i = i + 1;
        } else
          i++;
      }
    } catch (Exception e) {
      usage();
      e.printStackTrace();
      System.exit(1);
    }

    //  Get the temp directory
    tmpDir = System.getProperty("java.io.tmpdir") + "/";

    switch (command) {
    case 1: // createRaProperties
      if (rarName == null)
        usage();
      raconfig.createRaProperties(rarName);
      break;
    case 2: // updateRAR
      if (raProperties == null)
        usage();
      raconfig.updateRAR(raProperties);
      break;
    case 3: // extractFile
      if (extractFile != null) {
        if (rarName != null) {
          raconfig.extractFromRAR(rarName,extractFile);
        } else if (jarName != null) {
          raconfig.extractFromJAR(jarName,extractFile);
        } else
          usage();
      } else
        usage();
      break;
    case 4: // updateHostPort
      if (rarName == null
          || hostName == null
          || port == null
          || serverId == null)
        usage();
      raconfig.updateHostPort(rarName,hostName,port,new Short(serverId).shortValue());
      break;
    case 5: // updateA3Servers
      if (rarName == null
          || hostName == null
          || port == null
          || serverId == null)
        usage();
      raconfig.updateA3Servers(rarName,hostName,port,new Short(serverId).shortValue());
      break;
    case 6: // updateZIP
      if ( path != null
           && newFileName != null) {
        if (rarName != null)
          raconfig.updateZIP(rarName,path,newFileName,oldFileName);
        if (jarName != null)
          raconfig.updateZIP(jarName,path,newFileName,oldFileName);
      } else
        usage();
      break;
    default:
      usage();
      break;
    }
  }

  /**
   * Usage of RAConfig.
   */
  public static void usage() {
    StringBuffer buff = new StringBuffer();

    buff.append("\n\n");
    buff.append("\nConfigure your Joram RAR:");
    buff.append("\n  -resource adapter deployment descriptor (ra.xml)");
    buff.append("\n  -joram server configuration file (a3servers.xml)");
    buff.append("\n\n");
    buff.append("\nSimple: create ra.properties, modify ra.properties and update rar.");
    buff.append("\n  create ra.properties         : java RAconfig -rar rarName -c");
    buff.append("\n  update RAR                   : java RAconfig -u ra.properties");
    buff.append("\n");
    buff.append("\nChange host and port value in ra.xml and a3servers.xml.");
    buff.append("\n  update RAR (host/port)       : java RAconfig -rar rarName -uhp host port serverId");
    buff.append("\nChange host and port value only in a3servers.xml");
    buff.append("\n  update A3servers (host/port) : java RAconfig -rar rarName -ua3 host port serverId");
    buff.append("\n\n");
    buff.append("\nExpert: extract ra.xml and a3servers.xml (joram-config.jar), modify and update jar/rar.");
    buff.append("\n  extract file from RAR        : java RAconfig -rar rarName -x fileName");
    buff.append("\n  update RAR                   : java RAconfig -rar rarName -uz path newFileName oldFileName");
    buff.append("\n");
    buff.append("\n  extract file from JAR        : java RAconfig -jar jarName -x fileName");
    buff.append("\n  update JAR                   : java RAconfig -jar jarName -uz path newFileName oldFileName");
    buff.append("\n\n");
    buff.append("\nVerbose                        : -v");
    buff.append("\n\n");
    buff.append("\nexample :");
    buff.append("\n   java RAconfig -u ra.properties");
    buff.append("\n   java RAconfig -rar joram.rar -uhp localhost 16010 0");
    buff.append("\n   java RAconfig -rar joram.rar -uz META-INF/ra.xml ra.xml META-INF/ra.xml");
    buff.append("\n\n\n");

    System.out.println(buff.toString());
  }


  /**
   * create ra.properties
   * build from the ra.xml file from RAR.
   * @param rarName  String input RAR file name
   * @throws Exception to throw if an Exception occurs
   */
  private void createRaProperties(String rarName)
    throws Exception {

    if (debug)
      System.out.println("RAConfig.createRaProperties(" + rarName + ")");
    else if (verbose)
      System.out.println("create ra.properties " + rarName);

    File file = new File(rarName);
    if (file.exists()) {
      ZipFile zipFile = new ZipFile(file.getAbsolutePath());
      for (Enumeration zippedFiles = zipFile.entries(); zippedFiles.hasMoreElements(); ) {
        //Retrieve entry of existing files
        ZipEntry currEntry = (ZipEntry) zippedFiles.nextElement();
        if (debug)
          System.out.println("RAConfig.createRaProperties : currEntry = " + currEntry);
        if (currEntry.getName().equalsIgnoreCase(RA_XML)) {
          InputStream reader = zipFile.getInputStream(currEntry);
          StringBuffer buff = new StringBuffer();
          buff.append("RAR_NAME  ");
          buff.append(file.getAbsolutePath());
          buff.append("\n");
          //parse ra.xml file
          buff.append(parse(reader));
          // create ra.properties file
          createFile(RA_PROPERTIES,buff.toString());
          break;
        }
      }
      zipFile.close();
    }
  }


  /**
   * Extract the fileName from the RAR file.
   * @param rarName   RAR file name
   * @param fileName  file name
   * @throws Exception to throw if an Exception occurs
   */
  private void extractFromRAR(String rarName, String fileName)
    throws Exception {

    if (debug)
      System.out.println("RAConfig.extractFromRAR(" + rarName + "," + fileName + ")");
    else if (verbose)
      System.out.println("extract \"" + fileName + "\" from \"" + rarName + "\"");

    InputStream res = null;
    File file = new File(rarName);
    if (file.exists()) {
      ZipFile zipFile = new ZipFile(file.getAbsolutePath());
      for (Enumeration zippedFiles = zipFile.entries(); zippedFiles.hasMoreElements(); ) {
        //Retrieve entry of existing files
        ZipEntry currEntry = (ZipEntry) zippedFiles.nextElement();
        if (debug)
          System.out.println("RAConfig.extractFromRAR : currEntry = " + currEntry);
        if (currEntry.getName().equalsIgnoreCase(fileName)
            || currEntry.getName().equalsIgnoreCase("META-INF/"+fileName)) {
          // the fileName is found.
          res =  zipFile.getInputStream(currEntry);
          break;
        } else if (currEntry.getName().endsWith(".jar")) {
          // search fileName in jar file.
          InputStream reader = zipFile.getInputStream(currEntry);
          res = extractFromJAR(fileName,reader);
          if (res == null)
            continue;
          else {
            // the fileName found in jar file.
            reader.close();
            break;
          }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -