List of usage examples for java.lang Class getClassLoader
@CallerSensitive
@ForceInline
public ClassLoader getClassLoader()
From source file:com.alibaba.wasp.ipc.WaspRPC.java
private static synchronized RpcEngine getProtocolEngine(Class protocol, Configuration conf) { RpcEngine engine = PROTOCOL_ENGINES.get(protocol); if (engine == null) { // check for a configured default engine Class<?> defaultEngine = conf.getClass(RPC_ENGINE_PROP, ProtobufRpcEngine.class); // check for a per interface override Class<?> impl = conf.getClass(RPC_ENGINE_PROP + "." + protocol.getName(), defaultEngine); LOG.debug("Using " + impl.getName() + " for " + protocol.getName()); engine = (RpcEngine) ReflectionUtils.newInstance(impl, conf); if (protocol.isInterface()) PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine); PROTOCOL_ENGINES.put(protocol, engine); }//from www . j a v a2s . c om return engine; }
From source file:ml.shifu.dtrain.DTrainRequestProcessor.java
/** * Find a jar that contains a class of the same name, if any. It will return a jar file, even if that is not the * first thing on the class path that has a class with the same name. * /*from w ww .j a v a 2 s . com*/ * @param myClass * the class to find * @return a jar file that contains the class, or null */ @SuppressWarnings("rawtypes") private static String findContainingJar(Class myClass) { ClassLoader loader = myClass.getClassLoader(); String classFile = myClass.getName().replaceAll("\\.", "/") + ".class"; try { for (Enumeration itr = loader.getResources(classFile); itr.hasMoreElements();) { URL url = (URL) itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it // would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, // so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } else if ("file".equals(url.getProtocol())) { String toReturn = url.getPath(); toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:ml.shifu.guagua.yarn.GuaguaYarnClient.java
/** * Find a jar that contains a class of the same name, if any. It will return a jar file, even if that is not the * first thing on the class path that has a class with the same name. * //w w w .ja va 2 s . co m * @param my_class * the class to find. * @return a jar file that contains the class, or null. * @throws IOException * in case when read class file. */ private static String findContainingJar(Class<?> my_class) { ClassLoader loader = my_class.getClassLoader(); String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; try { for (Enumeration<?> itr = loader.getResources(class_file); itr.hasMoreElements();) { URL url = (URL) itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:net.yasion.common.core.bean.wrapper.CachedIntrospectionResults.java
/** * Create CachedIntrospectionResults for the given bean class. * /*from w ww . j a va 2s . c o m*/ * @param beanClass * the bean class to analyze * @return the corresponding CachedIntrospectionResults * @throws BeansException * in case of introspection failure */ public static CachedIntrospectionResults forClass(Class<?> beanClass) throws BeansException { CachedIntrospectionResults results = strongClassCache.get(beanClass); if (results != null) { return results; } results = softClassCache.get(beanClass); if (results != null) { return results; } results = new CachedIntrospectionResults(beanClass); ConcurrentMap<Class<?>, CachedIntrospectionResults> classCacheToUse; if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader()) || isClassLoaderAccepted(beanClass.getClassLoader())) { classCacheToUse = strongClassCache; } else { if (logger.isDebugEnabled()) { logger.debug( "Not strongly caching class [" + beanClass.getName() + "] because it is not cache-safe"); } classCacheToUse = softClassCache; } CachedIntrospectionResults existing = classCacheToUse.putIfAbsent(beanClass, results); return (existing != null ? existing : results); }
From source file:org.dhatim.util.ClassUtil.java
/** * Get the specified resource as a stream. * * @param resourceName/* w w w. jav a2 s . co m*/ * The name of the class to load. * @param caller * The class of the caller. * @return The input stream for the resource or null if not found. */ public static InputStream getResourceAsStream(final String resourceName, final Class caller) { final String resource; if (!resourceName.startsWith("/")) { final Package callerPackage = caller.getPackage(); if (callerPackage != null) { resource = callerPackage.getName().replace('.', '/') + '/' + resourceName; } else { resource = resourceName; } return getResourceAsStream(resource, caller.getClassLoader()); } else { return getResourceAsStream(resourceName, caller.getClassLoader()); } }
From source file:org.apache.hadoop.ipc.RPC.java
/** Construct a client-side proxy object that implements the named protocol, * talking to a server at the named address. */ public static VersionedProtocol getProxy(Class<? extends VersionedProtocol> protocol, long clientVersion, InetSocketAddress addr, UserGroupInformation ticket, Configuration conf, SocketFactory factory, int rpcTimeout) throws IOException { if (UserGroupInformation.isSecurityEnabled()) { SaslRpcServer.init(conf);//from w w w. j a v a 2s . c o m } VersionedProtocol proxy = (VersionedProtocol) Proxy.newProxyInstance(protocol.getClassLoader(), new Class[] { protocol }, new Invoker(protocol, addr, ticket, conf, factory, rpcTimeout)); long serverVersion = proxy.getProtocolVersion(protocol.getName(), clientVersion); if (serverVersion == clientVersion) { return proxy; } else { throw new VersionMismatch(protocol.getName(), clientVersion, serverVersion); } }
From source file:com.springframework.beans.CachedIntrospectionResults.java
/** * Create CachedIntrospectionResults for the given bean class. * @param beanClass the bean class to analyze * @return the corresponding CachedIntrospectionResults * @throws BeansException in case of introspection failure *///from w w w. jav a 2s . c o m @SuppressWarnings("unchecked") static CachedIntrospectionResults forClass(Class<?> beanClass) throws BeansException { CachedIntrospectionResults results = strongClassCache.get(beanClass); if (results != null) { return results; } results = softClassCache.get(beanClass); if (results != null) { return results; } results = new CachedIntrospectionResults(beanClass); ConcurrentMap<Class<?>, CachedIntrospectionResults> classCacheToUse; if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader()) || isClassLoaderAccepted(beanClass.getClassLoader())) { classCacheToUse = strongClassCache; } else { if (logger.isDebugEnabled()) { logger.debug( "Not strongly caching class [" + beanClass.getName() + "] because it is not cache-safe"); } classCacheToUse = softClassCache; } CachedIntrospectionResults existing = classCacheToUse.putIfAbsent(beanClass, results); return (existing != null ? existing : results); }
From source file:org.apache.hadoop.hbase.ipc.HBaseClientRPC.java
static synchronized RpcClientEngine getProtocolEngine(Class<? extends IpcProtocol> protocol, Configuration conf) {//from w w w. j a v a 2 s . c om RpcClientEngine engine = PROTOCOL_ENGINES.get(protocol); if (engine == null) { // check for a configured default engine Class<?> defaultEngine = conf.getClass(RPC_ENGINE_PROP, ProtobufRpcClientEngine.class); // check for a per interface override Class<?> impl = conf.getClass(RPC_ENGINE_PROP + "." + protocol.getName(), defaultEngine); LOG.debug("Using " + impl.getName() + " for " + protocol.getName()); engine = (RpcClientEngine) ReflectionUtils.newInstance(impl, conf); if (protocol.isInterface()) { PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine); } PROTOCOL_ENGINES.put(protocol, engine); } return engine; }
From source file:org.codice.ddf.itests.common.AbstractIntegrationTest.java
public static InputStream getFileContentAsStream(String filePath, Class classRelativeToResource) { return classRelativeToResource.getClassLoader().getResourceAsStream(filePath); }
From source file:com.depas.utils.FileUtils.java
private static String getConfigDirectoryPath(Class<?> c, String rootDirectory) { // get url to iwc/idc directory on classpath try {// w w w . j a va2 s .c o m URL url = ClassLoader.getSystemResource(rootDirectory); if (url == null) { // try this classpath, for running in eclipse url = c.getClassLoader().getResource(rootDirectory); } if (url != null) { String path = url.getPath(); if (!path.endsWith("\\") && !path.endsWith("/")) { path += "/"; } return URLDecoder.decode(path, "UTF-8"); } else { return null; } } catch (Exception ex) { logger.warn("Error obtaining file path to '" + rootDirectory + "' config directory: " + ex); return null; } }