Monday, April 16, 2012

Public, Protected, and Private Interfaces... Yes Interfaces.

One of the junior developers came to me on Friday and asked if an interface could be private, or protected. I said sure, but it must be an inner interface. The interface must be contained within a containing class. So I decided to create a couple of examples of interface fun for you to appreciate.

The NetBeans project is located here: interfaces.zip

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.bluelotussoftware.example.core;
 
/*
 * Odd looking import to allow C to be used without nesting (chaining).
 */
import com.bluelotussoftware.example.core.A.B.C;
 
/**
 *
 * @author John Yeary
 * @version 1.0
 */
public class A {
 
    /**
     * This can only be used by classes, or interfaces declared inside the
     * Parent class.
     */
    private interface W {
 
        void w();
    }
 
    /**
     * This can be used by classes, or interfaces declared inside the parent
     * class, or the same package.
     */
    protected interface X {
 
        String x();
    }
 
    /**
     * This can be used by classes, or interfaces declared inside the parent
     * class, or classes inside the same package.
     */
    interface Y {
 
        String y();
    }
 
    /**
     * This can be used by any class.
     */
    public interface Z {
 
        String z();
    }
 
    /**
     * Inner class
     */
    class B implements W {
 
        @Override
        public void w() {
        }
 
        class C extends B {
 
            @Override
            public void w() {
                System.out.println("NO!");
            }
        }
    }
 
    private void run(Y z) {
        System.out.println(z.y());
    }
 
    void execute() {
        // Anonymous method local inner class.
        run(new Y() {
 
            @Override
            public String y() {
                return "YES!";
            }
        });
 
 
        // Anonymous method local inner class with method call.
        System.out.println(new X() {
 
            @Override
            public String x() {
                return "It is a question of something...";
            }
        }.x());
 
 
        // Construction of a multi-nested inner class
        C c = new A().new B().new C();
        c.w();
    }
 
    public static void main(String... args) {
        new A().execute();
        // Constructor chaining with a method call.
        new A().new B().new C().w();
    }
}

JSF 2 on JBoss AS 5.1.0

I was confronted with a requirement to run JSF 2 on JBoss Application Server 5.1.0 on Friday. As you may know, JSF 2.0 will run on Java EE 5 application servers, but not with all the bells and whistles that we have come to expect with Java EE 6.

Requirements

Instructions

  1. Install JBoss Application Server.
  2. Make sure that the server is not running.
  3. Go to the <jboss_home>/server/default/deploy/jbossweb.sar/jsf-libsdirectory.
  4. move the jsf-api.jar and jsf-impl.jar to a safe location. We will be replacing it with the javax.faces.jar from the JSF 2.0.9.zip distribution.
    Note: the javax.faces and com.sun.faces files are combined in one jar now. The Mojarra Reference Implementation is in a single jar now.
  5. Go to the <jboss_home>/server/default/deployers/jbossweb.deployer directory.
  6. Open the web.xml in a text editor and comment out the following lines:
    • 1
      2
      3
      4
      5
      6
      <!--
      <context-param>
        <param-name>com.sun.faces.injectionProvider</param-name>
        <param-value>org.jboss.web.jsf.integration.injection.JBossDelegatingInjectionProvider</param-value>
      </context-param>
      -->
    • 1
      2
      3
      4
      5
      <!--
      <listener>
        <listener-class>org.jboss.web.jsf.integration.config.JBossJSFConfigureListener</listener-class>
      </listener>
      -->
    • 1
      2
      3
      4
      5
      <!--
      <listener>
        <listener-class>com.sun.faces.application.WebappLifecycleListener</listener-class>
      </listener>
      -->
  7. Start the application server and check the logs. You should see something like the following:
    14:23:30,058 INFO [TomcatDeployment] deploy, ctxPath=/admin-console
    14:23:30,257 INFO [config] Initializing Mojarra 2.0.9 (SNAPSHOT 20120202) for context '/admin-console'
    14:23:45,013 INFO [application] JSF1048: PostConstruct/PreDestroy annotations present. ManagedBeans
    methods marked with these annotations will have said annotations processed.

Summary

You should be able to install and use JSF 2 with Java EE 5 Application servers like JBoss AS 5.1.0 without issues by simply updating the required JSF libraries.

Monday, April 02, 2012

JSF 2.0 JQuery-JSF Integration

I am publishing an example of how to use jQuery with JSF. This is a basic jQuery Dialog which is integrated into a JSF 2.0 Reference Implementation (Mojarra) page.

There are a couple of JSF frameworks which take extensive use of jQuery like PrimeFaces which I would highly recommend.

This is just a simple example to show you how to get started.

Here is the NetBeans Apache Maven Mercurial project: jquery-jsf-integration

index.xhtml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>jQuery-JSF Integration Example</title>
        <h:outputStylesheet name="css/ui-lightness/jquery-ui-1.8.11.custom.css"/>
        <h:outputScript name="jquery-1.5.1.min.js" library="js"/>
        <h:outputScript name="jquery-ui-1.8.11.custom.min.js" library="js"/>
    </h:head>
    <h:body>
        <script type="text/javascript">
            // increase the default animation speed to exaggerate the effect
            $.fx.speeds._default = 1000;
            $(function() {
                $( "#pop" ).dialog({
                    autoOpen: false,
                    show: "blind",
                    hide: "explode",
                    title: "jQuery-JSF Integration"
                });
 
                $( "#link" ).click(function() {
                    $( "#pop" ).dialog( "open" );
                    return false;
                });
            });
        </script>
        <h1>
 
jQuery-JSF Integration Example</h1>
<p>
<h:outputLink id="link" value="#">
                <h:outputText value="jQuery Dialog (click)"/>
            </h:outputLink>
        </p>
<p>
<h:outputLink id="link" value="#" onmouseover="$('#pop').dialog('open'); return false;">
                <h:outputText value="jQuery Dialog (onmouseover)"/>
            </h:outputLink>
        </p>
<h:panelGroup id="pop">
            <ui:include src="included.xhtml"/>
        </h:panelGroup>
    </h:body>
</html>

included.xhtml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>included</title>
    </head>
    <body>
        <ui:component id="included">
            <p>
This is an animated dialog which is useful for displaying information.
The dialog window can be moved, resized and closed with the 'x' icon.</p>
<h:button onclick="$('#pop').dialog('close'); return false;" value="Close"/>
        </ui:component>
    </body>
</html>

JDBC-ODBC Bridge Example

I was recently asked how to use the JDBC-ODBC bridge. I told the person asking that "normally" you would want to use JDBC, and that the Sun developed bridge is not really for production use. I was reminded that there are some old databases out there which only support ODBC connections from Windows.

I wrote a quick example of how to use it which I thought I would share. I had to think about how to do it since it has been so long since I did such a thing. There are two examples of how to connect: one example is a "standard" JDBC connection, and the other takes advantage of the DataSource abstraction.

Note: I created an ODBC connection in Windows to the database called JDBCODBC for this example to work.

Here is the NetBeans code for the project: JavaJDBCODBC.zip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package bridge;
 
import java.sql.*;
import javax.naming.NamingException;
 
/**
 * <p>Example application of how to use the JDBC-ODBC Bridge.</p>
 * <p><b>Note:</b> You must have configured an ODBC connection called
 * <b>JDBCODBC</b>.</p>
 *
 * @author John Yeary
 * @version 1.0
 */
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException, NamingException {
 
        if (args.length < 3) {
            System.out.println("Usage: bridge.Main username password sql");
            System.out.println("\t Example: bridge.Main scott tiger \"select * from users\"");
            System.exit(-1);
        }
 
        // Standard JDBC Connection Example
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        java.util.Properties prop = new java.util.Properties();
        prop.put("user", args[0]);
        prop.put("password", args[1]);
        String url = "jdbc:odbc:JDBCODBC";
        Connection con = DriverManager.getConnection(url, prop);
 
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery(args[2]);
        ResultSetMetaData metaData = rs.getMetaData();
 
        System.out.println("===> MetaData <===");
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            System.out.println(metaData.getColumnName(i));
        }
        System.out.println("");
        rs.close();
        s.close();
        con.close();
 
        // DataSource Example
        sun.jdbc.odbc.ee.DataSource ds = new sun.jdbc.odbc.ee.DataSource();
        ds.setUser(args[0]);
        ds.setPassword(args[1]);
        ds.setDatabaseName("JDBCODBC");
        Connection conx = ds.getConnection();
        s = conx.createStatement();
        rs = s.executeQuery(args[2]);
        metaData = rs.getMetaData();
 
        System.out.println("===> DataSource <===");
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            System.out.println(metaData.getColumnName(i));
        }
        System.out.println("");
        conx.close();
    }
}

Popular Posts