List of usage examples for org.springframework.core.io Resource getFile
File getFile() throws IOException;
From source file:org.cloudifysource.usm.USMUtils.java
/************* * Return's the working directory of a PU. * * @param ctx//from ww w.j a v a2 s. co m * the spring application context. * @return the working directory. */ public static File getPUWorkDir(final ApplicationContext ctx) { File puWorkDir = null; if (isRunningInGSC(ctx)) { // running in GSC final ServiceClassLoader scl = (ServiceClassLoader) ctx.getClassLoader(); final URL url = scl.getSlashPath(); URI uri; try { uri = url.toURI(); } catch (final URISyntaxException e) { throw new IllegalArgumentException("Failed to create URI from URL: " + url, e); } puWorkDir = new File(uri); } else { final ResourceApplicationContext rac = (ResourceApplicationContext) ctx; try { final Field resourcesField = rac.getClass().getDeclaredField("resources"); final boolean accessibleBefore = resourcesField.isAccessible(); resourcesField.setAccessible(true); final Resource[] resources = (Resource[]) resourcesField.get(rac); for (final Resource resource : resources) { // find META-INF/spring/pu.xml final File file = resource.getFile(); if (file.getName().equals("pu.xml") && file.getParentFile().getName().equals("spring") && file.getParentFile().getParentFile().getName().equals("META-INF")) { puWorkDir = resource.getFile().getParentFile().getParentFile().getParentFile(); break; } } resourcesField.setAccessible(accessibleBefore); } catch (final Exception e) { throw new IllegalArgumentException("Could not find pu.xml in the ResourceApplicationContext", e); } if (puWorkDir == null) { throw new IllegalArgumentException("Could not find pu.xml in the ResourceApplicationContext"); } } if (!puWorkDir.exists()) { throw new IllegalStateException("Could not find PU work dir at: " + puWorkDir); } final File puExtDir = new File(puWorkDir, "ext"); if (!puExtDir.exists()) { throw new IllegalStateException("Could not find PU ext dir at: " + puExtDir); } return puWorkDir; }
From source file:org.codehaus.groovy.grails.commons.DefaultGrailsApplication.java
/** * Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names * *//*from w ww . j a v a2 s . c om*/ public DefaultGrailsApplication(Resource[] resources) { this(); for (Resource resource : resources) { Class<?> aClass; try { aClass = cl.loadClass(org.codehaus.groovy.grails.io.support.GrailsResourceUtils .getClassName(resource.getFile().getAbsolutePath())); } catch (ClassNotFoundException e) { throw new GrailsConfigurationException( "Class not found loading Grails application: " + e.getMessage(), e); } catch (IOException e) { throw new GrailsConfigurationException( "Class not found loading Grails application: " + e.getMessage(), e); } loadedClasses.add(aClass); } }
From source file:org.codehaus.groovy.grails.commons.DefaultGrailsApplication.java
/** * Loads a GrailsApplication using the given ResourceLocator instance which will search for appropriate class names * *//*ww w. j a v a 2 s.c o m*/ public DefaultGrailsApplication(org.codehaus.groovy.grails.io.support.Resource[] resources) { this(); for (org.codehaus.groovy.grails.io.support.Resource resource : resources) { Class<?> aClass; try { aClass = cl.loadClass(org.codehaus.groovy.grails.io.support.GrailsResourceUtils .getClassName(resource.getFile().getAbsolutePath())); } catch (ClassNotFoundException e) { throw new GrailsConfigurationException( "Class not found loading Grails application: " + e.getMessage(), e); } catch (IOException e) { throw new GrailsConfigurationException( "Class not found loading Grails application: " + e.getMessage(), e); } loadedClasses.add(aClass); } }
From source file:org.codehaus.groovy.grails.commons.GrailsApplicationFactoryBean.java
public void afterPropertiesSet() throws Exception { if (descriptor != null && descriptor.exists()) { LOG.info("Loading Grails application with information from descriptor."); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); List<Class<?>> classes = new ArrayList<Class<?>>(); InputStream inputStream = null; try {//from w w w. j a v a 2 s.co m inputStream = descriptor.getInputStream(); // Get all the resource nodes in the descriptor. // Xpath: /grails/resources/resource, where root is /grails GPathResult root = new XmlSlurper().parse(inputStream); GPathResult resources = (GPathResult) root.getProperty("resources"); GPathResult grailsClasses = (GPathResult) resources.getProperty("resource"); // Each resource node should contain a full class name, // so we attempt to load them as classes. for (int i = 0; i < grailsClasses.size(); i++) { GPathResult node = (GPathResult) grailsClasses.getAt(i); String className = node.text(); try { Class<?> clazz; if (classLoader instanceof GrailsClassLoader) { clazz = classLoader.loadClass(className); } else { clazz = Class.forName(className, true, classLoader); } classes.add(clazz); } catch (ClassNotFoundException e) { LOG.warn("Class with name [" + className + "] was not found, and hence not loaded. Possible empty class or script definition?"); } } } finally { if (inputStream != null) { inputStream.close(); } } Class<?>[] loadedClasses = classes.toArray(new Class[classes.size()]); grailsApplication = new DefaultGrailsApplication(loadedClasses, classLoader); } else if (!Environment.isWarDeployed()) { org.codehaus.groovy.grails.io.support.Resource[] buildResources = GrailsPluginUtils .getPluginBuildSettings().getArtefactResourcesForCurrentEnvironment(); Resource[] resources = new Resource[buildResources.length]; for (int i = 0; i < buildResources.length; i++) { org.codehaus.groovy.grails.io.support.Resource buildResource = buildResources[i]; resources[i] = new FileSystemResource(buildResource.getFile()); } grailsApplication = new DefaultGrailsApplication(resources); } else { grailsApplication = new DefaultGrailsApplication(); } ApplicationHolder.setApplication(grailsApplication); }
From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java
/** * Gets the class name of the specified Grails resource * * @param resource The Spring Resource//w ww . ja va2s .com * @return The class name or null if the resource is not a Grails class */ public static String getClassName(Resource resource) { try { return getClassName(resource.getFile().getAbsolutePath()); } catch (IOException e) { throw new GrailsConfigurationException( "I/O error reading class name from resource [" + resource + "]: " + e.getMessage(), e); } }
From source file:org.codehaus.groovy.grails.plugins.DefaultGrailsPlugin.java
private void evaluateOnChangeListener() { if (pluginBean.isReadableProperty(ON_SHUTDOWN)) { onShutdownListener = (Closure) GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, ON_SHUTDOWN);/*ww w. j a v a 2 s.c om*/ } if (pluginBean.isReadableProperty(ON_CONFIG_CHANGE)) { onConfigChangeListener = (Closure) GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, ON_CONFIG_CHANGE); } if (pluginBean.isReadableProperty(ON_CHANGE)) { onChangeListener = (Closure) GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, ON_CHANGE); } final boolean warDeployed = Metadata.getCurrent().isWarDeployed(); final boolean reloadEnabled = Environment.getCurrent().isReloadEnabled(); if (!((reloadEnabled || !warDeployed) && onChangeListener != null)) { return; } Object referencedResources = GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, WATCHED_RESOURCES); try { List resourceList = null; if (referencedResources instanceof String) { if (LOG.isDebugEnabled()) { LOG.debug("Configuring plugin " + this + " to watch resources with pattern: " + referencedResources); } resourceList = Collections.singletonList(referencedResources.toString()); } else if (referencedResources instanceof List) { resourceList = (List) referencedResources; } if (resourceList == null) { return; } List<String> resourceListTmp = new ArrayList<String>(); PluginBuildSettings pluginBuildSettings = GrailsPluginUtils.getPluginBuildSettings(); if (pluginBuildSettings == null) { return; } final org.codehaus.groovy.grails.io.support.Resource[] pluginDirs = pluginBuildSettings .getPluginDirectories(); final Environment env = Environment.getCurrent(); final String baseLocation = env.getReloadLocation(); for (Object ref : resourceList) { String stringRef = ref.toString(); if (warDeployed) { addBaseLocationPattern(resourceListTmp, baseLocation, stringRef); } else { for (org.codehaus.groovy.grails.io.support.Resource pluginDir : pluginDirs) { if (pluginDir == null) continue; String pluginResources = getResourcePatternForBaseLocation( pluginDir.getFile().getCanonicalPath(), stringRef); resourceListTmp.add(pluginResources); } addBaseLocationPattern(resourceListTmp, baseLocation, stringRef); } } watchedResourcePatternReferences = new String[resourceListTmp.size()]; for (int i = 0; i < watchedResourcePatternReferences.length; i++) { String resRef = resourceListTmp.get(i); watchedResourcePatternReferences[i] = resRef; } watchedResourcePatterns = new WatchPatternParser() .getWatchPatterns(Arrays.asList(watchedResourcePatternReferences)); } catch (IllegalArgumentException e) { if (GrailsUtil.isDevelopmentEnv()) { LOG.debug("Cannot load plug-in resource watch list from [" + ArrayUtils.toString(watchedResourcePatternReferences) + "]. This means that the plugin " + this + ", will not be able to auto-reload changes effectively. Try runnng grails upgrade.: " + e.getMessage()); } } catch (IOException e) { if (GrailsUtil.isDevelopmentEnv()) { LOG.debug("Cannot load plug-in resource watch list from [" + ArrayUtils.toString(watchedResourcePatternReferences) + "]. This means that the plugin " + this + ", will not be able to auto-reload changes effectively. Try runnng grails upgrade.: " + e.getMessage()); } } }
From source file:org.codehaus.groovy.grails.plugins.DefaultGrailsPlugin.java
@Override public void refresh() { // do nothing org.codehaus.groovy.grails.io.support.Resource descriptor = getDescriptor(); if (application == null || descriptor == null) { return;/* ww w . j a va2 s . c o m*/ } ClassLoader parent = application.getClassLoader(); GroovyClassLoader gcl = new GroovyClassLoader(parent); try { initialisePlugin(gcl.parseClass(descriptor.getFile())); } catch (Exception e) { LOG.error("Error refreshing plugin: " + e.getMessage(), e); } }
From source file:org.codehaus.groovy.grails.plugins.DefaultGrailsPluginManager.java
private Class<?> loadPluginClass(ClassLoader cl, Resource r) { Class<?> pluginClass;/*from w w w . j a v a2 s . co m*/ if (cl instanceof GroovyClassLoader) { try { if (LOG.isInfoEnabled()) { LOG.info("Parsing & compiling " + r.getFilename()); } pluginClass = ((GroovyClassLoader) cl).parseClass(IOGroovyMethods.getText(r.getInputStream())); } catch (CompilationFailedException e) { throw new PluginException("Error compiling plugin [" + r.getFilename() + "] " + e.getMessage(), e); } catch (IOException e) { throw new PluginException("Error reading plugin [" + r.getFilename() + "] " + e.getMessage(), e); } } else { String className = null; try { className = GrailsResourceUtils.getClassName(r.getFile().getAbsolutePath()); } catch (IOException e) { throw new PluginException( "Cannot find plugin class [" + className + "] resource: [" + r.getFilename() + "]", e); } try { pluginClass = Class.forName(className, true, cl); } catch (ClassNotFoundException e) { throw new PluginException( "Cannot find plugin class [" + className + "] resource: [" + r.getFilename() + "]", e); } } return pluginClass; }
From source file:org.codehaus.groovy.grails.support.DevelopmentResourceLoader.java
/** * Retrieves the real location of a GSP within a Grails project. * @param location The location of the GSP at deployment time * @return The location of the GSP at development time *///from www. j av a 2 s .c o m protected String getRealLocationInProject(String location) { if (new File(location).exists()) { return "file:" + location; } // don't mess with locations that are URLs (in other words, locations that have schemes) if (HAS_SCHEME_PATTERN.matcher(location).matches()) return location; if (!location.startsWith(SLASH)) location = SLASH + location; // don't mess with locations that are URLs (in other words, locations that have schemes) if (HAS_SCHEME_PATTERN.matcher(location).matches()) return location; // If the location (minus the "grails-app/.*" ending so that it matches the key value used in BuildSettings for // the inline plugin map) matches an "inline" plugin, use the location as-is // for the resource location. Otherwise, perform the logic to "normalize" the resource location based on // its relativity to the application (i.e. is it from a non-inline plugin, etc). if (BuildSettingsHolder.getSettings() .isInlinePluginLocation(new File(location.replaceAll(GRAILS_APP_DIR_PATTERN, "")))) { return "file:" + location; } if (!location.startsWith(GrailsResourceUtils.WEB_INF)) { return GrailsResourceUtils.WEB_APP_DIR + location; } final String noWebInf = location.substring(GrailsResourceUtils.WEB_INF.length() + 1); final String defaultPath = "file:" + baseLocation + SLASH + noWebInf; if (!noWebInf.startsWith(PLUGINS_PREFIX)) { return defaultPath; } if (application != null) { BuildSettings settings = BuildSettingsHolder.getSettings(); PluginBuildSettings pluginBuildSettings = org.codehaus.groovy.grails.plugins.GrailsPluginUtils .getPluginBuildSettings(); String pluginPath = StringUtils.substringAfter(noWebInf, SLASH); String pluginName = StringUtils.substringBefore(pluginPath, SLASH); String remainingPath = StringUtils.substringAfter(pluginPath, SLASH); org.codehaus.groovy.grails.io.support.Resource r = pluginBuildSettings.getPluginDirForName(pluginName); if (r != null) { try { return "file:" + r.getFile().getAbsolutePath() + SLASH + remainingPath; } catch (IOException e) { LOG.debug("Unable to locate plugin resource -- returning default path " + defaultPath + ".", e); return defaultPath; } } if (settings != null) { return "file:" + settings.getProjectPluginsDir().getAbsolutePath() + SLASH + pluginName + SLASH + remainingPath; } } return defaultPath; }
From source file:org.codehaus.groovy.grails.web.pages.GroovyPageResourceLoader.java
protected boolean isInlinePlugin(GrailsPluginInfo pluginInfo) throws IOException { // unfortunately pluginSettings.isInlinePluginLocation() does not work, paths are compare incorrectly for (org.codehaus.groovy.grails.io.support.Resource pluginDirResource : pluginSettings .getInlinePluginDirectories()) { if (compareFilePaths(pluginDirResource.getFile(), pluginInfo.getPluginDir().getFile())) { return true; }//from w w w . j a va 2 s .c o m } return false; }