0

我有一个 Wicket 应用程序,我正在尝试实现可以​​远程更改的单独配置。无论如何,这就是最终目标。

我想要做的是设置 Cayenne 通过手动启动它来工作,而不是使用 web.xml 文件。我尝试了很多不同的东西,但我不确定我是否完全理解上下文是如何应用于所有线程的。

我尝试在我的 Application 类中创建一个 ServerRuntime。我还尝试了每个页面使用的自定义 BasePage 类。我可以通过在 BasePage 上执行以下操作来使其工作,但它是不一致的:

public class BasePage ....
    public static ServerRuntime runtime = new ServerRuntime("cayenne-config.xml");//This is in my BasePage class, but I've also tried this in the Application class

    @Override
    protected void init() { 
BaseContext.bindThreadObjectContext(Application.runtime.getContext());//This is in my BasePage class
    }

就像我说的那样,这种方法有效,但并不一致。我不断收到错误

BaseContext.getThreadObjectContext();

错误是这样的:

java.lang.IllegalStateException: Current thread has no bound ObjectContext.

我似乎找不到太多关于此的信息。我尝试做这样的事情,并使用这些来访问运行时,但没有任何东西能始终如一地工作。

WebUtil.setCayenneRuntime(this.getServletContext(), runtime);
BaseContext.bindThreadObjectContext(WebUtil.getCayenneRuntime(((Application)getApplication()).getServletContext()).getContext());

任何帮助将不胜感激。

4

1 回答 1

0

我想出了一个自己做这件事的方法。

我正在扩展 CayenneFilter 并覆盖 init 方法。

在那里,我几乎复制了他们的确切代码。我将能够在这里检查任何配置并加载正确的 xml 文件。这显然不是解决方案,但绝对是向前迈出的一步,并且可能是我最终这样做的方式。

无论哪种方式,这都是我测试过的工作。

@WebFilter(filterName = "cayenne-config", displayName = "cayenne-config", urlPatterns = {"/*"})
public class TestFilter extends CayenneFilter
{
    @Override
    public void init(FilterConfig config) throws ServletException
    {
        this.checkAlreadyConfigured(config.getServletContext());
        this.servletContext = config.getServletContext();
        WebConfiguration configAdapter = new WebConfiguration(config);
        Collection modules = configAdapter.createModules(new Module[]{new WebModule()});
        ServerRuntime runtime = new ServerRuntime("cayenne-test.xml", (Module[])modules.toArray(new Module[modules.size()]));
        WebUtil.setCayenneRuntime(config.getServletContext(), runtime);
    }
}

我认为不需要注释(我在 web.xml 文件中全部指定),但我想我会把它留在这里,这样你就可以看到它正在改变。

如果我能找到一种方法来更改配置 (FilterConfig) 值(初始化参数),那么我可以将其更改为我想要使用的 xml 文件的名称,而不是覆盖整个方法。我无法弄清楚如何做到这一点,但我会稍后再看。

如果有人有另一个更好的答案,我很想听听。

于 2017-10-12T20:15:43.263 回答