public static InterfaceImpl loadInterface(
      PageContext pc, Page page, PageSource ps, String callPath, boolean isRealPath)
      throws PageException {
    InterfaceImpl rtn = null;
    if (pc.getConfig().debug()) {
      DebugEntryTemplate debugEntry = pc.getDebugger().getEntry(pc, ps);
      pc.addPageSource(ps, true);

      int currTime = pc.getExecutionTime();
      long exeTime = 0;
      long time = System.nanoTime();
      try {
        debugEntry.updateFileLoadTime((int) (System.nanoTime() - time));
        exeTime = System.nanoTime();
        if (page == null) page = ps.loadPage(pc, false);
        rtn = initInterface(pc, page, callPath, isRealPath);
      } finally {
        int diff = ((int) (System.nanoTime() - exeTime) - (pc.getExecutionTime() - currTime));
        pc.setExecutionTime(pc.getExecutionTime() + (int) (System.nanoTime() - time));
        debugEntry.updateExeTime(diff);
        pc.removeLastPageSource(true);
      }
    }
    // no debug
    else {
      pc.addPageSource(ps, true);
      try {
        if (page == null) page = ps.loadPage(pc, false);
        rtn = initInterface(pc, page, callPath, isRealPath);
      } finally {
        pc.removeLastPageSource(true);
      }
    }
    return rtn;
  }
  public static Page loadPage(PageContext pc, PageSource ps, boolean forceReload)
      throws PageException {
    if (pc.getConfig().debug()) {
      DebugEntryTemplate debugEntry = pc.getDebugger().getEntry(pc, ps);
      pc.addPageSource(ps, true);

      int currTime = pc.getExecutionTime();
      long exeTime = 0;
      long time = System.currentTimeMillis();
      try {
        debugEntry.updateFileLoadTime((int) (System.currentTimeMillis() - time));
        exeTime = System.currentTimeMillis();
        return ps.loadPage(pc, forceReload);
      } finally {
        int diff =
            ((int) (System.currentTimeMillis() - exeTime) - (pc.getExecutionTime() - currTime));
        pc.setExecutionTime(pc.getExecutionTime() + (int) (System.currentTimeMillis() - time));
        debugEntry.updateExeTime(diff);
        pc.removeLastPageSource(true);
      }
    }
    // no debug
    pc.addPageSource(ps, true);
    try {
      return ps.loadPage(pc, forceReload);
    } finally {
      pc.removeLastPageSource(true);
    }
  }
Exemple #3
0
 public static String optimizeRelPath(PageContext pc, String relPath) {
   int index;
   String requestURI = relPath, queryString = null;
   if ((index = relPath.indexOf('?')) != -1) {
     requestURI = relPath.substring(0, index);
     queryString = relPath.substring(index + 1);
   }
   PageSource ps = PageSourceImpl.best(((PageContextImpl) pc).getRelativePageSources(requestURI));
   requestURI = ps.getFullRealpath();
   if (queryString != null) return requestURI + "?" + queryString;
   return requestURI;
 }
 public static ComponentImpl loadComponent(
     PageContext pc,
     PageSource ps,
     String callPath,
     boolean isRealPath,
     boolean silent,
     boolean executeConstr)
     throws PageException {
   return _loadComponent(
       pc, toCIPage(ps.loadPage(pc, false), callPath), callPath, isRealPath, false, executeConstr);
 }
Exemple #5
0
  private void removeOldest(PageSourcePool[] pools) {
    PageSourcePool pool = null;
    Object key = null;
    PageSource ps = null;

    long date = -1;
    for (int i = 0; i < pools.length; i++) {
      try {
        Object[] keys = pools[i].keys();
        for (int y = 0; y < keys.length; y++) {
          ps = pools[i].getPageSource(keys[y], false);
          if (date == -1 || date > ps.getLastAccessTime()) {
            pool = pools[i];
            key = keys[y];
            date = ps.getLastAccessTime();
          }
        }
      } catch (Throwable t) {
        pools[i].clear();
      }
    }
    if (pool != null) pool.remove(key);
  }
  static void _onRequest(
      PageContext pc, PageSource requestedPage, PageSource application, RequestListener rl)
      throws PageException {
    ((PageContextImpl) pc).setAppListenerType(AppListenerUtil.TYPE_CLASSIC);

    // on requestStart
    if (application != null) pc.doInclude(new PageSource[] {application}, false);

    if (rl != null) {
      requestedPage = rl.execute(pc, requestedPage);
      if (requestedPage == null) return;
    }

    // request
    try {
      pc.doInclude(new PageSource[] {requestedPage}, false);
    } catch (MissingIncludeException mie) {
      ApplicationContext ac = pc.getApplicationContext();
      boolean rethrow = true;
      if (ac instanceof ClassicApplicationContext) {
        ClassicApplicationContext cfc = (ClassicApplicationContext) ac;
        UDF udf = cfc.getOnMissingTemplate();
        if (udf != null) {
          String targetPage = requestedPage.getFullRealpath();
          rethrow = (!Caster.toBooleanValue(udf.call(pc, new Object[] {targetPage}, true), true));
        }
      }
      if (rethrow) throw mie;
    }

    // on Request End
    if (application != null) {
      PageSource onReqEnd = application.getRealPage("OnRequestEnd.cfm");
      if (onReqEnd.exists()) pc.doInclude(new PageSource[] {onReqEnd}, false);
    }
  }
  private static Object _search(
      PageContext pc,
      PageSource loadingLocation,
      String rawPath,
      Boolean searchLocal,
      Boolean searchRoot,
      boolean executeConstr,
      short returnType,
      boolean isExtendedComponent)
      throws PageException {
    PageSource currPS = pc.getCurrentPageSource();
    Page currP = currPS.loadPage(pc, false);

    // first try for the current dialect
    Object obj =
        _search(
            pc,
            loadingLocation,
            rawPath,
            searchLocal,
            searchRoot,
            executeConstr,
            returnType,
            currP,
            currPS.getDialect(),
            isExtendedComponent);
    // then we try the opposite dialect
    if (obj == null) {
      obj =
          _search(
              pc,
              loadingLocation,
              rawPath,
              searchLocal,
              searchRoot,
              executeConstr,
              returnType,
              currP,
              currPS.getDialect() == CFMLEngine.DIALECT_CFML
                  ? CFMLEngine.DIALECT_LUCEE
                  : CFMLEngine.DIALECT_CFML,
              isExtendedComponent);
    }

    if (obj == null)
      throw new ExpressionException(
          "invalid "
              + toStringType(returnType, currPS.getDialect())
              + " definition, can't find "
              + toStringType(returnType, currPS.getDialect())
              + " ["
              + rawPath
              + "]");
    return obj;
  }
  private static Object _search(
      PageContext pc,
      PageSource loadingLocation,
      String rawPath,
      Boolean searchLocal,
      Boolean searchRoot,
      boolean executeConstr,
      short returnType,
      Page currP,
      int dialect,
      boolean isExtendedComponent)
      throws PageException {
    ConfigImpl config = (ConfigImpl) pc.getConfig();
    boolean doCache = config.useComponentPathCache();
    String sub = null;
    if (returnType != RETURN_TYPE_PAGE && rawPath.indexOf(':') != -1) {
      int d = rawPath.indexOf(':');
      int s = rawPath.indexOf('.');
      if (d > s) {
        sub = rawPath.substring(d + 1);
        rawPath = rawPath.substring(0, d);
      }
    }

    // app-String appName=pc.getApplicationContext().getName();
    rawPath = rawPath.trim().replace('\\', '/');
    String path = (rawPath.indexOf("./") == -1) ? rawPath.replace('.', '/') : rawPath;

    boolean isRealPath = !StringUtil.startsWith(path, '/');
    // PageSource currPS = pc.getCurrentPageSource();
    // Page currP=currPS.loadPage(pc,false);
    PageSource ps = null;
    CIPage page = null;

    // MUSTMUST improve to handle different extensions
    String pathWithCFC =
        path.concat(
            "."
                + (dialect == CFMLEngine.DIALECT_CFML
                    ? Constants.getCFMLComponentExtension()
                    : Constants.getLuceeComponentExtension()));

    // no cache for per application pathes
    Mapping[] acm = pc.getApplicationContext().getComponentMappings();
    if (!ArrayUtil.isEmpty(acm)) {
      Mapping m;
      for (int y = 0; y < acm.length; y++) {
        m = acm[y];
        ps = m.getPageSource(pathWithCFC);
        page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
        if (page != null) {

          return returnType == RETURN_TYPE_PAGE
              ? page
              : load(
                  pc,
                  page,
                  trim(path.replace('/', '.')),
                  sub,
                  isRealPath,
                  returnType,
                  isExtendedComponent,
                  executeConstr);
        }
      }
    }

    if (searchLocal == null)
      searchLocal =
          Caster.toBoolean(rawPath.indexOf('.') == -1 ? true : config.getComponentLocalSearch());
    if (searchRoot == null) searchRoot = Caster.toBoolean(config.getComponentRootSearch());

    // CACHE
    // check local in cache
    String localCacheName = null;
    if (searchLocal && isRealPath) {
      localCacheName = currP.getPageSource().getDisplayPath().replace('\\', '/');
      localCacheName =
          localCacheName.substring(0, localCacheName.lastIndexOf('/') + 1).concat(pathWithCFC);
      if (doCache) {
        page = config.getCachedPage(pc, localCacheName);
        if (page != null)
          return returnType == RETURN_TYPE_PAGE
              ? page
              : load(
                  pc,
                  page,
                  trim(path.replace('/', '.')),
                  sub,
                  isRealPath,
                  returnType,
                  isExtendedComponent,
                  executeConstr);
      }
    }

    // check import cache
    if (doCache && isRealPath) {
      ImportDefintion impDef = config.getComponentDefaultImport();
      ImportDefintion[] impDefs =
          currP == null ? new ImportDefintion[0] : currP.getImportDefintions();
      int i = -1;
      do {

        if (impDef.isWildcard() || impDef.getName().equalsIgnoreCase(path)) {
          page = config.getCachedPage(pc, "import:" + impDef.getPackageAsPath() + pathWithCFC);
          if (page != null)
            return returnType == RETURN_TYPE_PAGE
                ? page
                : load(
                    pc,
                    page,
                    trim(path.replace('/', '.')),
                    sub,
                    isRealPath,
                    returnType,
                    isExtendedComponent,
                    executeConstr);
        }
        impDef = ++i < impDefs.length ? impDefs[i] : null;
      } while (impDef != null);
    }

    if (doCache) {
      // check global in cache
      page = config.getCachedPage(pc, pathWithCFC);
      if (page != null)
        return returnType == RETURN_TYPE_PAGE
            ? page
            : load(
                pc,
                page,
                trim(path.replace('/', '.')),
                sub,
                isRealPath,
                returnType,
                isExtendedComponent,
                executeConstr);
    }

    // SEARCH
    // search from local
    if (searchLocal && isRealPath) {
      // check realpath
      PageSource[] arr = ((PageContextImpl) pc).getRelativePageSources(pathWithCFC);
      page = toCIPage(PageSourceImpl.loadPage(pc, arr, null));
      if (page != null) {
        if (doCache) config.putCachedPageSource(localCacheName, page.getPageSource());
        return returnType == RETURN_TYPE_PAGE
            ? page
            : load(
                pc,
                page,
                trim(path.replace('/', '.')),
                sub,
                isRealPath,
                returnType,
                isExtendedComponent,
                executeConstr);
      }
    }

    // search with imports
    Mapping[] cMappings = config.getComponentMappings();

    if (isRealPath) {

      ImportDefintion impDef = config.getComponentDefaultImport();
      ImportDefintion[] impDefs =
          currP == null ? new ImportDefintion[0] : currP.getImportDefintions();
      PageSource[] arr;

      int i = -1;
      do {
        if (impDef.isWildcard() || impDef.getName().equalsIgnoreCase(path)) {

          // search from local first
          if (searchLocal) {
            arr =
                ((PageContextImpl) pc)
                    .getRelativePageSources(impDef.getPackageAsPath() + pathWithCFC);
            page = toCIPage(PageSourceImpl.loadPage(pc, arr, null));
            if (page != null) {
              if (doCache)
                config.putCachedPageSource(
                    "import:" + impDef.getPackageAsPath() + pathWithCFC, page.getPageSource());
              return returnType == RETURN_TYPE_PAGE
                  ? page
                  : load(
                      pc,
                      page,
                      trim(path.replace('/', '.')),
                      sub,
                      isRealPath,
                      returnType,
                      isExtendedComponent,
                      executeConstr);
            }
          }

          // search mappings and webroot
          page =
              toCIPage(
                  PageSourceImpl.loadPage(
                      pc,
                      ((PageContextImpl) pc)
                          .getPageSources("/" + impDef.getPackageAsPath() + pathWithCFC),
                      null));
          if (page != null) {
            String key = impDef.getPackageAsPath() + pathWithCFC;
            if (doCache && !((MappingImpl) page.getPageSource().getMapping()).isAppMapping())
              config.putCachedPageSource("import:" + key, page.getPageSource());
            return returnType == RETURN_TYPE_PAGE
                ? page
                : load(
                    pc,
                    page,
                    trim(path.replace('/', '.')),
                    sub,
                    isRealPath,
                    returnType,
                    isExtendedComponent,
                    executeConstr);
          }

          // search component mappings
          Mapping m;
          for (int y = 0; y < cMappings.length; y++) {
            m = cMappings[y];
            ps = m.getPageSource(impDef.getPackageAsPath() + pathWithCFC);
            page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
            if (page != null) {
              if (doCache)
                config.putCachedPageSource(
                    "import:" + impDef.getPackageAsPath() + pathWithCFC, page.getPageSource());
              return returnType == RETURN_TYPE_PAGE
                  ? page
                  : load(
                      pc,
                      page,
                      trim(path.replace('/', '.')),
                      sub,
                      isRealPath,
                      returnType,
                      isExtendedComponent,
                      executeConstr);
            }
          }
        }
        impDef = ++i < impDefs.length ? impDefs[i] : null;
      } while (impDef != null);
    }

    String p;
    if (isRealPath) p = '/' + pathWithCFC;
    else p = pathWithCFC;

    // search mappings and webroot
    page = toCIPage(PageSourceImpl.loadPage(pc, ((PageContextImpl) pc).getPageSources(p), null));
    if (page != null) {
      String key = pathWithCFC;
      if (doCache && !((MappingImpl) page.getPageSource().getMapping()).isAppMapping())
        config.putCachedPageSource(key, page.getPageSource());
      return returnType == RETURN_TYPE_PAGE
          ? page
          : load(
              pc,
              page,
              trim(path.replace('/', '.')),
              sub,
              isRealPath,
              returnType,
              isExtendedComponent,
              executeConstr);
    }

    // search component mappings
    Mapping m;
    for (int i = 0; i < cMappings.length; i++) {
      m = cMappings[i];
      ps = m.getPageSource(p);
      page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));

      // recursive search
      if (page == null && config.doComponentDeepSearch() && path.indexOf('/') == -1) {
        ps = MappingUtil.searchMappingRecursive(m, pathWithCFC, true);
        if (ps != null) {
          page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
          if (page != null) doCache = false; // do not cache this, it could be ambigous
        }
      }

      if (page != null) {
        if (doCache) config.putCachedPageSource(pathWithCFC, page.getPageSource());
        return returnType == RETURN_TYPE_PAGE
            ? page
            : load(
                pc,
                page,
                trim(path.replace('/', '.')),
                sub,
                isRealPath,
                returnType,
                isExtendedComponent,
                executeConstr);
      }
    }

    // search relative to active component (this get not cached because the cache get ambigous if we
    // do)
    if (searchLocal && isRealPath) {
      if (loadingLocation == null) {
        Component c = pc.getActiveComponent();
        if (c != null) loadingLocation = c.getPageSource();
      }

      if (loadingLocation != null) {
        ps = loadingLocation.getRealPage(pathWithCFC);
        if (ps != null) {
          page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));

          if (page != null) {
            return returnType == RETURN_TYPE_PAGE
                ? page
                : load(
                    pc,
                    page,
                    trim(path.replace('/', '.')),
                    sub,
                    isRealPath,
                    returnType,
                    isExtendedComponent,
                    executeConstr);
          }
        }
      }
    }
    // translate cfide. to org.lucee.cfml
    if (StringUtil.startsWithIgnoreCase(rawPath, "cfide.")) {
      String rpm = Constants.DEFAULT_PACKAGE + "." + rawPath.substring(6);
      try {
        return _search(
            pc,
            loadingLocation,
            rpm,
            searchLocal,
            searchRoot,
            executeConstr,
            returnType,
            currP,
            dialect,
            false);
      } catch (ExpressionException ee) {
        return null;
        // throw new ExpressionException("invalid "+toStringType(returnType)+" definition, can't
        // find "+rawPath+" or "+rpm);
      }
    }
    return null;
    // throw new ExpressionException("invalid "+toStringType(returnType)+" definition, can't find
    // "+toStringType(returnType)+" ["+rawPath+"]");
  }