Search in sources :

Example 1 with NamingException

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

the class ResourceAnnotationHandler method handleClass.

public void handleClass(Class<?> clazz) {
    Resource resource = (Resource) clazz.getAnnotation(Resource.class);
    if (resource != null) {
        String name = resource.name();
        String mappedName = resource.mappedName();
        if (name == null || name.trim().equals(""))
            throw new IllegalStateException("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)");
        try {
            if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context, name, mappedName))
                if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName))
                    throw new IllegalStateException("No resource at " + (mappedName == null ? name : mappedName));
        } catch (NamingException e) {
            LOG.warn(e);
        }
    }
}
Also used : Resource(javax.annotation.Resource) NamingException(javax.naming.NamingException)

Example 2 with NamingException

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

the class ResourceAnnotationHandler method handleMethod.

/**
     * Process a Resource annotation on a Method.
     * <p>
     * This will generate a JNDI entry, and an Injection to be
     * processed when an instance of the class is created.
     * 
     * @param clazz the class to process 
     * @param method the method to process
     */
public void handleMethod(Class<?> clazz, Method method) {
    Resource resource = (Resource) method.getAnnotation(Resource.class);
    if (resource != null) {
        //JavaEE Spec 5.2.3: Method cannot be static
        if (Modifier.isStatic(method.getModifiers())) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": cannot be static");
            return;
        }
        // only 1 parameter
        if (!method.getName().startsWith("set")) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": invalid java bean, does not start with 'set'");
            return;
        }
        if (method.getParameterCount() != 1) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": invalid java bean, not single argument to method");
            return;
        }
        if (Void.TYPE != method.getReturnType()) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": invalid java bean, not void");
            return;
        }
        //default name is the javabean property name
        String name = method.getName().substring(3);
        name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
        name = clazz.getCanonicalName() + "/" + name;
        name = (resource.name() != null && !resource.name().trim().equals("") ? resource.name() : name);
        String mappedName = (resource.mappedName() != null && !resource.mappedName().trim().equals("") ? resource.mappedName() : null);
        Class<?> paramType = method.getParameterTypes()[0];
        Class<?> resourceType = resource.type();
        //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;
        }
        //check if an injection has already been setup for this target by web.xml
        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, method, paramType);
        if (injection == null) {
            try {
                //try binding name to environment
                //try the webapp's environment first
                boolean bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context, name, mappedName);
                //try the server's environment
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName);
                //try the jvm's environment
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(null, name, mappedName);
                //NamingEntry, just a value bound in java:comp/env
                if (!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;
                    }
                }
                if (bound) {
                    LOG.debug("Bound " + (mappedName == null ? name : mappedName) + " as " + name);
                    //   Make the Injection for it
                    injection = new Injection();
                    injection.setTarget(clazz, method, paramType, resourceType);
                    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(paramType)) {
                    // JavaEE Spec. sec 5.4.1.3
                    throw new IllegalStateException("No resource at " + (mappedName == null ? name : mappedName));
                }
            } catch (NamingException e) {
                // JavaEE Spec. sec 5.4.1.3
                if (!isEnvEntryType(paramType))
                    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)

Example 3 with NamingException

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

the class DatabaseAdaptor method initialize.

public void initialize() throws Exception {
    if (_datasource != null)
        //already set up
        return;
    if (_jndiName != null) {
        InitialContext ic = new InitialContext();
        _datasource = (DataSource) ic.lookup(_jndiName);
    } else if (_driver != null && _connectionUrl != null) {
        DriverManager.registerDriver(_driver);
    } else if (_driverClassName != null && _connectionUrl != null) {
        Class.forName(_driverClassName);
    } else {
        try {
            InitialContext ic = new InitialContext();
            //last ditch effort
            _datasource = (DataSource) ic.lookup("jdbc/sessions");
        } catch (NamingException e) {
            throw new IllegalStateException("No database configured for sessions");
        }
    }
}
Also used : NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 4 with NamingException

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

the class localContextRoot method unbind.

/**
     *
     *
     * @see javax.naming.Context#unbind(javax.naming.Name)
     */
public void unbind(Name name) throws NamingException {
    synchronized (__root) {
        if (name.size() == 0)
            return;
        if (__root.isLocked())
            throw new NamingException("This context is immutable");
        Name cname = __root.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 unbind it
        if (cname.size() == 1) {
            __root.removeBinding(cname);
        } 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 = __root.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), __root, _env);
                    } catch (NamingException e) {
                        throw e;
                    } catch (Exception e) {
                        __log.warn("", e);
                        throw new NamingException(e.getMessage());
                    }
                }
            }
            if (ctx instanceof Context) {
                ((Context) ctx).unbind(cname.getSuffix(1));
            } else
                throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
        }
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NotContextException(javax.naming.NotContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 5 with NamingException

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

the class localContextRoot method list.

/**
     *
     *
     * @see javax.naming.Context#list(javax.naming.Name)
     */
public NamingEnumeration list(Name name) throws NamingException {
    synchronized (__root) {
        //return __root.list(getSuffix(name));
        Name cname = __root.toCanonicalName(name);
        if (cname == null) {
            List<Binding> empty = Collections.emptyList();
            return new NameEnumeration(empty.iterator());
        }
        if (cname.size() == 0) {
            return new NameEnumeration(__root.getBindings().values().iterator());
        }
        //multipart name
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = __root.getBinding(firstComponent);
            if (binding == null)
                throw new NameNotFoundException();
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                if (__log.isDebugEnabled())
                    __log.debug("Dereferencing Reference for " + name.get(0));
                try {
                    ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            }
        }
        if (!(ctx instanceof Context))
            throw new NotContextException();
        return ((Context) ctx).list(cname.getSuffix(1));
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NotContextException(javax.naming.NotContextException) NameEnumeration(org.eclipse.jetty.jndi.NameEnumeration) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Aggregations

NamingException (javax.naming.NamingException)1243 InitialContext (javax.naming.InitialContext)416 Context (javax.naming.Context)259 IOException (java.io.IOException)163 Attribute (javax.naming.directory.Attribute)111 DirContext (javax.naming.directory.DirContext)100 SearchResult (javax.naming.directory.SearchResult)96 ArrayList (java.util.ArrayList)95 SQLException (java.sql.SQLException)93 NameNotFoundException (javax.naming.NameNotFoundException)88 DataSource (javax.sql.DataSource)84 Attributes (javax.naming.directory.Attributes)83 Properties (java.util.Properties)77 Reference (javax.naming.Reference)77 InitialDirContext (javax.naming.directory.InitialDirContext)77 Test (org.junit.Test)75 Hashtable (java.util.Hashtable)73 SearchControls (javax.naming.directory.SearchControls)71 HashMap (java.util.HashMap)55 LdapContext (javax.naming.ldap.LdapContext)55