The Greenville Java Users Group (GreenJUG) is conducting an ten (10) week boot camp to prepare its members for the CX-310-065 exam. It is a great opportunity to get a better understanding of Java™ and a obtain a valuable industry certification.
The code examples, books, and notes can be found on the Blue Lotus Software Sun™ Certified Java™ Programmer (SCJP) Boot Camp site. All of the code is Open Source Apache 2 licensed. The schedule for classes and final exam preparation are on the GreenJUG site.
Pages
▼
Sunday, August 31, 2008
Friday, August 29, 2008
Making a System Call in Java
This is an example on how to make a system call to the local operating system to execute external programs. This example was written to work on Mac OS X, but will work on other UNIX type operating systems likely without issue. It will not work as written on Windows unless Cygwin is installed and in the PATH.
1 /*
2 * $Id: SystemCallExample.java 106 2008-08-30 00:58:59Z jyeary $
3 *
4 * Software License Agreement (BSD License)
5 *
6 * Copyright (c) 2008, Blue Lotus Software, LLC
7 * All rights reserved.
8 *
9 * Redistribution and use of this software in source and binary forms, with or
10 * without modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the
15 * following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the
19 * following disclaimer in the documentation and/or other
20 * materials provided with the distribution.
21 *
22 * Neither the name of Blue Lotus Software, LLC nor the names of its
23 * contributors may be used to endorse or promote products
24 * derived from this software without specific prior
25 * written permission of Blue Lotus Software, LLC
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 package com.bluelotussoftware.examples;
37
38 import java.io.BufferedInputStream;
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.InputStreamReader;
43
44 /**
45 * This is a demo application on how to use Java to make system calls.
46 * The use of system calls is platform specific. This code may not be
47 * transportable accross platforms,i.e. UNIX, Mac OS X, and Windows.
48 * @author John Yeary
49 * @version 1.0
50 */
51 public class SystemCallExample {
52
53 /**
54 * Creates a new instance of SystemCallExample
55 */
56 public SystemCallExample() {
57 }
58
59 /**
60 * Main program method
61 * @param args the command line arguments
62 */
63 public static void main(String[] args) {
64 Runtime r = Runtime.getRuntime();
65
66 try {
67 /*
68 * Here we are executing the UNIX command ls for directory listing.
69 * The format returned is the long format which includes file
70 * information and permissions.
71 */
72 Process p = r.exec("ls -l");
73 InputStream in = p.getInputStream();
74 BufferedInputStream buf = new BufferedInputStream(in);
75 InputStreamReader inread = new InputStreamReader(buf);
76 BufferedReader bufferedreader = new BufferedReader(inread);
77
78 // Read the ls output
79 String line;
80 while ((line = bufferedreader.readLine()) != null) {
81 System.out.println(line);
82 }
83 // Check for ls failure
84 try {
85 if (p.waitFor() != 0) {
86 System.err.println("exit value = " + p.exitValue());
87 }
88 } catch (InterruptedException e) {
89 System.err.println(e);
90 } finally {
91 // Close the InputStream
92 bufferedreader.close();
93 inread.close();
94 buf.close();
95 in.close();
96 }
97 } catch (IOException e) {
98 System.err.println(e.getMessage());
99 }
100 }
101 }
102
Saturday, August 16, 2008
How to implement EJB 3.0 <env-entry/> in ejb-jar.xml
A topic which is mentioned in the EJB 3.0 Specification (JSR-220) is the ability to add environment entries in ejb-jar.xml file. What I found interesting is that I could not Google for a good example of how to implement it. As a result, I thought I would provide a good simple example.
The first item you should examine is the schema resources for Java EE. Specifically we want to examine the ejb-jar_3_0.xsd. This file contains all of the information we need to create and validate an ejb-jar.xml file.
First we create an ejb-jar.xml file in our EJB project. This file should contain the following information at a minimum:
From here we can add our beans and environment variables. I have created an example project which demonstrates how to use the environment variables. Here is my ejb-jar file:
Here is my stateless session bean:
When I deploy the application to GlassFish, I can use the Web Service Tester to validate that I get the injected values.
As we can see, the values s1 and s2 are sucessfully injected. The complete Netbeans 6.1 project and source can be found here.
The first item you should examine is the schema resources for Java EE. Specifically we want to examine the ejb-jar_3_0.xsd. This file contains all of the information we need to create and validate an ejb-jar.xml file.
First we create an ejb-jar.xml file in our EJB project. This file should contain the following information at a minimum:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ejb-jar
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
7 version="3.0">
8 </ejb-jar>
From here we can add our beans and environment variables. I have created an example project which demonstrates how to use the environment variables. Here is my ejb-jar file:
In my example, I use a stateless session bean with @WebService annotation. This allows me to test it right in GlassFish. I also use the @Resource annotation to inject my environment entries. I have used the name attribute to give it a JNDI lookup value. This works for GlassFish. Other containers may use the mappedName attribute.
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ejb-jar
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
7 version="3.0">
8 <enterprise-beans>
9 <session>
10 <ejb-name>EnvironmentalResourceInjectionExampleBean</ejb-name>
11 <env-entry>
12 <env-entry-name>s1</env-entry-name>
13 <env-entry-type>java.lang.String</env-entry-type>
14 <env-entry-value>Hello</env-entry-value>
15 </env-entry>
16 <env-entry>
17 <env-entry-name>s2</env-entry-name>
18 <env-entry-type>java.lang.String</env-entry-type>
19 <env-entry-value>EJB Resource Injection World!</env-entry-value>
20 </env-entry>
21 </session>
22 </enterprise-beans>
23 </ejb-jar>
Here is my stateless session bean:
1 /*
2 *
3 * Blue Lotus Software, LLC
4 *
5 * $Id: EnvironmentalResourceInjectionExampleBean.java 100 2008-08-16 21:57:19Z jyeary $
6 *
7 * Copyright [2008] [John Yeary]
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package com.bluelotussoftware.ee.examples;
19
20 import javax.annotation.Resource;
21 import javax.ejb.Stateless;
22 import javax.jws.WebMethod;
23 import javax.jws.WebService;
24
25 /**
26 *
27 * @author John Yeary
28 * @version 1.0
29 */
30 @Stateless
31 @WebService
32 public class EnvironmentalResourceInjectionExampleBean implements
33 EnvironmentalResourceInjectionExampleRemote,
34 EnvironmentalResourceInjectionExampleLocal {
35
36 @Resource(name = "s1")
37 String s1;
38 @Resource(name = "s2")
39 String s2;
40
41 public String printEnv() {
42 return s1 + " " + s2;
43 }
44
45 @WebMethod
46 public String printEnvironment() {
47 return printEnv();
48 }
49 }
When I deploy the application to GlassFish, I can use the Web Service Tester to validate that I get the injected values.
As we can see, the values s1 and s2 are sucessfully injected. The complete Netbeans 6.1 project and source can be found here.