Archive

Archive for the ‘Uncategorized’ Category

logback configuration for gradle/springboot type projects …

yet another painful trawl to set up logging, and have it behave like spring boot default.

So to basics.

  1. create your gradle project, i’m using Intellij Idea for that.  You need to include the latest springboot-gradle-plugin.  Then ensure you apply the plugin.  Then fill your dependencies, but include latest log back from maven central and Slf4j-api.  This will provide default mapping for logger directly to logback.
  2. group 'org.softwood'
    version '1.0-SNAPSHOT'
    
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
        }
    }
    
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'groovy'
    apply plugin: 'java'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile 'org.apache.camel:camel-spring-boot-starter:2.18.4'
        compile 'org.apache.camel:camel-groovy:2.18.4'
        compile 'org.apache.camel:camel-stream:2.18.4'
        compile 'org.codehaus.groovy:groovy-all:2.4.11'
        //add Logback
        compile 'ch.qos.logback:logback-classic:1.2.3'
        compile 'ch.qos.logback:logback-core:1.2.3'
        compile 'org.slf4j:slf4j-api:1.7.25'
    
        testCompile group: 'junit', name: 'junit', version: '4.11'
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }
    
    
  3. Then put either a logback.groovy or a logback.xml file into the project classpath. By default i’ve used logback.groovy for this groovy project and put it under the resources directory (along with application.yml etc you may want to tailor the spring application)

    I did some digging and if you want coloured output as the spring boot default you need a logback.groovy a bit like this. This does a scan for changes which you can disable, and calls two closures to configure the appenders, and the loggers

    
    import org.springframework.boot.ApplicationPid
    
    import java.nio.charset.Charset
    
    import static ch.qos.logback.classic.Level.*
    import ch.qos.logback.core.ConsoleAppender
    import ch.qos.logback.classic.encoder.PatternLayoutEncoder
    import ch.qos.logback.core.status.OnConsoleStatusListener
    import ch.qos.logback.core.FileAppender
    
    displayStatusOnConsole()
    scan('5 minutes')  // Scan for changes every 5 minutes.
    setupAppenders()
    setupLoggers()
    
    def displayStatusOnConsole() {
        statusListener OnConsoleStatusListener
    }
    
    def setupAppenders() {
    
        //add to converters to handle colour and whitespace
        conversionRule 'clr', org.springframework.boot.logging.logback.ColorConverter
        conversionRule 'wex', org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter
    
    
        def consolePatternFormat = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr([%property{PID} - %thread]){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(>){faint} %m%n%wex"
        def filePatternFormat = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%property{PID} - %thread] %-5level %-12logger{12}:[.%M] > %msg%n%wex"
        if (!System.getProperty("PID")) {
            System.setProperty("PID", (new ApplicationPid()).toString())
        }
    
    
        //def logfileDate = timestamp('yyyy-MM-dd') // Formatted current date.
        // hostname is a binding variable injected by Logback.
        //def filePatternFormat = "%d{HH:mm:ss.SSS} %-5level [${hostname}] %logger - %msg%n"
        //appender('logfile', FileAppender) {
        //    file = "simple.${logfileDate}.log"
        //    encoder(PatternLayoutEncoder) {
        //        pattern = filePatternFormat
        //    }
        //}
    
        // Add custom converter for %smiley pattern.
        //conversionRule 'smiley', SmileyConverter
    
        
        appender('STDOUT', ConsoleAppender) {
            encoder(PatternLayoutEncoder) {
                charset = Charset.forName('UTF-8')
                pattern = consolePatternFormat
                //pattern =
            }
        }
    }
    
    def setupLoggers() {
        //logger 'com.mrhaki.java.Simple', getLogLevel(), ['logfile']
        logger 'application.Application', DEBUG
        root DEBUG, ['STDOUT']
    }
    
    def getLogLevel() {
        (isDevelopmentEnv() ? DEBUG : INFO)
    }
    
    def isDevelopmentEnv() {
        def env =  System.properties['app.env'] ?: 'DEV'
        env == 'DEV'
    }
    
    
  4. As this is a groovy project you can just use @SLF4J annotation to your class and it will inject the log variable automatically – less noise that creating the logger. Now use the logh variable in a method as you need it
    @Slf4j //inject logger
    @SpringBootApplication
    public class Application {
        @Autowired
        ApplicationContext ctx
    ...
    public someMethod () {
                log.debug "log something "
    }
    ...
    

That will give you a basic config that you can extend as you see fit, and add new classes to log as you go.

Advertisements
Categories: Uncategorized

Eclipse high display scale (DPI) – ‘tactical’ fix for Windows

March 26, 2016 Leave a comment

I Eventually found a fix that seems to work for eclipse Mars (2) and Neon builds. It doesn’t seem to have any impact on Eclipse Luna (great shame i was trying to get IBM RSA 9.5 which is based on Luna to scale – failed back to IBM for an upgrade path).  But if you are doing general development work with eclipse on windows with high DPI screen this is massive improvement – it makes it all usable again.

My laptop is running Windows 10, but i suspect this works for Windows 8/8.1 just the same, but i no longer have such a build to try it on.

Essentially this is just a re-post of the answer i found here referenced fix

To get this to work you have to first make an edit to your registry  as follows.  You have to add a new key, and set its value to 1 (true), as follows. The registry structure up to the SideBySide entry should already be in existence when you navigate in the registry browser.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SideBySide\PreferExternalManifest (DWORD) to 1

The next thing you have to do is to create a new text file in the same directory as your eclipse.exe installation.
The file should be called eclipse.exe.manifest and have the following ‘magic’ text cut and past and then saved.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <description>eclipse</description>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel xmlns:ms_asmv3="urn:schemas-microsoft-com:asm.v3"
                               level="asInvoker"
                               ms_asmv3:uiAccess="false">
                </requestedExecutionLevel>
            </requestedPrivileges>
        </security>
    </trustInfo>
    <asmv3:application>
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
            <ms_windowsSettings:dpiAware xmlns:ms_windowsSettings="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</ms_windowsSettings:dpiAware>
        </asmv3:windowsSettings>
    </asmv3:application>
</assembly>

Then stand back and pray. Double click your eclipse.exe file and it should come up
looking as it would in the ‘good’ old days, and the icons will to be legible.

I am sure Neon is supposed to have proper SWT full DPI scaling and detection on release, but i tried Neon-M5 today, and it was still
illegible. This tactical fix makes it work for now – hoorah.

Groovy DSL and example notes to myself

February 22, 2016 Leave a comment

most of this stuff is already out there but i wanted to try and make sure i’d understood command chaining and other tricks

so first off we note that groovy allows your to drop ‘()’ off methods where there are multiple attributes (its still required for no arg method though)

def method1 ()
def method2 (arg1, arg2=null)
def method3 (Map map, arg1, arg2=null, ...)

//then
method2 (someArg) //is equivalent to
method2 someArg

//also with mixed named/unamed attributes groovy collects all named
// attributes as into a 1st map attribute of the method

//these two are equivalent
method3 (address: "the house", 0143 222222, street: "south close")
method3 address: "the house", street: "south close", 0143 222222

Also if your function returns itself or another function (or a closure) then the dot invocation is optional, for the compiler can read this when we drop the ‘.’

def method4 () {
    println "hello"
return this
}
method4().method4 () //is equivalent to 
method4() method4()

so this gives rise to some interesting examples of using groovy to work to build
DSL facilities.

so we’ll build an example up as we go of a groovy script to show some of the options, using a simple robot like DSL which can go in some direction, travel some distance, at some speed.

first i added a couple of static imports for enums that come later in the script

import static Direction.*
import static DistanceUnit.*
import groovy.transform.TupleConstructor

i then defined a couple of enums for a DistanceUnit and Duration as we are going
to embellish Numbers with some additional method facilities

//using static import to get 'left' and 'right' direction  enums -
//see static imports above
enum Direction {
    forward, backward, left, right
}

enum DistanceUnit {
 centimeters ('cm', 0.01),
 meters ('m', 1),
 kilometers ('km', 1000)

 String abbreviation
 double multiplier

 DistanceUnit (String abbr, double mult) {
 this.abbreviation = abbr
 this.multiplier = mult}
}

enum Duration {
    millisecond ('ms', 0.001),
    second ('s', 1),
    hour ('h', 60),
    day ('d', 60*24)

    String abbreviation
    double multiplier

    Duration (String abbr, double mult) {
        this.abbreviation = abbr
        this.multiplier = mult}
}


//define a symbol for hour in script, used when getting a speed = dist/dur
def h = Duration.hour

We the want to create a couple of classes for Speed and Distance, but use the @TupleConstructor annotation so that we can avoid having to type ‘new’ when we wish to create some instances of these classes. Also as we wish to have something like ‘5.km/h’ we need to overload the div operator for the Distance class and return a Speed from this operation.

//avoid having to use new in declaration
@TupleConstructor
class Distance {
    Number amount
    DistanceUnit unit

    //added div overload so that we can have distance over unit of time
    Speed div (Duration dur) {
        new Speed (amount, unit, Duration.hour)
    }

    String toString () {"$amount $unit"}
}
@TupleConstructor
class Speed {
    Number amount
    DistanceUnit unit
    Duration duration

    String toString () {"$amount $unit/$duration.abbreviation"}
}

Now through metaprogramming we can embellish Numbers with some methods to get a Distance like this

//add new meta method to Number- return the number (delegate) as instance of Distance
//global change to Number though - probably want to limit this using categories but forces us to inject
//and use groovy shell evaluate to do the assessment
Number.metaClass.getMeters = {new Distance (delegate, DistanceUnit.meters)}

However this is a global change to Number, and we probably wish to limit the scope of these methods, so lets define a Category instead like this

//limit the scope of added methods to use closure
class DistanceCategory {
    static Distance getCentimetres (Number num) {
        new Distance (num, DistanceUnit.centimeters)
    }

    static Distance getMeters (Number num) {
        new Distance (num, DistanceUnit.meters)
    }

    static Distance getKilometers (Number num) {
        new Distance (num, DistanceUnit.kilometers)
    }

    //short form
    static getCm (Number num) {
        getCentimetres(num)
    }

    static getM (Number num) {
        getMeters(num)
    }

    static getKm (Number num) {
        getKilometers (num)
    }
}

So using the above our first go for a DSL might be to be to define a function like this

def move1 (Direction dir, Distance d) {
        println "move1: moving $dir by $d"
}

//works because 3.meters calls the new metamethod to return instance 
//of Distance.  
//wrap in 'use' closure if using category class
move1 left, 3.meters

thats not too bad, we have removed the parentheses, and invoked the getMeters()
metamethod to return a Distance.

This will print ‘move1: moving left by 3 meters’ on the console

we can get a bit more readable by defining our move method like this. Ive used
the category class this time, not the global addition of meta method to Number.

//when a method call is made with a mix of named and no named params, then grooy puts all
//the named params into a map - as first param, and the rest as unnamed param in the order seen
//so define a new move like this

def move2 (Map m, Direction dir) {
    println "move2: moving $dir by $m.by at $m.at"
}

//use the move DSL and Category to add metamethods
use (DistanceCategory) {
move2 right, by: 3.meters, at: 5.km / h
}

This works as groovy collects the named parameters and drops them into the map,
and provides the remaining attributes, after that.

This writes ‘move2: moving right by 3 meters at 5 kilometers/h’ to the console.

However it would be nice to drop the ‘,’ and the : in the maps to make this more
readable. so here is the last trick using command chains (return a callable class – here a closure for next call)

//using command chains now
def move3 (Direction dir) {
    [by: {Distance dist ->
        [at: {Speed speed ->
            println "move3: moving $dir by $dist, at $speed"
        }]
    }]
}

use (DistanceCategory) {
    move3 right by 3.meters at 5.km / h
    //is the same as
    move3 (right).by (3.meters).at(5.km/h)
}

now when you provide the direction to the move3 method, it returns a map with key ‘by’ and a single closure as its value which is invoked with 3.meters. This
immediately returns another map with key ‘at’, and another closure which is
invoked with the 5km/h (using the overloaded Distance class to pass in a Speed as the attribute to the closure, the ‘h’ property was defined previously in the script binding above.

This can make your head explode walking through the tautology, but if you step through what the compiler sees you should be able to ‘see’ what this does.

Lastly I tried this

def func (arg = null) {
    println "got args as " + arg ?: null
    if (!arg) {
        println "setting up no arg closure in map"
        [by : { -> println "no args closure: hello william "}()]
    }
    else {
        println "setting up with arg closure in map"
        [by: { mapArg -> println "with args closure : $mapArg" }]
    }
}

//println "calling no args on func : "
func() by

//println "calling with args on func : "
println "calling args on func : "
func("someArg") by 3

In the first case we call ‘func’ with no args, and the function definition defines a single map with key ‘by’ that has a closure with no args. as no args as passed you have to add the ‘()’ in the first map to execute the returned closure. In the second case you provide an attribute for func, which sets up a map with key ‘by’ that takes an argument. so you can trigger this with the by 3 and call that closure when it returns.

All this best done with a clear head and step each bit through at a time.

You can also provide the DSL boilerplating in one class and separate the language script in another through using groovy shell evaluate and injecting the compiler customisers, static imports and extended base script to that the evaluate method inherits all the DSL
definition parts like this

def binding = new Binding (//add some variables etc ) 

def importCustomizer = new ImportCustomizer()
importCustomizer.addStaticStars Direction.name //etc 

def conf = new CompilerConfiguration()
conf.addCompilationCustomers (importCustomizer)
conf.scriptBaseClass = SomeDSLBaseScript.name  //specify the script base class

def shell = new GroovyShell (this.class.classLoader, binding, config)
shell.evaluate '''
      move left...
'''
Categories: Groovy, Uncategorized Tags:

On Grails Neo4j and unit integration testing …

March 22, 2014 Leave a comment

Life is never as easy as you imagine …

So first get your grails environment set up, i’m using GGTS 3.5M2 and grails 2.3.7

Create yourself a grails project and wait for all the scripts to finish. Then in your projects /conf/BuildConfig you need to the add the following plugin -> :neo4j:1.1.1, and whilst your at it you need to comment out the database-migration plugin as there is a bug somewhere that stops your integration tests from running. ( i have left the hibernate plugin installed as i am setting the static mapWith clause on the domain classes i want to persist through Neo4j (trying mixed mode persistence and see what happens))

   plugins {
	compile ":neo4j:1.1.1"
		
        // plugins for the build system only
        build ":tomcat:7.0.52.1"

        // plugins for the compile step
        compile ":scaffolding:2.0.2"
        compile ':cache:1.1.1'

        // plugins needed at runtime but not for compilation
        runtime ":hibernate:3.6.10.9" // or ":hibernate4:4.3.4"
        //runtime ":database-migration:1.3.8"
        runtime ":jquery:1.11.0.2"
        runtime ":resources:1.2.7"
        // Uncomment these (or add new ones) to enable additional resources capabilities
        //runtime ":zipped-resources:1.0.1"
        //runtime ":cached-resources:1.1"
        //runtime ":yui-minify-resources:0.1.5"

        // An alternative to the default resources plugin is the asset-pipeline plugin
        //compile ":asset-pipeline:1.6.1"

        // Uncomment these to enable additional asset-pipeline capabilities
        //compile ":sass-asset-pipeline:1.5.5"
        //compile ":less-asset-pipeline:1.5.3"
        //compile ":coffee-asset-pipeline:1.5.0"
        //compile ":handlebars-asset-pipeline:1.3.0.1"
    }

also we’ll start with embeddedGraphDatabase mode – but you still need to inform grails where the backing store is in /conf/DataSource.groovy – else it’ll moan saying it can’t find grails.neo4j configuration if you don’t do this. The directory you point the database in must be writable on your local machine. I added mine just above the environments section in DataSource.groovy like this

grails {
	neo4j {
			type = "embedded"
			location = "users/802518659/Documents/data/neo4j"
			params = []
	}
}
...

The plugins user guide can be found here

With that in place – your ready to go. You start as normal and write a domain class – say something like this. Note the mapWith clause to tell grails to use Neo

domain:com.softwood.interaction.groovy

class Interaction {

String name
//List tasks //declared i wanted a list not a set

static hasMany = [tasks: Task]

static mapWith = “neo4j”

static constraints = {
name nullable:true
}
}
[/sourcode]

Now some quick notes on testing. When you create a domain class GGTS will automatically create a spoc unit test for you (new default in GGTS).

So now some more fun. Spoc is great, and i’ll post something separately about spoc and grails.

For now you need to know a couple of things (and i forgot of course). Grails does lots of Dynamic additions to your domain classes – so what looks simple as you type generates a complex class with loads of extra ‘Grails’ goodness including addTo() methods if you have a one to many defined on the one side.

However, in unit testing mode this embellishment does not take place and you pretty much get your plain POJO. This can be confusing as you imagine that this embellishment always happens. It does in integration and real running (the grails framework is brought up for both ) but not in unit testing

so if you write a test like this for the domain class like this

unit test:InteractionSpec

@TestFor(Request)
@Mock (Task) // add this to Mock the collaborator class
class RequestSpec extends Specification {
..
void “test create”() {
given:

Interaction req = new Interaction (name:”job1”)

when:
assertTrue req.validate()
req.save (flush:true) //

def task1 = new Task (name: ‘new task’)
assert task1
assert task1.validate()

req.addToTasks (task1) //fails unit test – cant find addToTasks method

[/sourceocde]

Most disconcerting, So whats going on. Its a unit test so that extra goodness hasn’t been added. This is what i’d forgotten. If you look in the manual it shows you about unit testing a controller say and using the @Mock ([…]) for the domain collaborators. But here your trying to unit test the domain class itself.

Once you get that then there are two ways round it. One the brute force path, is to do the work manually like this

...
		req.tasks = new HashSet()
		task1.job = req
		req.tasks.add (task1)
		req.save(flush:true ) //often have to flush this in unit test else grails wont write immediately

However this is alot of extra work. The other approach is to use the @Mock ([…]) after the @TestFor () like this, and add the domain class and any collaborator classes to the array of classes to Mock. You’ll now note that the respondsTo method assertion now passes, and the whole test will pass.

@TestFor(Interaction)
@Mock ([Interaction, Task])
class InteractionSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test create"() {
				
		given: 
		def interaction = new Interaction (name: "first job")
		def task1 = new Task (name:"task1")
		def task2 = new Task (name:"task2")
		
		when:
		assert interaction.respondsTo ("addToTasks")
		def res =   interaction.save (flush: true)
		Interaction thing = Interaction.get (1)
		thing.addToTasks (task1)
		thing.addToTasks (task2)
		
		def tasks = thing.tasks.collect{it.name}.sort()
		
		then:
		assert thing.name == "first job"
		assert thing.tasks.size() == 2
		assert ["task1", "task2"] == tasks
		
    }
}

%d bloggers like this: