Example #1
0
 static void genclass(DelegatorGenerator dg, Class intfcl, String fqcn, File srcroot)
     throws IOException {
   File genDir = new File(srcroot, dirForFqcn(fqcn));
   if (!genDir.exists()) {
     System.err.println(
         JdbcProxyGenerator.class.getName()
             + " -- creating directory: "
             + genDir.getAbsolutePath());
     genDir.mkdirs();
   }
   String fileName = CodegenUtils.fqcnLastElement(fqcn) + ".java";
   Writer w = null;
   try {
     w = new BufferedWriter(new FileWriter(new File(genDir, fileName)));
     dg.writeDelegator(intfcl, fqcn, w);
     w.flush();
     System.err.println("Generated " + fileName);
   } finally {
     try {
       if (w != null) w.close();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
  /* use badsource and badsink */
  public void bad(HttpServletRequest request, HttpServletResponse response) throws Throwable {
    String data = bad_source(request, response);

    /* POTENTIAL FLAW: unvalidated or sandboxed value */
    File fIn = new File(data);
    if (fIn.exists() && fIn.isFile()) {
      IO.writeLine(new BufferedReader(new FileReader(fIn)).readLine());
    }
  }
Example #3
0
  public static void addToResultsFile(
      String executingDatabaseName,
      long runTime,
      long rowsLoaded,
      long rowsLoadedPerSecond,
      File resultsFile) {

    boolean resultsFileCreated = false;

    try {
      if (!resultsFile.exists()) {

        resultsFileCreated = resultsFile.createNewFile();
      }

      OutputStream resultsFileStream = new FileOutputStream(resultsFile, true);

      if (resultsFileCreated) {
        // Write header lines.

        String header =
            "timeOfTest, runTime, databaseBeingTested, rowsLoaded, rowsLoadedPerSecond, hotStart, config, replicas"
                + "\n";

        resultsFileStream.write(header.getBytes());
      }

      String results =
          startDate.getTime()
              + ", "
              + runTime
              + ", "
              + executingDatabaseName
              + ", "
              + rowsLoaded
              + ", "
              + rowsLoadedPerSecond
              + ", "
              + hotStart
              + ", "
              + configInfo
              + ", "
              + numberOfReplicas
              + "\n";
      resultsFileStream.write(results.getBytes());

      resultsFileStream.flush();

      resultsFileStream.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #4
0
  // Logs a new entry in the library RSS file
  public static synchronized void addRSSEntry(
      String title, String link, String description, File rssFile) {
    // File rssFile=new File("nofile.xml");

    try {
      System.out.println("Looking for RSS file: " + rssFile.getCanonicalPath());
      if (rssFile.exists()) {

        SAXReader reader = new SAXReader();
        Document document = reader.read(rssFile);
        Element root = document.getRootElement();
        Element channel = root.element("channel");
        List items = channel.elements("item");
        int numItems = items.size();
        items = null;
        if (numItems > 9) {
          Element removeThisItem = channel.element("item");
          channel.remove(removeThisItem);
        }

        Element newItem = channel.addElement("item");
        Element newTitle = newItem.addElement("title");
        Element newLink = newItem.addElement("link");
        Element newDescription = newItem.addElement("description");
        newTitle.setText(title);
        newDescription.setText(description);
        newLink.setText(link);

        Element pubDate = channel.element("pubDate");
        pubDate.setText((new java.util.Date()).toString());

        // now save changes
        FileWriter mywriter = new FileWriter(rssFile);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));
        XMLWriter writer = new XMLWriter(mywriter, format);
        writer.write(document);
        writer.close();
      }
    } catch (IOException ioe) {
      System.out.println("ERROR: Could not find the RSS file.");
      ioe.printStackTrace();
    } catch (DocumentException de) {
      System.out.println("ERROR: Could not read the RSS file.");
      de.printStackTrace();
    } catch (Exception e) {
      System.out.println("Unknown exception trying to add an entry to the RSS file.");
      e.printStackTrace();
    }
  }
Example #5
0
 /**
  * If 'file' exists, attempt to delete it. Logs, but doesn't throw upon failure.
  *
  * @param file
  * @param userId only for logging.
  */
 private void deleteFileIfNeeded(File file, int userId) {
   if (file.exists()) {
     boolean ok = file.delete();
     if (!ok) {
       // This is a problem because it may delay informing a client to leave
       // activation.
       LogMessageGen lmg = new LogMessageGen();
       lmg.setSubject("Unable to delete file");
       lmg.param(LoggingConsts.USER_ID, userId);
       lmg.param(LoggingConsts.FILENAME, file.getAbsolutePath());
       m_logCategory.error(lmg.toString());
     }
   }
 }
Example #6
0
  public static File findResourceOnFileSystem(String resourceName) {
    File resourceFile = null;

    URL resourceURL = ServletUtilities.class.getClassLoader().getResource(resourceName);
    if (resourceURL != null) {
      String resourcePath = resourceURL.getPath();
      if (resourcePath != null) {
        File tmp = new File(resourcePath);
        if (tmp.exists()) {
          resourceFile = tmp;
        }
      }
    }
    return resourceFile;
  }
Example #7
0
 private static void createContactDir(String strContact) {
   File dirCurrent = new File(strDestVMDir + strContact);
   if (!dirCurrent.exists()) {
     try {
       boolean success = (dirCurrent).mkdir();
       if (!success) {
         throw new Exception("Well that wasn't planned for...");
       }
     } catch (Exception e) {
       System.err.println(e.getLocalizedMessage());
       System.err.println();
       System.err.println(e.getStackTrace());
       System.exit(0);
     }
   }
 }
Example #8
0
  /** ���ļ���ͬһ��Ŀ¼�¸���һ�� fileName ԭ4�ļ��� path �ļ���·�� cFileName ���ƺ���ļ��� */
  public static void copy(String fileName, String cFileName, String path) {
    // ԭ�ļ�
    File file = new File(path + fileName);
    if (file.isFile()) {
      InputStream is = null;
      OutputStream out = null;
      try {
        // ԭ�ļ���һ��������
        is = new FileInputStream(file);
        // �����ļ���·��
        File toFile = new File(path + cFileName);
        if (toFile.exists()) {
          toFile.delete();
        }
        // ���������ļ�
        toFile.createNewFile();
        // �����ļ��������

        out = new FileOutputStream(toFile);
        // ����
        byte[] b = new byte[256];
        for (int length = 0; (length = is.read(b, 0, 256)) > 0; ) {
          out.write(b, 0, length);
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        try {
          if (is != null) {
            is.close();
          }
        } catch (IOException ex1) {
        }
        try {
          if (out != null) {
            out.close();
          }
        } catch (IOException ex2) {
        }
      }
      // file.delete();
    }
    // return abstractPath;
  }
Example #9
0
  /**
   * �����Ƶ���ǰ���µ��ļ���,���������·��
   *
   * @param fileName String
   */
  public static String move(String fileName, String loadPath) {
    String path = getPath(loadPath);

    // �������µ��ļ�·���ַ�
    String abstractPath = getAbstractPath(path);

    File file = new File(path + fileName);
    if (file.isFile()) {
      InputStream is = null;
      OutputStream out = null;
      try {
        is = new FileInputStream(file);
        path = path + abstractPath + fileName;
        File toFile = new File(path);
        if (toFile.exists()) {
          toFile.delete();
        }
        toFile.createNewFile();
        out = new FileOutputStream(toFile);
        byte[] b = new byte[256];
        for (int length = 0; (length = is.read(b, 0, 256)) > 0; ) {
          out.write(b, 0, length);
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        try {
          if (is != null) {
            is.close();
          }
        } catch (IOException ex1) {
        }
        try {
          if (out != null) {
            out.close();
          }
        } catch (IOException ex2) {
        }
      }
      file.delete();
    }
    return abstractPath;
  }
  /**
   * Private method to delete a visualization script file of a quiz taken by a student.
   *
   * @param uniqueId a unique number of that indicates the instance of the quiz taking in the
   *     database
   * @param studentName student's user name
   * @param testName quiz name
   */
  private void deleteVisualization(String uniqueId, String studentName, String testName) {
    try {
      Class.forName(GaigsServer.DBDRIVER);
      db =
          DriverManager.getConnection(GaigsServer.DBURL, GaigsServer.DBLOGIN, GaigsServer.DBPASSWD);

      Statement stmt = db.createStatement();

      // get quiz visulization type
      ResultSet rs =
          stmt.executeQuery("select visual_type from test where name = '" + testName + "'");
      // construct the quiz vizualisation file name
      String visFileName =
          new String("./StudentQuizzes/" + studentName + "/" + testName + uniqueId);
      if (rs.next())
        if (rs.getString(1).trim().equalsIgnoreCase("gaigs")) visFileName = visFileName + ".sho";
        else if (rs.getString(1).trim().equalsIgnoreCase("samba"))
          visFileName = visFileName + ".sam";
        else if (rs.getString(1).trim().equalsIgnoreCase("animal"))
          visFileName = visFileName + ".ani";
        else {
          System.err.println("visualization type unknown for quiz: " + rs.getString(3) + "\n");
        }
      else {
        System.err.println("Visulization type not found for quiz: " + rs.getString(3) + "\n");
      }

      // delete the file
      File visFile = new File(visFileName);
      if (debug) System.out.println("deleting " + visFileName);
      if (visFile.exists())
        if (!visFile.delete()) System.err.println("can't delete the visualization file");

      rs.close();
      stmt.close();
      db.close();
    } catch (Exception e) {
      System.err.println("Execption thrown from deleteVisualisation: " + e.getMessage());
    }
  }
  public static void main(String[] args) throws Exception {

    File f = new File(inputFile);
    if (f.exists()) {
      Scanner input = new Scanner(f);
      String credentials = input.nextLine();
      input.close();
      String uname = credentials.split(",")[0].split("=")[1];
      String pword = credentials.split(",")[1].split("=")[1];

      con = connectToDb(uname, pword);
      if (con != null) {
        System.out.println("Connected to Database:" + con);
        loadData(uname, pword);
        task1();
        task2();
        task3();
        task4();
        con.close();
      } else System.out.println("Connection Error");
    } else System.out.println("Input file system.in not found");
  }
Example #12
0
  public ClientSession getClientSession(String activationToken) {
    if (!StringUtils.hasValue(activationToken)) {
      m_logCategory.warn("activationToken is required");
      return null;
    }

    File file = getSessionFile(activationToken);
    if (!file.exists()) {
      // Can't assume it's an authentication problem -- session file is removed
      // during active->recovery transition.
      // If this occurs, the client gets a 401 and will call into 'transition' or 'checkin'.
      LogMessageGen lmg = new LogMessageGen();
      lmg.setSubject("Session file not found");
      lmg.param(LoggingConsts.FILENAME, file.getAbsolutePath());
      m_logCategory.info(lmg.toString());
      return null;
    }
    try {
      ClientSession clientSession = null;
      clientSession = new ClientSession();
      clientSession.read(file);
      clientSession.setActivationToken(activationToken);
      return clientSession;
    } catch (IOException ioe) {
      // Possible if the state file was removed after the file.exists() above,
      // or some other problem.
      // info, not warning because a client could ask for this file right when
      // the session is being removed.  The client will then do a 'checkin',
      // same as described above.
      LogMessageGen lmg = new LogMessageGen();
      lmg.setSubject("Unable to read session file");
      lmg.param(LoggingConsts.FILENAME, file.getAbsolutePath());
      m_logCategory.info(lmg.toString(), ioe);
    }
    return null;
  }
  /**
   * Deletes a course from the database. Also deletes all the quiz visualization files from the
   * students' folder who is registered in the course for all quizzes related to the course.
   * Caution: vizualisation file will be deleted eventhough it also relates to anther course if the
   * student is also registered to that course. (FIX ME!) Throws InvalidDBRequestException if the
   * course is not in the database, error occured during deletion, or other exception occured.
   *
   * @param courseNum course number
   * @param instructor instructor's user name who owns the course
   * @throws InvalidDBRequestException
   */
  public void deleteCourse(String courseNum, String instructor) throws InvalidDBRequestException {
    String courseId = new String(courseNum + instructor);

    try {
      Class.forName(GaigsServer.DBDRIVER);
      db =
          DriverManager.getConnection(GaigsServer.DBURL, GaigsServer.DBLOGIN, GaigsServer.DBPASSWD);

      Statement stmt = db.createStatement();
      ResultSet rs;

      int count = 0;

      // check if course in the database
      rs = stmt.executeQuery("select course_id from course where course_id = '" + courseId + "'");
      if (!rs.next()) throw new InvalidDBRequestException("Course is not in the database");
      else {
        // delete course from course table
        count = stmt.executeUpdate("delete from course where course_id = '" + courseId + "'");
        if (count != 1) throw new InvalidDBRequestException("Something happen during deletion!");
        else {
          // delete quiz visualization files
          rs =
              stmt.executeQuery(
                  "select distinct unique_id, s.user_login, s.test_name from scores s, courseRoster r, courseTest t "
                      + "where s.test_name = t.test_name "
                      + "and r.user_login = s.user_login "
                      + "and r.course_id = t.course_id "
                      + "and t.course_id = '"
                      + courseId
                      + "'");
          while (rs.next()) {
            deleteVisualization(
                rs.getString(1).trim(), rs.getString(2).trim(), rs.getString(3).trim());
            count =
                stmt.executeUpdate(
                    "delete from scores where unique_id = " + rs.getString(1).trim());
          }

          // delete course from other tables
          count =
              stmt.executeUpdate("delete from courseRoster where course_id = '" + courseId + "'");
          count = stmt.executeUpdate("delete from courseTest where course_id = '" + courseId + "'");

          // delete menu file
          File menuFile = new File("./html_root/cat/" + courseId + ".list");
          if (menuFile.exists())
            if (!menuFile.delete()) System.err.println("Failed to delete the menu");
        }
      }

      rs.close();
      stmt.close();
      db.close();
    } catch (SQLException e) {
      System.err.println("Invalid SQL in addCourse: " + e.getMessage());
      throw new InvalidDBRequestException("??? ");
    } catch (ClassNotFoundException e) {
      System.err.println("Driver Not Loaded");
      throw new InvalidDBRequestException("Internal Server Error");
    }
  }
Example #14
0
  /**
   * Main function of the DOTS. Read the config file,create dotslogging,start performance client,
   * check db connection,create keyboard thread. Create new testcase according the cpu_usage and
   * connections, Write test summary and exception
   */
  public static void main(String[] args) {

    int sleepInterval = 1;
    int waitInterval = 0;
    int averageUsage = 0;
    boolean reached = false;
    Calendar currentTime;

    /* Load DotsConfig value */
    loadConfig(args);
    /* Create the instance of DotsLogging */
    DotsLogging logging = new DotsLogging(TESTCASENAME);

    DotsLogging.logMessage("Database Opensource Test Suite V1.0");
    DotsLogging.logMessage("Start to run JDBC API Test Case - " + TESTCASENAME);
    DotsLogging.logSummary("Start to run JDBC API Test Case - " + TESTCASENAME);
    if (!DotsConfig.RUN_AUTO) {
      System.out.println("\nDatabase Opensource Test Suite V1.0");
      System.out.println("\nStart to run JDBC API test case - " + TESTCASENAME);
    }

    DotsLogging.logMessage("Initialization started");
    if (!DotsConfig.RUN_AUTO) {
      System.out.println("\nTo stop running the test case, type STOP then press Enter\n");
    }
    /* Start performance monitor thread */
    startPerfCtl();

    /* Check database connection */
    checkDBConn();

    DotsLogging.logMessage("Testing Database Connections ...OK");

    /* Start summary writer thread */
    DotsSummary summary = new DotsSummary();
    summary.start();

    DotsLogging.logMessage("Starting Summary Writer ... OK ");

    /* Set begin and end time */
    Calendar endTime = Calendar.getInstance();
    DotsSummary.startTime = System.currentTimeMillis();
    endTime.add(Calendar.SECOND, DotsConfig.DURATION * 60);

    /* Start keyboard thread */
    if (!DotsConfig.RUN_AUTO) {
      startKeyboard();
      DotsLogging.logMessage("Starting Keyboard Thread ... OK");
    }

    boolean firstRun = true; // used for LOB manipulation
    String lobFileName[] = new String[10]; // used for LOB manipulation

    averageUsage = DotsConfig.CPU_USAGE;
    int targetCpu = DotsConfig.CPU_TARGET;
    try {
      while (true) {
        currentTime = Calendar.getInstance();
        if (currentTime.after(endTime)) break;
        if (DotsConfig.TERMINATION) break;
        /* Check if need create a new database access thread*/
        if ((DotsConfig.AUTO_MODE && (averageUsage < targetCpu))
            || (!DotsConfig.AUTO_MODE
                && (DotsConfig.THRDGRP.activeCount() < DotsConfig.CONNECTIONS))) {
          Connection conn =
              createConnection(
                  DotsConfig.DRIVER_CLASS_NAME,
                  DotsConfig.URL,
                  DotsConfig.DB_UID,
                  DotsConfig.DB_PASSWD);
          if (conn != null) {
            /* 5 second is a unit*/
            sleepInterval = DotsConfig.INTERVAL * 12;
            /* Create DB access thread */
            switch (TESTCASENAME.charAt(4)) {
              case '1':
                if (TESTCASENAME.equals("BTCJ1")) {
                  BTCJ1 btcj1 = new BTCJ1(conn);
                  Thread thrdb1 = new Thread(DotsConfig.THRDGRP, btcj1);
                  thrdb1.start();
                } else {
                  ATCJ1 atcj1 = new ATCJ1(conn);
                  Thread thrda1 = new Thread(DotsConfig.THRDGRP, atcj1);
                  thrda1.start();
                }
                break;
              case '2':
                if (TESTCASENAME.equals("BTCJ2")) {
                  BTCJ2 btcj2 = new BTCJ2(conn);
                  Thread thrdb2 = new Thread(DotsConfig.THRDGRP, btcj2);
                  thrdb2.start();
                } else {
                  ATCJ2 atcj2 = new ATCJ2(conn);
                  Thread thrda2 = new Thread(DotsConfig.THRDGRP, atcj2);
                  thrda2.start();
                }
                break;
              case '3':
                BTCJ3 btcj3 = new BTCJ3(conn);
                Thread thrdb3 = new Thread(DotsConfig.THRDGRP, btcj3);
                thrdb3.start();

                break;
              case '4':
                BTCJ4 btcj4 = new BTCJ4(conn);
                Thread thrdb4 = new Thread(DotsConfig.THRDGRP, btcj4);
                thrdb4.start();

                break;
              case '5':
                BTCJ5 btcj5 = new BTCJ5(conn);
                Thread thrdb5 = new Thread(DotsConfig.THRDGRP, btcj5);
                thrdb5.start();
                break;
              case '6':
                if (firstRun) {
                  for (int i = 0; i < 10; i++) {
                    lobFileName[i] = DotsGenerator.mdClob(DotsGenerator.mdInt(10, 100));
                  }
                  firstRun = false;
                }
                BTCJ6 btcj6 = new BTCJ6(conn, lobFileName);
                Thread thrdb6 = new Thread(DotsConfig.THRDGRP, btcj6);
                thrdb6.start();
                break;
              case '7':
                if (firstRun) {
                  for (int i = 0; i < 10; i++) {
                    lobFileName[i] = DotsGenerator.mdBlob(DotsGenerator.mdInt(10, 100));
                  }
                  firstRun = false;
                }
                BTCJ7 btcj7 = new BTCJ7(conn, lobFileName);
                Thread thrdb7 = new Thread(DotsConfig.THRDGRP, btcj7);
                thrdb7.start();
                break;
              case '8':
                BTCJ8 btcj8 = new BTCJ8(conn);
                Thread thrdb8 = new Thread(DotsConfig.THRDGRP, btcj8);
                thrdb8.start();
                break;
            }
          } else {
            /* If createConnection failed,sleepInterval will double,
            but the maximum of sleepInterval is 1 hour */
            sleepInterval = sleepInterval * 2;
            if (sleepInterval > 720) sleepInterval = 720;
          }
          try {
            averageUsage = 0;
            /* Wait 5*sleepInterval seconds,check TERMINATION per 5 second */
            for (int j = 0; j < sleepInterval; j++) {
              averageUsage += DotsConfig.CPU_USAGE;
              Thread.sleep(5000);
              if ((j % 12) == 0)
                DotsLogging.logMessage(
                    "Active Threads = "
                        + DotsConfig.THRDGRP.activeCount()
                        + " Average CPU Usage = "
                        + DotsConfig.CPU_USAGE
                        + "%");
              if (DotsConfig.TERMINATION) break;
              currentTime = Calendar.getInstance();
              if (currentTime.after(endTime)) {
                DotsConfig.TERMINATION = true;
                break;
              }
            }
            /* Calculate the average usage of this sleepInterval */
            averageUsage = averageUsage / sleepInterval;
            DotsLogging.logMessage(
                sleepInterval / 12 + " Minutes Average CPU Usage = " + averageUsage + "%");
          } catch (Exception e) {
            DotsLogging.logException("Dots.main: " + e);
          }

        } else {
          /* When CPU_TARGET is reached,DOTS will not create new thread until
          average usage is less than CPU_TARGET-10 */
          if (DotsConfig.AUTO_MODE && !reached) {
            reached = true;
            targetCpu = DotsConfig.CPU_TARGET - 10;
            DotsLogging.logMessage("CPU Target " + DotsConfig.CPU_TARGET + "% is achieved now.");
          }
          /* Wait 5 minutes then check create connection condition */
          try {
            averageUsage = 0;
            for (int i = 0; i < 60; i++) {
              averageUsage += DotsConfig.CPU_USAGE;
              Thread.sleep(5000);
              if ((i % 12) == 0)
                DotsLogging.logMessage(
                    "Active Threads = "
                        + DotsConfig.THRDGRP.activeCount()
                        + " Average CPU Usage = "
                        + DotsConfig.CPU_USAGE
                        + "%");
              if (DotsConfig.TERMINATION) break;
              currentTime = Calendar.getInstance();
              if (currentTime.after(endTime)) {
                DotsConfig.TERMINATION = true;
                break;
              }
            }
            averageUsage = averageUsage / 60;
            DotsLogging.logMessage("5 Minutes Average CPU Usage = " + averageUsage + "%");
          } catch (Exception e) {
            DotsLogging.logException("Dots.main: " + e);
          }
        }
      }
    } catch (Throwable t) {
      DotsLogging.logException("Dots.main: " + t);
      System.out.println("Dots.main:" + t);
    }

    /* DURATION is reached or TERMINATION is set by other threads */
    DotsConfig.TERMINATION = true;
    DotsLogging.logMessage("Dots is Terminating ...");
    /* Clear temporary file used by testcase 6&7 */
    if ((TESTCASENAME.charAt(4) == '6') || (TESTCASENAME.charAt(4) == '7')) {
      DotsLogging.logMessage("Cleaning temporary file ...");

      if (!DotsConfig.RUN_AUTO) {
        System.out.println("Cleaning temporary file ...");
      }
      try {
        for (int j = 0; j < 10; j++) {
          File tmpFile = new File(lobFileName[j]);
          if (tmpFile.exists()) tmpFile.delete();
        }
      } catch (Exception e) {
        DotsLogging.logException("Clean temporary file error.");
      }
    }
    /* Write summary file and wait DB thread to exit */
    try {
      if (!DotsConfig.RUN_AUTO) {
        System.out.println("\nWriting summary file ...");
      }
      summary.writeSummaryFile();
      summary.canExit = true;
      DotsPerf.canExit = true;
      DotsLogging.logSummary("Waiting for the active threads to exit ......");
      if (!DotsConfig.RUN_AUTO) {
        System.out.println("\nWaiting for the active threads to exit ......");
      }
      while (DotsConfig.THRDGRP.activeCount() > 0) {
        waitInterval++;
        Thread.sleep(1000);
        if (!DotsConfig.RUN_AUTO) {
          System.out.println("Current active thread number is " + DotsConfig.THRDGRP.activeCount());
        }
        if (waitInterval > 60) break; // Max wait time is 1 minute
      }
    } catch (InterruptedException e) {
      DotsLogging.logException("Dots.main:" + e);
    }
    DotsLogging.logSummary("All Database Access Threads exit.");
    DotsLogging.close();
    if (!DotsConfig.RUN_AUTO) {
      System.out.println("DOTS closed successfully.");
    }
    System.exit(0);
  }
Example #15
0
  public static void main(String[] argv) throws Exception {
    String[] sArray = new String[8];
    long time = System.currentTimeMillis();
    try {
      FileInputStream fstream = new FileInputStream("system.in");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      int i = 0;
      while ((sArray[i] = br.readLine()) != null) {
        sArray[i] = sArray[i].split("=")[1].trim();
        System.out.println(sArray[i]);
        i++;
      }
      in.close();
    } catch (Exception e) {

    }
    System.out.println("-------- Oracle JDBC Connection Testing ------");

    try {

      Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {

      System.out.println("Where is your Oracle JDBC Driver?");
      e.printStackTrace();
      return;
    }

    System.out.println("Oracle JDBC Driver Registered!");

    Connection connection = null;

    try {

      connection =
          DriverManager.getConnection(
              "jdbc:oracle:thin:@//oracle1.cise.ufl.edu:1521/orcl", sArray[0], sArray[1]);

    } catch (SQLException e) {

      System.out.println("Connection Failed! Check output console");
      e.printStackTrace();
      return;
    }
    ResultSet rset = null;
    if (connection != null) {
      System.out.println(" -|||- ");

      Process p =
          Runtime.getRuntime()
              .exec("sqlplus " + sArray[0] + "@orcl/" + sArray[1] + " @adwords.sql");
      p.waitFor();

      // CallableStatement cstmt;
      System.out.println(" ||| ");

      System.out.println(
          "Time taken in Milliseconds (establishing connection): "
              + (System.currentTimeMillis() - time));
      time = System.currentTimeMillis();

      Process proc3 =
          Runtime.getRuntime()
              .exec(
                  "sqlldr "
                      + sArray[0]
                      + "/"
                      + sArray[1]
                      + "@orcl DATA=Keywords.dat CONTROL=Keywords.ctl LOG=Keywords.log");
      proc3.waitFor();

      Process proc1 =
          Runtime.getRuntime()
              .exec(
                  "sqlldr "
                      + sArray[0]
                      + "/"
                      + sArray[1]
                      + "@orcl DATA=Advertisers.dat CONTROL=Advertisers.ctl LOG=Advertiser.log");
      proc1.waitFor();

      Process proc2 =
          Runtime.getRuntime()
              .exec(
                  "sqlldr "
                      + sArray[0]
                      + "/"
                      + sArray[1]
                      + "@orcl DATA=Queries.dat CONTROL=Queries.ctl LOG=Queries.log");
      proc2.waitFor();
      System.out.println(" ||| ");

      System.out.println(
          "Time taken in Milliseconds (loading dat): " + (System.currentTimeMillis() - time));
      time = System.currentTimeMillis();

      CallableStatement callableStatement = null;
      String storeProc = "{call sq(?,?,?,?,?,?)}";

      try {
        callableStatement = connection.prepareCall(storeProc);
        callableStatement.setInt(1, Integer.parseInt(sArray[2]));
        callableStatement.setInt(2, Integer.parseInt(sArray[3]));
        callableStatement.setInt(3, Integer.parseInt(sArray[4]));
        callableStatement.setInt(4, Integer.parseInt(sArray[5]));
        callableStatement.setInt(5, Integer.parseInt(sArray[6]));
        callableStatement.setInt(6, Integer.parseInt(sArray[7]));
        callableStatement.executeUpdate();
      } catch (SQLException e) {
        System.out.println(e.getMessage());
      }

      /*callableStatement = connection.prepareCall("{call sq( " +
      Integer.parseInt(sArray[2]) + ", " +
      Integer.parseInt(sArray[3]) + ", " +
      Integer.parseInt(sArray[4]) + ", " +
      Integer.parseInt(sArray[5]) + ", " +
      Integer.parseInt(sArray[6]) + ", " +
      Integer.parseInt(sArray[7]) + " ) }");*/
      System.out.println(" ||| ");

      System.out.println(
          "Time taken in Milliseconds (sql procedure): " + (System.currentTimeMillis() - time));
      time = System.currentTimeMillis();

      FileWriter fw = null;
      File file = null;
      PrintWriter pw = null;
      Statement resStmt = null;
      ResultSet resRs = null;
      int qid = 0;
      int rank;

      for (int i = 0; i < 6; i++) {

        file = new File("system.out." + (i + 1));
        try {
          if (!file.exists()) file.createNewFile();
          else file.delete();

          fw = new FileWriter(file.getPath(), true);
        } catch (Exception e) {
          System.out.println(e.getMessage());
        }
        pw = new PrintWriter(fw);
        resStmt = connection.createStatement();
        resRs =
            resStmt.executeQuery(
                "SELECT * FROM OUTPUT" + (i + 1) + " order by qid asc, adrank asc");
        while (resRs.next()) {
          qid = resRs.getInt("QID");
          rank = (int) resRs.getFloat("ADRANK");
          int advertiserId = resRs.getInt("ADVERTISERID");
          float balance = resRs.getFloat("BALANCE");
          float budget = resRs.getFloat("BUDGET");
          StringBuffer resStr = new StringBuffer();
          resStr.append(qid + "," + rank + "," + advertiserId + "," + balance + "," + budget);
          // System.out.println(resStr.toString());
          pw.println(resStr.toString());
        }

        pw.close();
      }

    } else {
      System.out.println("Failed to make connection!");
    }

    System.out.println(
        "Time taken in Milliseconds (file write): " + (System.currentTimeMillis() - time));
  }
  /**
   * Method to move files from HDFS to local filesystem
   *
   * <p>localPath: Path on the machines filesystem fs:FileSystem object from HDFS pathList:List of
   * paths for files that might need to be backed up size:max size in bytes to be backed up
   *
   * <p>ReturnsDate of the last files backed up if reached size limit, else, zero
   */
  public long backupFiles(
      String localPath, String preservePath, FileSystem fs, ArrayList<Path> pathList, long size) {
    Path fsPath;
    long tmpSize = 0;
    long tmpDate = 0;

    // Start iterating over all paths
    for (Path hdfsPath : pathList) {
      try {
        long nFileSize = fs.getContentSummary(hdfsPath).getLength();
        tmpSize = tmpSize + nFileSize;

        if ((tmpSize <= size) || (size == 0)) {
          FileStatus stat = fs.getFileStatus(hdfsPath);

          System.err.println(
              "File "
                  + hdfsPath.toUri().getPath()
                  + " "
                  + nFileSize
                  + " bytes, "
                  + "perms: "
                  + stat.getOwner()
                  + "/"
                  + stat.getGroup()
                  + ", "
                  + stat.getPermission().toString());

          tmpDate = stat.getModificationTime() / 1000;

          String sFsPath = localPath + hdfsPath.toUri().getPath();
          fsPath = new Path(sFsPath);

          File f = new File(sFsPath);

          // COMMENTED OUT: until a few backup cycles run
          // and the mtime gets in fact set on all copied
          // files.
          //
          // ignore it if the file exists and has the same mtime
          // if (f.exists() && f.isFile() && f.lastModified() == stat.getModificationTime())
          // {
          // System.out.println("no need to backup " + f.toString() + ", mtime matches hdfs");
          // continue;
          // }

          if (false == m_bDryRun) {
            // check if we need to back up the local file
            // (not directory), if it already exists.
            if (f.exists() && f.isFile()) {
              // ignore files with substrings in the
              // no-preserve file
              if (true == doPreserveFile(sFsPath)) {
                // move it to the backup path
                String sNewPath = preservePath + hdfsPath.toUri().getPath();
                File newFile = new File(sNewPath);

                // create directory structure for new file?
                if (false == newFile.getParentFile().exists()) {
                  if (false == newFile.getParentFile().mkdirs()) {
                    System.err.println("Failed to mkdirs " + newFile.getParentFile().toString());
                    System.exit(1);
                  }
                }

                // rename existing file to new location
                if (false == f.renameTo(newFile)) {
                  System.err.println(
                      "Failed to renameTo " + f.toString() + " to " + newFile.toString());
                  System.exit(1);
                }

                System.out.println("preserved " + f.toString() + " into " + newFile.toString());
              } else {
                System.out.println("skipped preservation of " + f.toString());
              }
            }

            // copy from hdfs to local filesystem
            fs.copyToLocalFile(hdfsPath, fsPath);

            // set the mtime to match hdfs file
            f.setLastModified(stat.getModificationTime());

            // compare checksums on both files
            compareChecksums(fs, hdfsPath, sFsPath);
          }

          // don't print the progress after every file -- go
          // by at least 1% increments
          long nPercentDone = (long) (100 * tmpSize / m_nTotalBytes);
          if (nPercentDone > m_nLastPercentBytesDone) {
            System.out.println(
                "progress: copied "
                    + prettyPrintBytes(tmpSize)
                    + ", "
                    + nPercentDone
                    + "% done"
                    + ", tstamp="
                    + tmpDate);

            m_nLastPercentBytesDone = nPercentDone;
          }

          if (m_nSleepSeconds > 0) {
            try {
              Thread.sleep(1000 * m_nSleepSeconds);
            } catch (Exception e2) {
              // ignore
            }
          }
        } else {
          return tmpDate;
        }
      } catch (IOException e) {
        System.err.println("FATAL ERROR: Something wrong with the file");
        System.err.println(e);
        System.out.println(tmpDate);
        System.exit(1);

        return 0;
      }
    }

    return 0;
  }
Example #17
0
  // Logs a new ATOM entry
  public static synchronized void addATOMEntry(
      String title, String link, String description, File atomFile, String context) {
    try {

      if (atomFile.exists()) {

        // System.out.println("ATOM file found!");
        /** Namespace URI for content:encoded elements */
        String CONTENT_NS = "http://www.w3.org/2005/Atom";

        /** Parses RSS or Atom to instantiate a SyndFeed. */
        SyndFeedInput input = new SyndFeedInput();

        /** Transforms SyndFeed to RSS or Atom XML. */
        SyndFeedOutput output = new SyndFeedOutput();

        // Load the feed, regardless of RSS or Atom type
        SyndFeed feed = input.build(new XmlReader(atomFile));

        // Set the output format of the feed
        feed.setFeedType("atom_1.0");

        List<SyndEntry> items = feed.getEntries();
        int numItems = items.size();
        if (numItems > 9) {
          items.remove(0);
          feed.setEntries(items);
        }

        SyndEntry newItem = new SyndEntryImpl();
        newItem.setTitle(title);
        newItem.setLink(link);
        newItem.setUri(link);
        SyndContent desc = new SyndContentImpl();
        desc.setType("text/html");
        desc.setValue(description);
        newItem.setDescription(desc);
        desc.setType("text/html");
        newItem.setPublishedDate(new java.util.Date());

        List<SyndCategory> categories = new ArrayList<SyndCategory>();
        if (CommonConfiguration.getProperty("htmlTitle", context) != null) {
          SyndCategory category2 = new SyndCategoryImpl();
          category2.setName(CommonConfiguration.getProperty("htmlTitle", context));
          categories.add(category2);
        }
        newItem.setCategories(categories);
        if (CommonConfiguration.getProperty("htmlAuthor", context) != null) {
          newItem.setAuthor(CommonConfiguration.getProperty("htmlAuthor", context));
        }
        items.add(newItem);
        feed.setEntries(items);

        feed.setPublishedDate(new java.util.Date());

        FileWriter writer = new FileWriter(atomFile);
        output.output(feed, writer);
        writer.toString();
      }
    } catch (IOException ioe) {
      System.out.println("ERROR: Could not find the ATOM file.");
      ioe.printStackTrace();
    } catch (Exception e) {
      System.out.println("Unknown exception trying to add an entry to the ATOM file.");
      e.printStackTrace();
    }
  }
  /**
   * Compare the checksums of the hdfs file as well as the local copied file.
   *
   * @author [email protected]
   * @date Fri Jan 27 06:06:00 2012
   */
  boolean compareChecksums(FileSystem fs, Path p, String sFsPath) {
    try {
      // get hdfs file info
      FileStatus stat = fs.getFileStatus(p);

      // get HDFS checksum
      FileChecksum ck = fs.getFileChecksum(p);
      String sCk, sCkShort;
      if (ck == null) {
        sCk = sCkShort = "<null>";
      } else {
        sCk = ck.toString();
        sCkShort = sCk.replaceAll("^.*:", "");
      }

      // System.out.println(p.toUri().getPath() + " len=" + stat.getLen()
      // + " " + stat.getOwner() + "/" + stat.getGroup()
      // + " checksum=" + sCk);

      // find the local file
      File fLocal = new File(sFsPath);
      if (!fLocal.exists()) {
        System.out.println("CHECKSUM-ERROR: file does not exist: " + sFsPath);
        return false;
      }
      if (!fLocal.isFile()) {
        System.out.println("CHECKSUM-ERROR: path is not a file: " + sFsPath);
        return false;
      }
      if (stat.getLen() != fLocal.length()) {
        System.out.println(
            "CHECKSUM-ERROR: length mismatch: "
                + sFsPath
                + " hdfslen="
                + stat.getLen()
                + " fslen="
                + fLocal.length());
        return false;
      }

      // get local fs checksum
      FileChecksum ckLocal = getLocalFileChecksum(sFsPath);
      if (ckLocal == null) {
        System.out.println("ERROR Failed to get checksum for local file " + sFsPath);
        return false;
      }

      // compare checksums as a string, after stripping the
      // algorithm name from the beginning
      String sCkLocal = ckLocal.toString();
      String sCkLocalShort = sCkLocal.replaceAll("^.*:", "");

      if (false == sCkShort.equals(sCkLocalShort)) {
        System.out.println(
            "CHECKSUM-ERROR: checksum mismatch: "
                + sFsPath
                + "\nhdfs = "
                + sCk
                + "\nlocal= "
                + sCkLocal);
        return false;
      }

      return true;
    } catch (IOException e) {
      System.out.println("CHECKSUM-ERROR: " + sFsPath + " exception " + e.toString());
    }

    return false;
  }
  static void loadData(String uname, String pword) throws Exception {
    System.out.println("Data load started..\n");
    PreparedStatement dropTrans = con.prepareStatement("drop table Trans");
    PreparedStatement dropItems = con.prepareStatement("drop table Items");
    PreparedStatement dropFISet = con.prepareStatement("drop table FISet");
    PreparedStatement dropPairs = con.prepareStatement("drop table pairs");
    PreparedStatement dropTempFISet = con.prepareStatement("drop table TempFISet");
    PreparedStatement dropTemp = con.prepareStatement("drop table temp");
    PreparedStatement dropTempset = con.prepareStatement("drop table tempset");
    PreparedStatement dropTempright = con.prepareStatement("drop table tempright");
    PreparedStatement dropArtable = con.prepareStatement("drop table artable");
    PreparedStatement createTrans =
        con.prepareStatement("CREATE TABLE TRANS (TRANSID NUMBER,ITEMID NUMBER)");
    PreparedStatement createItems =
        con.prepareStatement("CREATE TABLE ITEMS(ITEMID NUMBER,ITEMNAME VARCHAR2(100))");
    PreparedStatement createFISet =
        con.prepareStatement("CREATE TABLE FISet(ISetID NUMBER,ITEMID NUMBER,percent NUMBER)");
    PreparedStatement createPairs =
        con.prepareStatement("create table pairs(id1 number,id2 number)");
    PreparedStatement createTempFISet =
        con.prepareStatement("CREATE TABLE TempFISet(ISetid number,itemid number,percent number)");
    PreparedStatement createTemp = con.prepareStatement("CREATE TABLE Temp (ITEMID NUMBER)");
    PreparedStatement createTempset = con.prepareStatement("CREATE TABLE tempset (ITEMID NUMBER)");
    PreparedStatement createTempright =
        con.prepareStatement("CREATE TABLE tempright (ITEMID NUMBER)");
    PreparedStatement createArtable =
        con.prepareStatement(
            "CREATE TABLE artable(ruleid NUMBER,itemid NUMBER,itemname VARCHAR2(100 BYTE),confidence NUMBER,isleft CHAR(1 BYTE),support NUMBER)");

    try {
      dropTrans.executeUpdate();
    } catch (Exception e) {

    }
    try {

      dropItems.executeUpdate();
    } catch (Exception e) {

    }
    try {
      dropFISet.executeUpdate();
    } catch (Exception e) {

    }
    try {
      dropPairs.executeUpdate();
    } catch (Exception e) {

    }
    try {
      dropTempset.executeUpdate();
    } catch (Exception e) {

    }
    try {
      dropTempright.executeUpdate();
    } catch (Exception e) {

    }
    try {
      dropArtable.executeUpdate();
    } catch (Exception e) {

    }
    try {
      dropTempFISet.executeUpdate();
    } catch (Exception e) {

    }
    try {
      dropTemp.executeUpdate();
    } catch (Exception e) {

    }
    con.commit();

    createTrans.executeUpdate();
    createItems.executeUpdate();
    createFISet.executeUpdate();
    createPairs.executeUpdate();
    createTempFISet.executeUpdate();
    createTemp.executeUpdate();
    createTempset.executeUpdate();
    createTempright.executeUpdate();
    createArtable.executeUpdate();
    writeItemsControlFile();
    writeTransControlFile();
    String loadItemsString =
        "sqlldr " + uname.trim() + "/" + pword.trim() + "@orcl control=items.ctl";
    String loadTransString =
        "sqlldr " + uname.trim() + "/" + pword.trim() + "@orcl control=trans.ctl";

    Process p1 = Runtime.getRuntime().exec(loadItemsString);
    p1.waitFor();
    Process p2 = Runtime.getRuntime().exec(loadTransString);
    p2.waitFor();
    File f = new File("arm.sql");
    if (f.exists()) {
      Process p3 =
          Runtime.getRuntime()
              .exec("sqlplus " + uname.trim() + "@orcl/" + pword.trim() + " @arm.sql");
      p3.waitFor();
      con.commit();
      System.out.println("Data Load Completed...\n");
    } else {
      System.out.println(
          "The arm.sql file which has the stored procedures forthe project is not in the current directory");
      System.exit(0);
    }
  }
Example #20
0
  public static void main(String arg[]) {
    Hashtable ignoreList = new Hashtable();
    Class cl = null;
    Model model = null;
    System.out.println("Synchronizing forms with database...");
    Db.init();

    try {
      DatabaseMetaData meta = Db.getCon().getMetaData();
      String[] types = {"TABLE"};
      ResultSet rs = meta.getTables(null, null, "%", types);

      // read ignore.list
      ignoreList = AutogenerateUtil.readIgnoreList();
      // prepare directory
      File fDir = new File("../../web/WEB-INF/views/crud_form");
      if (!fDir.exists()) fDir.mkdir();
      while (rs.next()) {
        // proper file name generationm
        String className = "";
        String tableName = rs.getString("TABLE_NAME");
        className = StringUtil.toProperClassName(tableName); // space allowed...
        // tableName = tableName.toUpperCase(); //If Oracle that need uppercase tablename. In MySQL
        // in Mac OS X (and probably Linux), it mustbe case sensitive
        // open table
        String sql = "select * from " + tableName;
        PreparedStatement pstmt =
            Db.getCon()
                .prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = pstmt.executeQuery();
        ResultSetMetaData metaColumn = resultSet.getMetaData();
        int nColoumn = metaColumn.getColumnCount();

        // get foreign keys,and stored it in hashtable
        ResultSet rsFk = meta.getImportedKeys(Db.getCon().getCatalog(), null, tableName);
        Hashtable hashFk = new Hashtable();
        System.out.println("FK Infos for table " + tableName);
        while (rsFk.next()) {
          String pkTableName = rsFk.getString("PKTABLE_NAME");
          String pkColumnName = rsFk.getString("PKCOLUMN_NAME");
          String fkColumnName = rsFk.getString("FKCOLUMN_NAME");

          int fkSequence = rsFk.getInt("KEY_SEQ");
          System.out.println(
              tableName + "." + fkColumnName + " => " + pkTableName + "." + pkColumnName);
          hashFk.put(fkColumnName, pkColumnName);
          hashFk.put(fkColumnName + "_table", pkTableName);
        }
        rsFk.close();

        // create form page
        System.out.println(
            "Creating form page for "
                + tableName
                + " from table + "
                + application.config.Database.DB
                + "."
                + tableName);
        fDir = new File("../../web/WEB-INF/views/" + tableName);
        if (!fDir.exists()) fDir.mkdir();
        File f = new File("../../web/WEB-INF/views/" + tableName + "/form_" + tableName + ".jsp");
        if (ignoreList.get("form_" + tableName + ".jsp") != null) {
          Logger.getLogger(GenerateForm.class.getName())
              .log(Level.INFO, "Ignoring creation of form_" + tableName + ".jsp");
        } else {
          Writer out = new FileWriter(f);
          out.write(
              "<%@ page contentType=\"text/html; charset=UTF-8\" language=\"java\" import=\"java.sql.*,recite18th.library.Db,application.config.Config,recite18th.library.Pagination\" %>");
          out.write("<%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\" %>\n");
          out.write("<%@ taglib uri=\"http://java.sun.com/jsp/jstl/functions\" prefix=\"fn\" %>\n");

          // create model for this class, use in detecting its PK Field
          cl = Class.forName("application.models." + className + "Model");
          model = (Model) cl.newInstance();

          // iterate all columns
          resultSet.beforeFirst();
          resultSet.next();
          out.write(
              "<table border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#E8EDFF\">\n");
          out.write("<tr>\n");
          out.write("<td>\n");
          out.write(
              "<form action=\"<%=Config.base_url%>index/"
                  + className
                  + "/save\" method=\"post\" enctype=\"multipart/form-data\">\n"); // I hope it's
          // okay to
          // default it to
          // multipart data
          out.write("<table id=\"hor-zebra\" summary=\"Form " + className + "\">\n");
          out.write("<thead>\n");
          out.write("<tr>\n");
          out.write("<th colspan=\"2\" class=\"odd\" scope=\"col\">Form " + className + " </th>\n");
          out.write("</tr>\n");
          out.write("</thead>\n");
          out.write("<tbody>\n");

          for (int i = 1; i <= nColoumn; i++) {
            String columnName = metaColumn.getColumnName(i);
            String dataType = metaColumn.getColumnClassName(i);
            out.write("<tr>\n");

            // if(!columnName.equals(model.getPkFieldName())) // implementing the case of PK not
            // AutoIncrement
            // if(!metaColumn.isAutoIncrement(i))
            // {
            // varying field input for type

            // foreign field, as chooser page view
            if (hashFk.get(columnName)
                != null) // TODO: what if PK is chooser also?? :) CUrrently I add it manually the
            // hidden_*Pk_nama* field
            {
              String fkTableName = hashFk.get(columnName + "_table") + "";
              String fkColumnName = hashFk.get(columnName) + "";
              String fkController = StringUtil.toProperClassName(fkTableName);

              out.write("<td>" + columnName + "</td>\n");
              out.write("<td>\n");
              out.write(
                  "<input name=\""
                      + columnName
                      + "\" type=\"hidden\" id=\""
                      + columnName
                      + "\" value=\"${model."
                      + columnName
                      + "}\"/>\n");
              out.write(
                  "<input name=\"label_"
                      + columnName
                      + "\" readonly=\"true\" type=\"text\" id=\"label_"
                      + columnName
                      + "\" value=\"${model."
                      + columnName
                      + "}\"/>\n"); // TODO : translate I
              out.write(
                  "<a href=\"<%=Config.base_url%>index/"
                      + fkController
                      + "/chooseView?height=220&width=700\" class=\"thickbox\">Pilih</a>");
              out.write("</td>\n");
            } else {

              // regular field input, not foreign key case
              if (!columnName.equals(model.getPkFieldName())) {
                out.write("<td>" + columnName + "</td>\n");
                out.write("<td>\n");

                // ENUM Column, displayed as HTML SELECT. May will only work for mysql only...
                Logger.getLogger(GenerateForm.class.getName())
                    .log(Level.INFO, columnName + " type is " + metaColumn.getColumnType(i));
                if (metaColumn.getColumnType(i) == 1) {
                  String enum_content[][] =
                      Db.getDataSet(
                          "SELECT SUBSTRING(COLUMN_TYPE,6,length(SUBSTRING(COLUMN_TYPE,6))-1) as enum_content "
                              + " FROM information_schema.COLUMNS "
                              + " WHERE TABLE_NAME='"
                              + tableName
                              + "' "
                              + " AND COLUMN_NAME='"
                              + columnName
                              + "'");
                  if (enum_content.length > 0) {
                    // Logger.getLogger(Model.class.getName()).log(Level.INFO, "Enum Content = " +
                    // enum_content[0][0]);
                    String enum_list[] = enum_content[0][0].split(",");
                    out.write("<select name=\"" + columnName + "\" id=\"" + columnName + "\">\n");
                    for (int ienum_list = 0; ienum_list < enum_list.length; ienum_list++)
                      out.write(
                          "\t<option <c:if test=\"${model."
                              + columnName
                              + "=='"
                              + enum_list[ienum_list].substring(
                                  1, enum_list[ienum_list].length() - 1)
                              + "'}\"> selected=\"selected\" </c:if> value=\""
                              + enum_list[ienum_list].substring(
                                  1, enum_list[ienum_list].length() - 1)
                              + "\">"
                              + enum_list[ienum_list].substring(
                                  1, enum_list[ienum_list].length() - 1)
                              + "</option>\n");
                    out.write("</select>\n\n");

                  } else {
                    // no enum content detected.. :)
                    out.write(
                        "<input name=\""
                            + columnName
                            + "\" type=\"text\" id=\""
                            + columnName
                            + "\" value=\"${model."
                            + columnName
                            + "}\"/>\n");
                  }
                } else if (metaColumn.getColumnType(i) == 91) {
                  out.write(
                      "<input name=\""
                          + columnName
                          + "\" type=\"text\" id=\""
                          + columnName
                          + "\" value=\"${model."
                          + columnName
                          + "}\"/>\n");
                  out.write("<script>\n");
                  out.write(
                      " if(!isValidDate($('#"
                          + columnName
                          + "').val())) $('#"
                          + columnName
                          + "').val('1980-1-1');\n"); // TODO: default value
                  out.write("  (function($){\n");

                  out.write("  $('#" + columnName + "').click(function() {\n");
                  out.write("    $('#" + columnName + "').DatePickerShow();\n");
                  out.write("  });\n");

                  out.write("  $('#" + columnName + "').DatePicker({\n");
                  out.write("    format:'Y-m-d',\n");
                  out.write("    date: $('#" + columnName + "').val(),\n");
                  out.write("    current: $('#" + columnName + "').val(),\n");
                  out.write("    starts: 1,\n");
                  out.write("    position: 'r',\n");

                  out.write("    onBeforeShow: function(){\n");
                  out.write(
                      "      $('#"
                          + columnName
                          + "').DatePickerSetDate($('#"
                          + columnName
                          + "').val(), true);\n");
                  out.write("    },\n");

                  out.write("    onChange: function(formated, dates){\n");
                  out.write("      $('#" + columnName + "').DatePickerHide();\n");
                  out.write("      $('#" + columnName + "').val(formated);\n");
                  out.write("    }\n");
                  out.write("  });\n");
                  out.write("  })(jQuery)\n");
                  out.write(" </script>\n");
                } else {
                  out.write(
                      "<input name=\""
                          + columnName
                          + "\" type=\"text\" id=\""
                          + columnName
                          + "\" value=\"${model."
                          + columnName
                          + "}\"/>\n");
                  out.write("${" + columnName + "_error}\n"); // regular input field
                }
              } else { // PK case
                if (metaColumn.isAutoIncrement(i)) {
                  out.write(
                      "<input name=\"hidden_"
                          + columnName
                          + "\" type=\"hidden\" id=\"hidden_"
                          + columnName
                          + "\" value=\"${model."
                          + columnName
                          + "}\"/>\n");
                } else {
                  out.write("<td>" + columnName + "</td>\n");
                  out.write("<td>\n");

                  out.write(
                      "<input name=\""
                          + columnName
                          + "\" type=\"text\" id=\""
                          + columnName
                          + "\" value=\"${model."
                          + columnName
                          + "}\"/>\n");
                  out.write("${" + columnName + "_error}\n");
                  out.write(
                      "<input name=\"hidden_"
                          + columnName
                          + "\" type=\"hidden\" id=\"hidden_"
                          + columnName
                          + "\" value=\"${model."
                          + columnName
                          + "}\"/>\n");
                }
              }
              out.write("</td>\n");
            }
            out.write("</tr>\n");
          }
          out.write("<tr class=\"odd\">\n");
          out.write("<td>&nbsp;</td>\n");
          out.write("<td><input type=\"submit\" name=\"Submit\" value=\"Simpan\">");
          out.write(
              "<input name=\"Button\" type=\"button\" id=\"Submit\" value=\"Batal\" onClick=\"javascript:history.back(-1);\"></td>\n");
          out.write("</tr>\n");
          out.write("</tbody>\n");
          out.write("</table>\n");
          out.write("</form></td>\n");
          out.write("</tr>\n");
          out.write("</table>\n");
          out.flush();
          out.close();
        }

        // create viewPage
        if (ignoreList.get("view_" + tableName + ".jsp") != null) {
          Logger.getLogger(GenerateForm.class.getName())
              .log(Level.INFO, "Ignoring creation of view_" + tableName + ".jsp");
        } else {
          System.out.println("Creating view page " + tableName);

          fDir = new File("../../web/WEB-INF/views/" + tableName);
          if (!fDir.exists()) fDir.mkdir();
          File fView =
              new File("../../web/WEB-INF/views/" + tableName + "/view_" + tableName + ".jsp");
          Writer outView = new FileWriter(fView);
          outView.write(
              "<%@ page contentType=\"text/html; charset=UTF-8\" language=\"java\" import=\"java.sql.*,recite18th.library.Db,application.config.Config,recite18th.library.Pagination\" %>");
          outView.write("<%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\" %>\n");
          outView.write(
              "<%@ taglib uri=\"http://java.sun.com/jsp/jstl/functions\" prefix=\"fn\" %>\n");
          outView.write("<% int pagenum = 0; %>\n");
          // outView.write("<%@ include file=\"/WEB-INF/views/header.jsp\" %>");
          outView.write(
              "<a href=\"<%=Config.base_url%>index/" + className + "/input/-1\">Tambah Data</a>\n");
          outView.write(
              "|| <a href=\"<%=Config.base_url%>index/" + className + "/print\">Cetak</a>\n");
          outView.write("<table width=\"100%\" id=\"rounded-corner\">\n");
          outView.write("<thead>\n");
          // iterate all columns : table header
          outView.write("  <tr>\n");
          outView.write("  <th scope=\"col\" class=\"rounded-company\">No.</th>\n");
          resultSet.beforeFirst();
          resultSet.next();

          // get Primary Key Field Name : often use
          String pkFieldName = "";
          try {
            Class params[] = null;
            Method objMethod = cl.getMethod("getPkFieldName", params);
            pkFieldName = "" + objMethod.invoke(model);
          } catch (Exception ex) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
          }

          // ALL Lower Case
          pkFieldName = pkFieldName.toLowerCase();

          // customize column view page
          for (int i = 1; i <= nColoumn; i++) {
            String columnName =
                metaColumn.getColumnName(i).toLowerCase(); // Caution : ALL LowerCase
            String dataType = metaColumn.getColumnClassName(i);
            String thClass = "rounded-q1";
            String thTitle = StringUtil.toProperFieldTitle(columnName);

            if (TableCustomization.getTable(tableName)
                != null) // there is customization for this table
            {
              if (TableCustomization.getTable(tableName).get(columnName) != null) {
                thTitle = TableCustomization.getTable(tableName).get(columnName) + "";
                outView.write(
                    "  <th scope=\"col\" class=\"" + thClass + "\">" + thTitle + "</th>\n");
              }
            } else { // standard view for this table : hide PK, because mostly is auto increment
              if (!metaColumn.isAutoIncrement(i))
                outView.write(
                    "  <th scope=\"col\" class=\"" + thClass + "\">" + thTitle + "</th>\n");
            }
          }

          outView.write("  <th scope=\"col\" class=\"rounded-q4\">Aksi</th>\n");
          outView.write("  </tr>\n");
          outView.write("</thead>\n");
          outView.write("<tfoot>\n");
          outView.write("  <tr>\n");
          outView.write(
              "    <td colspan=\""
                  + (nColoumn + 1)
                  + "\" class=\"rounded-foot-left\"><%=Pagination.createLinks(pagenum)%></td>\n");
          outView.write("    <td class=\"rounded-foot-right\">&nbsp;</td>\n");
          outView.write("  </tr>\n");
          outView.write("</tfoot>\n");

          outView.write("<tbody>\n");
          outView.write("  <c:forEach items=\"${row}\" var=\"item\" varStatus=\"status\" >\n");
          outView.write("    <tr>\n");
          outView.write("      <td>${status.count}</td>\n");

          // iterate all columns : table data
          resultSet.beforeFirst();
          resultSet.next();
          for (int i = 1; i <= nColoumn; i++) {
            String columnName = metaColumn.getColumnName(i);
            // if(!columnName.equals(pkFieldName)) //TOFIX : currently, PK Field is not shown
            if (TableCustomization.getTable(tableName) != null) {
              if (TableCustomization.getTable(tableName).get(columnName) != null) {
                outView.write("      <td>${item." + columnName + "}</td>\n");
              }
            } else {
              if (!metaColumn.isAutoIncrement(i))
                outView.write("      <td>${item." + columnName + "}</td>\n");
            }
          }

          outView.write("      <td>\n");
          outView.write(
              "         <a href=\"<%=Config.base_url%>index/"
                  + className
                  + "/input/${item."
                  + pkFieldName
                  + "}\">Ubah</a>\n");
          outView.write(
              "         <a href=\"<%=Config.base_url%>index/"
                  + className
                  + "/delete/${item."
                  + pkFieldName
                  + "}\" onClick=\"return confirm('Apakah Anda yakin?');\">Hapus</a>\n");
          outView.write("      </td>\n");

          outView.write("    </tr>\n");
          outView.write("  </c:forEach>\n");
          outView.write("</tbody>\n");
          outView.write("</table>\n");
          // outView.write("<%@ include file=\"/WEB-INF/views/footer.jsp\" %>");
          outView.flush();
          outView.close();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #21
0
  public void purgeLogEvents(final List<SearchConstraint> constraints, final int daysToKeep) {
    blockHereIfBadNfs();

    StringBuffer sql =
        new StringBuffer(
            "select u.mail_directory || '/logs/' || e.event_id || '.log' "
                + "from dat_client_log_events e, dat_user_account u "
                + "where event_time < current_timestamp - (? || ' days')::interval "
                + "and e.has_log_file != 0 "
                + "and e.user_id = u.object_id");
    appendWhereConstraints(sql, constraints, s_propToColumnMap);
    m_logCategory.info("Purge event logs query is\n" + sql);

    StringBuffer sql2 =
        new StringBuffer(
            "delete from dat_client_log_events "
                + "where event_time < current_timestamp - (? || ' days')::interval");
    if (!constraints.isEmpty()) {
      sql2.append(" and event_id in (select e.event_id from dat_client_log_events e ");
      appendWhere(sql2, constraints, s_propToColumnMap);
      sql2.append(")");
    }
    m_logCategory.info("Purge event logs query is\n" + sql2);

    try {
      Connection c = m_txManager.getConnection();
      PreparedStatement stmt = null;
      ResultSet rs = null;
      boolean needsRollback = true;
      try {
        stmt = c.prepareStatement(sql.toString());
        stmt.setInt(1, daysToKeep);
        rs = stmt.executeQuery();
        while (rs.next()) {
          File logFile = new File(getNfsRoot(), rs.getString(1));
          // nfs usage, but DB transaction takes no locks.
          if (logFile.exists()) {
            boolean ok = logFile.delete();
            if (!ok) {
              m_logger
                  .log("Unable to delete")
                  .param(LoggingConsts.FILENAME, logFile.getAbsolutePath())
                  .warn();
            }
          } // file exists.
        } // each row

        // Below, no nfs usage occurs.  We can use the same connection.
        // the 'sql delete' may use DB locks.
        stmt = c.prepareStatement(sql2.toString());
        stmt.setInt(1, daysToKeep);
        stmt.executeUpdate();
        c.commit();
        needsRollback = false;
      } finally {
        DbUtils.safeClose(rs);
        DbUtils.safeClose(stmt);
        if (needsRollback) {
          c.rollback();
        }
        m_txManager.returnConnection(c);
      }
    } catch (Exception ex) {
      m_logger.log("Error in purgeLogEvents").warn(ex);
    }
  }
Example #22
0
  public ClientSession createClientSession(IClientInfo client, String clientType) {

    // Both OE and BB share the session file.  Don't stomp an existing file.
    if (client == null || !client.isConfigured()) {
      throw new IllegalStateException("Client not configured.");
    }

    clientType = clientType == null ? "" : clientType;

    ClientSession session = null;
    String tmpName = null;
    String targetName = null;
    try {
      File targetFile = getSessionFile(client.getLastActivationToken());
      targetName = targetFile.getAbsolutePath();
      if (targetFile.exists()) {
        session = new ClientSession();
        session.read(targetFile);
        if (session.getLastActivationId() != client.getLastActivationId()
            || session.getUserId() != client.getUserId()) {
          // Existing session file found but it's clearly not ours.
          // A hash collision is improbable.  If this occurs, its more likely to be a
          // bug.  We can't safely rewrite the session file because another client
          // might later call getClientSession() and read our email.
          // This is a support concern.
          LogMessageGen lmg = new LogMessageGen();
          lmg.setSubject("Found existing session, but the content is unexpected.");
          lmg.param(LoggingConsts.FILENAME, targetFile.getAbsolutePath());
          m_logCategory.error(lmg.toString());
          throw new ClientServiceException("");
        }
        session.setActivationToken(client.getLastActivationToken());
      } // targetFile already found.

      if (session == null) {
        UserAccount user = m_userService.getUserAccount(client.getCustomerId(), client.getUserId());
        session = client.createSession(user);

        // What if the BB and OE create a session at the same time?
        // This ensures a unique filename.
        // The client who does the last renameTo() below wins the race
        // and that becomes the common session file.

        tmpName = targetFile.getAbsolutePath() + "_tmp_" + clientType;
        File tmpFile = new File(tmpName);
        session.write(tmpFile);

        // If 'targetFile' already exists it'll get clobbered.
        // Makes the session targetFile generation atomic.
        boolean ok = FileUtils.clobberingRename(tmpFile, targetFile);
        if (!ok) {
          throw new IOException("Rename failed");
        }
      } // need to create session.
    } catch (Exception ex) {
      if (ex instanceof ClientServiceException) {
        // We already logged it.
        throw (ClientServiceException) ex;
      }
      LogMessageGen lmg = new LogMessageGen();
      lmg.setSubject("Unable to generate session file.");
      lmg.param(LoggingConsts.USER_ID, client.getUserId());
      lmg.param(LoggingConsts.TEMP_NAME, tmpName);
      lmg.param(LoggingConsts.FILENAME, targetName);

      m_logCategory.error(lmg.toString(), ex);
      throw new ClientServiceException("");
    }
    return session;
  }
Example #23
0
  public static void main(String[] argv) {
    try {
      if (argv.length != 1) {
        System.err.println(
            "java " + JdbcProxyGenerator.class.getName() + " <source-root-directory>");
        return;
      }

      File srcroot = new File(argv[0]);
      if (!srcroot.exists() || !srcroot.canWrite()) {
        System.err.println(
            JdbcProxyGenerator.class.getName()
                + " -- sourceroot: "
                + argv[0]
                + " must exist and be writable");
        return;
      }

      DelegatorGenerator mdgen = new NewProxyMetaDataGenerator();
      DelegatorGenerator rsgen = new NewProxyResultSetGenerator();
      DelegatorGenerator stgen = new NewProxyAnyStatementGenerator();
      DelegatorGenerator cngen = new NewProxyConnectionGenerator();

      /*
       * Eliminating for c3p0-0.9.5

      // Usually stgen can be used for all three Statement types. However, we have temporarily
      // implemented some very partial JDBC4 methods to maintain Hibernate support, prior to full JDBC4
      // support in a future version. To do this, we'll need to force some methods into the PreparedStatement
      // (and therefore CallableStatement) interfaces, methods that don't exist in the current JDBC3 build.
      // We should be able to get rid of psgen (in favor of stgen above) when we are actually building against
      // JDBC4 (so we don't need to artificially inject methods).
             //DelegatorGenerator psgen = new NewProxyAnyStatementGenerator();
      //psgen.setReflectiveDelegateMethods( JDBC4TemporaryPreparedStatementMethods.class.getMethods() );

      */

      genclass(cngen, Connection.class, "com.mchange.v2.c3p0.impl.NewProxyConnection", srcroot);
      genclass(stgen, Statement.class, "com.mchange.v2.c3p0.impl.NewProxyStatement", srcroot);
      // genclass( psgen, PreparedStatement.class,
      // "com.mchange.v2.c3p0.impl.NewProxyPreparedStatement", srcroot );
      // genclass( psgen, CallableStatement.class,
      // "com.mchange.v2.c3p0.impl.NewProxyCallableStatement", srcroot );
      genclass(
          stgen,
          PreparedStatement.class,
          "com.mchange.v2.c3p0.impl.NewProxyPreparedStatement",
          srcroot);
      genclass(
          stgen,
          CallableStatement.class,
          "com.mchange.v2.c3p0.impl.NewProxyCallableStatement",
          srcroot);
      genclass(rsgen, ResultSet.class, "com.mchange.v2.c3p0.impl.NewProxyResultSet", srcroot);
      genclass(
          mdgen,
          DatabaseMetaData.class,
          "com.mchange.v2.c3p0.impl.NewProxyDatabaseMetaData",
          srcroot);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #24
0
 /**
  * If we'll block due to an nfs issue, let it block here before we open a DB connection. This
  * reduces the window where an nfs problem can hoard a connection. We don't care if exists() fails
  * because file calls usually create dirs lazily.
  */
 private void blockHereIfBadNfs() {
   m_cacheBaseDir.exists();
 }
Example #25
0
  /**
   * Perrform process.
   *
   * @return Message that would be set to process infor summary (no use currently)
   * @throws Exception if not successful
   */
  protected String doIt() throws Exception {
    // 	load query into cache file directly
    // int userId= this.getAD_User_ID();
    // User user= SecurityUtils.getUser(userId);

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    QueryEngine engine = QueryEngine.getInstance();
    conn = engine.getConnection();

    Configurations conf =
        (Configurations)
            WebUtils.getServletContextManager().getActor(nds.util.WebKeys.CONFIGURATIONS);
    String folder = conf.getProperty("ahyy.payment.folder.download", "e:/act/ahyy/download");
    File fd = new File(folder);
    if (!fd.exists()) fd.mkdirs();
    ahyyCode = conf.getProperty("ahyy.payment.code");
    if (ahyyCode == null || ahyyCode.length() != 8)
      throw new NDSException("Wrong code for bank payment interface");

    SimpleDateFormat df = new SimpleDateFormat("yyMMdd");
    String ftpFile = "MPAYREP" + ahyyCode + df.format(new java.util.Date());
    String file = folder + "/" + ftpFile;
    StringBuffer sb = new StringBuffer();
    String lineSep = Tools.LINE_SEPARATOR;
    try {
      // 下载文件
      CommandExecuter cmd = new CommandExecuter(folder + "/log/" + ftpFile + ".log");
      String exec = conf.getProperty("ahyy.payment.download", "e:/act/bin/download.cmd");
      int err = cmd.run(exec + " " + ftpFile);
      if (err != 0) {
        throw new NDSException("Error(code=" + err + ") when doing " + exec + " " + ftpFile);
      }
      if (!(new File(file)).exists()) {
        throw new NDSException("File not downloaded when doing " + exec + " " + ftpFile);
      }
      BufferedReader in = new BufferedReader(new FileReader(file));
      in.readLine(); // skip first line, which is summary
      String line = in.readLine();
      pstmt =
          conn.prepareStatement(
              "update b_pay_sum set state=?, err_code =? where billno=? and state='R'");
      while (line != null) {
        log.debug(line);
        if (line.length() < 34) {
          log.debug("line not has length: 34");
        }

        /*
        String billType= line.substring(0,2);
        String billNo= line.substring(0, 22);// billno
        String amt=line.substring(22,34);
        String ack=line.substring(34,36);*/
        String billNo = line.substring(0, 20); // billno
        String amt = line.substring(20, 32);
        String ack = line.substring(32, 34);
        if ("00".equals(ack)) {
          java.math.BigDecimal bd = new java.math.BigDecimal(amt);
          pstmt.setString(1, "Y");
          pstmt.setNull(2, java.sql.Types.VARCHAR);
          pstmt.setString(3, billNo);
          int ret = pstmt.executeUpdate();
          if (ret != 1) {
            sb.append(line + "(updated " + ret + " lines)").append(lineSep);
          }
        } else {
          pstmt.setString(1, "P");
          pstmt.setString(2, ack);
          pstmt.setString(3, billNo);
          int ret = pstmt.executeUpdate();
          if (ret != 1) {
            sb.append(line + "(updated " + ret + " lines)").append(lineSep);
          }
        }
        line = in.readLine();
      }
      in.close();
      try {
        log.debug("Move " + file + " to " + folder + "/log/" + ftpFile);
        (new File(file)).renameTo(new File(folder + "/log/" + ftpFile));
      } catch (Throwable t) {
        log.error("Fail to move " + file + " to " + folder + "/log/" + ftpFile, t);
      }
      Vector vet = new Vector();
      String log = sb.toString();
      this.log.debug(log);
      this.addLog(log);
      return "完成";
    } finally {
      if (rs != null)
        try {
          rs.close();
        } catch (Throwable t) {
        }
      if (pstmt != null)
        try {
          pstmt.close();
        } catch (Throwable t) {
        }
      if (conn != null)
        try {
          conn.close();
        } catch (Throwable t) {
        }
    }
  }