Esempio n. 1
0
  private static PackageBufINf upLoadConfigFile(
      ManagerConnection c, ByteBuffer buffer, byte packetId, String fileName, String content) {
    logger.info("Upload Daas Config file " + fileName + " ,content:" + content);
    String tempFileName = System.currentTimeMillis() + "_" + fileName;
    File tempFile = new File(SystemConfig.getHomePath(), "conf" + File.separator + tempFileName);
    BufferedOutputStream buff = null;
    boolean suc = false;
    try {
      byte[] fileData = content.getBytes("UTF-8");
      if (fileName.endsWith(".xml")) {
        checkXMLFile(fileName, fileData);
      }
      buff = new BufferedOutputStream(new FileOutputStream(tempFile));
      buff.write(fileData);
      buff.flush();

    } catch (Exception e) {
      logger.warn("write file err " + e);
      e.printStackTrace();
      return showInfo(c, buffer, packetId, "write file err " + e);

    } finally {
      if (buff != null) {
        try {
          buff.close();
          suc = true;
        } catch (IOException e) {
          logger.warn("save config file err " + e);
        }
      }
    }
    if (suc) {
      // if succcess
      File oldFile = new File(SystemConfig.getHomePath(), "conf" + File.separator + fileName);
      if (oldFile.exists()) {
        File backUP =
            new File(
                SystemConfig.getHomePath(),
                "conf" + File.separator + fileName + "_" + System.currentTimeMillis() + "_auto");
        if (!oldFile.renameTo(backUP)) {
          String msg = "rename old file failed";
          logger.warn(msg + " for upload file " + oldFile.getAbsolutePath());
          return showInfo(c, buffer, packetId, msg);
        }
      }
      File dest = new File(SystemConfig.getHomePath(), "conf" + File.separator + fileName);
      if (!tempFile.renameTo(dest)) {
        String msg = "rename file failed";
        logger.warn(msg + " for upload file " + tempFile.getAbsolutePath());
        return showInfo(c, buffer, packetId, msg);
      }
      return showInfo(c, buffer, packetId, "SUCCESS SAVED FILE:" + fileName);
    } else {
      return showInfo(c, buffer, packetId, "UPLOAD ERROR OCCURD:" + fileName);
    }
  }
Esempio n. 2
0
  private static PackageBufINf listConfigFiles(
      ManagerConnection c, ByteBuffer buffer, byte packetId) {
    PackageBufINf bufINf = new PackageBufINf();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    try {
      int i = 1;
      File[] file = new File(SystemConfig.getHomePath(), "conf").listFiles();
      for (File f : file) {
        if (f.isFile()) {
          RowDataPacket row = new RowDataPacket(FIELD_COUNT);
          row.add(
              StringUtil.encode(
                  (i++) + " : " + f.getName() + "  time:" + df.format(new Date(f.lastModified())),
                  c.getCharset()));
          row.packetId = ++packetId;
          buffer = row.write(buffer, c, true);
        }
      }

      bufINf.buffer = buffer;
      bufINf.packetId = packetId;
      return bufINf;

    } catch (Exception e) {
      e.printStackTrace();
      RowDataPacket row = new RowDataPacket(FIELD_COUNT);
      row.add(StringUtil.encode(e.toString(), c.getCharset()));
      row.packetId = ++packetId;
      buffer = row.write(buffer, c, true);
      bufINf.buffer = buffer;
    }
    bufINf.packetId = packetId;
    return bufINf;
  }
Esempio n. 3
0
  private static PackageBufINf showConfigFile(
      ManagerConnection c, ByteBuffer buffer, byte packetId, String fileName) {
    File file = new File(SystemConfig.getHomePath(), "conf" + File.separator + fileName);
    BufferedReader br = null;
    PackageBufINf bufINf = new PackageBufINf();
    try {
      br = new BufferedReader(new FileReader(file));
      String line = null;
      while ((line = br.readLine()) != null) {
        if (line.isEmpty()) {
          continue;
        }
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(line, c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
      }
      bufINf.buffer = buffer;
      bufINf.packetId = packetId;
      return bufINf;

    } catch (Exception e) {
      e.printStackTrace();
      RowDataPacket row = new RowDataPacket(FIELD_COUNT);
      row.add(StringUtil.encode(e.toString(), c.getCharset()));
      row.packetId = ++packetId;
      buffer = row.write(buffer, c, true);
      bufINf.buffer = buffer;
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    bufINf.packetId = packetId;
    return bufINf;
  }
Esempio n. 4
0
  private static void checkXMLFile(String xmlFileName, byte[] data)
      throws ParserConfigurationException, SAXException, IOException {
    InputStream dtdStream = new ByteArrayInputStream(new byte[0]);
    File confDir = new File(SystemConfig.getHomePath(), "conf");
    if (xmlFileName.equals("schema.xml")) {
      dtdStream = MycatServer.class.getResourceAsStream("/schema.dtd");
      if (dtdStream == null) {
        dtdStream = new ByteArrayInputStream(readFileByBytes(new File(confDir, "schema.dtd")));
      }

    } else if (xmlFileName.equals("server.xml")) {
      dtdStream = MycatServer.class.getResourceAsStream("/server.dtd");
      if (dtdStream == null) {
        dtdStream = new ByteArrayInputStream(readFileByBytes(new File(confDir, "server.dtd")));
      }
    } else if (xmlFileName.equals("rule.xml")) {
      dtdStream = MycatServer.class.getResourceAsStream("/rule.dtd");
      if (dtdStream == null) {
        dtdStream = new ByteArrayInputStream(readFileByBytes(new File(confDir, "rule.dtd")));
      }
    }
    ConfigUtil.getDocument(dtdStream, new ByteArrayInputStream(data));
  }