/** * @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(); }
/** * 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; }
/** * 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(); }
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); } }
/** * 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(); } /**
/** * 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; }
/** * @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(); }
@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); } }
/** * 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); }
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); } }
/** * @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; }
/** * 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); }
@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); } } } } };
/** * 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); }
/** * 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); }
public static String getCrumb(StaplerRequest req) { Jenkins h = Jenkins.getInstanceOrNull(); CrumbIssuer issuer = h != null ? h.getCrumbIssuer() : null; return issuer != null ? issuer.getCrumb(req) : ""; }
/** * 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); }
@Override public void run() { try { Jenkins j = Jenkins.getInstanceOrNull(); if (j != null) { j.getQueue().save(); } } finally { synchronized (lock) { nextSave = null; } } }
public static String getCrumbRequestField() { Jenkins h = Jenkins.getInstanceOrNull(); CrumbIssuer issuer = h != null ? h.getCrumbIssuer() : null; return issuer != null ? issuer.getDescriptor().getCrumbRequestField() : ""; }
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> {