Saturday, April 26, 2014

JSF CRUD Tutorial

Following instructions at https://netbeans.org/kb/docs/web/jsf20-crud.html
at end of "Creating the Web Application Project" was curious so in Libraries - GlassFish Server 4, expanded javax.servlet.jsp.jstl.jar - org.apache.taglibs.standard.tag.common.xml - ParseSupport.class it opened a generated source file. I noticed the button "Attach Sources"
I then used https://blogs.oracle.com/geertjan/entry/how_to_set_up_glassfish1 as a guide to download the sources. I did Team - Subversion -Checkout - Repository URL: https://svn.java.net/svn/jstl~svn and downloaded to a new folder java.net-src\jstl in the NetBeans projects folder. Then I clicked the "Attach Sources..." button and added the folder %USERPROFILE%\Documents\NetBeansProjects\java.net-src\jstl\javax.servlet.jsp.jstl-1.2.1\src\main\java.
Going back to the .class files under the javax.servlet.jsp.jstl.jar file I am now able to double-click one and see the source.

Wednesday, April 23, 2014

Standalone JAX-WS Lab

This question made me curious: http://stackoverflow.com/questions/5166269/what-happens-when-a-java-class-is-annotated-webservice?rq=1

Thus I created the standalone server per: http://stackoverflow.com/questions/1792737/in-process-soap-service-server-for-java

basically set up the .java files per the answer and do:

C:\Work\Testing\java\webservices\standalone_ws>mkdir buildsvc

C:\Work\Testing\java\webservices\standalone_ws>javac -d buildsvc helloservice/endpoint/*java

C:\Work\Testing\java\webservices\standalone_ws>java -cp buildsvc helloservice.endpoint.Server
Starting Server
Server ready...

Then, create a client:

package simpleclient;

import helloservice.endpoint.*;
import javax.xml.ws.WebServiceRef;
import javax.xml.namespace.QName;

public class HelloClient {
  @WebServiceRef(wsdlLocation="http://localhost:9000/SoapContext/SoapPort?WSDL")
  static HelloService service;

  public static void main(String[] args) {
    try {
      HelloClient client = new HelloClient();
      client.doTest(args);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  public void doTest(String[] args) {
    try {
QName qname = new QName("http://endpoint.helloservice/", "HelloService");
HelloService service = new HelloService(null, qname);
      System.out.println("Retrieving the port from the following service: " + service);
      Hello port = service.getHelloPort();
      System.out.println("Invoking the sayHello operation    on the port.");

      String name;
      if (args.length > 0) {
        name = args[0];
      } else {
        name = "No Name";
      }

      String response = port.sayHello(name);
      System.out.println(response);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

Test:

mkdir build
wsimport -d build -keep -verbose http://localhost:9000/SoapContext/SoapPort?WSDL
javac -cp build simpleclient/HelloClient.java
java -cp build;. simpleclient.HelloClient bah

Then to make things interesting and to see the full stack trace to see how the WebService annotation is processed on the server or at least for clues, change the sayHello method:

  public String sayHello(String name) {
    return message + name + "." + (1/(1-1));
  }

to throw an ArithmeticException. Redo above steps and see the stacktrace coming from the cmd window for the running server:

Starting Server
Server ready...
Apr 23, 2014 10:42:41 PM com.sun.xml.internal.ws.server.sei.TieHandler createResponse
SEVERE: / by zero
java.lang.ArithmeticException: / by zero
        at helloservice.endpoint.Hello.sayHello(Hello.java:12)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.Trampoline.invoke(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.xml.internal.ws.api.server.MethodUtil.invoke(Unknown Source)
        at com.sun.xml.internal.ws.api.server.InstanceResolver$1.invoke(Unknown Source)
        at com.sun.xml.internal.ws.server.InvokerTube$2.invoke(Unknown Source)
        at com.sun.xml.internal.ws.server.sei.SEIInvokerTube.processRequest(Unknown Source)
        at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Unknown Source)
        at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Unknown Source)
        at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Unknown Source)
        at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Unknown Source)
        at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(Unknown Source)
        at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(Unknown Source)
        at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(Unknown Source)
        at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(Unknown Source)
        at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(Unknown Source)
        at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source)
        at sun.net.httpserver.AuthFilter.doFilter(Unknown Source)
        at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source)
        at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(Unknown Source)
        at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source)
        at sun.net.httpserver.ServerImpl$Exchange.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)


From there you can go look up these methods on grepcode for OpenJDK and see what's actually happening.



Tuesday, April 22, 2014

JAX-RS Labs

doing http://www.vogella.com/tutorials/REST/article.html

To view HTTP headers in Fiddler, add to vm arguments of run configuration

-DproxyHost=localhost
-DproxyPort=8888

then restart fiddler

Discovered that Jersey 1.x is deprecated in favor of Jersey 2 which uses Moxy instead of Jackson. The catch is that the latter don't play well with Tomcat, so I'll be abandoning tutorials on JAX-RS prior to 2011, and sticking to Glassfish.

Tuesday, April 8, 2014

Spring MVC Labs

http://saltnlight5.blogspot.com/2013/10/getting-started-with-annotation-based.html  -- gives an example of how to take advantage of Servlet 3/Tomcat 7 web.xml-free setup. To boot also shows how to use the http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/usage.html Tomcat 7 Maven plugin.
Alls I did was create a new Maven project in Eclipse Kepler EE version, naming the groupId mvcmvnsprng and the artifact id mvcmvn. Then created a source folder under Java Resources named "javasrc" in which I put the mentioned packages and java files. Also updated the pom.xml and right click project and click Validate. I suppose you could also Maven, download sources or just wait for it happen automatically. index.jsp was already there with hello world in it. then I changed to cmd to root folder of project containing the pom.xml and typed: mvn org.apache.tomcat.maven:tomcat7-maven-plugin:run
That downloaded the tomcat plugin and started the web server! Loading localhost:8080/mvcmvn/ I could see "Hello World!" it was that easy.
Also did the follow up which showed how to connect the same to embedded H2 database via jdbctemplate http://java.dzone.com/articles/getting-started-spring-jdbc

The next step was to add logging. Taking hints from several tutorials I ended up just right-clicking the project and choosing maven add dependency twice. Once by typing commons-logging and choosing 1.1.1. Then for log4j by typing "log4j" and choosing 1.2.17. It added a "bundle" element which cause errors so I deleted it. Then I added the log4j.properties copied from http://www.beingjavaguys.com/2013/05/logging-in-spring-framework-using-log4j_8.html  to src/main/resources (so its in root of a classpath). That was it. Didn't need to use Logger.getLogger or import org.apache.log4j.Logger because apparently it's plugged in automatically via spring and appache commons logging. So the LOG.info command in PingService.java worked; it outputed the message in the logingFile.log and to the batch output.

Word of caution: Don't even think of doing Spring MVC in JBoss. All indicators are that Spring was built with Tomcat in mind, JBoss was built with JEE in mind, and never the twain should cross paths (unless you want to spend all-nighters trying to fit a square peg (Spring MVC 3) in a round hole (JBoss 7) like I just did.

Saturday, April 5, 2014

JBoss EJB Lab

Followed: http://www.26miles.com/26miles/downloads/JBoss%205.0.1.GA/Simple%20Web%20Application%20using%20JBoss%20and%20Eclipse.doc

and http://www.26miles.com/26miles/downloads/JBoss%205.0.1.GA/Simple%20EJB%202.1%20Application%20using%20JBoss%20and%20Eclipse.doc
 in that order.

Used jboss-4.2.2.GA instead. and set up SimpleStandAloneClient per doc.

Once "SimpleSession" was working, copied target JAR to server\default\deploy

Changed string in context.lookup to match target ejb jndi (note if there's a jboss.xml in the jar as there was in this case, check it for actual jndi-name to use instead of ejb-name from ejb-jar.xml). Change properties passed to InitialContext as well.

Target's ejb-jar.xml contained an assembly-descriptor so need to configure security in client app.

Add:             System.setSecurityManager(new RMISecurityManager());
to top of of main right after opening of try block.

Instead of adding the credentials to the properties, use SecurityAssociation as follows (inserted before InitialContext context = new InitialContext(properties)

SecurityAssociation.setPrincipal(new SimplePrincipal("user"));
SecurityAssociation.setCredential("password");

Add roles.properties and user.properties files to server\default\conf containing security-role name corresponding to target ejb/method from ejb-jar.xml.

will need to execute it like java -Djava.security.policy=client.policy SimpleStandAloneClient
with client.policy containing e.g., for all permissions (ok only for lab):
grant {
permission java.security.AllPermission;
};


add server\default\deploy\oracle-ds.xml with security-domain info (see https://community.jboss.org/wiki/EncryptingDataSourcePasswords)
modify server\default\conf\login-config.xml
add ojdbc5.jar to server\default\lib