Search in sources :

Example 6 with NamingContext

use of org.eclipse.jetty.jndi.NamingContext in project jetty.project by eclipse.

the class EnvConfiguration method configure.

@Override
public void configure(WebAppContext context) throws Exception {
    if (LOG.isDebugEnabled())
        LOG.debug("Created java:comp/env for webapp " + context.getContextPath());
    //look in WEB-INF/jetty-env.xml
    if (jettyEnvXmlUrl == null) {
        //look for a file called WEB-INF/jetty-env.xml
        //and process it if it exists
        org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
        if (web_inf != null && web_inf.isDirectory()) {
            org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
            if (jettyEnv.exists()) {
                jettyEnvXmlUrl = jettyEnv.getURL();
            }
        }
    }
    if (jettyEnvXmlUrl != null) {
        synchronized (localContextRoot.getRoot()) {
            // create list and listener to remember the bindings we make.
            final List<Bound> bindings = new ArrayList<Bound>();
            NamingContext.Listener listener = new NamingContext.Listener() {

                public void unbind(NamingContext ctx, Binding binding) {
                }

                public Binding bind(NamingContext ctx, Binding binding) {
                    bindings.add(new Bound(ctx, binding.getName()));
                    return binding;
                }
            };
            try {
                localContextRoot.getRoot().addListener(listener);
                XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
                WebAppClassLoader.runWithServerClassAccess(() -> {
                    configuration.configure(context);
                    return null;
                });
            } finally {
                localContextRoot.getRoot().removeListener(listener);
                context.setAttribute(JETTY_ENV_BINDINGS, bindings);
            }
        }
    }
    //add java:comp/env entries for any EnvEntries that have been defined so far
    bindEnvEntries(context);
}
Also used : Binding(javax.naming.Binding) ArrayList(java.util.ArrayList) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) NamingContext(org.eclipse.jetty.jndi.NamingContext)

Example 7 with NamingContext

use of org.eclipse.jetty.jndi.NamingContext in project jetty.project by eclipse.

the class EnvConfiguration method destroy.

/**
     * Remove all jndi setup
     * @throws Exception if unable to destroy
     */
@Override
public void destroy(WebAppContext context) throws Exception {
    try {
        //unbind any NamingEntries that were configured in this webapp's name space           
        NamingContext scopeContext = (NamingContext) NamingEntryUtil.getContextForScope(context);
        scopeContext.getParent().destroySubcontext(scopeContext.getName());
    } catch (NameNotFoundException e) {
        LOG.ignore(e);
        LOG.debug("No jndi entries scoped to webapp {}", context);
    } catch (NamingException e) {
        LOG.debug("Error unbinding jndi entries scoped to webapp " + context, e);
    }
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) NamingException(javax.naming.NamingException) NamingContext(org.eclipse.jetty.jndi.NamingContext)

Aggregations

NamingContext (org.eclipse.jetty.jndi.NamingContext)7 Context (javax.naming.Context)5 InitialContext (javax.naming.InitialContext)5 NameNotFoundException (javax.naming.NameNotFoundException)5 NamingException (javax.naming.NamingException)5 Binding (javax.naming.Binding)4 Name (javax.naming.Name)4 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)4 Reference (javax.naming.Reference)4 CompoundName (javax.naming.CompoundName)3 NotContextException (javax.naming.NotContextException)3 OperationNotSupportedException (javax.naming.OperationNotSupportedException)3 URLClassLoader (java.net.URLClassLoader)2 LinkRef (javax.naming.LinkRef)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NameClassPair (javax.naming.NameClassPair)1 NamingEnumeration (javax.naming.NamingEnumeration)1 StringRefAddr (javax.naming.StringRefAddr)1