boot/src/main/java/org/apidesign/html/boot/impl/FnContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Nov 2013 15:15:54 +0100
changeset 323 86aabecda7a3
parent 322 4a93f2679691
child 335 c74fed864c9c
child 358 80702021b851
permissions -rw-r--r--
Introducing Agent-Class to allow java.lang.instrument-like transforms of the @JavaScriptBody annotation
jaroslav@309
     1
/**
jaroslav@309
     2
 * HTML via Java(tm) Language Bindings
jaroslav@309
     3
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@309
     4
 *
jaroslav@309
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@309
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@309
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@309
     8
 *
jaroslav@309
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@309
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@309
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@309
    12
 * GNU General Public License for more details. apidesign.org
jaroslav@309
    13
 * designates this particular file as subject to the
jaroslav@309
    14
 * "Classpath" exception as provided by apidesign.org
jaroslav@309
    15
 * in the License file that accompanied this code.
jaroslav@309
    16
 *
jaroslav@309
    17
 * You should have received a copy of the GNU General Public License
jaroslav@309
    18
 * along with this program. Look for COPYING file in the top folder.
jaroslav@309
    19
 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
jaroslav@309
    20
 */
jaroslav@309
    21
package org.apidesign.html.boot.impl;
jaroslav@309
    22
jaroslav@322
    23
import java.io.Closeable;
jaroslav@322
    24
import java.io.IOException;
jaroslav@322
    25
import java.util.logging.Logger;
jaroslav@309
    26
import org.apidesign.html.boot.spi.Fn;
jaroslav@309
    27
jaroslav@309
    28
/**
jaroslav@309
    29
 *
jaroslav@309
    30
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@309
    31
 */
jaroslav@322
    32
public final class FnContext implements Closeable {
jaroslav@322
    33
    private static final Logger LOG = Logger.getLogger(FnContext.class.getName());
jaroslav@322
    34
jaroslav@323
    35
    private Object prev;
jaroslav@322
    36
    private FnContext(Fn.Presenter p) {
jaroslav@322
    37
        this.prev = p;
jaroslav@309
    38
    }
jaroslav@322
    39
jaroslav@322
    40
    @Override
jaroslav@322
    41
    public void close() throws IOException {
jaroslav@323
    42
        if (prev != this) {
jaroslav@323
    43
            currentPresenter((Fn.Presenter)prev);
jaroslav@323
    44
            prev = this;
jaroslav@322
    45
        }
jaroslav@322
    46
    }
jaroslav@322
    47
/*
jaroslav@322
    48
    @Override
jaroslav@322
    49
    protected void finalize() throws Throwable {
jaroslav@322
    50
        if (prev != null) {
jaroslav@322
    51
            LOG.warning("Unclosed context!");
jaroslav@322
    52
        }
jaroslav@322
    53
    }
jaroslav@322
    54
*/
jaroslav@322
    55
    public static Closeable activate(Fn.Presenter newP) {
jaroslav@322
    56
        return new FnContext(currentPresenter(newP));
jaroslav@322
    57
    }
jaroslav@322
    58
    
jaroslav@309
    59
    
jaroslav@309
    60
    private static final ThreadLocal<Fn.Presenter> CURRENT = new ThreadLocal<Fn.Presenter>();
jaroslav@309
    61
jaroslav@309
    62
    public static Fn.Presenter currentPresenter(Fn.Presenter p) {
jaroslav@309
    63
        Fn.Presenter prev = CURRENT.get();
jaroslav@309
    64
        CURRENT.set(p);
jaroslav@309
    65
        return prev;
jaroslav@309
    66
    }
jaroslav@309
    67
jaroslav@309
    68
    public static Fn.Presenter currentPresenter() {
jaroslav@309
    69
        Fn.Presenter p = CURRENT.get();
jaroslav@309
    70
        if (p == null) {
jaroslav@309
    71
            throw new IllegalStateException("No current WebView context around!");
jaroslav@309
    72
        }
jaroslav@309
    73
        return p;
jaroslav@309
    74
    }
jaroslav@309
    75
    
jaroslav@309
    76
}