String message = "Malformed URL: " + root + ", " + resourceName; if (se == null) { se = new ResourceException(message); } else { se = new ResourceException(message, se); groovyScriptConn = null; if (se == null) { se = new ResourceException(message); } else { se = new ResourceException(message, se); if (se == null) se = new ResourceException("No resource for " + resourceName + " was found");
@Override public void initialize(ITenantManagement tenantManagement) throws SiteWhereException { Binding binding = new Binding(); binding.setVariable("logger", LOGGER); binding.setVariable("tenantBuilder", new TenantManagementRequestBuilder(tenantManagement)); try { // Use the system account for logging "created by" on created elements. SecurityContextHolder.getContext().setAuthentication(SiteWhereServer.getSystemAuthentication()); getConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } finally { SecurityContextHolder.getContext().setAuthentication(null); } }
@Override public void initialize(IUserManagement userManagement) throws SiteWhereException { Binding binding = new Binding(); binding.setVariable("logger", LOGGER); binding.setVariable("userBuilder", new UserManagementRequestBuilder(userManagement)); try { // Use the system account for logging "created by" on created elements. SecurityContextHolder.getContext().setAuthentication(SiteWhereServer.getSystemAuthentication()); getConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } finally { SecurityContextHolder.getContext().setAuthentication(null); } }
/** * Interface method for ResourceContainer. This is used by the GroovyScriptEngine. */ public URLConnection getResourceConnection (String name) throws ResourceException { name = removeNamePrefix(name).replace('\\', '/'); //remove the leading / as we are trying with a leading / now if (name.startsWith("WEB-INF/groovy/")) { name = name.substring(15);//just for uniformity } else if (name.startsWith("/")) { name = name.substring(1); } /* * Try to locate the resource and return an opened connection to it. */ try { URL url = servletContext.getResource('/' + name); if (url == null) { url = servletContext.getResource("/WEB-INF/groovy/" + name); } if (url == null) { throw new ResourceException("Resource \"" + name + "\" not found!"); } return url.openConnection(); } catch (IOException e) { throw new ResourceException("Problems getting resource named \"" + name + "\"!", e); } }
@Override public void initialize(IUserManagement userManagement) throws SiteWhereException { // Skip if not enabled. if (!isEnabled()) { return; } Binding binding = new Binding(); binding.setVariable("logger", LOGGER); binding.setVariable("userBuilder", new UserManagementRequestBuilder(userManagement)); try { // Use system account for logging "created by" on created elements. SecurityContextHolder.getContext().setAuthentication(SiteWhereServer.getSystemAuthentication()); SiteWhere.getServer().getGroovyConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } finally { SecurityContextHolder.getContext().setAuthentication(null); } }
/** * Get the class of the scriptName in question, so that you can instantiate * Groovy objects with caching and reloading. * * @param scriptName resource name pointing to the script * @return the loaded scriptName as a compiled class * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException { URLConnection conn = rc.getResourceConnection(scriptName); String path = conn.getURL().toExternalForm(); ScriptCacheEntry entry = scriptCache.get(path); Class clazz = null; if (entry != null) clazz = entry.scriptClass; try { if (isSourceNewer(entry)) { try { String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : config.getSourceEncoding(); String content = IOGroovyMethods.getText(conn.getInputStream(), encoding); clazz = groovyLoader.parseClass(content, path); } catch (IOException e) { throw new ResourceException(e); } } } finally { forceClose(conn); } return clazz; }
@Override public void initialize(IScheduleManagement scheduleManagement) throws SiteWhereException { // Skip if not enabled. if (!isEnabled()) { return; } Binding binding = new Binding(); binding.setVariable("logger", LOGGER); binding.setVariable("scheduleBuilder", new ScheduleManagementRequestBuilder(scheduleManagement)); try { // Use system account for logging "created by" on created elements. SecurityContextHolder.getContext().setAuthentication(SiteWhereServer.getSystemAuthentication()); getGroovyConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } finally { SecurityContextHolder.getContext().setAuthentication(null); } }
String message = "Malformed URL: " + roots[i] + ", " + resourceName; if (se == null) { se = new ResourceException(message); } else { se = new ResourceException(message, se); se = new ResourceException(message); } else { se = new ResourceException(message, se);
@Override public void initialize(ITenantManagement tenantManagement) throws SiteWhereException { // Skip if not enabled. if (!isEnabled()) { return; } Binding binding = new Binding(); binding.setVariable(IGroovyVariables.VAR_LOGGER, LOGGER); binding.setVariable(IGroovyVariables.VAR_TENANT_MANAGEMENT_BUILDER, new TenantManagementRequestBuilder(tenantManagement)); try { // Use system account for logging "created by" on created elements. SecurityContextHolder.getContext().setAuthentication(SiteWhereServer.getSystemAuthentication()); SiteWhere.getServer().getGroovyConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } finally { SecurityContextHolder.getContext().setAuthentication(null); } }
String message = "Malformed URL: " + root + ", " + resourceName; if (se == null) { se = new ResourceException(message); } else { se = new ResourceException(message, se); groovyScriptConn = null; if (se == null) { se = new ResourceException(message); } else { se = new ResourceException(message, se); if (se == null) se = new ResourceException("No resource for " + resourceName + " was found");
@Override public void initialize(IDeviceManagement deviceManagement, IDeviceEventManagement deviceEventManagement, IAssetModuleManager assetModuleManager) throws SiteWhereException { Binding binding = new Binding(); binding.setVariable("logger", LOGGER); binding.setVariable("deviceBuilder", new DeviceManagementRequestBuilder(deviceManagement)); binding.setVariable("eventBuilder", new DeviceEventRequestBuilder(deviceManagement, deviceEventManagement)); try { // Use the system account for logging "created by" on created elements. SecurityContextHolder.getContext().setAuthentication(SiteWhereServer.getSystemAuthentication()); getConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } finally { SecurityContextHolder.getContext().setAuthentication(null); } }
String message = "Malformed URL: " + root + ", " + resourceName; if (se == null) { se = new ResourceException(message); } else { se = new ResourceException(message, se); se = new ResourceException(message); } else { se = new ResourceException(message, se); if(se == null) se = new ResourceException("No resource for " + resourceName + " was found");
@Override public void initialize(IDeviceManagement deviceManagement, IDeviceEventManagement deviceEventManagement, IAssetManagement assetManagement, IAssetModuleManager assetModuleManager) throws SiteWhereException { // Skip if not enabled. if (!isEnabled()) { return; } Binding binding = new Binding(); binding.setVariable(IGroovyVariables.VAR_LOGGER, LOGGER); binding.setVariable(IGroovyVariables.VAR_DEVICE_MANAGEMENT_BUILDER, new DeviceManagementRequestBuilder(deviceManagement)); binding.setVariable(IGroovyVariables.VAR_EVENT_MANAGEMENT_BUILDER, new DeviceEventRequestBuilder(deviceManagement, deviceEventManagement)); binding.setVariable(IGroovyVariables.VAR_ASSET_MANAGEMENT_BUILDER, new AssetManagementRequestBuilder(assetManagement, assetModuleManager)); try { // Use system account for logging "created by" on created elements. SecurityContextHolder.getContext().setAuthentication(SiteWhereServer.getSystemAuthentication()); getGroovyConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } finally { SecurityContextHolder.getContext().setAuthentication(null); } }
String message = "Malformed URL: " + roots[i] + ", " + resourceName; if (se == null) { se = new ResourceException(message); } else { se = new ResourceException(message, se); se = new ResourceException(message); } else { se = new ResourceException(message, se);
@Override public boolean isFiltered(IDeviceEvent event, IDevice device, IDeviceAssignment assignment) throws SiteWhereException { Binding binding = new Binding(); binding.setVariable("logger", getLogger()); binding.setVariable("event", event); binding.setVariable("device", device); binding.setVariable("assignment", assignment); try { Object result = getConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); if (!(result instanceof Boolean)) { throw new SiteWhereException("Groovy filter script returned non-boolean result."); } return !((Boolean) result).booleanValue(); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy filter script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy filter script.", e); } }
exception = exception == null ? new ResourceException(message) : new ResourceException(message, exception); exception = exception == null ? new ResourceException(message) : new ResourceException(message, exception); exception = new ResourceException("No resource for " + name + " was found");
@Override public void initialize(IAssetModuleManager assetModuleManager, IAssetManagement assetManagement) throws SiteWhereException { // Skip if not enabled. if (!isEnabled()) { return; } Binding binding = new Binding(); binding.setVariable("logger", LOGGER); binding.setVariable("assetBuilder", new AssetManagementRequestBuilder(assetManagement, assetModuleManager)); try { getGroovyConfiguration().getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } }
/** * Interface method for ResourceContainer. This is used by the GroovyScriptEngine. */ public URLConnection getResourceConnection(String name) throws ResourceException { name = removeNamePrefix(name); name = name.replaceAll("\\\\", "/"); //remove the leading / as we are trying with a leading / now if (name.startsWith("/")) name = name.substring(1); /* * Try to locate the resource and return an opened connection to it. */ try { String tryScriptName = "/" + name; URL url = servletContext.getResource(tryScriptName); if (url == null) { tryScriptName = "/WEB-INF/groovy/" + name; url = servletContext.getResource("/WEB-INF/groovy/" + name); } if (url == null) { throw new ResourceException("Resource \"" + name + "\" not found!"); } return url.openConnection(); } catch (IOException e) { throw new ResourceException("Problems getting resource named \"" + name + "\"!", e); } }
@Override public boolean isFiltered(IDeviceEvent event, IDevice device, IDeviceAssignment assignment) throws SiteWhereException { Binding binding = new Binding(); binding.setVariable("logger", getLogger()); binding.setVariable("event", event); binding.setVariable("device", device); binding.setVariable("assignment", assignment); try { Object result = SiteWhere.getServer().getTenantGroovyConfiguration(getTenant()).getGroovyScriptEngine() .run(getScriptPath(), binding); if (!(result instanceof Boolean)) { throw new SiteWhereException("Groovy filter script returned non-boolean result."); } return !((Boolean) result).booleanValue(); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy filter script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy filter script.", e); } }
@Override public URLConnection getResourceConnection(String name) throws ResourceException { LOGGER.debug("Calling getResourceConnection() with " + name); try { URLConnection result = null; if (name.startsWith(ResourceUrlConnection.PROTO_SITEWHERE)) { result = new ResourceUrlConnection(new URL(null, name, handler)); } else { result = getGlobalResourceConnection(name, handler); } try { result.getInputStream(); } catch (IOException e) { throw new ResourceException(e); } return result; } catch (MalformedURLException e) { throw new ResourceException("Invalid resource URL.", e); } }