Java Code Examples for com.intellij.openapi.util.ClassLoaderUtil

The following are top voted examples for showing how to use com.intellij.openapi.util.ClassLoaderUtil. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: vso-intellij   File: TfsRequestManager.java   View source code 6 votes vote down vote up
private static <T> T executeRequestImpl(final URI serverUri,
                                        final Ref<Credentials> credentialsRef,
                                        final Request<T> request,
                                        final ProgressIndicator pi)
  throws Exception {
  return ClassLoaderUtil.runWithClassLoader(TfsRequestManager.class.getClassLoader(), new ThrowableComputable<T, Exception>() {
    @Override
    public T compute() throws Exception {
      Credentials credentials = credentialsRef.get();
      boolean needsAuthentication =
        credentials == null ||
        request.retrieveAuthorizedCredentials() && (credentials.getUserName().length() == 0 || credentials.getDomain().length() == 0);
      if (needsAuthentication) {
        TfsServerConnectionHelper.ServerDescriptor descriptor =
          TfsServerConnectionHelper.connect(serverUri, credentialsRef.get(), true, pi);
        credentialsRef.set(descriptor.authorizedCredentials);
      }
      return request.execute(credentialsRef.get(), serverUri, pi);
    }
  });
}
 
Example 2
Project: vso-intellij   File: MementoTest.java   View source code 6 votes vote down vote up
private static <T> T runWithPatchedClassloader(ThrowableComputable<T, Exception> computable) throws Exception {
  return ClassLoaderUtil.runWithClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()) {
    @Override
    public InputStream getResourceAsStream(String name) {
      if (XML_ENTITIES_URL.equals(name)) {
        String resource = XML_ENTITIES_CONTENT;
        try {
          return new ByteArrayInputStream(resource.getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException e) {
          // should not happen, fallback
        }
      }
      return super.getResourceAsStream(name);
    }
  }, computable);
}
 
Example 3
Project: vso-intellij   File: WebServiceHelper.java   View source code 5 votes vote down vote up
public static ConfigurationContext getStubConfigurationContext() {
  return ClassLoaderUtil.runWithClassLoader(TFSVcs.class.getClassLoader(), new Computable<ConfigurationContext>() {
    public ConfigurationContext compute() {
      try {
        ConfigurationContext configContext = ConfigurationContextFactory.createDefaultConfigurationContext();
        configContext.getAxisConfiguration().addMessageBuilder(SOAP_BUILDER_KEY, new CustomSOAPBuilder());
        return configContext;
      }
      catch (Exception e) {
        LOG.error("Axis2 configuration error", e);
        return null;
      }
    }
  });
}
 
Example 4
Project: consulo   File: Jsr223IdeScriptEngineManagerImpl.java   View source code 5 votes vote down vote up
@Nullable
@Override
public IdeScriptEngine getEngineForLanguage(@Nonnull final String language, @Nullable ClassLoader loader) {
  ClassLoader l = ObjectUtils.notNull(loader, AllPluginsLoader.INSTANCE);
  return ClassLoaderUtil.runWithClassLoader(l, new Computable<IdeScriptEngine>() {
    @Override
    public IdeScriptEngine compute() {
      return createIdeScriptEngine(getScriptEngineManager().getEngineByName(language));
    }
  });
}
 
Example 5
Project: consulo   File: Jsr223IdeScriptEngineManagerImpl.java   View source code 5 votes vote down vote up
@Nullable
@Override
public IdeScriptEngine getEngineForFileExtension(@Nonnull final String extension, @Nullable ClassLoader loader) {
  ClassLoader l = ObjectUtils.notNull(loader, AllPluginsLoader.INSTANCE);
  return ClassLoaderUtil.runWithClassLoader(l, new Computable<IdeScriptEngine>() {
    @Override
    public IdeScriptEngine compute() {
      return createIdeScriptEngine(getScriptEngineManager().getEngineByExtension(extension));
    }
  });
}
 
Example 6
Project: consulo   File: Jsr223IdeScriptEngineManagerImpl.java   View source code 5 votes vote down vote up
@Override
public Object eval(@Nonnull final String script) throws IdeScriptException {
  return ClassLoaderUtil.runWithClassLoader(myLoader, new ThrowableComputable<Object, IdeScriptException>() {
    @Override
    public Object compute() throws IdeScriptException {
      try {
        return myEngine.eval(script);
      }
      catch (Throwable e) {
        throw new IdeScriptException(e);
      }
    }
  });
}
 
Example 7
Project: vso-intellij   File: TfsUtil.java   View source code 4 votes vote down vote up
public static <T, E extends Throwable> T forcePluginClassLoader(@NotNull ThrowableComputable<T, E> computable) throws E {
  return ClassLoaderUtil.runWithClassLoader(null, computable);
}
 
Example 8
Project: vso-intellij   File: TfsUtil.java   View source code 4 votes vote down vote up
public static <E extends Throwable> void forcePluginClassLoader(@NotNull ThrowableRunnable<E> runnable) throws E {
  ClassLoaderUtil.runWithClassLoader(null, runnable);
}
 
Example 9
Project: consulo   File: FileTemplateUtil.java   View source code 4 votes vote down vote up
public static PsiElement createFromTemplate(@Nonnull final FileTemplate template,
                                            @NonNls @Nullable String fileName,
                                            @Nullable Map<String, Object> additionalProperties,
                                            @Nonnull final PsiDirectory directory,
                                            @Nullable ClassLoader classLoader) throws Exception {
  final Project project = directory.getProject();

  Map<String, Object> properties = new THashMap<>();
  FileTemplateManager.getInstance(project).fillDefaultVariables(properties);

  if(additionalProperties != null) {
    properties.putAll(additionalProperties);
  }

  FileTemplateManager.getInstance(project).addRecentName(template.getName());
  fillDefaultProperties(properties, directory);

  final CreateFromTemplateHandler handler = findHandler(template);
  if (fileName != null && properties.get(FileTemplate.ATTRIBUTE_NAME) == null) {
    properties.put(FileTemplate.ATTRIBUTE_NAME, fileName);
  }
  else if (fileName == null && handler.isNameRequired()) {
    fileName = (String)properties.get(FileTemplate.ATTRIBUTE_NAME);
    if (fileName == null) {
      throw new Exception("File name must be specified");
    }
  }

  //Set escaped references to dummy values to remove leading "\" (if not already explicitely set)
  String[] dummyRefs = calculateAttributes(template.getText(), properties, true, project);
  for (String dummyRef : dummyRefs) {
    properties.put(dummyRef, "");
  }

  handler.prepareProperties(properties);

  final String fileName_ = fileName;
  String mergedText =
          ClassLoaderUtil.runWithClassLoader(classLoader != null ? classLoader : FileTemplateUtil.class.getClassLoader(), (ThrowableComputable<String, IOException>)() -> template.getText(properties));
  final String templateText = StringUtil.convertLineSeparators(mergedText);
  final Exception[] commandException = new Exception[1];
  final PsiElement[] result = new PsiElement[1];
  CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
    try {
      result[0] = handler.createFromTemplate(project, directory, fileName_, template, templateText, properties);
    }
    catch (Exception ex) {
      commandException[0] = ex;
    }
  }), handler.commandName(template), null);
  if (commandException[0] != null) {
    throw commandException[0];
  }
  return result[0];
}
 
Example 10
Project: consulo   File: BootstrapClassLoaderUtil.java   View source code 4 votes vote down vote up
@Nonnull
public static ClassLoader initClassLoader(boolean updatePlugins) throws MalformedURLException {
  PathManager.loadProperties();

  Collection<URL> classpath = new LinkedHashSet<URL>();
  addParentClasspath(classpath, false);
  addIDEALibraries(classpath);
  addAdditionalClassPath(classpath);
  addParentClasspath(classpath, true);

  UrlClassLoader.Builder builder =
          UrlClassLoader.build().urls(filterClassPath(new ArrayList<URL>(classpath))).allowLock().usePersistentClasspathIndexForLocalClassDirectories()
                  .useCache();
  if (Boolean.valueOf(System.getProperty(PROPERTY_ALLOW_BOOTSTRAP_RESOURCES, "true"))) {
    builder.allowBootstrapResources();
  }

  ClassLoaderUtil.addPlatformLoaderParentIfOnJdk9(builder);

  UrlClassLoader newClassLoader = builder.get();

  StartupActionLogger logger = null;
  try {
    logger = new StartupActionLogger();

    logger.info("start: update=" + updatePlugins);
    // prepare plugins
    if (updatePlugins) {
      try {
        StartupActionScriptManager.executeActionScript(logger);
      }
      catch (IOException e) {
        logger.error(e);

        Main.showMessage("Plugin Installation Error", e);
      }
    }
  }
  finally {
    if (logger != null) {
      try {
        logger.close();
      }
      catch (IOException ignored) {
      }
    }
  }

  Thread.currentThread().setContextClassLoader(newClassLoader);
  return newClassLoader;
}