store.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 627 行 · 第 1/2 页

JAVA
627
字号
      for (Iterator<Api> i = _api.values().iterator(); i.hasNext(); ) {        Api api = i.next();        if (api.isLocal()) {          Path index = api.getLocationPath().lookup("index.html");          Environment.addDependency(index);        }      }    } catch (Exception ex) {      log.log(Level.FINE,"resin-javadoc init error",ex);      _initError = ex;    }  }  public Exception getInitError()  {    return _initError;  }  /**   * True if the timestamp file indicates that the database needs updating to   * match the index file(s).   */   public boolean isNeedUpdate()  {    try {      ReadStream rs = _timestampFile.openRead();      String lms = rs.readLine();      long crc = Long.parseLong(lms);      lms = rs.readLine();      long tsfiles = Long.parseLong(lms);      rs.close();      if (crc != _crc64) {        log.info(L.l("javadoc index needs update - config has changed"));        if (log.isLoggable(Level.FINE))          log.finer(L.l("timestamp file {0} lastmodified {1}",_timestampFile,new Long(crc)));      }      else if (tsfiles != getTimestampForLocalFiles()) {        log.info(L.l("javadoc index needs update - a local api has changed"));      }      else {        return false;      }    } catch (Exception ex) {      log.info(L.l("javadoc index needs update - timestamp file could not be read"));      if (log.isLoggable(Level.FINE))        log.finer(L.l("timestamp file {0} {1}",_timestampFile,ex));    }    return true;  }  /**   * Clear the database tables (if they exist) and create new    * contents based on the passed javadoc index file(s).   */   public void createFromIndex()    throws SQLException, IOException  {    long st = System.currentTimeMillis();    int cnt = 0;    log.info(L.l("creating javadoc index db entries"));    Connection conn = null;    try {      conn = _pool.getConnection();      StoreWriter sw = new StoreWriter(this);      sw.clear(conn);      for (Iterator<Api> i = _api.values().iterator(); i.hasNext(); ) {        Api api = i.next();        cnt += sw.add(conn,api);      }    } finally {      try {        if (conn != null)          conn.close();      } catch (SQLException e) {        log.warning(L.l("conn.close() error",e));      }    }    updateTimestampFile();    long tm = System.currentTimeMillis() - st;    log.info(L.l("created {0} javadoc index entries in {1}sec", new Integer(cnt), new Double( (double) tm / 1000.0)));  }  private long getTimestampForLocalFiles()  {    long ts = 0L;    for (Iterator<Api> i = _api.values().iterator(); i.hasNext(); ) {      Api api = i.next();      if (api.isLocal()) {        ts += api.getLocationPath().lookup("index.html").getLastModified();      }    }    return ts;  }  private void updateTimestampFile()    throws IOException  {    _timestampFile.getParent().mkdirs();    WriteStream ws = _timestampFile.openWrite();    ws.println(_crc64);    ws.println(getTimestampForLocalFiles());    ws.close();  }  /**   * Look for a javadoc item.  The string can begin with `package', `class',   * `method', `var', or `any'.  If it begins with none of them, `any' is   * assumed.  The rest of the string is used as the name to search for.   * If the name conatins the `*' character, the `*' will match any    * characters.   *   * @returns a list of the results, a list with size 0 for no results    */  public LinkedList<JavadocItem> query(String query, int offset, int limit)    throws SQLException   {    LinkedList<JavadocItem> results = new LinkedList<JavadocItem>();    try {      int type = -1;      int i = query.indexOf(' ');      if (i > -1) {        String t = query.substring(0,i);        if (t.equals("package"))          type = JavadocItem.PACKAGE;        else if (t.equals("class"))          type = JavadocItem.CLASS;        else if (t.equals("method"))          type = JavadocItem.METHOD;        else if (t.equals("var"))          type = JavadocItem.VARIABLE;        else if (t.equals("any"))          type = JavadocItem.ANY;        if (type > -1) {          while (i < query.length() && Character.isWhitespace(query.charAt(i)))            i++;          if (i >= query.length())            return results;          else            query = query.substring(i);        }      }      // handle the special case of ClassName.method      if (query.length() > 0 && Character.isUpperCase(query.charAt(0))) {        int di = query.indexOf('.');        if (di > -1 ) {          CharBuffer cb = CharBuffer.allocate();          cb.append('*');          cb.append(query);          cb.append('*');          query = cb.close();        }      }      if (type < 0)        type = JavadocItem.ANY;      Connection conn = null;      try {        conn = _pool.getConnection();        Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);        // construct the query or queries        boolean like = query.indexOf("*") > -1 ? true : false;         String nameToUse = (query.indexOf(".") > -1) ? "item.fullname" : "item.name";        String safequery = escapeSQL(query);        CharBuffer cb = CharBuffer.allocate();        cb.append("SELECT item.name,item.fullname,item.typ,item.anchor,item.description,file.api,file.path FROM ");          cb.append(getTableNameItem());          cb.append(" AS item, ");          cb.append(getTableNameFile());          cb.append(" AS file WHERE file.id = item.file_id");        if (type != JavadocItem.ANY) {          cb.append(" AND item.typ = ");          cb.append(type);        }        String q_select = cb.close();        if (like) {           cb = CharBuffer.allocate();          cb.append(q_select);          cb.append(" AND ");          cb.append(nameToUse);          cb.append(" LIKE '");          cb.append(safequery);          cb.append("'");          results = doQuery(safequery,stmt,cb,offset,limit,results);        }        else {          cb = CharBuffer.allocate();          cb.append(q_select);          cb.append(" AND ");          cb.append(nameToUse);          cb.append(" LIKE '");          cb.append(safequery);          cb.append("%'");          results = doQuery(safequery,stmt,cb,offset,limit,results);        }        // see if the first one is exact        if (results.size() > 0) {          JavadocItem item = results.getFirst();          if (item.getName().equals(query) || item.getFullName().equals(query)) {            item.setExact(true);          }        }        stmt.close();      } finally {        if (conn != null)          conn.close();      }    }     catch (SQLException ex) {      if (log.isLoggable(Level.CONFIG))        log.log(Level.CONFIG,L.l("error with query `{0}': {1}",query,ex.getMessage()));      throw ex;    }    return results;  }  private String escapeSQL(String q)  {    CharBuffer cb = CharBuffer.allocate();    for (int i = 0; i < q.length(); i++) {      char c = q.charAt(i);      switch (c) {        case '\'':          cb.append("\\'");          break;        case '\"':          cb.append("\\\"");          break;        case '*':          cb.append("%");          break;        case '%':          cb.append('\\');        default:          cb.append(c);      }    }    return cb.close();  }  private LinkedList<JavadocItem> doQuery(String query, Statement stmt, CharBuffer cb, int offset, int limit, LinkedList<JavadocItem> results)    throws SQLException  {    if (limit <= 0) {      return results;    }    cb.append(" ORDER BY if(item.name = '");    cb.append(query);    cb.append("' OR item.fullname = '");    cb.append(query);    cb.append("',0,1) ");    cb.append(",item.typ");    cb.append(", LENGTH(item.name)");    cb.append(" LIMIT ");    cb.append(limit);    if (offset > 0) {      cb.append(" OFFSET ");      cb.append(offset);    }    String q = cb.close();    log.finest(L.l("query is [{0}]",q));    ResultSet rs = stmt.executeQuery(q);    try {      while (rs.next()) {        results.add(new JavadocItem(              rs.getString(1),                     // name              rs.getString(2),                     // fullname              rs.getInt(3),                        // type              rs.getString(4),                     // anchor              rs.getString(5),                      // description              new JavadocFile(_api.get(rs.getString(6)), // api                               rs.getString(7))          // path              ));      }    } finally {      rs.close();    }    return results;  }}

⌨️ 快捷键说明

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