groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject Re: Convert @Grab to Grape.grab ?
Date Mon, 20 Jan 2020 23:18:23 GMT
What about:
classLoader: getClass().classLoader.rootLoader


On Tue, Jan 21, 2020 at 9:16 AM Nelson, Erick <Erick.Nelson@hdsupply.com>
wrote:

> I’m still missing something.
>
>
>
> *$ cat grabtest*
>
> #!/usr/bin/env /opt/apps/tools/groovy/bin/groovy
>
>
>
> @Grab('com.hdsupply:script:5.0')
>
> @groovy.transform.BaseScript(script.BaseScript)
>
> import script.*
>
>
>
> /*
>
> @Grapes([
>
>   @GrabConfig(systemClassLoader=true),
>
>   @Grab('com.oracle:ojdbc6:11.2.0.1.0'),
>
> ])
>
> import groovy.sql.Sql
>
> */
>
>
>
> println TestOracle.testConnect()
>
>
>
> class TestOracle {
>
>     static groovy.sql.Sql testConnect () {
>
>          groovy.grape.Grape.grab(classLoader:
> *java.lang.ClassLoader.getSystemClassLoader(),*
>
>                                  group:'com.oracle', module:'ojdbc6',
> version:'11.2.0.1.0')
>
>          groovy.sql.Sql.newInstance([user:'tauser02',
> driverClassName:'oracle.jdbc.OracleDriver',
>
>                            url:'jdbc:oracle:thin:@//
> xxxxxxxxxx.com:1521/ecprd4',
>
>                            password:’xxxxxx’])
>
>     }
>
> }
>
>
>
>
>
> *$ ./grabtest -v*
>
> 18:11:06,854 INFO  grabtest - begin
>
> 18:11:06,859 INFO  grabtest - [script.args [-v], cli options [help =
> false, verbose = true, debug = false, test = false]]
>
> 18:11:06,864 INFO  grabtest - loading property file
> [/opt/apps/tools/.grscript/application.properties]
>
> 18:11:07,469 FATAL grabtest - unhandled exception
>
> *java.sql.SQLException: No suitable driver found for
> jdbc:oracle:thin:@//ecprd4.hdsupply.net:1521/ecprd4
> <http://ecprd4.hdsupply.net:1521/ecprd4>*
>
>         at java.sql.DriverManager.getConnection(DriverManager.java:689)
>
>         at java.sql.DriverManager.getConnection(DriverManager.java:247)
>
>         at groovy.sql.Sql.newInstance(Sql.java:598)
>
>         at groovy.sql.Sql$newInstance.call(Unknown Source)
>
>         at
> org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
>
>         at
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
>
>         at
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
>
>         at TestOracle.testConnect(grabtest:21)
>
>         at TestOracle$testConnect.call(Unknown Source)
>
>         at
> org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
>
>         at
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
>
>         at
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:119)
>
>         at grabtest._run_script(grabtest:15)
>
>         at script.BaseScript.run(BaseScript.groovy:86)
>
>         at
> groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
>
>         at groovy.lang.GroovyShell.run(GroovyShell.java:376)
>
>         at groovy.lang.GroovyShell.run(GroovyShell.java:365)
>
>         at groovy.ui.GroovyMain.processOnce(GroovyMain.java:592)
>
>         at groovy.ui.GroovyMain.run(GroovyMain.java:336)
>
>         at groovy.ui.GroovyMain.access$1400(GroovyMain.java:69)
>
>         at groovy.ui.GroovyMain$GroovyCommand.process(GroovyMain.java:295)
>
>         at groovy.ui.GroovyMain.processArgs(GroovyMain.java:134)
>
>         at groovy.ui.GroovyMain.main(GroovyMain.java:116)
>
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>
>         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>
>         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>
>         at java.lang.reflect.Method.invoke(Method.java:498)
>
>         at
> org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:110)
>
>         at
> org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:128)
>
> 18:11:07,476 INFO  grabtest - end [0.911 seconds]
>
>
>
>
>
>
>
> *From: *Paul King <paulk@asert.com.au>
> *Reply-To: *"users@groovy.apache.org" <users@groovy.apache.org>, "
> paulk@asert.com.au" <paulk@asert.com.au>
> *Date: *Monday, January 20, 2020 at 2:56 PM
> *To: *"users@groovy.apache.org" <users@groovy.apache.org>
> *Subject: *Re: Convert @Grab to Grape.grab ?
>
>
>
> Yes, that sounds right.
>
>
>
> On Tue, Jan 21, 2020 at 8:52 AM Jason S <jstell@gmail.com> wrote:
>
> Maybe
>
>
>
> groovy.grape.Grape.grab(classLoader: ClassLoader.getSystemClassLoader(),
> group: 'com.oracle.ojdbc', module: 'ojdbc8', version: '19.3.0.0')
>
>
>
> ... though that could fail with a Security manager exception.
>
>
>
> On Mon, Jan 20, 2020 at 4:46 PM Nelson, Erick <Erick.Nelson@hdsupply.com>
> wrote:
>
> Ya, ive tried that, it’s the grabconfig systemClassLoader=true  that is
> making this difficult for me (I think).
>
> I get the usual error as if I did not put the grab config in the @grab
> annotation …
>
>
>
> java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@//
> ecprd4.hdsupply.net:1521/ecprd4
>
>
>
> I’m still going through groovy.grape.GrabAnnotationTransformation to see
> how it is done there.
>
>
>
>
>
> *From: *Paul King <paulk@asert.com.au>
> *Reply-To: *"users@groovy.apache.org" <users@groovy.apache.org>, "
> paulk@asert.com.au" <paulk@asert.com.au>
> *Date: *Monday, January 20, 2020 at 2:32 PM
> *To: *"users@groovy.apache.org" <users@groovy.apache.org>
> *Subject: *Re: Convert @Grab to Grape.grab ?
>
>
>
> It's certainly possible since that is what the compiler does. The compiler
> goes the extra step and adds the jar onto the classpath at compilation time
> as well but you might not need that.
>
>
>
> From a script you'd do something like:
> groovy.grape.Grape.grab(classLoader: getClass().classLoader, group:
> 'com.oracle.ojdbc', module: 'ojdbc8', version: '19.3.0.0')
>
>
>
> Depending on your context you might need to get the classloader in
> a slightly different way.
>
>
>
> Cheers, Paul.
>
>
>
>
>
>
>
> On Tue, Jan 21, 2020 at 6:36 AM Nelson, Erick <Erick.Nelson@hdsupply.com>
> wrote:
>
> Is it possible to convert a Grab annotation to a Grapes.grab call?
>
>
>
> Example:
>
> Convert this…
>
>
>
> @Grapes([
>
>   @GrabConfig(systemClassLoader=true),
>
>   @Grab('com.oracle:ojdbc6:11.2.0.1.0'),
>
> ])
>
> import groovy.sql.Sql
>
>
>
> to this…
>
>
>
> groovy.grape.Grape.grab(group:'com.oracle', module:'ojdbc6',
> version:'11.2.0.1.0')
>
>
>
>
>
> I’m assuming I’d need to add map entries for classLoader: and refObject:
> but I’m not sure how to assemble these if needed.
>
>
>
> I’d like to put the Grapes.grab call in my compiled library.  Is this even
> possible?
>
>
>
> My library connects to oracle like this…
>
>
>
> Sql.withInstanceOracle(user:'tauser03',name:'ecprd4') { sql -> }
>
>
>
> The static method withInstanceOracle is a metaClass method that under the
> hood calls this…
>
>
>
>
>
>     *static* Sql newInstance (Map map) {
>
>         // groovy.grape.Grape.grab(group:'com.oracle', module:'ojdbc6',
> version:'11.2.0.1.0')
>
>         Map connectionMap = *buildConnectionMap*(map)
>
>         *log*.info "connecting to [${connectionMap.url}] as
> [${connectionMap.user}]"
>
>         Sql sql = Sql.*newInstance*(connectionMap)
>
>         sql.connection.setAutoCommit(*false*)
>
>         sql
>
>     }
>
>
>
> In my Oracle class.
>
>

Mime
View raw message