Example usage for org.springframework.ide.eclipse.core.java JdtUtils getClassLoader

List of usage examples for org.springframework.ide.eclipse.core.java JdtUtils getClassLoader

Introduction

In this page you can find the example usage for org.springframework.ide.eclipse.core.java JdtUtils getClassLoader.

Prototype

public static ClassLoader getClassLoader(IProject project, ClassLoader parentClassLoader) 

Source Link

Document

Create a ClassLoader from the class path configuration of the given project.

Usage

From source file:org.eclipse.virgo.ide.runtime.internal.core.ServerPublishOperation.java

/**
 * Checks if the given <code>file</code> is a root node that is a known Spring namespace.
 *//*from  www  . j  av a  2  s  .c  o  m*/
private boolean checkIfSpringConfigurationFile(IFile file) {
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getExistingModelForRead(file);
        if (model == null) {
            model = StructuredModelManager.getModelManager().getModelForRead(file);
        }
        if (model != null) {
            IDOMDocument document = ((DOMModelImpl) model).getDocument();
            if (document != null && document.getDocumentElement() != null) {
                String namespaceUri = document.getDocumentElement().getNamespaceURI();
                if (NamespaceUtils.DEFAULT_NAMESPACE_URI.equals(namespaceUri)
                        || new DelegatingNamespaceHandlerResolver(
                                JdtUtils.getClassLoader(file.getProject(), null), null)
                                        .resolve(namespaceUri) != null) {
                    return false;
                }
            }
        }
    } catch (Exception e) {
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
        model = null;
    }
    return true;
}

From source file:org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig.java

@Override
protected void readConfig() {
    if (!isModelPopulated) {

        w.lock();/*ww w .  j  av a2s  .c o m*/
        if (this.isModelPopulated) {
            w.unlock();
            return;
        }

        try {
            if (this.configClass == null) {
                return;
            }

            IBeansProject beansProject = BeansModelUtils.getParentOfClass(this, IBeansProject.class);
            if (beansProject == null) {
                return;
            }

            final ClassLoader cl = JdtUtils.getClassLoader(beansProject.getProject(),
                    ApplicationContext.class.getClassLoader());

            if (cl.getResource(this.configClass.getFullyQualifiedName().replace('.', '/') + ".class") == null) {
                return;
            }

            Callable<Integer> loadBeanDefinitionOperation = new Callable<Integer>() {
                public Integer call() throws Exception {
                    // Obtain thread context classloader and override with the project classloader
                    ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
                    Thread.currentThread().setContextClassLoader(cl);

                    // Create special ReaderEventListener that essentially just passes through component definitions
                    ReaderEventListener eventListener = new BeansConfigPostProcessorReaderEventListener();
                    problemReporter = new BeansConfigProblemReporter();
                    beanNameGenerator = new UniqueBeanNameGenerator(BeansJavaConfig.this);
                    registry = new ScannedGenericBeanDefinitionSuppressingBeanDefinitionRegistry();

                    try {
                        registerAnnotationProcessors(eventListener);
                        registerBean(eventListener, cl);

                        IBeansConfigPostProcessor[] postProcessors = BeansConfigPostProcessorFactory
                                .createPostProcessor(ConfigurationClassPostProcessor.class.getName());
                        for (IBeansConfigPostProcessor postProcessor : postProcessors) {
                            executePostProcessor(postProcessor, eventListener);
                        }
                    } finally {
                        // Reset the context classloader
                        Thread.currentThread().setContextClassLoader(threadClassLoader);
                        LogFactory.release(cl); //Otherwise permgen leak?
                    }
                    return 0;
                }
            };

            FutureTask<Integer> task = new FutureTask<Integer>(loadBeanDefinitionOperation);
            BeansCorePlugin.getExecutorService().submit(task);
            task.get(BeansCorePlugin.getDefault().getPreferenceStore()
                    .getInt(BeansCorePlugin.TIMEOUT_CONFIG_LOADING_PREFERENCE_ID), TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            problems.add(new ValidationProblem(IMarker.SEVERITY_ERROR,
                    "Loading of configuration '" + this.configClass.getFullyQualifiedName()
                            + "' took more than "
                            + BeansCorePlugin.getDefault().getPreferenceStore()
                                    .getInt(BeansCorePlugin.TIMEOUT_CONFIG_LOADING_PREFERENCE_ID)
                            + "sec",
                    file, 1));
        } catch (Exception e) {
            problems.add(new ValidationProblem(IMarker.SEVERITY_ERROR,
                    String.format("Error occured processing Java config '%s'. See Error Log for more details",
                            e.getCause().getMessage()),
                    getElementResource()));
            BeansCorePlugin.log(new Status(IStatus.INFO, BeansCorePlugin.PLUGIN_ID,
                    String.format("Error occured processing '%s'", this.configClass.getFullyQualifiedName()),
                    e.getCause()));
        } finally {
            // Prepare the internal cache of all children for faster access
            List<ISourceModelElement> allChildren = new ArrayList<ISourceModelElement>(imports);
            allChildren.addAll(aliases.values());
            allChildren.addAll(components);
            allChildren.addAll(beans.values());
            Collections.sort(allChildren, new Comparator<ISourceModelElement>() {
                public int compare(ISourceModelElement element1, ISourceModelElement element2) {
                    return element1.getElementStartLine() - element2.getElementStartLine();
                }
            });
            this.children = allChildren.toArray(new IModelElement[allChildren.size()]);

            this.isModelPopulated = true;
            w.unlock();
        }

    }
}