use of javax.naming.Referenceable in project jetty.project by eclipse.
the class NamingContext method bind.
/*------------------------------------------------*/
/**
* Bind a name to an object
*
* @param name Name of the object
* @param obj object to bind
* @exception NamingException if an error occurs
*/
public void bind(Name name, Object obj) throws NamingException {
if (isLocked())
throw new NamingException("This context is immutable");
Name cname = toCanonicalName(name);
if (cname == null)
throw new NamingException("Name is null");
if (cname.size() == 0)
throw new NamingException("Name is empty");
//if no subcontexts, just bind it
if (cname.size() == 1) {
//get the object to be bound
Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
// Check for Referenceable
if (objToBind instanceof Referenceable) {
objToBind = ((Referenceable) objToBind).getReference();
}
//anything else we should be able to bind directly
addBinding(cname, objToBind);
} else {
if (__log.isDebugEnabled())
__log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
//walk down the subcontext hierarchy
//need to ignore trailing empty "" name components
String firstComponent = cname.get(0);
Object ctx = null;
if (firstComponent.equals(""))
ctx = this;
else {
Binding binding = getBinding(firstComponent);
if (binding == null) {
if (_supportDeepBinding) {
Name subname = _parser.parse(firstComponent);
Context subctx = new NamingContext((Hashtable) _env.clone(), firstComponent, this, _parser);
addBinding(subname, subctx);
binding = getBinding(subname);
} else {
throw new NameNotFoundException(firstComponent + " is not bound");
}
}
ctx = binding.getObject();
if (ctx instanceof Reference) {
//deference the object
try {
ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
__log.warn("", e);
throw new NamingException(e.getMessage());
}
}
}
if (ctx instanceof Context) {
((Context) ctx).bind(cname.getSuffix(1), obj);
} else
throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
}
}
use of javax.naming.Referenceable in project jetty.project by eclipse.
the class NamingContext method rebind.
/*------------------------------------------------*/
/**
* Overwrite or create a binding
*
* @param name a <code>Name</code> value
* @param obj an <code>Object</code> value
* @exception NamingException if an error occurs
*/
public void rebind(Name name, Object obj) throws NamingException {
if (isLocked())
throw new NamingException("This context is immutable");
Name cname = toCanonicalName(name);
if (cname == null)
throw new NamingException("Name is null");
if (cname.size() == 0)
throw new NamingException("Name is empty");
//if no subcontexts, just bind it
if (cname.size() == 1) {
//check if it is a Referenceable
Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
if (objToBind instanceof Referenceable) {
objToBind = ((Referenceable) objToBind).getReference();
}
removeBinding(cname);
addBinding(cname, objToBind);
} else {
//walk down the subcontext hierarchy
if (__log.isDebugEnabled())
__log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
String firstComponent = cname.get(0);
Object ctx = null;
if (firstComponent.equals(""))
ctx = this;
else {
Binding binding = getBinding(name.get(0));
if (binding == null)
throw new NameNotFoundException(name.get(0) + " is not bound");
ctx = binding.getObject();
if (ctx instanceof Reference) {
//deference the object
try {
ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
__log.warn("", e);
throw new NamingException(e.getMessage());
}
}
}
if (ctx instanceof Context) {
((Context) ctx).rebind(cname.getSuffix(1), obj);
} else
throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
}
}
use of javax.naming.Referenceable in project wildfly by wildfly.
the class NamingContext method rebind.
/** {@inheritDoc} */
public void rebind(final Name name, Object object) throws NamingException {
check(name, JndiPermission.ACTION_REBIND);
if (namingStore instanceof WritableNamingStore) {
final Name absoluteName = getAbsoluteName(name);
if (object instanceof Referenceable) {
object = ((Referenceable) object).getReference();
}
getWritableNamingStore().rebind(absoluteName, object);
} else {
throw NamingLogger.ROOT_LOGGER.readOnlyNamingContext();
}
}
use of javax.naming.Referenceable in project jdk8u_jdk by JetBrains.
the class DirectoryManager method getObjectInstance.
/**
* Creates an instance of an object for the specified object,
* attributes, and environment.
* <p>
* This method is the same as <tt>NamingManager.getObjectInstance</tt>
* except for the following differences:
*<ul>
*<li>
* It accepts an <tt>Attributes</tt> parameter that contains attributes
* associated with the object. The <tt>DirObjectFactory</tt> might use these
* attributes to save having to look them up from the directory.
*<li>
* The object factories tried must implement either
* <tt>ObjectFactory</tt> or <tt>DirObjectFactory</tt>.
* If it implements <tt>DirObjectFactory</tt>,
* <tt>DirObjectFactory.getObjectInstance()</tt> is used, otherwise,
* <tt>ObjectFactory.getObjectInstance()</tt> is used.
*</ul>
* Service providers that implement the <tt>DirContext</tt> interface
* should use this method, not <tt>NamingManager.getObjectInstance()</tt>.
*<p>
*
* @param refInfo The possibly null object for which to create an object.
* @param name The name of this object relative to <code>nameCtx</code>.
* Specifying a name is optional; if it is
* omitted, <code>name</code> should be null.
* @param nameCtx The context relative to which the <code>name</code>
* parameter is specified. If null, <code>name</code> is
* relative to the default initial context.
* @param environment The possibly null environment to
* be used in the creation of the object factory and the object.
* @param attrs The possibly null attributes associated with refInfo.
* This might not be the complete set of attributes for refInfo;
* you might be able to read more attributes from the directory.
* @return An object created using <code>refInfo</code> and <tt>attrs</tt>; or
* <code>refInfo</code> if an object cannot be created by
* a factory.
* @exception NamingException If a naming exception was encountered
* while attempting to get a URL context, or if one of the
* factories accessed throws a NamingException.
* @exception Exception If one of the factories accessed throws an
* exception, or if an error was encountered while loading
* and instantiating the factory and object classes.
* A factory should only throw an exception if it does not want
* other factories to be used in an attempt to create an object.
* See <tt>DirObjectFactory.getObjectInstance()</tt>.
* @see NamingManager#getURLContext
* @see DirObjectFactory
* @see DirObjectFactory#getObjectInstance
* @since 1.3
*/
public static Object getObjectInstance(Object refInfo, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
ObjectFactory factory;
ObjectFactoryBuilder builder = getObjectFactoryBuilder();
if (builder != null) {
// builder must return non-null factory
factory = builder.createObjectFactory(refInfo, environment);
if (factory instanceof DirObjectFactory) {
return ((DirObjectFactory) factory).getObjectInstance(refInfo, name, nameCtx, environment, attrs);
} else {
return factory.getObjectInstance(refInfo, name, nameCtx, environment);
}
}
// use reference if possible
Reference ref = null;
if (refInfo instanceof Reference) {
ref = (Reference) refInfo;
} else if (refInfo instanceof Referenceable) {
ref = ((Referenceable) (refInfo)).getReference();
}
Object answer;
if (ref != null) {
String f = ref.getFactoryClassName();
if (f != null) {
// if reference identifies a factory, use exclusively
factory = getObjectFactoryFromReference(ref, f);
if (factory instanceof DirObjectFactory) {
return ((DirObjectFactory) factory).getObjectInstance(ref, name, nameCtx, environment, attrs);
} else if (factory != null) {
return factory.getObjectInstance(ref, name, nameCtx, environment);
}
// class path and reference does not contain a URL for it
return refInfo;
} else {
// if reference has no factory, check for addresses
// containing URLs
// ignore name & attrs params; not used in URL factory
answer = processURLAddrs(ref, name, nameCtx, environment);
if (answer != null) {
return answer;
}
}
}
// try using any specified factories
answer = createObjectFromFactories(refInfo, name, nameCtx, environment, attrs);
return (answer != null) ? answer : refInfo;
}
use of javax.naming.Referenceable in project aries by apache.
the class DirObjectFactoryHelper method getObjectInstance.
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
// Step 1
if (obj instanceof Referenceable) {
obj = ((Referenceable) obj).getReference();
}
Object result = obj;
// Step 2
if (obj instanceof Reference) {
Reference ref = (Reference) obj;
String className = ref.getFactoryClassName();
if (className != null) {
// Step 3
result = getObjectInstanceUsingClassName(obj, className, obj, name, nameCtx, environment, attrs);
}
}
// Step 4
if (result == null || result == obj) {
result = getObjectInstanceUsingObjectFactoryBuilders(obj, name, nameCtx, environment, attrs);
}
// Step 5
if (result == null || result == obj) {
if ((obj instanceof Reference && ((Reference) obj).getFactoryClassName() == null) || !(obj instanceof Reference)) {
result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment, attrs);
}
}
// to https://www.osgi.org/bugzilla/show_bug.cgi?id=138
if (result == null || result == obj) {
result = getObjectInstanceViaContextDotObjectFactories(obj, name, nameCtx, environment, attrs);
}
return (result == null) ? obj : result;
}
Aggregations