List of usage examples for java.lang ClassLoader getResource
public URL getResource(String name)
From source file:com.flexive.shared.FxSharedUtils.java
/** * Get the name of the application server [fleXive] is running on * * @return name of the application server [fleXive] is running on * @since 3.1/* ww w . java 2 s . c o m*/ */ public synchronized static String getApplicationServerName() { if (appserver != null) return appserver; if (System.getProperty("product.name") != null) { // Glassfish 2 / Sun AS String ver = System.getProperty("product.name"); if (System.getProperty("com.sun.jbi.domain.name") != null) ver += " (Domain: " + System.getProperty("com.sun.jbi.domain.name") + ")"; appserver = ver; } else if (System.getProperty("glassfish.version") != null) { // Glassfish 3+ appserver = System.getProperty("glassfish.version"); } else if (System.getProperty("jboss.home.dir") != null) { appserver = "JBoss (unknown version)"; boolean found = false; try { final Class<?> cls = Class.forName("org.jboss.Version"); Method m = cls.getMethod("getInstance"); Object v = m.invoke(null); Method pr = cls.getMethod("getProperties"); Map props = (Map) pr.invoke(v); String ver = inspectJBossVersionProperties(props); found = true; appserver = ver; } catch (ClassNotFoundException e) { //ignore } catch (NoSuchMethodException e) { //ignore } catch (IllegalAccessException e) { //ignore } catch (InvocationTargetException e) { //ignore } if (!found) { // try JBoss 7 MBean lookup try { final ObjectName name = new ObjectName("jboss.as:management-root=server"); final Object version = ManagementFactory.getPlatformMBeanServer().getAttribute(name, "releaseVersion"); if (version != null) { appserver = "JBoss (" + version + ")"; found = true; } } catch (Exception e) { // ignore } } if (!found) { //try again with a JBoss 6.x specific locations try { final ClassLoader jbossCL = Class.forName("org.jboss.Main").getClassLoader(); if (jbossCL.getResource(JBOSS6_VERSION_PROPERTIES) != null) { Properties prop = new Properties(); prop.load(jbossCL.getResourceAsStream(JBOSS6_VERSION_PROPERTIES)); if (prop.containsKey("version.name")) { appserver = inspectJBossVersionProperties(prop); //noinspection UnusedAssignment found = true; } } } catch (ClassNotFoundException e) { //ignore } catch (IOException e) { //ignore } } } else if (System.getProperty("openejb.version") != null) { // try to get Jetty version String jettyVersion = ""; try { final Class<?> cls = Class.forName("org.mortbay.jetty.Server"); jettyVersion = " (Jetty " + cls.getPackage().getImplementationVersion() + ")"; } catch (ClassNotFoundException e) { // no Jetty version... } appserver = "OpenEJB " + System.getProperty("openejb.version") + jettyVersion; } else if (System.getProperty("weblogic.home") != null) { String server = System.getProperty("weblogic.Name"); String wlVersion = ""; try { final Class<?> cls = Class.forName("weblogic.common.internal.VersionInfo"); Method m = cls.getMethod("theOne"); Object serverVersion = m.invoke(null); Method sv = m.invoke(null).getClass().getMethod("getImplementationVersion"); wlVersion = " " + String.valueOf(sv.invoke(serverVersion)); } catch (ClassNotFoundException e) { //ignore } catch (NoSuchMethodException e) { //ignore } catch (InvocationTargetException e) { //ignore } catch (IllegalAccessException e) { //ignore } if (StringUtils.isEmpty(server)) appserver = "WebLogic" + wlVersion; else appserver = "WebLogic" + wlVersion + " (server: " + server + ")"; } else if (System.getProperty("org.apache.geronimo.home.dir") != null) { String gVersion = ""; try { final Class<?> cls = Class.forName("org.apache.geronimo.system.serverinfo.ServerConstants"); Method m = cls.getMethod("getVersion"); gVersion = " " + String.valueOf(m.invoke(null)); m = cls.getMethod("getBuildDate"); gVersion = gVersion + " (" + String.valueOf(m.invoke(null)) + ")"; } catch (ClassNotFoundException e) { //ignore } catch (NoSuchMethodException e) { //ignore } catch (InvocationTargetException e) { //ignore } catch (IllegalAccessException e) { //ignore } appserver = "Apache Geronimo " + gVersion; } else { appserver = "unknown"; } return appserver; }
From source file:com.lp.client.frame.component.PanelDokumentenablage.java
@Override public void filesDropped(DragAndDropTarget source, List<File> files) { if (source == dropArea) { if (files == null || files.size() == 0) return; try {/* w ww . ja va 2s .co m*/ file = files.get(0); if (!file.exists()) { DialogFactory.showModalDialog(LPMain.getTextRespectUISPr("lp.error"), LPMain.getMessageTextRespectUISPr("lp.dokumente.typnichtunterstuetzt", Helper.getMime(file.getName()))); return; } setDefaultsForNew(); wtfName.setText(" "); components2Dto(); new DialogJCRUploadSettings(LPMain.getInstance().getDesktop(), jcrDocDto); JCRFileUploader.uploadFiles(files, jcrDocDto); refresh(); } catch (Throwable e) { ClassLoader classloader = org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader(); URL res = classloader.getResource("org/apache/poi/poifs/filesystem/POIFSFileSystem.class"); String path = res.getPath(); System.out.println("Core POI came from " + path); e.printStackTrace(); } } }
From source file:io.druid.cli.validate.DruidJsonValidator.java
@Override public void run() { File file = new File(jsonFile); if (!file.exists()) { System.out.printf("File[%s] does not exist.%n", file); }/*w ww .ja v a 2 s .co m*/ final Injector injector = makeInjector(); final ObjectMapper jsonMapper = injector.getInstance(ObjectMapper.class); registerModules(jsonMapper, Iterables.concat( Initialization.getFromExtensions(injector.getInstance(ExtensionsConfig.class), DruidModule.class), Arrays.asList(new FirehoseModule(), new IndexingHadoopModule(), new IndexingServiceFirehoseModule(), new LocalDataStorageDruidModule(), new ParsersModule()))); final ClassLoader loader; if (Thread.currentThread().getContextClassLoader() != null) { loader = Thread.currentThread().getContextClassLoader(); } else { loader = DruidJsonValidator.class.getClassLoader(); } if (toLogger) { logWriter = new NullWriter() { private final Logger logger = new Logger(DruidJsonValidator.class); @Override public void write(char[] cbuf, int off, int len) { logger.info(new String(cbuf, off, len)); } }; } try { if (type.equalsIgnoreCase("query")) { jsonMapper.readValue(file, Query.class); } else if (type.equalsIgnoreCase("hadoopConfig")) { jsonMapper.readValue(file, HadoopDruidIndexerConfig.class); } else if (type.equalsIgnoreCase("task")) { jsonMapper.readValue(file, Task.class); } else if (type.equalsIgnoreCase("parse")) { final StringInputRowParser parser; if (file.isFile()) { logWriter.write("loading parse spec from file '" + file + "'"); parser = jsonMapper.readValue(file, StringInputRowParser.class); } else if (loader.getResource(jsonFile) != null) { logWriter.write("loading parse spec from resource '" + jsonFile + "'"); parser = jsonMapper.readValue(loader.getResource(jsonFile), StringInputRowParser.class); } else { logWriter.write("cannot find proper spec from 'file'.. regarding it as a json spec"); parser = jsonMapper.readValue(jsonFile, StringInputRowParser.class); } if (resource != null) { final CharSource source; if (new File(resource).isFile()) { logWriter.write("loading data from file '" + resource + "'"); source = Resources.asByteSource(new File(resource).toURL()) .asCharSource(Charset.forName(parser.getEncoding())); } else if (loader.getResource(resource) != null) { logWriter.write("loading data from resource '" + resource + "'"); source = Resources.asByteSource(loader.getResource(resource)) .asCharSource(Charset.forName(parser.getEncoding())); } else { logWriter.write("cannot find proper data from 'resource'.. regarding it as data string"); source = CharSource.wrap(resource); } readData(parser, source); } } else { throw new UOE("Unknown type[%s]", type); } } catch (Exception e) { System.out.println("INVALID JSON!"); throw Throwables.propagate(e); } }
From source file:org.apache.druid.cli.validate.DruidJsonValidator.java
@Override public void run() { File file = new File(jsonFile); if (!file.exists()) { LOG.info("File[%s] does not exist.%n", file); }//from ww w .j ava 2 s . c o m final Injector injector = makeInjector(); final ObjectMapper jsonMapper = injector.getInstance(ObjectMapper.class); registerModules(jsonMapper, Iterables.concat( Initialization.getFromExtensions(injector.getInstance(ExtensionsConfig.class), DruidModule.class), Arrays.asList(new FirehoseModule(), new IndexingHadoopModule(), new IndexingServiceFirehoseModule(), new LocalDataStorageDruidModule()))); final ClassLoader loader; if (Thread.currentThread().getContextClassLoader() != null) { loader = Thread.currentThread().getContextClassLoader(); } else { loader = DruidJsonValidator.class.getClassLoader(); } if (toLogger) { logWriter = new NullWriter() { private final Logger logger = new Logger(DruidJsonValidator.class); @Override public void write(char[] cbuf, int off, int len) { logger.info(new String(cbuf, off, len)); } }; } try { if ("query".equalsIgnoreCase(type)) { jsonMapper.readValue(file, Query.class); } else if ("hadoopConfig".equalsIgnoreCase(type)) { jsonMapper.readValue(file, HadoopDruidIndexerConfig.class); } else if ("task".equalsIgnoreCase(type)) { jsonMapper.readValue(file, Task.class); } else if ("parse".equalsIgnoreCase(type)) { final StringInputRowParser parser; if (file.isFile()) { logWriter.write("loading parse spec from file '" + file + "'"); parser = jsonMapper.readValue(file, StringInputRowParser.class); } else if (loader.getResource(jsonFile) != null) { logWriter.write("loading parse spec from resource '" + jsonFile + "'"); parser = jsonMapper.readValue(loader.getResource(jsonFile), StringInputRowParser.class); } else { logWriter.write("cannot find proper spec from 'file'.. regarding it as a json spec"); parser = jsonMapper.readValue(jsonFile, StringInputRowParser.class); } parser.initializeParser(); if (resource != null) { final CharSource source; if (new File(resource).isFile()) { logWriter.write("loading data from file '" + resource + "'"); source = Resources.asByteSource(new File(resource).toURI().toURL()) .asCharSource(Charset.forName(parser.getEncoding())); } else if (loader.getResource(resource) != null) { logWriter.write("loading data from resource '" + resource + "'"); source = Resources.asByteSource(loader.getResource(resource)) .asCharSource(Charset.forName(parser.getEncoding())); } else { logWriter.write("cannot find proper data from 'resource'.. regarding it as data string"); source = CharSource.wrap(resource); } readData(parser, source); } } else { throw new UOE("Unknown type[%s]", type); } } catch (Exception e) { LOG.error(e, "INVALID JSON!"); throw Throwables.propagate(e); } }
From source file:io.github.jeddict.jpa.modeler.initializer.JPAModelerUtil.java
@Override public void init() { if (ENTITY_ICON_PATH == null) { PACKAGE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/java/PACKAGE.png"; JAVA_CLASS_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/java/JAVA_CLASS.png"; ABSTRACT_JAVA_CLASS_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/java/ABSTRACT_JAVA_CLASS.png"; ENTITY_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/java/ENTITY.png"; ABSTRACT_ENTITY_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/java/ABSTRACT_ENTITY.png"; MAPPED_SUPER_CLASS_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/java/MAPPED_SUPER_CLASS.png"; EMBEDDABLE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/java/EMBEDDABLE.png"; ID_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/id-attribute.png"; BASIC_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/basic-attribute.png"; BASIC_COLLECTION_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/basic-collection-attribute.png"; EMBEDDED_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/embedded-attribute.gif"; EMBEDDED_ID_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/embedded-id-attribute.png"; MULTI_VALUE_EMBEDDED_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/multi-value-embedded.gif"; SINGLE_VALUE_EMBEDDED_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/single-value-embedded.gif"; TRANSIENT_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/transient-attribute.png"; VERSION_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/version-attribute.png"; UMTM_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/umtm-attribute.png"; BMTM_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/bmtm-attribute.png"; UMTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/umto-attribute.png"; BMTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/bmto-attribute.png"; PK_UMTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/pk-umto-attribute.png"; PK_BMTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/pk-bmto-attribute.png"; UOTM_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/uotm-attribute.png"; BOTM_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/botm-attribute.png"; UOTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/uoto-attribute.png"; BOTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/boto-attribute.png"; PK_UOTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/pk-uoto-attribute.png"; PK_BOTO_ATTRIBUTE_ICON_PATH = "io/github/jeddict/jpa/modeler/resource/image/pk-boto-attribute.png"; BASE_ELEMENT_ICONS.put(JavaClass.class, JAVA_CLASS_ICON_PATH); BASE_ELEMENT_ICONS.put(Entity.class, ENTITY_ICON_PATH); BASE_ELEMENT_ICONS.put(MappedSuperclass.class, MAPPED_SUPER_CLASS_ICON_PATH); BASE_ELEMENT_ICONS.put(Embeddable.class, EMBEDDABLE_ICON_PATH); ClassLoader cl = JPAModelerUtil.class.getClassLoader(); UMTM_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(UMTM_ATTRIBUTE_ICON_PATH)).getImage(); BMTM_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(BMTM_ATTRIBUTE_ICON_PATH)).getImage(); UMTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(UMTO_ATTRIBUTE_ICON_PATH)).getImage(); BMTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(BMTO_ATTRIBUTE_ICON_PATH)).getImage(); PK_UMTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(PK_UMTO_ATTRIBUTE_ICON_PATH)).getImage(); PK_BMTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(PK_BMTO_ATTRIBUTE_ICON_PATH)).getImage(); UOTM_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(UOTM_ATTRIBUTE_ICON_PATH)).getImage(); BOTM_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(BOTM_ATTRIBUTE_ICON_PATH)).getImage(); UOTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(UOTO_ATTRIBUTE_ICON_PATH)).getImage(); BOTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(BOTO_ATTRIBUTE_ICON_PATH)).getImage(); PK_UOTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(PK_UOTO_ATTRIBUTE_ICON_PATH)).getImage(); PK_BOTO_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(PK_BOTO_ATTRIBUTE_ICON_PATH)).getImage(); GENERALIZATION_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/generalization.png")).getImage(); GENERALIZATION_ANCHOR = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/generalization-anchor.png")) .getImage();//from w w w. java2 s . com OTOR_SOURCE_ANCHOR_SHAPE = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/one-to-one.gif")).getImage(); OTOR_TARGET_ANCHOR_SHAPE = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/one-to-one-arrow.png")).getImage(); OTMR_SOURCE_ANCHOR_SHAPE = OTOR_SOURCE_ANCHOR_SHAPE; OTMR_TARGET_ANCHOR_SHAPE = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/one-to-many-arrow.png")) .getImage(); MTOR_SOURCE_ANCHOR_SHAPE = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/many-to-one.gif")).getImage(); MTOR_TARGET_ANCHOR_SHAPE = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/one-to-one-arrow.png")).getImage(); MTMR_SOURCE_ANCHOR_SHAPE = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/many-to-many.gif")).getImage(); MTMR_TARGET_ANCHOR_SHAPE = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/many-to-many-arrow.png")) .getImage(); OTOR_ICON = OTOR_SOURCE_ANCHOR_SHAPE; OTMR_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/one-to-many.gif")).getImage(); MTOR_ICON = MTOR_SOURCE_ANCHOR_SHAPE; MTMR_ICON = MTMR_SOURCE_ANCHOR_SHAPE; UNI_DIRECTIONAL_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/uni.png")).getImage(); BI_DIRECTIONAL_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/bi.png")).getImage(); PK_UNI_DIRECTIONAL_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/pk-uni.png")).getImage(); PK_BI_DIRECTIONAL_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/pk-bi.png")).getImage(); COMPOSITION_ATTRIBUTE_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/composition.png")).getImage(); JAVA_CLASS_ICON = new ImageIcon(cl.getResource(JAVA_CLASS_ICON_PATH)).getImage(); ABSTRACT_JAVA_CLASS_ICON = new ImageIcon(cl.getResource(ABSTRACT_JAVA_CLASS_ICON_PATH)).getImage(); ENTITY_ICON = new ImageIcon(cl.getResource(ENTITY_ICON_PATH)).getImage(); ABSTRACT_ENTITY_ICON = new ImageIcon(cl.getResource(ABSTRACT_ENTITY_ICON_PATH)).getImage(); MAPPED_SUPER_CLASS_ICON = new ImageIcon(cl.getResource(MAPPED_SUPER_CLASS_ICON_PATH)).getImage(); EMBEDDABLE_ICON = new ImageIcon(cl.getResource(EMBEDDABLE_ICON_PATH)).getImage(); PACKAGE_ICON = new ImageIcon(cl.getResource(PACKAGE_ICON_PATH)).getImage(); ID_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(ID_ATTRIBUTE_ICON_PATH)).getImage(); BASIC_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(BASIC_ATTRIBUTE_ICON_PATH)).getImage(); BASIC_COLLECTION_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(BASIC_COLLECTION_ATTRIBUTE_ICON_PATH)) .getImage(); EMBEDDED_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(EMBEDDED_ATTRIBUTE_ICON_PATH)).getImage(); EMBEDDED_ID_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(EMBEDDED_ID_ATTRIBUTE_ICON_PATH)).getImage(); MULTI_VALUE_EMBEDDED_ATTRIBUTE_ICON = new ImageIcon( cl.getResource(MULTI_VALUE_EMBEDDED_ATTRIBUTE_ICON_PATH)).getImage(); SINGLE_VALUE_EMBEDDED_ATTRIBUTE_ICON = new ImageIcon( cl.getResource(SINGLE_VALUE_EMBEDDED_ATTRIBUTE_ICON_PATH)).getImage(); TRANSIENT_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(TRANSIENT_ATTRIBUTE_ICON_PATH)).getImage(); VERSION_ATTRIBUTE_ICON = new ImageIcon(cl.getResource(VERSION_ATTRIBUTE_ICON_PATH)).getImage(); CREATE_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/add-element.png")); EDIT_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/edit-element.png")); DELETE_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/delete-element.png")); DELETE_ALL_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/delete-all-element.png")); PAINT_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/paint.png")); SUCCESS_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/success_16.png")); WARNING_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/warning_16.png")); ERROR_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/error_16.png")); HOME_ICON = new ImageIcon(cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/home.png")); UP_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/anchor_up.png")).getImage(); DOWN_ICON = new ImageIcon( cl.getResource("io/github/jeddict/jpa/modeler/resource/image/misc/anchor_down.png")).getImage(); } }
From source file:com.ms.commons.test.BaseTestCase.java
protected String getAbstractBasePath() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String slashClassName = this.getClass().getName().replace('.', '/'); String classPath = classLoader.getResource(slashClassName + ".class").getPath(); int classIndex = classPath.indexOf(slashClassName); String basePart = classPath.substring(0, classIndex); return basePart; }
From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java
public void contextInitialized(ServletContextEvent servletContextEvent) { final ServletContext servletContext = servletContextEvent.getServletContext(); stopThreads = !"false".equals(servletContext.getInitParameter("ClassLoaderLeakPreventor.stopThreads")); stopTimerThreads = !"false" .equals(servletContext.getInitParameter("ClassLoaderLeakPreventor.stopTimerThreads")); executeShutdownHooks = !"false" .equals(servletContext.getInitParameter("ClassLoaderLeakPreventor.executeShutdownHooks")); threadWaitMs = getIntInitParameter(servletContext, "ClassLoaderLeakPreventor.threadWaitMs", THREAD_WAIT_MS_DEFAULT);/* w w w .j a v a 2s . com*/ shutdownHookWaitMs = getIntInitParameter(servletContext, "ClassLoaderLeakPreventor.shutdownHookWaitMs", SHUTDOWN_HOOK_WAIT_MS_DEFAULT); info("Settings for " + this.getClass().getName() + " (CL: 0x" + Integer.toHexString(System.identityHashCode(getWebApplicationClassLoader())) + "):"); info(" stopThreads = " + stopThreads); info(" stopTimerThreads = " + stopTimerThreads); info(" executeShutdownHooks = " + executeShutdownHooks); info(" threadWaitMs = " + threadWaitMs + " ms"); info(" shutdownHookWaitMs = " + shutdownHookWaitMs + " ms"); final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { // If package org.jboss is found, we may be running under JBoss mayBeJBoss = (contextClassLoader.getResource("org/jboss") != null); } catch (Exception ex) { // Do nothing } info("Initializing context by loading some known offenders with system classloader"); // This part is heavily inspired by Tomcats JreMemoryLeakPreventionListener // See http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java?view=markup try { // Switch to system classloader in before we load/call some JRE stuff that will cause // the current classloader to be available for garbage collection Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader()); java.awt.Toolkit.getDefaultToolkit(); // Will start a Thread java.security.Security.getProviders(); java.sql.DriverManager.getDrivers(); // Load initial drivers using system classloader javax.imageio.ImageIO.getCacheDirectory(); // Will call sun.awt.AppContext.getAppContext() try { Class.forName("javax.security.auth.Policy").getMethod("getPolicy").invoke(null); } catch (IllegalAccessException iaex) { error(iaex); } catch (InvocationTargetException itex) { error(itex); } catch (NoSuchMethodException nsmex) { error(nsmex); } catch (ClassNotFoundException e) { // Ignore silently - class is deprecated } try { javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (Exception ex) { // Example: ParserConfigurationException error(ex); } try { Class.forName("javax.xml.bind.DatatypeConverterImpl"); // Since JDK 1.6. May throw java.lang.Error } catch (ClassNotFoundException e) { // Do nothing } try { Class.forName("javax.security.auth.login.Configuration", true, ClassLoader.getSystemClassLoader()); } catch (ClassNotFoundException e) { // Do nothing } // This probably does not affect classloaders, but prevents some problems with .jar files try { // URL needs to be well-formed, but does not need to exist new URL("jar:file://dummy.jar!/").openConnection().setDefaultUseCaches(false); } catch (Exception ex) { error(ex); } ///////////////////////////////////////////////////// // Load Sun specific classes that may cause leaks final boolean isSunJRE = System.getProperty("java.vendor").startsWith("Sun"); try { Class.forName("com.sun.jndi.ldap.LdapPoolManager"); } catch (ClassNotFoundException cnfex) { if (isSunJRE) error(cnfex); } try { Class.forName("sun.java2d.Disposer"); // Will start a Thread } catch (ClassNotFoundException cnfex) { if (isSunJRE && !mayBeJBoss) // JBoss blocks this package/class, so don't warn error(cnfex); } try { Class<?> gcClass = Class.forName("sun.misc.GC"); final Method requestLatency = gcClass.getDeclaredMethod("requestLatency", long.class); requestLatency.invoke(null, 3600000L); } catch (ClassNotFoundException cnfex) { if (isSunJRE) error(cnfex); } catch (NoSuchMethodException nsmex) { error(nsmex); } catch (IllegalAccessException iaex) { error(iaex); } catch (InvocationTargetException itex) { error(itex); } // Cause oracle.jdbc.driver.OracleTimeoutPollingThread to be started with contextClassLoader = system classloader try { Class.forName("oracle.jdbc.driver.OracleTimeoutThreadPerVM"); } catch (ClassNotFoundException e) { // Ignore silently - class not present } } finally { // Reset original classloader Thread.currentThread().setContextClassLoader(contextClassLoader); } }
From source file:com.ms.commons.test.BaseTestCase.java
protected String getClassBasePath() { TestCaseInfo info = this.getClass().getAnnotation(TestCaseInfo.class); if ((info != null) && (info.basePath().length() != 0)) { return info.basePath(); }// w w w . j a v a 2s . c om ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String slashClassName = this.getClass().getName().replace('.', '/'); String classPath = classLoader.getResource(slashClassName + ".class").getPath(); int classIndex = classPath.indexOf(slashClassName); String basePart = classPath.substring(0, classIndex); return getFinalClassBasePath(basePart); }
From source file:ffx.ui.ModelingPanel.java
private void runScript() { String name = activeCommand;//from ww w. j a v a2s . c o m name = name.replace('.', File.separatorChar); ClassLoader loader = getClass().getClassLoader(); URL embeddedScript = loader.getResource("ffx/scripts/" + name + ".ffx"); if (embeddedScript == null) { embeddedScript = loader.getResource("ffx/scripts/" + name + ".groovy"); } File scriptFile = null; if (embeddedScript != null) { try { scriptFile = new File( StringUtils.copyInputStreamToTmpFile(embeddedScript.openStream(), name, ".ffx")); } catch (IOException e) { logger.log(Level.WARNING, "Exception extracting embedded script {0}\n{1}", new Object[] { embeddedScript.toString(), e.toString() }); } } if (scriptFile != null && scriptFile.exists()) { String argLine = statusLabel.getText().replace('\n', ' '); String args[] = argLine.trim().split(" +"); // Remove the command (first token) and system name (last token). args = copyOfRange(args, 1, args.length - 1); List<String> argList = Arrays.asList(args); ffxLauncher = new FFXLauncher(argList, scriptFile); ffxThread = new Thread(ffxLauncher); ffxThread.setName(statusLabel.getText()); ffxThread.setPriority(Thread.MAX_PRIORITY); ffxThread.start(); } else { logger.warning(format("%s was not found.", name)); } }
From source file:org.apache.hadoop.crypto.key.kms.server.MiniKMS.java
public void start() throws Exception { ClassLoader cl = Thread.currentThread().getContextClassLoader(); System.setProperty(KMSConfiguration.KMS_CONFIG_DIR, kmsConfDir); File aclsFile = new File(kmsConfDir, "kms-acls.xml"); if (!aclsFile.exists()) { InputStream is = cl.getResourceAsStream("mini-kms-acls-default.xml"); OutputStream os = new FileOutputStream(aclsFile); IOUtils.copy(is, os);/*from w ww . j av a2 s . co m*/ is.close(); os.close(); } File coreFile = new File(kmsConfDir, "core-site.xml"); if (!coreFile.exists()) { Configuration core = new Configuration(); Writer writer = new FileWriter(coreFile); core.writeXml(writer); writer.close(); } File kmsFile = new File(kmsConfDir, "kms-site.xml"); if (!kmsFile.exists()) { Configuration kms = new Configuration(false); kms.set(KMSConfiguration.KEY_PROVIDER_URI, "jceks://file@" + new Path(kmsConfDir, "kms.keystore").toUri()); kms.set("hadoop.kms.authentication.type", "simple"); Writer writer = new FileWriter(kmsFile); kms.writeXml(writer); writer.close(); } System.setProperty("log4j.configuration", log4jConfFile); jetty = createJettyServer(keyStore, keyStorePassword, inPort); // we need to do a special handling for MiniKMS to work when in a dir and // when in a JAR in the classpath thanks to Jetty way of handling of webapps // when they are in the a DIR, WAR or JAR. URL webXmlUrl = cl.getResource("kms-webapp/WEB-INF/web.xml"); if (webXmlUrl == null) { throw new RuntimeException("Could not find kms-webapp/ dir in test classpath"); } boolean webXmlInJar = webXmlUrl.getPath().contains(".jar!/"); String webappPath; if (webXmlInJar) { File webInf = new File("target/" + UUID.randomUUID().toString() + "/kms-webapp/WEB-INF"); webInf.mkdirs(); new File(webInf, "web.xml").delete(); InputStream is = cl.getResourceAsStream("kms-webapp/WEB-INF/web.xml"); OutputStream os = new FileOutputStream(new File(webInf, "web.xml")); IOUtils.copy(is, os); is.close(); os.close(); webappPath = webInf.getParentFile().getAbsolutePath(); } else { webappPath = cl.getResource("kms-webapp").getPath(); } WebAppContext context = new WebAppContext(webappPath, "/kms"); if (webXmlInJar) { context.setClassLoader(cl); } jetty.addHandler(context); jetty.start(); kmsURL = new URL(getJettyURL(jetty), "kms"); }