org.eclipse.jetty.plus.jndi.NamingEntryUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jetty-plus Show documentation
Show all versions of jetty-plus Show documentation
Jetty JavaEE style services
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.plus.jndi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NameNotFoundException;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
public class NamingEntryUtil
{
private static final Logger LOG = Log.getLogger(NamingEntryUtil.class);
/**
* Link a name in a webapp's java:/comp/evn namespace to a pre-existing
* resource. The pre-existing resource can be either in the webapp's
* naming environment, or in the container's naming environment. Webapp's
* environment takes precedence over the server's namespace.
*
* @param scope the scope of the lookup
* @param asName the name to bind as
* @param mappedName the name from the environment to link to asName
* @return true if bind success, false if not bound
* @throws NamingException if unable to bind
*/
public static boolean bindToENC(Object scope, String asName, String mappedName)
throws NamingException
{
if (asName == null || asName.trim().equals(""))
throw new NamingException("No name for NamingEntry");
if (mappedName == null || "".equals(mappedName))
mappedName = asName;
NamingEntry entry = lookupNamingEntry(scope, mappedName);
if (entry == null)
return false;
entry.bindToENC(asName);
return true;
}
/**
* Find a NamingEntry in the given scope.
*
* @param scope the object scope
* @param jndiName the jndi name
* @return the naming entry for the given scope
* @throws NamingException if unable to lookup naming entry
*/
public static NamingEntry lookupNamingEntry(Object scope, String jndiName)
throws NamingException
{
NamingEntry entry = null;
try
{
Name scopeName = getNameForScope(scope);
InitialContext ic = new InitialContext();
NameParser parser = ic.getNameParser("");
Name namingEntryName = makeNamingEntryName(parser, jndiName);
scopeName.addAll(namingEntryName);
entry = (NamingEntry)ic.lookup(scopeName);
}
catch (NameNotFoundException ignored)
{
}
return entry;
}
public static Object lookup(Object scope, String jndiName) throws NamingException
{
Name scopeName = getNameForScope(scope);
InitialContext ic = new InitialContext();
NameParser parser = ic.getNameParser("");
scopeName.addAll(parser.parse(jndiName));
return ic.lookup(scopeName);
}
/**
* Get all NameEntries of a certain type in the given naming
* environment scope (server-wide names or context-specific names)
*
* @param scope the object scope
* @param clazz the type of the entry
* @return all NameEntries of a certain type in the given naming environment scope (server-wide names or context-specific names)
* @throws NamingException if unable to lookup the naming entries
*/
public static List extends T> lookupNamingEntries(Object scope, Class clazz)
throws NamingException
{
try
{
Context scopeContext = getContextForScope(scope);
Context namingEntriesContext = (Context)scopeContext.lookup(NamingEntry.__contextName);
ArrayList