public Context createContext()

in naming/src/main/java/org/apache/servicemix/naming/OSGiInitialContextFactoryBuilder.java [104:190]


    public Context createContext (Hashtable<?,?> env) throws NamingException {

        InitialContextFactory icf = null;
        ServiceReference ref = null;

        String icfFactory = (String) env.get(Context.INITIAL_CONTEXT_FACTORY);

        boolean icfFactorySet = true;

        if (icfFactory == null) {
            icfFactory = InitialContextFactory.class.getName();
            icfFactorySet = false;
        }

        try {
            ServiceReference[] refs = context.getAllServiceReferences(icfFactory, null);
            if (refs != null) {
                ref = refs[0];
                icf = (InitialContextFactory) context.getService(ref);
            }
        } catch (InvalidSyntaxException e) {
            NamingException e4 = new NamingException("Argh this should never happen :)");
            e4.initCause(e);

            throw e4;
        }

        if (icf == null) {
            try {
                ServiceReference[] refs = context.getAllServiceReferences(InitialContextFactoryBuilder.class.getName(), null);

                if (refs != null) {
                    for (ServiceReference icfbRef : refs) {
                        InitialContextFactoryBuilder builder = (InitialContextFactoryBuilder) context.getService(icfbRef);

                        icf = builder.createInitialContextFactory(env);

                        context.ungetService(icfbRef);
                        if (icf != null) {
                            break;
                        }
                    }
                }
            } catch (InvalidSyntaxException e) {
                NamingException e4 = new NamingException("Argh this should never happen :)");
                e4.initCause(e);
                throw e4;
            }
        }

        if (icf == null && icfFactorySet) {
            try {
                Class<?> clazz = Class.forName(icfFactory, true, Thread.currentThread().getContextClassLoader());
                icf = (InitialContextFactory) clazz.newInstance();
            } catch (ClassNotFoundException e11) {
                NamingException e = new NamingException("Argh this should never happen :)");
                e.initCause(e11);
                throw e;
            } catch (InstantiationException e2) {
                NamingException e4 = new NamingException("Argh this should never happen :)");
                e4.initCause(e2);
                throw e4;
            } catch (IllegalAccessException e1) {
                NamingException e4 = new NamingException("Argh this should never happen :)");
                e4.initCause(e1);
                throw e4;
            }
        }

        if (icf == null) {
            NamingException e3 = new NoInitialContextException("We could not find an InitialContextFactory to use");

            throw e3;
        }

        Context ctx = icf.getInitialContext(env);

        if (ref != null) context.ungetService(ref);

        if (ctx == null) {
          NamingException e = new NamingException("The ICF returned a null context");
          throw e;
        }

        return ctx;

    }