@Override public <T> Iterable<T> find(final Class<T> api, final ClassLoader loader) { return SironaServiceLoader.load( api, loader ); } }
public static <S> SironaServiceLoader<S> load( Class<S> service, ClassLoader loader ) { return new SironaServiceLoader<S>( service, loader ); }
private static void addIfNotSo(final Set<URL> urls, final URL url) { if (UNIX && isNative(url)) { return; } urls.add(url); }
public static URL[] findUrls(final ClassLoader classLoader) throws IOException { if (classLoader == null || (SYSTEM.getParent() != null && classLoader == SYSTEM.getParent())) { return NO_URL; } final Set<URL> urls = new HashSet<URL>(); if (URLClassLoader.class.isInstance(classLoader) && !DONT_USE_GET_URLS) { if (!isSurefire(classLoader)) { for (final URL[] item : new URL[][] { URLClassLoader.class.cast(classLoader).getURLs(), findUrls(classLoader.getParent()) }) { for (final URL url : item) { addIfNotSo(urls, url); } } } else { // http://jira.codehaus.org/browse/SUREFIRE-928 - we could reuse findUrlFromResources but this seems faster urls.addAll(fromClassPath()); } } // DONT_USE_GET_URLS || java -jar xxx.jar and use MANIFEST.MF Class-Path? // here perf is not an issue since we would either miss all the classpath or we have a single jar if (urls.size() <= 1) { final Set<URL> urlFromResources = findUrlFromResources(classLoader); if (!urls.isEmpty()) { final URL theUrl = urls.iterator().next(); if ("file".equals(theUrl.getProtocol())) { // theUrl can be file:xxxx but it is the same entry actually urlFromResources.remove(new URL("jar:" + theUrl.toExternalForm() + "!/")); } } urls.addAll(urlFromResources); } return urls.toArray(new URL[urls.size()]); }
@Created protected void initMarkerIfNeeded() { if (marker == null) { marker = Localhosts.get(); } LOGGER.warning("This storage used on app side can be a bit slow, maybe consider using a remote collector"); }
private static Object tryStringConstructor( String type, final String value ) throws Exception { return ClassLoaders.current().loadClass( type ).getConstructor( String.class ).newInstance( value ); }
public static String generateReport(final String label, final Role role, final long start, final long end) { if (!Environment.isCollector()) { final SortedMap<Long, Double> gaugeValues = Repository.INSTANCE.getGaugeValues(start, end, role); return "[" + Graphs.toJson(label, Graphs.DEFAULT_COLOR, aggregate(gaugeValues)) + "]";
public static File toFile(final URL url) { if ("jar".equals(url.getProtocol())) { try { final String spec = url.getFile(); final int separator = spec.indexOf('!'); if (separator == -1) { return null; } return toFile(new URL(spec.substring(0, separator + 1))); } catch (final MalformedURLException e) { return null; } } else if ("file".equals(url.getProtocol())) { String path = decode(url.getFile()); if (path.endsWith("!")) { path = path.substring(0, path.length() - 1); } return new File(path); } return null; }
/** * this method mimic the 1.6 ServiceLoader if you don't need 1.5 do not use that :-) * * @return */ public Iterator<S> iterator1_5() { String resourceName = "META-INF/services/" + this.service.getName(); try { List<String> serviceNames = new ArrayList<String>(); Enumeration<URL> urls = this.loader.getResources( resourceName ); while ( urls.hasMoreElements() ) { serviceNames.addAll( parseFile( urls.nextElement() ) ); } return initInstances( serviceNames ).iterator(); } catch ( Throwable t ) { throw new RuntimeException( t.getMessage(), t ); } }
private void reload() { final String name = getClass().getSimpleName().toLowerCase(Locale.ENGLISH).replace("nodestatusdatastore", ""); final long period = getPeriod(name); if (period < 0) { return; } final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory(name + "-status-schedule-")); final ScheduledFuture<?> future = ses.scheduleAtFixedRate(new ReportStatusTask(), period, period, TimeUnit.MILLISECONDS); scheduledTask.set(new BatchFuture(ses, future)); }
public Iterator<S> iterator() { try { return java.util.ServiceLoader.load( this.service, this.loader ).iterator(); } catch ( Throwable t ) { // olamy revert to 1.5 way... return iterator1_5(); } }
public static boolean isNative(final URL url) { final File file = toFile(url); if (file != null) { final String name = file.getName(); if (!name.endsWith(".jar") && !file.isDirectory() && name.contains(".so") && file.getAbsolutePath().startsWith("/usr/lib")) { return true; } } return false; }
@Created protected void initMarkerIfNotAlreadyDone() { if (marker == null) { marker = Localhosts.get(); } LOGGER.warning("This storage used on app side can be a bit slow, maybe consider using a remote collector"); }
private static <T> T newInstance(final Class<T> clazz, final String config) throws Exception { Class<?> loadedClass; try { loadedClass = ClassLoaders.current().loadClass(config); } catch (final Throwable throwable) { // NoClassDefFoundError or Exception loadedClass = clazz; } return clazz.cast(internalProcessInstance(loadedClass.newInstance())); }
@Override public void onStartup(final Set<Class<?>> classes, final ServletContext ctx) throws ServletException { if (Environment.isCollector()) { return;
@Created // call it only when main impl not in delegated mode so use IoC lifecycle management public void initBatch() { final String name = getClass().getSimpleName().toLowerCase(Locale.ENGLISH).replace("gaugedatastore", ""); final long period = getPeriod(name); final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory(name + "-gauge-schedule-")); final ScheduledFuture<?> future = ses.scheduleAtFixedRate(new PushGaugesTask(), period, period, TimeUnit.MILLISECONDS); scheduledTask = new BatchFuture(ses, future); }
@Created protected void initMarkerIfNeeded() { if (marker == null) { marker = Localhosts.get(); } LOGGER.warning("This storage used on app side can be a bit slow, maybe consider using a remote collector"); }
public static <T> T getInstance(final Class<T> clazz, String className) { try { Class<?> tClass = ClassLoaders.current().loadClass( className ); return (T) tClass.newInstance(); } catch ( Exception e ) { throw new SironaException( "cannot instantiante instance of '" + className + "'",e ); } }
public WebSocketClient buildOrGet() { WebSocketClient webSocketClient = client.get(); if (webSocketClient == null) { webSocketClient = new WebSocketClient(retries, uri.endsWith("/") ? uri : (uri + '/'), authorization, marker == null ? Localhosts.get(): marker); if (!client.compareAndSet(null, webSocketClient)) { webSocketClient.close(); webSocketClient = client.get(); } } return webSocketClient; } }
if ( Number.class.isAssignableFrom( ClassLoaders.current().loadClass( type ) ) )