Grails Bootstrap XmlSlurper Service no result - grails

I'm new to Grails and working on my little project. What I have problem with is my XML reading Service - ReadService - has a function read() that returns XmlSlurper(). Function reads the xml file, as it's write it in console with println. But when I try to print the result XmlSlurper in Bootstrap, I don't get anything.
class BootStrap {
def ReadService
def init = {
servletContext ->
def xml = ReadService.read()
println xml
}
}
Can someone tell me what I am doing wrong?

It looks like you just have misspelled your service.
Your ReadService must be saved in grails-app/services.
To use the service you have to declare them like this
def readService
and within init you could use them as follows:
def xml = readService.read()
As you can see, the first letter must be lowercase.

Okay, I see now the problem isn't with reading the xml itself, rather with bootstraping with it, as changing Service name to lovercase helped. I use ReaderService to read xml file with addresses and build address objects. Here is mine Bootstrap code:
`
class BootStrap {
def readService
def init = {
servletContext ->
def xml = readService.read()
println xml
def adress = new mobilmed.Adres(ulica:xml.adress.street, dom:xml.adress.house, miejscowość:xml.adress.residance, kodPocztowy:xml.adress.postCode, poczta:xml.adress.post).save()
if(adres.hasErrors()){
println adres.errors}
}
}
When I run it, I got classical "no such property" error.
| Error 2012-08-28 06:42:36,784 [pool-7-thread-1] ERROR context.GrailsContextLoader - Error executing bootstraps: No such property: adres for class: BootStrap
Message: No such property: adres for class: BootStrap
Line | Method
->> 10 | doCall in BootStrap$_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 301 | evaluateEnvironmentSpecificBlock in grails.util.Environment
| 294 | executeForEnvironment . . . . . in ''
| 270 | executeForCurrentEnvironment in ''
| 303 | innerRun . . . . . . . . . . . . in java.util.concurrent.FutureTask$Sync
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . . . . . . . . . . . . . in java.lang.Thread
Of course, i checked, and Boostraping for defined variables works just fine, like this:
def karolzam = new mobilmed.Adres(ulica:"Turkusowa", dom:"8/22", miejscowość:"Lublin", kodPocztowy:"20-572", poczta:"Lublin").save()
if(karolzam.hasErrors()){
println karolzam.errors}
Looks like the Bootstrap don't "seeing" variables that can print on console. :/

Related

Spring securtiy Acl throwing nullpointer on addPermission

I am using Springsecurity-ACL-2.0-RC1 and webflow 2.0.8.1
So when calling my Service to addPermission via:
service.addPermission(domainobject, username, permission)
which basically is:
void addPermission(def domainobject, String username,
Permission permission) {
aclUtilService.addPermission domainobject.class, domainobject.id, username, permission
}
I will get a NullPointer:
ERROR builder.ClosureInvokingAction - Exception occured invoking flow action: Cannot get property 'className' on null object
Message: Cannot get property 'className' on null object
Line | Method
->> 263 | createAcl in grails.plugin.springsecurity.acl.jdbc.GormAclLookupStrategy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 229 | doCall in grails.plugin.springsecurity.acl.jdbc.GormAclLookupStrategy$_convertEntries_closure4
| 228 | convertEntries . . . . in grails.plugin.springsecurity.acl.jdbc.GormAclLookupStrategy
| 146 | lookupObjectIdentities in ''
| 107 | doCall . . . . . . . . in grails.plugin.springsecurity.acl.jdbc.GormAclLookupStrategy$_readAclsById_closure1
| 76 | readAclsById in grails.plugin.springsecurity.acl.jdbc.GormAclLookupStrategy
| 288 | readAclsById . . . . . in grails.plugin.springsecurity.acl.AclService
| 268 | readAclById in ''
| 259 | readAclById . . . . . in ''
| 177 | updateAcl in ''
| 90 | addPermission . . . . in grails.plugin.springsecurity.acl.AclUtilService
| 55 | addPermission in ''
Well I basically have tried to rewrite my service, based on the Tutorial/Example-Application - even used the code from them with basically no result.
So I am running out of ideas on how to fix it, I am thinking about removing the webflows, and simply build this flow on my own.
Thank you
Simon
PS: This is my first entry on stackoverflow, so if I have missed crucial information just tell me, I will add it as soon as possible
looks like youre not passing a Domainobject
Domainobject foo = bar
service.addPermission(foo, username, permission)

trouble bootstrapping data with Grails Audit-Trail plugin installed

I am working on a Grails 2.3.4 application. I am adding the Audit-Trail Plugin v2.0.3 to our project because we have a requirement to track all create and modify times and users to help debug problems that might arise. I have sucessfully installed the plugin and everything works EXCEPT that we cannot bootstrap data in the development enviornment anymore.
This is in our Config.groovy:
plugin {
audittrail {
createdBy.field = "createUser"
createdBy.type = "java.lang.String"
createdDate.field = "createDate"
editedBy.field = "updateUser"
editedBy.type = "java.lang.String"
editedDate.field = "updateDate"
/*
This closure returns the value of the user for the plugin.
*/
currentUserClosure = { ctx ->
ctx.userServiceProxy.getUserToken()
}
}
Here is the UserService
package com.perceptivecloud.frg.datadefinition
/**
* Service class to help the Audit-Trail plugin get userId information.
*
* TODO:This will need to be revisited when real Authorization is implemented.
*/
class UserService {
/*
force a new instance of the service per session.
see http://grails.org/doc/latest/guide/services.html#scopedServices for details
*/
static scope = "session"
String userToken
void setUserToken(String userId) {
userToken = userId
}
String getUserToken() {
userToken
}
}
The proxy service is defined in resources.groovy:
userServiceProxy(ScopedProxyFactoryBean) {
targetBeanName = 'userService'
proxyTargetClass = true
}
Our Domain class looks like:
#AuditStamp
class MyDomainClass {
String name
static constraints = {
name nullable: false
}
}
Our BootStrap.groovy looks like:
def userServiceProxy
...
development {
userServiceProxy.setUserToken("bootstrap")
def claims = new MyDomainClass(name: 'Submit claim')
claims.save(flush: true)
}
When I start up the app, I get a stack trace that looks like:
| Error 2014-03-13 13:10:48,963 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Error creating bean with name 'userService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Message: Error creating bean with name 'userService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Line | Method
->> 41 | doCall in BootStrap$_closure1_closure2_closure4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 321 | execute in grails.util.Environment$EnvironmentBlockEvaluator
| 302 | executeForEnvironment . . . in grails.util.Environment
| 277 | executeForCurrentEnvironment in ''
| 262 | run . . . . . . . . . . . . in java.util.concurrent.FutureTask
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run in java.lang.Thread
When I tried to create a session in bootstrap like so:
development {
WebUtils.storeGrailsWebRequest(new GrailsWebRequest(new GrailsMockHttpServletRequest(),
new GrailsMockHttpServletResponse(), (ServletContext)servletContext))
WebUtils.retrieveGrailsWebRequest().applicationContext.getBean('userServiceProxy')
.setUserToken("userToken")
def claims = new MyDomainClass(name: 'Submit claim')
claims.save(flush: true)
}
I got the following error.
| Error 2014-03-13 12:50:34,179 [localhost-startStop-1] ERROR hibernate.AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in MyDomainClass entry (don't flush the Session after an exception occurs)
Line | Method
->> 112 | doCall in Config$_run_closure2_closure7_closure11_closure12
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 67 | currentUserId in grails.plugin.audittrail.AuditTrailHelper
| 45 | doCall . . . . . . . . . . . in grails.plugin.audittrail.AuditTrailInterceptor$_onSave_closure1
| 37 | onSave in grails.plugin.audittrail.AuditTrailInterceptor
| 46 | doCall . . . . . . . . . . . in BootStrap$_closure1_closure2_closure4
| 321 | execute in grails.util.Environment$EnvironmentBlockEvaluator
| 302 | executeForEnvironment . . . in grails.util.Environment
| 277 | executeForCurrentEnvironment in ''
| 262 | run . . . . . . . . . . . . in java.util.concurrent.FutureTask
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run in java.lang.Thread
From what these messages tell me, somehow I need to be able to save the objects within a request or session context, and I do not know how to do that.
At absolute minimum, the problem I am trying to solve is making sure there is consistent data in the database every time the development environment is fired up while using the Audit-Trail plugin.

Spring Security Core Plugin 2.0-RC2 upgrade issue?

I have a grails project and I just upgraded my Spring Security Core plugin to the latest version however I get the following message when trying to run the app:
| Error Compilation error: startup failed:
Compile error during compilation with javac.
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/DatabaseOnlyLdapAuthoritiesPopulator.java:20: cannot find symbol
symbol : class GrailsUserDetailsService
location: package org.codehaus.groovy.grails.plugins.springsecurity
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService;
^
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/DatabaseOnlyLdapAuthoritiesPopulator.java:37: cannot find symbol
symbol : class GrailsUserDetailsService
location: class org.codehaus.groovy.grails.plugins.springsecurity.ldap.DatabaseOnlyLdapAuthoritiesPopulator
private GrailsUserDetailsService _userDetailsService;
^
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/DatabaseOnlyLdapAuthoritiesPopulator.java:71: cannot find symbol
symbol : class GrailsUserDetailsService
location: class org.codehaus.groovy.grails.plugins.springsecurity.ldap.DatabaseOnlyLdapAuthoritiesPopulator
public void setUserDetailsService(final GrailsUserDetailsService service) {
^
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/GrailsLdapAuthoritiesPopulator.java:20: cannot find symbol
symbol : class GrailsUserDetailsService
location: package org.codehaus.groovy.grails.plugins.springsecurity
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService;
^
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/GrailsLdapAuthoritiesPopulator.java:36: cannot find symbol
symbol : class GrailsUserDetailsService
location: class org.codehaus.groovy.grails.plugins.springsecurity.ldap.GrailsLdapAuthoritiesPopulator
private GrailsUserDetailsService _userDetailsService;
^
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/GrailsLdapAuthoritiesPopulator.java:147: cannot find symbol
symbol : class GrailsUserDetailsService
location: class org.codehaus.groovy.grails.plugins.springsecurity.ldap.GrailsLdapAuthoritiesPopulator
public void setUserDetailsService(final GrailsUserDetailsService service) {
^
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/GrailsLdapUserDetailsManager.java:3: cannot find symbol
symbol : class GrailsUserDetailsService
location: package org.codehaus.groovy.grails.plugins.springsecurity
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService;
^
/home/dev/.grails/2.1.0/projects/app/plugins/spring-security-ldap-1.0.6/src/java/org/codehaus/groovy/grails/plugins/springsecurity/ldap/GrailsLdapUserDetailsManager.java:13: cannot find symbol
symbol: class GrailsUserDetailsService
public class GrailsLdapUserDetailsManager extends LdapUserDetailsManager implements GrailsUserDetailsService {
^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
8 errors
Can someone please help with this?
Thanks
**EDIT****
I have now gone through the plugins and core files and made sure all new Spring Security imports are correct. The app now compiles fine however when I run it and try to access the home page I get the following errors:
| Running Grails application
Configuring Spring Security UI ...
... finished configuring Spring Security UI
Configuring Spring Security Core ...
... finished configuring Spring Security Core
| Error 2014-03-10 11:44:51,598 [pool-7-thread-1] ERROR plugins.DefaultGrailsPluginManager - Error configuring dynamic methods for plugin [springSecurityCore:2.0-RC2]: null
Message: null
Line | Method
->> 308 | compileStaticRules in grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 218 | initialize in ''
| 738 | initializeFromAnnotations in SpringSecurityCoreGrailsPlugin
| 599 | doCall in SpringSecurityCoreGrailsPlugin$_closure3
| 303 | innerRun . . . . . . . . in java.util.concurrent.FutureTask$Sync
| 138 | run in java.util.concurrent.FutureTask
| 895 | runTask . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run in ''
^ 662 | run . . . . . . . . . . . in java.lang.Thread
| Server running. Browse to http://localhost:8080/my_app
| Error 2014-03-10 11:49:36,541 [http-bio-8080-exec-2] ERROR [/my_app].[gsp] - Servlet.service() for servlet [gsp] in context with path [/my_app] threw exception
Message: null
Line | Method
->> 273 | isAjax in grails.plugin.springsecurity.SpringSecurityUtils
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 45 | determineUrlToUseForThisRequest in grails.plugin.springsecurity.web.authentication.AjaxAwareAuthenticationEntryPoint
| 53 | doFilter . . . . . . . . . . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
| 49 | doFilter in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter
| 82 | doFilter . . . . . . . . . . . in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
| 895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run . . . . . . . . . . . . . . in ''
^ 662 | run in java.lang.Thread
Can someone please offer any guidance on this?
Thanks
Try cleaning all output artefacts
grails clean-all
Kill all java processes
Clear caches of your IDE and restart the IDE and retry.
I got this error when using Intellij Idea and ReflectionUtils class was having inconsistent behavior. Possibly a class caching issue.
You might have to upgrade to spring-security-ldap plugin to 2.0-RC2 in order to use Spring Security Core 2.0_RC2.
Reason:
Notice the change in package name in latest version of security core plugin.
org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService
grails.plugin.springsecurity.userdetails.GrailsUserDetailsService
I discovered that if you are using annotations, you need to provide an array of roles and no longer a single string:
#Secured(['IS_AUTHENTICATED_FULLY'])

Default plugins errors after upgraded to Grails 2.2.2 from 2.2.0

After upgrading from 2.2.0 to 2.2.2 I'm getting the following errors when I run grails:
2013-05-08 16:23:34,957 [localhost-startStop-1] DEBUG support.PluginAwareResourceBundleMessageSource - Could not resolve any resources for plugin resources-1.1.6
Message: class path resource [web-app/file:/Users/charlie/.grails/2.2.2/projects/gpa/resources/plugins/resources-1.1.6/grails-app/i18n/] cannot be resolved to URL because it does not exist
Line | Method
->> 70 | getObject in org.grails.datastore.gorm.bean.factory.AbstractMappingContextFactoryBean
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 109 | postProcessBeanFactory in org.grails.datastore.gorm.plugin.support.PersistenceContextInterceptorAggregator
| 303 | innerRun . . . . . . . in java.util.concurrent.FutureTask$Sync
| 138 | run in java.util.concurrent.FutureTask
| 895 | runTask . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run in ''
^ 680 | run . . . . . . . . . in java.lang.Thread
I get several of those for each of the plugins. However I get one for hibernate too which is commented out:
2013-05-08 16:23:35,032 [localhost-startStop-1] DEBUG support.PluginAwareResourceBundleMessageSource - Could not resolve any resources for plugin hibernate-2.2.2
Message: class path resource [web-app/file:/Users/charlie/.grails/2.2.2/projects/gpa/resources/plugins/hibernate-2.2.2/grails-app/i18n/] cannot be resolved to URL because it does not exist
Line | Method
->> 70 | getObject in org.grails.datastore.gorm.bean.factory.AbstractMappingContextFactoryBean
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 109 | postProcessBeanFactory in org.grails.datastore.gorm.plugin.support.PersistenceContextInterceptorAggregator
| 303 | innerRun . . . . . . . in java.util.concurrent.FutureTask$Sync
| 138 | run in java.util.concurrent.FutureTask
| 895 | runTask . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run in ''
^ 680 | run . . . . . . . . . in java.lang.Thread
Here is my BuildConfig.groovy:
plugins {
//runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.8.3"
runtime ":resources:1.1.6"
compile ":lesscss-resources:1.3.3"
// Uncomment these (or add new ones) to enable additional resources capabilities
//runtime ":zipped-resources:1.0"
//runtime ":cached-resources:1.0"
//runtime ":yui-minify-resources:0.1.4"
build ":tomcat:$grailsVersion"
//runtime ":database-migration:1.2.1"
//compile ":cache:1.0.1"
compile ":mongodb:1.2.0"
compile ":mail:1.0.1"
compile ":bcrypt:1.0"
}
The thing is it runs, but for some reason I can't figure out why these errors are being printed out. Any help? And why is it complaining about Hibernate when its commented out?
It looks like you're running a master application with additional plugins not mentioned in your paste thus the use of PluginAwareResourceBundleMessageSource.
I would recommend the following in this order :
rm -rf ~/.grails/2.2.2/projects/gpa
rm -rf YOUR_PROJECT_FOLDER/target
grails clean
grails refresh-dependencies
grails YOUR_ADDITIONAL_SYSTEM_PROPERTIES_IF_ANY run-app

Grails 2.2.1 is not reading plugin Config file

I have an app and a plugin built with Grails 2.1.1 that i upgraded to 2.2.1 and I am running into a weird issue. My application started blowing up all over because configuration values defined in my app Config.groovy all of a sudden resolve to groovy.Util.ConfigObject instead of their actual values. This is part 1 of the problem, part 2 is that in my app, I define in my app's Config.groovy
grails.config.defaults.locations = [
"classpath:DemoPluginConfig.groovy",
]
DemoPluginConfig.groovy contains this property
oo.memcached.timeout=400000
and the DemoPluginConfig.groovy file is in the src/java folder of my Demo plugin.
I have a memcache service where i am trying to set that timeout property once the service is ready:
def void afterPropertiesSet() {
ConnectionFactoryBuilder cfb = new ConnectionFactoryBuilder()
def config = grailsApplication.config;
def operationTimeOut = config.oo.memcached.timeout
cfb.setOpTimeout(timeOut)
}
grails run-app blows up at this point with error:
Caused by MissingMethodException: No signature of method: net.spy.memcached.ConnectionFactoryBuilder.setOpTimeout() is applicable for argument types: (groovy.util.ConfigObject) values: [[:]]
Possible solutions: setOpTimeout(long)
->> 55 | unwrap in org.codehaus.groovy.runtime.ScriptBytecodeAdapter
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 46 | call in org.codehaus.groovy.runtime.callsite.PojoMetaClassSite
| 45 | defaultCall . . . . . . . . . . in org.codehaus.groovy.runtime.callsite.CallSiteArray
| 108 | call in org.codehaus.groovy.runtime.callsite.AbstractCallSite
| 116 | call . . . . . . . . . . . . . in ''
| 20 | afterPropertiesSet in com.millennialmedia.ui.core.MemcachedService
| 1514 | invokeInitMethods . . . . . . . in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
| 1452 | initializeBean in ''
| 519 | doCreateBean . . . . . . . . . in ''
| 122 | doCreateBean in org.codehaus.groovy.grails.commons.spring.ReloadAwareAutowireCapableBeanFactory
| 456 | createBean . . . . . . . . . . in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
| 271 | resolveInnerBean in org.springframework.beans.factory.support.BeanDefinitionValueResolver
| 126 | resolveValueIfNecessary . . . . in ''
| 1360 | applyPropertyValues in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
| 1118 | populateBean . . . . . . . . . in ''
If i try to run grails run-app a couple more time it starts up successfully but the app blows up on loading a page with another groovy.Util.ConfigObject is not serializable error. Step by step debugging before the app blows up shows that by the time we get to the setOptTimeOut line, grailsApplication.config only contains config options from my app's Config.groovy, as if the values from the plugin DemoPLuginConfig.groovy were just ignored.
Reverting back to 2.1.1 brings everything back to normal. At this point I have no idea where to look.
Shouldn't you be using operationTimeOut instead of timeOut below
cfb.setOpTimeout(timeOut)
Try to put your Groovy configuration in grails-app/conf instead of src/java. Then, see if you can reproduce your error on a consistent basis.
If all fails, grails clean may prevail.
Hope it helps

Resources