Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Friday, April 5, 2019

Java 11 and Groovy Compatibility and JAXBContext !


If you see the following error during compile : 


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileTestGroovy'.
> org/codehaus/groovy/ast/MethodCallTransformation


Add the folling line to your gradle file :

ext['groovy.version'] = '2.5.6'  //i.e your grovy version.


Another issues with java 11

 Unable to load class groovy.xml.jaxb.Jaxb GroovyMethods due to missing dependency javax/xml/bind/JAXBContext 

You can fix this by adding the following dependency on the latest groovy version


Read More...

Tuesday, November 3, 2009

Converting Groovy Array type to Java Array Type

Following is the code snipped in groovy that demonstrate the conversion Groovy Array type to Java Array Type.
Code for Groovy

def param=[]
param+="paramater 1"
param+="paramater 2"
param+="paramater 3"
param+="paramater 4"
def stringArgForJava = (String[])params


Test.callJavaClass(stringArgForJava) //static method


The sample java class is as follow;

class Test
{
public static void callJavaClass(String[] arg)
{
for(int i=0;i {
System.out.println("params "+i ":" +params[i]);
}

}
}








Read More...

Tuesday, October 13, 2009

Read a variable defined on the config file from a controller in a grail application



Let's say, you have the config.groovy file as follow.


...........
..........

environments {
production {
myVariable1="I am variable1 on RPOD"
myVariable1="I am variable1 on RPOD"
}



development {
myVariable1="I am variable1 on DEV"
myVariable1="I am variable1 on RPOD"
}

log4j {
appender.stdout = "org.apache.log4j.ConsoleAppender"
.........
........

Now the above varaible can be easily access through any of the controller as follow.


def config = ConfigurationHolder.config
def myVaraible1 = config.myVaraible1
def myVaraible2 = config.myVaraible2



Read More...

Wednesday, July 8, 2009

Reading the status code of HTTP Request !

When you make an HTTP request to any server url throuh your client software, lets say IE, HTTP Status codes are returned by the server to the client software to determine the outcome of your request. There are five different classes of status code range. They are
Status codes 100-101
Status codes 200-206
Status codes 300-307
Status codes 400-417
Status codes 500-505

Following is a simple program written in groovy that checks and returns the status code of any url.

import java.net.URL;
def url
def message =""
boolean fail=true
def urlString="http://192.168.0.1/test/test.html"
println checkURL(urlString)

def checkURL(urlString){
int status_code
def tempmessage=""
URL URLserver = new URL("urlString");
try{
URLConnection connection = (HttpURLConnection)URLserver.openConnection();

status_code = connection.getResponseCode();


switch (status_code)
{
case 200:
tempmessage=" Connection Successful"
break;
case 401:
tempmessage="UnAuthorised"
break;
case 405 :
tempmessage= " Resource Not Found"
break;
case 408:
tempmessage="Request Time-out"
break;
case 500:
tempmessage="Internal Server Error"
break;
case 502:
tempmessage="Bad Gateway"
break;
case 503:
tempmessage="Service Temporarily Unavailable"
break;
default :
tempmessage= connection.getResponseMessage()
break;

}
}
catch (Exception exc){
tempmessage=exc

}

return "\n "+urlString+" :"+status_code+":"+tempmessage;

}




Visit http://www.w3.org/Protocols/HTTP/HTRESP.html for detail about the status codes.



Read More...

Friday, January 30, 2009

Read Environment Variables with Groovy

Environment variables can be easily accessed with Groovy with getenv() method as shown below.


def env = System.getenv()
//Print all the environment variables.

env.each{
println it
}
// You can also access the specific variable, say 'username', as show below
String user= env['USERNAME']


Read More...

Wednesday, July 16, 2008

Generate Random Alphanumeric Random String of N Characters with Groovy or Java

I was googling for a Alpha-numeric Random String generation. I come up with two solution. One option is to generate Random Alphanumeric Strings with RandomStringUtils available on Apache Common . The following section of code is borrowed from java2s.com


import org.apache.commons.lang.RandomStringUtils;
public class RandomStringUtilsTrial {
public static void main(String[] args) {

//Random 8 chars string where letters are enabled while numbers are not.
System.out.print("8 char string using letters but no numbers >>>");
System.out.println(RandomStringUtils.random(8, true, false));

}
}


Other Option can be using the Java Uitil class Random as shown below. You don’t have to worry about Apache Common API. The following code section (written for Groovy) generates 100 '32 characters' long Alphanumeric Random string.



private static String validChars ="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"
private int IDlength=32
int maxIndex = validChars.length()
for(i in 0..99)
{ String resultID = ""
java.util.Random rnd = new java.util.Random(System.currentTimeMillis()*(new java.util.Random().nextInt()))
for ( i in 0..IDlength ) {
int rndPos = Math.abs(rnd.nextInt() % maxIndex);
resultID += validChars.charAt(rndPos)
number=resultID
}

println resultID
}



Good luck !
Read More...

Wednesday, May 7, 2008

Read custom created XML file from a Groovy/Java class in a J2EE Web Application

I have a following xml file that I created to store the application name and Application Owner email address

//File name: EmailInfo.xml

< emailInfo>
< application> < name> Saleways < /name> < email> david@david.com< /email> < /application>
< application> < name> Mercantile House< /name> < email> binod@hotmail.com< /email> < /application>
< application> < name> BishowShop< /name> < email> bishow@hotmail.com< /email> < /application>
< /emailInfo>


Now I have a J2EE application that contains a Groovy or a Java class that access the above XML file. The method to access above html file using Groovy is presented below. Note that the xml file resides on the same location where the class exists.


def appName=[]
def appEmail=[]
java.net.URL url= this.getClass().getClassLoader().getResource("EmailInfo.xml");
java.net.URI uri = new URI(url.toString())
File f=new File(uri)
def emailInfo = new XmlSlurper().parse(f) //Specific to groovy
emailInfo.application.name.each {appName += it} //specific to groovy
emailInfo.application.email.each {appEmail += it} // specific to groovy

You simply can modify the above code for java classes. The final result for the above code is that appName contains the list of the Name and appEmail contains the list of the Email address. You can put it into hash map as below.

def emailMap=[:] //define an empty map
for(i in 0 .. appName.size()-1)
emailMap.put(appName[i].toString(),appEmail[i].toString())

Now you simply can access the email address of a person as shown below

println emailMap[“Mercantile House”] //prints binod@hotmail.com

Read More...

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday

TOP