Search in sources :

Example 81 with NameNotFoundException

use of javax.naming.NameNotFoundException in project cayenne by apache.

the class JNDIDataSourceFactoryIT method testGetDataSource_NameNotBound.

@Test
public void testGetDataSource_NameNotBound() throws Exception {
    DataNodeDescriptor descriptor = new DataNodeDescriptor();
    descriptor.setParameters("jdbc/TestDS");
    JNDISetup.doSetup();
    JNDIDataSourceFactory factory = new JNDIDataSourceFactory();
    injector.injectMembers(factory);
    try {
        factory.getDataSource(descriptor);
        fail("Didn't throw on unbound name");
    } catch (NameNotFoundException e) {
    // expected
    }
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Test(org.junit.Test)

Example 82 with NameNotFoundException

use of javax.naming.NameNotFoundException in project Payara by payara.

the class ResourceNamingService method publishObject.

public void publishObject(GenericResourceInfo resourceInfo, String jndiName, Object object, boolean rebind) throws NamingException {
    String applicationName = resourceInfo.getApplicationName();
    String moduleName = resourceInfo.getModuleName();
    moduleName = org.glassfish.resourcebase.resources.util.ResourceUtil.getActualModuleName(moduleName);
    if (!isGlobalName(resourceInfo.getName()) && applicationName != null && moduleName != null) {
        Object alreadyBoundObject = null;
        if (rebind) {
            try {
                namingManager.unbindModuleObject(applicationName, moduleName, getModuleScopedName(jndiName));
            } catch (NameNotFoundException e) {
            // ignore
            }
        } else {
            try {
                alreadyBoundObject = namingManager.lookupFromModuleNamespace(applicationName, moduleName, getModuleScopedName(jndiName), null);
            } catch (NameNotFoundException e) {
            // ignore
            }
            if (alreadyBoundObject != null) {
                throw new NamingException("Object already bound for jndiName " + "[ " + jndiName + " ] of  module namespace [" + moduleName + "] " + "of application [" + applicationName + "] ");
            }
        }
        JNDIBinding bindings = new ModuleScopedResourceBinding(getModuleScopedName(jndiName), object);
        List<JNDIBinding> list = new ArrayList<JNDIBinding>();
        list.add(bindings);
        if (_logger.isLoggable(Level.FINEST)) {
            debug("application=" + applicationName + ", module name=" + moduleName + ", binding name=" + jndiName);
        }
        namingManager.bindToModuleNamespace(applicationName, moduleName, list);
    } else if (!isGlobalName(resourceInfo.getName()) && applicationName != null) {
        Object alreadyBoundObject = null;
        if (rebind) {
            try {
                namingManager.unbindAppObject(applicationName, getAppScopedName(jndiName));
            } catch (NameNotFoundException e) {
            // ignore
            }
        } else {
            try {
                alreadyBoundObject = namingManager.lookupFromAppNamespace(applicationName, getAppScopedName(jndiName), null);
            } catch (NameNotFoundException e) {
            // ignore
            }
            if (alreadyBoundObject != null) {
                throw new NamingException("Object already bound for jndiName " + "[ " + jndiName + " ] of application's namespace [" + applicationName + "]");
            }
        }
        JNDIBinding bindings = new ApplicationScopedResourceBinding(getAppScopedName(jndiName), object);
        List<JNDIBinding> list = new ArrayList<JNDIBinding>();
        list.add(bindings);
        if (_logger.isLoggable(Level.FINEST)) {
            debug("application=" + applicationName + ", binding name=" + jndiName);
        }
        namingManager.bindToAppNamespace(applicationName, list);
        bindAppScopedNameForAppclient(object, jndiName, applicationName);
    } else {
        namingManager.publishObject(jndiName, object, true);
    }
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) JNDIBinding(org.glassfish.api.naming.JNDIBinding) ArrayList(java.util.ArrayList) NamingException(javax.naming.NamingException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 83 with NameNotFoundException

use of javax.naming.NameNotFoundException in project geronimo-xbean by apache.

the class Lookup method list.

public void list(String name, InputStream in, PrintStream out) throws IOException {
    try {
        NamingEnumeration names = null;
        try {
            names = ctx.list(name);
        } catch (NameNotFoundException e) {
            out.print("lookup: ");
            out.print(name);
            out.println(": No such object or subcontext");
            return;
        } catch (Throwable e) {
            out.print("lookup: error: ");
            e.printStackTrace(new PrintStream(out));
            return;
        }
        if (names == null) {
            return;
        }
        while (names.hasMore()) {
            NameClassPair entry = (NameClassPair) names.next();
            out.println(entry.getName());
        }
    } catch (Exception e) {
        e.printStackTrace(new PrintStream(out));
    }
}
Also used : PrintStream(java.io.PrintStream) NameNotFoundException(javax.naming.NameNotFoundException) NameClassPair(javax.naming.NameClassPair) NamingEnumeration(javax.naming.NamingEnumeration) IOException(java.io.IOException) NameNotFoundException(javax.naming.NameNotFoundException)

Example 84 with NameNotFoundException

use of javax.naming.NameNotFoundException in project hibernate-orm by hibernate.

the class JBossStandaloneJtaExampleTest method bind.

/**
	 * Helper method that binds the a non serializable object to the JNDI tree.
	 *
	 * @param jndiName  Name under which the object must be bound
	 * @param who       Object to bind in JNDI
	 * @param classType Class type under which should appear the bound object
	 * @param ctx       Naming context under which we bind the object
	 * @throws Exception Thrown if a naming exception occurs during binding
	 */
private void bind(String jndiName, Object who, Class classType, Context ctx) throws Exception {
    // Ah ! This service isn't serializable, so we use a helper class
    NonSerializableFactory.bind(jndiName, who);
    Name n = ctx.getNameParser("").parse(jndiName);
    while (n.size() > 1) {
        String ctxName = n.get(0);
        try {
            ctx = (Context) ctx.lookup(ctxName);
        } catch (NameNotFoundException e) {
            System.out.println("Creating subcontext:" + ctxName);
            ctx = ctx.createSubcontext(ctxName);
        }
        n = n.getSuffix(1);
    }
    // The helper class NonSerializableFactory uses address type nns, we go on to
    // use the helper class to bind the service object in JNDI
    StringRefAddr addr = new StringRefAddr("nns", jndiName);
    Reference ref = new Reference(classType.getName(), addr, NonSerializableFactory.class.getName(), null);
    ctx.rebind(n.get(0), ref);
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NonSerializableFactory(org.jboss.util.naming.NonSerializableFactory) Name(javax.naming.Name)

Example 85 with NameNotFoundException

use of javax.naming.NameNotFoundException in project jetty.project by eclipse.

the class ResourceAnnotationHandler method handleField.

public void handleField(Class<?> clazz, Field field) {
    Resource resource = (Resource) field.getAnnotation(Resource.class);
    if (resource != null) {
        //JavaEE Spec 5.2.3: Field cannot be static
        if (Modifier.isStatic(field.getModifiers())) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + field.getName() + ": cannot be static");
            return;
        }
        //JavaEE Spec 5.2.3: Field cannot be final
        if (Modifier.isFinal(field.getModifiers())) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + field.getName() + ": cannot be final");
            return;
        }
        //work out default name
        String name = clazz.getCanonicalName() + "/" + field.getName();
        //allow @Resource name= to override the field name
        name = (resource.name() != null && !resource.name().trim().equals("") ? resource.name() : name);
        String mappedName = (resource.mappedName() != null && !resource.mappedName().trim().equals("") ? resource.mappedName() : null);
        //get the type of the Field
        Class<?> type = field.getType();
        //Servlet Spec 3.0 p. 76
        //If a descriptor has specified at least 1 injection target for this
        //resource, then it overrides this annotation
        MetaData metaData = _context.getMetaData();
        if (metaData.getOriginDescriptor("resource-ref." + name + ".injection") != null) {
            //it overrides this annotation
            return;
        }
        //No injections for this resource in any descriptors, so we can add it
        //Does the injection already exist?
        InjectionCollection injections = (InjectionCollection) _context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
        if (injections == null) {
            injections = new InjectionCollection();
            _context.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
        }
        Injection injection = injections.getInjection(name, clazz, field);
        if (injection == null) {
            //No injection has been specified, add it
            try {
                boolean bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context, name, mappedName);
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName);
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(null, name, mappedName);
                if (!bound) {
                    //see if there is an env-entry value been bound
                    try {
                        InitialContext ic = new InitialContext();
                        String nameInEnvironment = (mappedName != null ? mappedName : name);
                        ic.lookup("java:comp/env/" + nameInEnvironment);
                        bound = true;
                    } catch (NameNotFoundException e) {
                        bound = false;
                    }
                }
                //Check there is a JNDI entry for this annotation
                if (bound) {
                    LOG.debug("Bound " + (mappedName == null ? name : mappedName) + " as " + name);
                    //   Make the Injection for it if the binding succeeded
                    injection = new Injection();
                    injection.setTarget(clazz, field, type);
                    injection.setJndiName(name);
                    injection.setMappingName(mappedName);
                    injections.add(injection);
                    //TODO - an @Resource is equivalent to a resource-ref, resource-env-ref, message-destination
                    metaData.setOrigin("resource-ref." + name + ".injection", resource, clazz);
                } else if (!isEnvEntryType(type)) {
                    throw new IllegalStateException("No resource at " + (mappedName == null ? name : mappedName));
                }
            } catch (NamingException e) {
                // JavaEE Spec. sec 5.4.1.3
                if (!isEnvEntryType(type))
                    throw new IllegalStateException(e);
            }
        }
    }
}
Also used : InjectionCollection(org.eclipse.jetty.plus.annotation.InjectionCollection) NameNotFoundException(javax.naming.NameNotFoundException) MetaData(org.eclipse.jetty.webapp.MetaData) Resource(javax.annotation.Resource) NamingException(javax.naming.NamingException) Injection(org.eclipse.jetty.plus.annotation.Injection) InitialContext(javax.naming.InitialContext)

Aggregations

NameNotFoundException (javax.naming.NameNotFoundException)168 NamingException (javax.naming.NamingException)81 InitialContext (javax.naming.InitialContext)75 Context (javax.naming.Context)71 Reference (javax.naming.Reference)40 Test (org.junit.Test)39 NotContextException (javax.naming.NotContextException)35 Name (javax.naming.Name)34 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)33 OperationNotSupportedException (javax.naming.OperationNotSupportedException)29 CompositeName (javax.naming.CompositeName)27 Binding (javax.naming.Binding)22 CompoundName (javax.naming.CompoundName)16 LinkRef (javax.naming.LinkRef)16 IOException (java.io.IOException)12 NamingContext (org.eclipse.jetty.jndi.NamingContext)12 ArrayList (java.util.ArrayList)10 InvalidNameException (javax.naming.InvalidNameException)10 Attributes (javax.naming.directory.Attributes)10 HashMap (java.util.HashMap)7