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']
Friday, January 30, 2009
Read Environment Variables with Groovy
Environment variables can be easily accessed with Groovy with getenv() method as shown below.
Tuesday, November 4, 2008
Spring’s JDBC Template Set Up for a Grail Application
To take an advantage of the Spring JDBC Template for any Grail application, the first thing you need to do, is to define the DataSource bean in
For example ,
If you have used MS SQL server as your back end database, the driver class name and the url format is as follow.
Similarly for MYSQL database server, the driver class name and the url format could be as follows :
Once its set up you can simply make a query as follows
where myQuery is the query that needs to be execute on the remoteDatabase.
resources.groovy
, Don’t forget to have the jdbc driver(.jar) corresponding to your backend database server under the lib folder of your grail application.For example ,
resources.groovy
is shown as below,
import org.springframework.jdbc.core.JdbcTemplate
import org.apache.commons.dbcp.BasicDataSource
beans = {
myDataSource(BasicDataSource) {
driverClassName = "oracle.jdbc.OracleDriver"
url ="jdbc:oracle:thin:@ . . . . . “
username = "myUser"
password = "myPass"
}
jdbcTemplate(JdbcTemplate)
{
dataSource=myDataSource
}
}
If you have used MS SQL server as your back end database, the driver class name and the url format is as follow.
myDataSource(BasicDataSource) {
driverClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"
url = "jdbc:microsoft:sqlserver://myDataBaseServerURL"
}
Similarly for MYSQL database server, the driver class name and the url format could be as follows :
dataSource {
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql:// myDataBaseServerURL "
username = "myUser"
password = "myPass"
}
Once its set up you can simply make a query as follows
myResult = jdbcTemplate.queryForList (myQuery)
where myQuery is the query that needs to be execute on the remoteDatabase.
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
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.
Good luck !
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 !
Thursday, July 10, 2008
cURL , A command line tool with URL Support !
One of my co-worker introduced a tool named cURL to me today. It a quite handy command line tool for transferring files with URL Syntax supporting most of the interner protocols like FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, proxy tunneling , HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos and many more)
You can download cURL from http://curl.haxx.se/ . It’s a free and open software tools.
For example curl http://nepalnews.com : return a html version of your web page in your console window. Its detail tutorial and example can be found at http://curl.haxx.se/docs/httpscripting.html
I have use curl on the following
curl -X POST -H 'Content-type: text/xml' -d @ http://myserver.com/process.gsp < firstfocus.xml
Here I am sending an xml file, firstfocus.xml, to the server myserver.com . process.gsp do calls the controller which helps in the business logic at the back end and return the result on my console window. It’s a pretty good tool for testing and development environment.
Thanks to Joshua for letting me know about this tool and its usage.
You can download cURL from http://curl.haxx.se/ . It’s a free and open software tools.
For example curl http://nepalnews.com : return a html version of your web page in your console window. Its detail tutorial and example can be found at http://curl.haxx.se/docs/httpscripting.html
I have use curl on the following
curl -X POST -H 'Content-type: text/xml' -d @ http://myserver.com/process.gsp < firstfocus.xml
Here I am sending an xml file, firstfocus.xml, to the server myserver.com . process.gsp do calls the controller which helps in the business logic at the back end and return the result on my console window. It’s a pretty good tool for testing and development environment.
Thanks to Joshua for letting me know about this tool and its usage.
Saturday, June 7, 2008
Free Java Technology Cources!
Dear Readers,
If you are interested with free online courses on java technologies, http://www.javapassion.com/ could be a good platform for you. Shan Shin teaches the courses. Sang Shin is presently working for Sun Microsystems as a Technology architect, consultant, and evangelist. After a successful completion of any courses you are awarded with the Certificate from java passion. I suggest you all to register for at least one source at a time. Detail about the schedule and courses can be seen at the course home page. Currently he is offering the following courses and all are totally free.
If you are interested with free online courses on java technologies, http://www.javapassion.com/ could be a good platform for you. Shan Shin teaches the courses. Sang Shin is presently working for Sun Microsystems as a Technology architect, consultant, and evangelist. After a successful completion of any courses you are awarded with the Certificate from java passion. I suggest you all to register for at least one source at a time. Detail about the schedule and courses can be seen at the course home page. Currently he is offering the following courses and all are totally free.
- Java Programming (with Passion!)
- Performance, Debugging, Testing, Monitoring, and Management (with Passion!)
- Java EE Programming (with Passion!)
- Ajax Programming (with Passion!)
- Web Services Programming (with Passion!)
- Sun Java System Identity Manager (with Passion!)
- Java FX Programming (with Passion!)
- JRuby on Rails (with Passion!)
- Groovy and GRails (with Passion!)
- Java ME Mobility Programming (with Passion!)
Friday, May 9, 2008
How to read a Java Session Back bean of a JSF page from JSP pages
In this section I am describing about accessing a JSF back bean from a JSP page.
Lets us take an example of a java back bean ServerInfo for a JSF page
Bean is defined on faces-config.xml as following
Now lets take an example of JSF page section that sets up the java bean ServerInfoBean from the input text of the form as shown below
Now in process.jsp the back bean can be accessed as shown below
Lets us take an example of a java back bean ServerInfo for a JSF page
// File Name : ServerInfo.java
package thomson.com;
public class ServerInfo {
String server;
String environment;
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
}
Bean is defined on faces-config.xml as following
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<to-view-id>/process.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>ServerInfoBean</managed-bean-name>
<managed-bean-class>thomson.com.ServerInfo</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Now lets take an example of JSF page section that sets up the java bean ServerInfoBean from the input text of the form as shown below
//File name: index.jsp
<f:view>
<h:form id="serverForm">
<h:outputLabel for="serverName">
<h:outputText value="Enter the Server Name"/>
</h:outputLabel>
<h:inputText id="serverName" value="#{ServerInfoBean.server}" required="true"/>
<h:message for="serverName"/>
<br>
<h:outputLabel for="environment">
<h:outputText value="Select Environment"/>
</h:outputLabel>
</h:form>
</f:view>
Now in process.jsp the back bean can be accessed as shown below
File name : process.jsp
..
<%
ServerInfo mbean = (ServerInfo) request.getSession().getAttribute("ServerInfoBean");
String server = mbean.getServer();
Out.print(“server name read from the jsf back bean =” +server)
%>
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
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.
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.
Now you simply can access the email address of a person as shown below
//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
Welcome to my Technical Blog!
Subscribe to:
Posts (Atom)