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']

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.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
}
}
myDataSource(BasicDataSource) {
driverClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"
url = "jdbc:microsoft:sqlserver://myDataBaseServerURL"
}
dataSource {
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql:// myDataBaseServerURL "
username = "myUser"
password = "myPass"
}
myResult = jdbcTemplate.queryForList (myQuery)
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));
}
}
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
}
// 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;
}
}
<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>
//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>
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)
%>
//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>
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
def emailMap=[:] //define an empty map
for(i in 0 .. appName.size()-1)
emailMap.put(appName[i].toString(),appEmail[i].toString())
println emailMap[“Mercantile House”] //prints binod@hotmail.com
©mytechtoday.com 2006-2010