jenkins.model.Jenkins.getInstanceOrNull()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(115)

本文整理了Java中jenkins.model.Jenkins.getInstanceOrNull()方法的一些代码示例,展示了Jenkins.getInstanceOrNull()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jenkins.getInstanceOrNull()方法的具体详情如下:
包路径:jenkins.model.Jenkins
类名称:Jenkins
方法名:getInstanceOrNull

Jenkins.getInstanceOrNull介绍

[英]Gets the Jenkins singleton. #get is what you normally want.

In certain rare cases you may have code that is intended to run before Jenkins starts or while Jenkins is being shut down. For those rare cases use this method.

In other cases you may have code that might end up running on a remote JVM and not on the Jenkins master. For those cases you really should rewrite your code so that when the Callable is sent over the remoting channel it can do whatever it needs without ever referring to Jenkins; for example, gather any information you need on the master side before constructing the callable. If you must do a runtime check whether you are in the master or agent, use JenkinsJVM rather than this method, as merely loading the Jenkins class file into an agent JVM can cause linkage errors under some conditions.
[中]得到詹金斯单身汉#得到是你通常想要的。
在某些罕见的情况下,您可能有打算在Jenkins启动之前或关闭Jenkins时运行的代码。对于那些罕见的情况,请使用此方法。
在其他情况下,可能会有代码最终运行在远程JVM上,而不是在Jenkins主机上。对于那些情况,你真的应该重写你的代码,这样当Callable通过远程通道发送时,它就可以做任何它需要的事情,而不必再提及Jenkins;例如,在构建callable之前,在主端收集您需要的任何信息。如果您必须执行运行时检查,检查您是在master还是在agent中,请使用JenkinsJVM而不是此方法,因为在某些情况下,仅将Jenkins类文件加载到agent JVM中可能会导致链接错误。

代码示例

代码示例来源:origin: jenkinsci/jenkins

/**
 * @deprecated This is a historical alias for {@link #getInstanceOrNull} but with ambiguous nullability. Use {@link #get} in typical cases.
 */
@Nullable
@Deprecated
public static Jenkins getInstance() {
  return getInstanceOrNull();
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Gets the {@link Jenkins} singleton.
 * @return {@link Jenkins} instance
 * @throws IllegalStateException for the reasons that {@link #getInstanceOrNull} might return null
 * @since 2.98
 */
@Nonnull
public static Jenkins get() throws IllegalStateException {
  Jenkins instance = getInstanceOrNull();
  if (instance == null) {
    throw new IllegalStateException("Jenkins.instance is missing. Read the documentation of Jenkins.getInstanceOrNull to see what you are doing wrong.");
  }
  return instance;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Because servlet containers generally don't specify the ordering of the initialization
 * (and different implementations indeed do this differently --- See HUDSON-3878),
 * we cannot use Hudson to the CrumbIssuer into CrumbFilter eagerly.
 */
public CrumbIssuer getCrumbIssuer() {
  Jenkins h = Jenkins.getInstanceOrNull();
  if(h==null)     return null;    // before Jenkins is initialized?
  return h.getCrumbIssuer();
}

代码示例来源:origin: jenkinsci/jenkins

private Object readResolve() {
    Jenkins j = Jenkins.getInstanceOrNull();
    if (j == null) {
      return null;
    }
    // Will generally only work if called after job loading:
    return j.getItemByFullName(fullName);
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Gets all the {@link PageDecorator}s.
 */
public static List<PageDecorator> getPageDecorators() {
  // this method may be called to render start up errors, at which point Hudson doesn't exist yet. see HUDSON-3608 
  if(Jenkins.getInstanceOrNull()==null)  return Collections.emptyList();
  return PageDecorator.all();
}
/**

代码示例来源:origin: jenkinsci/jenkins

/**
 * Shortcut for {@code Jenkins.getInstanceOrNull()?.lookup.get(type)}
 */
public static @CheckForNull <T> T lookup(Class<T> type) {
  Jenkins j = Jenkins.getInstanceOrNull();
  return j != null ? j.lookup.get(type) : null;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * @since 2.147
 * @return whether to collect telemetry
 */
public static boolean isDisabled() {
  if (UsageStatistics.DISABLED) {
    return true;
  }
  Jenkins jenkins = Jenkins.getInstanceOrNull();
  return jenkins == null || !jenkins.isUsageStatisticsCollected();
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void restart() {
    Jenkins jenkins = Jenkins.getInstanceOrNull(); // guard against repeated concurrent calls to restart

    try {
      if (jenkins != null) {
        jenkins.cleanUp();
      }
    } catch (Exception e) {
      LOGGER.log(Level.SEVERE, "Failed to clean up. Restart will continue.", e);
    }

    System.exit(exitOnRestart);
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * In SMF managed environment, just commit a suicide and the service will be restarted by SMF.
 */
@Override
public void restart() throws IOException, InterruptedException {
  Jenkins jenkins = Jenkins.getInstanceOrNull(); // guard against repeated concurrent calls to restart
  try {
    if (jenkins != null) {
      jenkins.cleanUp();
    }
  } catch (Exception e) {
    LOGGER.log(Level.SEVERE, "Failed to clean up. Restart will continue.", e);
  }
  System.exit(0);
}

代码示例来源:origin: jenkinsci/jenkins

public static void removeFilter(Filter filter) throws ServletException {
  Jenkins j = Jenkins.getInstanceOrNull();
  if (j==null || getInstance(j.servletContext) == null) {
    LEGACY.remove(filter);
  } else {
    getInstance(j.servletContext).list.remove(filter);
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * @since 1.568
 */
@SuppressWarnings("deprecation")
public static Collection<? extends SCMListener> all() {
  Jenkins j = Jenkins.getInstanceOrNull();
  if (j == null) { // TODO use !Functions.isExtensionsAvailable() once JENKINS-33377
    return Collections.emptySet();
  }
  List<SCMListener> r = new ArrayList<SCMListener>(j.getExtensionList(SCMListener.class));
  for (SCMListener l : j.getSCMListeners()) {
    r.add(l);
  }
  return r;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Gets the extension list for a given type.
 * Normally calls {@link Jenkins#getExtensionList(Class)} but falls back to an empty list
 * in case {@link Jenkins#getInstanceOrNull()} is null.
 * Thus it is useful to call from {@code all()} methods which need to behave gracefully during startup or shutdown.
 * @param type the extension point type
 * @return some list
 * @since 1.572
 */
public static @Nonnull <T> ExtensionList<T> lookup(Class<T> type) {
  Jenkins j = Jenkins.getInstanceOrNull();
  return j == null ? create((Jenkins) null, type) : j.getExtensionList(type);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void run() {
    synchronized (Computer.this) {
      executors.remove(e);
      addNewExecutorIfNecessary();
      if (!isAlive()) {
        AbstractCIBase ciBase = Jenkins.getInstanceOrNull();
        if (ciBase != null) { // TODO confirm safe to assume non-null and use getInstance()
          ciBase.removeComputer(Computer.this);
        }
      }
    }
  }
};

代码示例来源:origin: jenkinsci/jenkins

/**
 * Wraps a {@link Runnable} with the  {@link Queue} lock held. 
 *
 * @param runnable the operation to wrap.
 * @since 1.618
 */
public static Runnable wrapWithLock(Runnable runnable) {
  final Jenkins jenkins = Jenkins.getInstanceOrNull();
  // TODO confirm safe to assume non-null and use getInstance()
  final Queue queue = jenkins == null ? null : jenkins.getQueue();
  return queue == null ? runnable : new LockedRunnable(runnable);
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Wraps a {@link java.util.concurrent.Callable} with the {@link Queue} lock held. 
 *
 * @param callable the operation to wrap.
 * @since 1.618
 */
public static <V> java.util.concurrent.Callable<V> wrapWithLock(java.util.concurrent.Callable<V> callable) {
  final Jenkins jenkins = Jenkins.getInstanceOrNull();
  // TODO confirm safe to assume non-null and use getInstance()
  final Queue queue = jenkins == null ? null : jenkins.getQueue();
  return queue == null ? callable : new LockedJUCCallable<V>(callable);
}

代码示例来源:origin: jenkinsci/jenkins

public static String getCrumb(StaplerRequest req) {
  Jenkins h = Jenkins.getInstanceOrNull();
  CrumbIssuer issuer = h != null ? h.getCrumbIssuer() : null;
  return issuer != null ? issuer.getCrumb(req) : "";
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Wraps a {@link hudson.remoting.Callable} with the  {@link Queue} lock held. 
 *
 * @param callable the operation to wrap.
 * @since 1.618
 */
public static <V, T extends Throwable> hudson.remoting.Callable<V, T> wrapWithLock(hudson.remoting.Callable<V, T> callable) {
  final Jenkins jenkins = Jenkins.getInstanceOrNull();
  // TODO confirm safe to assume non-null and use getInstance()
  final Queue queue = jenkins == null ? null : jenkins.getQueue();
  return queue == null ? callable : new LockedHRCallable<>(callable);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public void run() {
  try {
    Jenkins j = Jenkins.getInstanceOrNull();
    if (j != null) {
      j.getQueue().save();
    }
  } finally {
    synchronized (lock) {
      nextSave = null;
    }
  }
}

代码示例来源:origin: jenkinsci/jenkins

public static String getCrumbRequestField() {
  Jenkins h = Jenkins.getInstanceOrNull();
  CrumbIssuer issuer = h != null ? h.getCrumbIssuer() : null;
  return issuer != null ? issuer.getDescriptor().getCrumbRequestField() : "";
}

代码示例来源:origin: jenkinsci/jenkins

public static void applyConfiguration(SAXReader reader, Object context) throws IOException, InterruptedException {
  Collection<ParserConfigurator> all = Collections.emptyList();
  if (Jenkins.getInstanceOrNull()==null) {
    Channel ch = Channel.current();
    if (ch!=null)
      all = ch.call(new GetParserConfigurators());
  } else
    all = all();
  for (ParserConfigurator pc : all)
    pc.configure(reader,context);
}
private static class GetParserConfigurators extends SlaveToMasterCallable<Collection<ParserConfigurator>, IOException> {

相关文章

微信公众号

最新文章

更多

Jenkins类方法