use of javax.naming.NamingException in project tomcat by apache.
the class NamingContextListener method addService.
/**
* Set the specified web service in the naming context.
*
* @param service the web service descriptor
*/
public void addService(ContextService service) {
if (service.getWsdlfile() != null) {
URL wsdlURL = null;
try {
wsdlURL = new URL(service.getWsdlfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
if (wsdlURL == null) {
try {
wsdlURL = ((Context) container).getServletContext().getResource(service.getWsdlfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
}
if (wsdlURL == null) {
try {
wsdlURL = ((Context) container).getServletContext().getResource("/" + service.getWsdlfile());
log.debug(" Changing service ref wsdl file for /" + service.getWsdlfile());
} catch (MalformedURLException e) {
log.error(sm.getString("naming.wsdlFailed", e));
}
}
if (wsdlURL == null)
service.setWsdlfile(null);
else
service.setWsdlfile(wsdlURL.toString());
}
if (service.getJaxrpcmappingfile() != null) {
URL jaxrpcURL = null;
try {
jaxrpcURL = new URL(service.getJaxrpcmappingfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
if (jaxrpcURL == null) {
try {
jaxrpcURL = ((Context) container).getServletContext().getResource(service.getJaxrpcmappingfile());
} catch (MalformedURLException e) {
// Ignore and carry on
}
}
if (jaxrpcURL == null) {
try {
jaxrpcURL = ((Context) container).getServletContext().getResource("/" + service.getJaxrpcmappingfile());
log.debug(" Changing service ref jaxrpc file for /" + service.getJaxrpcmappingfile());
} catch (MalformedURLException e) {
log.error(sm.getString("naming.wsdlFailed", e));
}
}
if (jaxrpcURL == null)
service.setJaxrpcmappingfile(null);
else
service.setJaxrpcmappingfile(jaxrpcURL.toString());
}
// Create a reference to the resource.
Reference ref = new ServiceRef(service.getName(), service.getType(), service.getServiceqname(), service.getWsdlfile(), service.getJaxrpcmappingfile());
// Adding the additional port-component-ref, if any
Iterator<String> portcomponent = service.getServiceendpoints();
while (portcomponent.hasNext()) {
String serviceendpoint = portcomponent.next();
StringRefAddr refAddr = new StringRefAddr(ServiceRef.SERVICEENDPOINTINTERFACE, serviceendpoint);
ref.add(refAddr);
String portlink = service.getPortlink(serviceendpoint);
refAddr = new StringRefAddr(ServiceRef.PORTCOMPONENTLINK, portlink);
ref.add(refAddr);
}
// Adding the additional parameters, if any
Iterator<String> handlers = service.getHandlers();
while (handlers.hasNext()) {
String handlername = handlers.next();
ContextHandler handler = service.getHandler(handlername);
HandlerRef handlerRef = new HandlerRef(handlername, handler.getHandlerclass());
Iterator<String> localParts = handler.getLocalparts();
while (localParts.hasNext()) {
String localPart = localParts.next();
String namespaceURI = handler.getNamespaceuri(localPart);
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_LOCALPART, localPart));
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_NAMESPACE, namespaceURI));
}
Iterator<String> params = handler.listProperties();
while (params.hasNext()) {
String paramName = params.next();
String paramValue = (String) handler.getProperty(paramName);
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_PARAMNAME, paramName));
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_PARAMVALUE, paramValue));
}
for (int i = 0; i < handler.getSoapRolesSize(); i++) {
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_SOAPROLE, handler.getSoapRole(i)));
}
for (int i = 0; i < handler.getPortNamesSize(); i++) {
handlerRef.add(new StringRefAddr(HandlerRef.HANDLER_PORTNAME, handler.getPortName(i)));
}
((ServiceRef) ref).addHandler(handlerRef);
}
try {
if (log.isDebugEnabled()) {
log.debug(" Adding service ref " + service.getName() + " " + ref);
}
createSubcontexts(envCtx, service.getName());
envCtx.bind(service.getName(), ref);
} catch (NamingException e) {
log.error(sm.getString("naming.bindFailed", e));
}
}
use of javax.naming.NamingException in project tomcat by apache.
the class ContextBindings method bindThread.
/**
* Binds a naming context to a thread.
*
* @param obj Object bound to the required naming context
* @param token Security token
*
* @throws NamingException If no naming context is bound to the provided
* object
*/
public static void bindThread(Object obj, Object token) throws NamingException {
if (ContextAccessController.checkSecurityToken(obj, token)) {
Context context = objectBindings.get(obj);
if (context == null) {
throw new NamingException(sm.getString("contextBindings.unknownContext", obj));
}
threadBindings.put(Thread.currentThread(), context);
threadObjectBindings.put(Thread.currentThread(), obj);
}
}
use of javax.naming.NamingException in project tomcat by apache.
the class NamingContext method bind.
/**
* Binds a name to an object. All intermediate contexts and the target
* context (that named by all but terminal atomic component of the name)
* must already exist.
*
* @param name the name to bind; may not be empty
* @param obj the object to bind; possibly null
* @param rebind if true, then perform a rebind (ie, overwrite)
* @exception NameAlreadyBoundException if name is already bound
* @exception javax.naming.directory.InvalidAttributesException if object
* did not supply all mandatory attributes
* @exception NamingException if a naming exception is encountered
*/
protected void bind(Name name, Object obj, boolean rebind) throws NamingException {
if (!checkWritable()) {
return;
}
while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
if (name.isEmpty())
throw new NamingException(sm.getString("namingContext.invalidName"));
NamingEntry entry = bindings.get(name.get(0));
if (name.size() > 1) {
if (entry == null) {
throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0)));
}
if (entry.type == NamingEntry.CONTEXT) {
if (rebind) {
((Context) entry.value).rebind(name.getSuffix(1), obj);
} else {
((Context) entry.value).bind(name.getSuffix(1), obj);
}
} else {
throw new NamingException(sm.getString("namingContext.contextExpected"));
}
} else {
if ((!rebind) && (entry != null)) {
throw new NameAlreadyBoundException(sm.getString("namingContext.alreadyBound", name.get(0)));
} else {
// Getting the type of the object and wrapping it within a new
// NamingEntry
Object toBind = NamingManager.getStateToBind(obj, name, this, env);
if (toBind instanceof Context) {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
} else if (toBind instanceof LinkRef) {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
} else if (toBind instanceof Reference) {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
} else if (toBind instanceof Referenceable) {
toBind = ((Referenceable) toBind).getReference();
entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
} else {
entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
}
bindings.put(name.get(0), entry);
}
}
}
use of javax.naming.NamingException in project zeppelin by apache.
the class LdapGroupRealm method getRoleNamesForUser.
public Set<String> getRoleNamesForUser(String username, LdapContext ldapContext, String userDnTemplate) throws NamingException {
try {
Set<String> roleNames = new LinkedHashSet<>();
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(&(objectClass=groupOfNames)(member=" + userDnTemplate + "))";
Object[] searchArguments = new Object[] { username };
NamingEnumeration<?> answer = ldapContext.search(String.valueOf(ldapContext.getEnvironment().get("ldap.searchBase")), searchFilter, searchArguments, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
if (attrs != null) {
NamingEnumeration<?> ae = attrs.getAll();
while (ae.hasMore()) {
Attribute attr = (Attribute) ae.next();
if (attr.getID().equals("cn")) {
roleNames.add((String) attr.get());
}
}
}
}
return roleNames;
} catch (Exception e) {
LOG.error("Error", e);
}
return new HashSet<>();
}
use of javax.naming.NamingException in project hudson-2.x by hudson.
the class AbstractModelObject method getConfiguredHudsonProperty.
/**
* Checks jndi,environment, hudson environment and system properties for specified key.
* Property is checked in direct order:
* <ol>
* <li>JNDI ({@link InitialContext#lookup(String)})</li>
* <li>Hudson environment ({@link EnvVars#masterEnvVars})</li>
* <li>System properties ({@link System#getProperty(String)})</li>
* </ol>
* @param key - the name of the configured property.
* @return the string value of the configured property, or null if there is no property with that key.
*/
protected String getConfiguredHudsonProperty(String key) {
if (StringUtils.isNotBlank(key)) {
String resultValue;
try {
InitialContext iniCtxt = new InitialContext();
Context env = (Context) iniCtxt.lookup("java:comp/env");
resultValue = StringUtils.trimToNull((String) env.lookup(key));
if (null != resultValue) {
return resultValue;
}
// look at one more place. See http://issues.hudson-ci.org/browse/HUDSON-1314
resultValue = StringUtils.trimToNull((String) iniCtxt.lookup(key));
if (null != resultValue) {
return resultValue;
}
} catch (NamingException e) {
// ignore
}
// look at the env var next
resultValue = StringUtils.trimToNull(EnvVars.masterEnvVars.get(key));
if (null != resultValue) {
return resultValue;
}
// finally check the system property
resultValue = StringUtils.trimToNull(System.getProperty(key));
if (null != resultValue) {
return resultValue;
}
}
return null;
}
Aggregations