Search in sources :

Example 1 with TimeSpan

use of lucee.runtime.type.dt.TimeSpan in project Lucee by lucee.

the class HttpGetWithBody method setTimeout.

/**
 * set the value timeout
 *
 * @param timeout
 *            value to set
 * @throws ExpressionException
 */
public void setTimeout(Object timeout) throws PageException {
    if (timeout instanceof TimeSpan)
        this.timeout = (TimeSpan) timeout;
    else // seconds
    {
        int i = Caster.toIntValue(timeout);
        if (i < 0)
            throw new ApplicationException("invalid value [" + i + "] for attribute timeout, value must be a positive integer greater or equal than 0");
        this.timeout = new TimeSpanImpl(0, 0, 0, i);
    }
}
Also used : TimeSpan(lucee.runtime.type.dt.TimeSpan) ApplicationException(lucee.runtime.exp.ApplicationException) TimeSpanImpl(lucee.runtime.type.dt.TimeSpanImpl)

Example 2 with TimeSpan

use of lucee.runtime.type.dt.TimeSpan in project Lucee by lucee.

the class Lock method doStartTag.

@Override
public int doStartTag() throws PageException {
    if (timeoutInMillis <= 0) {
        TimeSpan remaining = PageContextUtil.remainingTime(pageContext, true);
        this.timeoutInMillis = toInt(remaining.getMillis());
    }
    manager = pageContext.getConfig().getLockManager();
    // check attributes
    if (name != null && scope != SCOPE_NONE) {
        throw new LockException(LockException.OPERATION_CREATE, this.name, "invalid attribute combination", "attribute [name] and [scope] can't be used together");
    }
    if (name == null && scope == SCOPE_NONE) {
        name = "id-" + id;
    }
    String lockType = null;
    if (name == null) {
        String cid = pageContext.getConfig().getIdentification().getId();
        // Session
        if (scope == SCOPE_REQUEST) {
            lockType = "request";
            name = "__request_" + cid + "__" + ((RequestImpl) pageContext.requestScope())._getId();
        } else // Session
        if (scope == SCOPE_SESSION) {
            lockType = "session";
            name = "__session_" + cid + "__" + pageContext.sessionScope()._getId();
        } else // Application
        if (scope == SCOPE_APPLICATION) {
            lockType = "application";
            name = "__application_" + cid + "__" + ((ApplicationImpl) pageContext.applicationScope())._getId();
        } else // Server
        if (scope == SCOPE_SERVER) {
            lockType = "server";
            name = "__server_" + ((ServerImpl) pageContext.serverScope())._getId();
        }
    }
    Struct cflock = new StructImpl();
    cflock.set("succeeded", Boolean.TRUE);
    cflock.set("errortext", "");
    pageContext.setVariable(result, cflock);
    start = System.nanoTime();
    try {
        // this has to be first, otherwise LockTimeoutException has nothing to release
        ((PageContextImpl) pageContext).setActiveLock(new ActiveLock(type, name, timeoutInMillis));
        data = manager.lock(type, name, timeoutInMillis, pageContext.getId());
    } catch (LockTimeoutException e) {
        LockManagerImpl mi = (LockManagerImpl) manager;
        Boolean hasReadLock = mi.isReadLocked(name);
        Boolean hasWriteLock = mi.isWriteLocked(name);
        String msg = LockTimeoutExceptionImpl.createMessage(type, name, lockType, timeoutInMillis, hasReadLock, hasWriteLock);
        _release(pageContext, System.nanoTime() - start);
        name = null;
        cflock.set("succeeded", Boolean.FALSE);
        cflock.set("errortext", msg);
        if (throwontimeout)
            throw new LockException(LockException.OPERATION_TIMEOUT, this.name, msg);
        return SKIP_BODY;
    } catch (InterruptedException e) {
        _release(pageContext, System.nanoTime() - start);
        cflock.set("succeeded", Boolean.FALSE);
        cflock.set("errortext", e.getMessage());
        if (throwontimeout)
            throw Caster.toPageException(e);
        return SKIP_BODY;
    }
    return EVAL_BODY_INCLUDE;
}
Also used : ApplicationImpl(lucee.runtime.type.scope.ApplicationImpl) PageContextImpl(lucee.runtime.PageContextImpl) Struct(lucee.runtime.type.Struct) TimeSpan(lucee.runtime.type.dt.TimeSpan) StructImpl(lucee.runtime.type.StructImpl) ActiveLock(lucee.runtime.debug.ActiveLock) LockException(lucee.runtime.exp.LockException) LockManagerImpl(lucee.runtime.lock.LockManagerImpl) RequestImpl(lucee.runtime.type.scope.RequestImpl) LockTimeoutException(lucee.runtime.lock.LockTimeoutException)

Example 3 with TimeSpan

use of lucee.runtime.type.dt.TimeSpan in project Lucee by lucee.

the class Query method doStartTag.

@Override
public int doStartTag() throws PageException {
    // default datasource
    if (datasource == null && (dbtype == null || !dbtype.equals("query"))) {
        Object obj = pageContext.getApplicationContext().getDefDataSource();
        if (StringUtil.isEmpty(obj)) {
            boolean isCFML = pageContext.getRequestDialect() == CFMLEngine.DIALECT_CFML;
            throw new ApplicationException("attribute [datasource] is required when attribute [dbtype] is not [query] and no default datasource is defined", "you can define a default datasource as attribute [defaultdatasource] of the tag " + (isCFML ? Constants.CFML_APPLICATION_TAG_NAME : Constants.LUCEE_APPLICATION_TAG_NAME) + " or as data member of the " + (isCFML ? Constants.CFML_APPLICATION_EVENT_HANDLER : Constants.LUCEE_APPLICATION_EVENT_HANDLER) + " (this.defaultdatasource=\"mydatasource\";)");
        }
        datasource = obj instanceof DataSource ? (DataSource) obj : pageContext.getDataSource(Caster.toString(obj));
    }
    // timeout
    if (datasource instanceof DataSourceImpl && ((DataSourceImpl) datasource).getAlwaysSetTimeout()) {
        TimeSpan remaining = PageContextUtil.remainingTime(pageContext, true);
        if (this.timeout == null || ((int) this.timeout.getSeconds()) <= 0 || timeout.getSeconds() > remaining.getSeconds()) {
            // not set
            this.timeout = remaining;
        }
    }
    // timezone
    if (timezone != null || (datasource != null && (timezone = datasource.getTimeZone()) != null)) {
        tmpTZ = pageContext.getTimeZone();
        pageContext.setTimeZone(timezone);
    }
    PageContextImpl pci = ((PageContextImpl) pageContext);
    // cache within
    if (StringUtil.isEmpty(cachedWithin)) {
        Object tmp = (pageContext).getCachedWithin(ConfigWeb.CACHEDWITHIN_QUERY);
        if (tmp != null)
            setCachedwithin(tmp);
    }
    // literal timestamp with TSOffset
    if (datasource instanceof DataSourceImpl)
        literalTimestampWithTSOffset = ((DataSourceImpl) datasource).getLiteralTimestampWithTSOffset();
    else
        literalTimestampWithTSOffset = false;
    previousLiteralTimestampWithTSOffset = pci.getTimestampWithTSOffset();
    pci.setTimestampWithTSOffset(literalTimestampWithTSOffset);
    return EVAL_BODY_BUFFERED;
}
Also used : TimeSpan(lucee.runtime.type.dt.TimeSpan) ApplicationException(lucee.runtime.exp.ApplicationException) DataSourceImpl(lucee.runtime.db.DataSourceImpl) PageContextImpl(lucee.runtime.PageContextImpl) DataSource(lucee.runtime.db.DataSource)

Example 4 with TimeSpan

use of lucee.runtime.type.dt.TimeSpan in project Lucee by lucee.

the class XMLConfigWebFactory method loadApplication.

/**
 * @param configServer
 * @param config
 * @param doc
 * @throws IOException
 * @throws PageException
 */
private static void loadApplication(ConfigServerImpl configServer, ConfigImpl config, Document doc, int mode) throws IOException, PageException {
    boolean hasCS = configServer != null;
    boolean hasAccess = ConfigWebUtil.hasAccess(config, SecurityManager.TYPE_SETTING);
    Element application = getChildByName(doc.getDocumentElement(), "application");
    Element scope = getChildByName(doc.getDocumentElement(), "scope");
    // Listener type
    ApplicationListener listener;
    if (mode == ConfigImpl.MODE_STRICT) {
        listener = new ModernAppListener();
    } else {
        listener = ConfigWebUtil.loadListener(getAttr(application, "listener-type"), null);
        if (listener == null) {
            if (hasCS && configServer.getApplicationListener() != null)
                listener = ConfigWebUtil.loadListener(configServer.getApplicationListener().getType(), null);
            if (listener == null)
                listener = new MixedAppListener();
        }
    }
    String[] strTypes = new String[] { "function", "include", "query", "resource", "http", "file", "webservice" };
    int[] types = new int[] { Config.CACHEDWITHIN_FUNCTION, Config.CACHEDWITHIN_INCLUDE, Config.CACHEDWITHIN_QUERY, Config.CACHEDWITHIN_RESOURCE, Config.CACHEDWITHIN_HTTP, Config.CACHEDWITHIN_FILE, Config.CACHEDWITHIN_WEBSERVICE };
    // cachedwithin
    for (int i = 0; i < types.length; i++) {
        String cw = getAttr(application, "cached-within-" + strTypes[i]);
        if (!StringUtil.isEmpty(cw, true))
            config.setCachedWithin(types[i], cw);
        else if (hasCS)
            config.setCachedWithin(types[i], configServer.getCachedWithin(types[i]));
    }
    // Type Checking
    Boolean typeChecking = Caster.toBoolean(getAttr(application, "type-checking"), null);
    if (typeChecking != null)
        config.setTypeChecking(typeChecking.booleanValue());
    else if (hasCS)
        config.setTypeChecking(configServer.getTypeChecking());
    // Listener Mode
    int listenerMode = ConfigWebUtil.toListenerMode(getAttr(application, "listener-mode"), -1);
    if (listenerMode == -1) {
        if (hasCS)
            listenerMode = configServer.getApplicationListener() == null ? ApplicationListener.MODE_CURRENT2ROOT : configServer.getApplicationListener().getMode();
        else
            listenerMode = ApplicationListener.MODE_CURRENT2ROOT;
    }
    listener.setMode(listenerMode);
    config.setApplicationListener(listener);
    // Req Timeout URL
    if (mode == ConfigImpl.MODE_STRICT) {
        config.setAllowURLRequestTimeout(false);
    } else {
        String allowURLReqTimeout = getAttr(application, "allow-url-requesttimeout");
        if (hasAccess && !StringUtil.isEmpty(allowURLReqTimeout)) {
            config.setAllowURLRequestTimeout(Caster.toBooleanValue(allowURLReqTimeout, false));
        } else if (hasCS)
            config.setAllowURLRequestTimeout(configServer.isAllowURLRequestTimeout());
    }
    // Req Timeout
    TimeSpan ts = null;
    if (hasAccess) {
        String reqTimeoutApplication = getAttr(application, "requesttimeout");
        // deprecated
        String reqTimeoutScope = getAttr(scope, "requesttimeout");
        if (!StringUtil.isEmpty(reqTimeoutApplication))
            ts = Caster.toTimespan(reqTimeoutApplication);
        if (ts == null && !StringUtil.isEmpty(reqTimeoutScope))
            ts = Caster.toTimespan(reqTimeoutScope);
    }
    if (ts != null && ts.getMillis() > 0)
        config.setRequestTimeout(ts);
    else if (hasCS)
        config.setRequestTimeout(configServer.getRequestTimeout());
    // script-protect
    String strScriptProtect = getAttr(application, "script-protect");
    if (hasAccess && !StringUtil.isEmpty(strScriptProtect)) {
        // print.err("sp:"+strScriptProtect);
        config.setScriptProtect(AppListenerUtil.translateScriptProtect(strScriptProtect));
    } else if (hasCS)
        config.setScriptProtect(configServer.getScriptProtect());
    // classic-date-parsing
    if (config instanceof ConfigServer) {
        if (mode == ConfigImpl.MODE_STRICT) {
            DateCaster.classicStyle = true;
        } else {
            String strClassicDateParsing = getAttr(application, "classic-date-parsing");
            if (!StringUtil.isEmpty(strClassicDateParsing)) {
                DateCaster.classicStyle = Caster.toBooleanValue(strClassicDateParsing, false);
            }
        }
    }
    // Cache
    Resource configDir = config.getConfigDir();
    String strCacheDirectory = application.getAttribute("cache-directory");
    if (hasAccess && !StringUtil.isEmpty(strCacheDirectory)) {
        strCacheDirectory = ConfigWebUtil.translateOldPath(strCacheDirectory);
        Resource res = ConfigWebUtil.getFile(configDir, strCacheDirectory, "cache", configDir, FileUtil.TYPE_DIR, config);
        config.setCacheDir(res);
    } else {
        config.setCacheDir(configDir.getRealResource("cache"));
    }
    String strMax = getAttr(application, "cache-directory-max-size");
    if (hasAccess && !StringUtil.isEmpty(strMax)) {
        config.setCacheDirSize(ByteSizeParser.parseByteSizeDefinition(strMax, config.getCacheDirSize()));
    } else if (hasCS)
        config.setCacheDirSize(configServer.getCacheDirSize());
    // admin sync
    ClassDefinition asc = getClassDefinition(application, "admin-sync-", config.getIdentification());
    if (!asc.hasClass())
        asc = getClassDefinition(application, "admin-synchronisation-", config.getIdentification());
    if (hasAccess && asc.hasClass()) {
        try {
            Class clazz = asc.getClazz();
            if (!Reflector.isInstaneOf(clazz, AdminSync.class))
                throw new ApplicationException("class [" + clazz.getName() + "] does not implement interface [" + AdminSync.class.getName() + "]");
            config.setAdminSyncClass(clazz);
        } catch (Exception e) {
            SystemOut.printDate(e);
        }
    } else if (hasCS)
        config.setAdminSyncClass(configServer.getAdminSyncClass());
}
Also used : ModernAppListener(lucee.runtime.listener.ModernAppListener) Element(org.w3c.dom.Element) Resource(lucee.commons.io.res.Resource) ClassDefinition(lucee.runtime.db.ClassDefinition) lucee.aprint(lucee.aprint) FunctionLibException(lucee.transformer.library.function.FunctionLibException) PageException(lucee.runtime.exp.PageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityException(lucee.runtime.exp.SecurityException) TagLibException(lucee.transformer.library.tag.TagLibException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) SAXException(org.xml.sax.SAXException) ClassException(lucee.commons.lang.ClassException) MalformedURLException(java.net.MalformedURLException) ExpressionException(lucee.runtime.exp.ExpressionException) ApplicationException(lucee.runtime.exp.ApplicationException) TimeSpan(lucee.runtime.type.dt.TimeSpan) ApplicationException(lucee.runtime.exp.ApplicationException) ApplicationListener(lucee.runtime.listener.ApplicationListener) CFXTagClass(lucee.runtime.cfx.customtag.CFXTagClass) CPPCFXTagClass(lucee.runtime.cfx.customtag.CPPCFXTagClass) JavaCFXTagClass(lucee.runtime.cfx.customtag.JavaCFXTagClass) MixedAppListener(lucee.runtime.listener.MixedAppListener)

Example 5 with TimeSpan

use of lucee.runtime.type.dt.TimeSpan in project Lucee by lucee.

the class TimespanCacheHandler method get.

@Override
public CacheItem get(PageContext pc, String cacheId, Object cachePolicy) throws PageException {
    Date cachedAfter;
    if (Decision.isDate(cachePolicy, false) && !(cachePolicy instanceof TimeSpan)) {
        // cachedAfter was passed
        cachedAfter = Caster.toDate(cachePolicy, null);
    } else {
        long cachedWithinMillis = Caster.toTimeSpan(cachePolicy).getMillis();
        if (cachedWithinMillis == 0) {
            this.remove(pc, cacheId);
            return null;
        }
        cachedAfter = new Date(System.currentTimeMillis() - cachedWithinMillis);
    }
    CacheItem cacheItem = this.get(pc, cacheId);
    if (cacheItem instanceof QueryResultCacheItem) {
        if (((QueryResultCacheItem) cacheItem).isCachedAfter(cachedAfter))
            return cacheItem;
        // cacheItem is from before cachedAfter, discard it so that it can be refreshed
        return null;
    }
    return cacheItem;
}
Also used : TimeSpan(lucee.runtime.type.dt.TimeSpan) QueryResultCacheItem(lucee.runtime.cache.tag.query.QueryResultCacheItem) CacheItem(lucee.runtime.cache.tag.CacheItem) Date(java.util.Date) QueryResultCacheItem(lucee.runtime.cache.tag.query.QueryResultCacheItem)

Aggregations

TimeSpan (lucee.runtime.type.dt.TimeSpan)14 ApplicationException (lucee.runtime.exp.ApplicationException)4 Date (java.util.Date)3 ApplicationContext (lucee.runtime.listener.ApplicationContext)3 Struct (lucee.runtime.type.Struct)3 List (java.util.List)2 Resource (lucee.commons.io.res.Resource)2 PageContextImpl (lucee.runtime.PageContextImpl)2 PageException (lucee.runtime.exp.PageException)2 ApplicationContextSupport (lucee.runtime.listener.ApplicationContextSupport)2 StructImpl (lucee.runtime.type.StructImpl)2 DateTime (lucee.runtime.type.dt.DateTime)2 TimeSpanImpl (lucee.runtime.type.dt.TimeSpanImpl)2 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1