/**
  * if database reset,return true;else return false.
  *
  * @param databaseName
  * @return
  */
 public boolean resetDatabase(@NonNull String databaseName) {
   if (databaseName.equals(this.databaseName)) {
     return false;
   }
   daoMapper = new HashMap<>();
   this.databaseName = databaseName;
   DatabaseProcessor.getInstance()
       .resetRapidORMDatabaseOpenHelper(getRapidORMDatabaseOpenHelper(databaseName));
   return true;
 }
 private void initial() {
   allTableClass = registerAllTableClass();
   DatabaseProcessor.getInstance().initializeConnection(this, allTableClass);
 }
Exemple #3
0
  /**
   * @param request JSONObject contain request data
   * @param response
   * @throws Exception
   */
  public static void processRequest(
      HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    JSONObject jsonResponse = new JSONObject();
    JSONObject jsonRequest;
    try {
      // get parameter
      String strServiceName = servletRequest.getServletPath();
      String strMethodName = servletRequest.getMethod();
      // get service config
      DictionaryNode ndClassConfig = mDic.getNode(strServiceName);
      if (ndClassConfig == null) {
        throw new Exception("unknown request " + strServiceName);
      }
      // request
      StringBuilder stringBuilder = new StringBuilder(1000);
      Scanner scanner = new Scanner(servletRequest.getInputStream());
      while (scanner.hasNextLine()) {
        stringBuilder.append(scanner.nextLine());
      }
      String strRequest = stringBuilder.toString();
      System.out.println(strRequest);
      // JSON request
      //			jsonRequest = new JSONObject("{}");
      jsonRequest = new JSONObject(strRequest);
      // Get class name & create class instance
      String strClassName = ndClassConfig.getString("Class");
      if (strClassName.length() == 0) throw new Exception("Class name was not passed");
      @SuppressWarnings("rawtypes")
      Class cls = Class.forName(strClassName);
      Object obj = cls.newInstance();

      // Check class
      if (!(obj instanceof DatabaseProcessor))
        throw new Exception("Class '" + strClassName + "' must be a Database Processor");
      DatabaseProcessor storage = (DatabaseProcessor) obj;
      // Get function name and method
      String strFunctionName = "";
      switch (strMethodName.toUpperCase()) {
        case "GET":
          strFunctionName = "doGet";
          break;
        case "POST":
          strFunctionName = "doPost";
          break;
        case "DELETE":
          strFunctionName = "doDelete";
          break;
        default:
          throw new Exception("method is unsupport");
      }
      if (strFunctionName.length() == 0) throw new Exception("Function name was not passed");
      // Get method from class name and function name
      @SuppressWarnings("unchecked")
      Method method = cls.getMethod(strFunctionName);
      if (method == null)
        throw new Exception(
            "Function '" + strClassName + "." + strFunctionName + "' was not declared");

      // Check function
      if (Modifier.isAbstract(method.getModifiers()))
        throw new Exception(
            "Function '" + strClassName + "." + strFunctionName + "' was not implemented");
      if (!Modifier.isPublic(method.getModifiers()))
        throw new Exception(
            "Function '" + strClassName + "." + strFunctionName + "' is not public");
      // Invoke function
      storage.setRequest(jsonRequest);
      storage.setResponse(jsonResponse);
      try {
        obj = method.invoke(storage);
        // response.put("handle", strHandle);
      } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Exception) throw (Exception) e.getTargetException();
        throw new Exception(e.getTargetException());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      jsonResponse.put("handle", "on_error");
      jsonResponse.put("message", ex.getMessage());
    } finally {
      // response
      servletResponse.setCharacterEncoding("utf-8");
      servletResponse.setContentType("text/plain");
      servletResponse.setHeader("Access-Control-Allow-Origin", "*, ");
      servletResponse.setHeader(
          "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      servletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
      // return response
      PrintWriter out = servletResponse.getWriter();
      out.println(jsonResponse.toString());
      out.flush();
    }
  }