Showing posts with label TomEE. Show all posts
Showing posts with label TomEE. Show all posts

Wednesday, May 15, 2013

TomEE in the cloud with Azure and Azure SQL DB

This blog post shows how to deploy the FaceID project on +Windows Azure. We will use +Apache TomEE, Ubuntu 12.04 and the Windows Azure SQL Database.

We are going to:

  • Prepare a Linux box on Azure
  • Install Java
  • Install TomEE 1.6 Snapshot
  • Install our FaceID application
  • Create an Azure SQL Database
  • Install MS SQL Server JDBC driver
  • Link the FaceID application and the Azure SQL Database
  • Deal with the clustered index issue
  • Have fun! 

Build the VM and install FaceID

First, create your virtual machine on Azure. This is the official tutorial: http://www.windowsazure.com/en-us/manage/linux/tutorials/virtual-machine-from-gallery/. Once you have it running, open a ssh session (Windows users can use putty) and upgrade your server.


After restarting your virtual machine, install java.


Some of the features used by the FaceID project are available on TomEE 1.6 Snapshot (and older). The latest snapshot versions of the server are available here. Go ahead and download TomEE Plus 1.6 Snapshot.


Unzip the distribution file.


Start your TomEE server.


You can also tail the log file.


Your TomEE server is now running but it is still not visible from the outside world. You will need to open the port 80. You can map the public port 80 to the private port 8080. The official endpoints how-to tutorial is available here: http://www.windowsazure.com/en-us/manage/windows/how-to-guides/setup-endpoints/.

After creating the endpoints, you will be able to access your server.


It is time to install the FaceID application on it. You can build it yourself (see previous blog posts) or you can download this pre-built war file: http://people.apache.org/~tveronezi/posts/20130514-azuredb/faceid.war.


Move the war file to the webapps directory.


If you access the faceid (in the case above it would be http://tomee-on-azure.cloudapp.net/faceid/) application now, you should see the popup security window.


We have no user for this application yet. Shutdown your server and update the /apache-tomee-plus-1.6.0-SNAPSHOT/conf/ tomcat-users.xml file. You should create an user with the solution-admin role.

 <?xml version='1.0' encoding='utf-8'?>  
 <tomcat-users>  
  <role rolename="solution-admin" />  
  <user username="myadmin" password="mypassword" roles="solution-admin" />  
 </tomcat-users>  

After restarting TomEE, you will be able to access your application.


Create and link an Azure SQL Database

The official how-to tutorial on how to use an Azure SQL Database with java is available here: http://www.windowsazure.com/en-us/develop/java/how-to-guides/using-sql-azure-in-java/.

Create your database.




Download Microsoft JDBC Driver 4.0 for SQL Server (http://www.microsoft.com/en-us/download/details.aspx?id=11774) and upload it to your virtual machine.



Uploading file with filezilla

Extract the file.


Copy the driver to the lib directory.


The FaceID points to a datasource called userJTA. If you don't define this datasource, TomEE will do it for you. The generated datasources use the default hsqldb database. In order to use Windows Azure SQL Database, you need to specify the datasource yourself. Edit the conf/tomee.xml file.


Replace the content of this file by...

 <?xml version="1.0" encoding="UTF-8"?>  
 <tomee>  
  <Resource id="userJTA" type="javax.sql.DataSource">  
 JdbcDriver=com.microsoft.sqlserver.jdbc.SQLServerDriver  
 JdbcUrl=jdbc:sqlserver://s9oprf6lip.database.windows.net:1433;database=tomee-azure;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;  
 UserName=tomee  
 Password={your password here}  
  </Resource>  
 </tomee>  

Restart your server. The application is connected to the Azure DB server, but you cannot persist data yet. As soon as you try to create a new user, TomEE throws an exception.


Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again. {prepstmnt 221417245 INSERT INTO User_securityGroups (USER_UID, element) VALUES (?, ?) [params=?, ?]} [code=40054, state=S0001]  

Go to the Azure Console and connect to your database.



Create the clustered index for the table that Azure is complaining about.

 CREATE CLUSTERED INDEX User_securityGroups_Index ON User_securityGroups (USER_UID)  


If you try to use your application again, you should be able to persist data on it.


Read more about Datasource Configurationhttp://tomee.apache.org/datasource-config.html.
FaceID source code: https://github.com/tveronezi/faceid
Apache TomEE: http://tomee.apache.org/

The Apache TomEE project always welcomes contributions. Join the fun!

[]s!

Thursday, May 9, 2013

Maven Archetype for our Scala JavaEE project


As requested, we now have a Maven Archetype for our Scala/JavaEE application. It makes easier to generate new projects by using the boxdata as template.

Due to the incompatibility of licenses, I cannot propose it as a TomEE sub-module; I plan to create other examples of Rich Internet Applications with Apache friendly licenses and include them in the TomEE project as soon as my free time permits. ExtJS and HighCharts are free of charge for personal use only. If you want to develop a commercial application with them, you will need to buy the commercial licenses for it. Regardless, you are free to hack and use the code that I created in your own projects (Apache License). In fact, I will be glad to have your contribution!

Let's build our application.
  • First, locate the ".m2" directory. It should be one of the children of our home directory. 
  • Create an archetype-catalog.xml file with the following content. 
 <archetype-catalog xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd">  
  <archetypes>  
   <archetype>  
    <groupId>boxdata</groupId>  
    <artifactId>boxdata-archetype</artifactId>  
    <version>1.0-SNAPSHOT</version>  
    <repository>http://people.apache.org/~tveronezi/m2repository</repository>  
    <description>boxdata-like application template</description>  
   </archetype>  
  </archetypes>  
 </archetype-catalog>  

Go to the directory where you want to create your new project and execute...
 mvn archetype:generate  


You should see the list of all the archetypes available in your machine. The last one in this list should be boxdata-archetype.


Now choose your parameters...


Done. Your project is ready!


Linux users can run the application by executing...
 make start-tomee  

Windows users can follow this post: http://buildnplay.blogspot.ca/2012/12/building-jee-application-with-intellij.html. It will show how to run the TomEE server with IntelliJ.

Importing an existing project with IntelliJ

Tip. You don't need to download TomEE. When you run "mvn install" maven will download the bleeding-edge version of the server for you. The file will be available under <YOUR PROJECT>-resources\target\tomee-runtime.tar.gz.

In case you don't want to create the archetype-catalog.xml file, you can clone and build the archetype in your localhost. The source-code is available here: https://github.com/tveronezi/boxdata-archetype. Feel free to clone it. Please contribute!

The TomEE project is always looking for new contributions. Go check it out. Lots of fun! http://tomee.apache.org/

[]s,

Monday, April 15, 2013

JavaEE with Scala and TomEE

We know how to build applications with Java and Groovy (see previous posts). Now it is time to give some love to a language that has recently caught my attention. Scala runs on the jvm, so there is no trick to make it work with TomEE. Due to its seamless integration with java, one can easily use the well-known java annotations in order to build a standard JavaEE application.

See an example of stateless bean definition:

 package boxdata.ejb  

 import boxdata.data.dto.DeviceUsageDto  
 import scala.collection.JavaConverters._  
 import javax.ejb.EJB  
 import javax.ejb.Stateless  
 
 @Stateless  
 class DeviceUsageEjb {
  
   @EJB  
   var disk: DiskUsageEjb = _
  
   @EJB  
   var file: FileUsageEjb = _  

   def getUsage: DeviceUsageDto = {  
     val diskUsage = disk.getUsage  
     val fileUsage = file.getUsage  
     val dto = new DeviceUsageDto  
     dto.diskUsageList = diskUsage.asJava  
     dto.fileUsageList = fileUsage.asJava  
     dto  
   }  
 }  

Notice the usage of the JavaEE annotations ("@Stateless" and "@EJB").

How would we define a rest application?

 package boxdata.rest  

 import javax.ws.rs.ApplicationPath  
 import javax.ws.rs.core.Application  
 import java.util  
 import scala.collection.JavaConverters._  

 @ApplicationPath("/rest")  
 class ApplicationConfig extends Application {  

   override def getClasses: util.Set[Class[_]] = {  
     Set[Class[_]](  
       classOf[DeviceUsage],  
       classOf[SystemLoad],  
       classOf[SystemThreads]  
     ).asJava  
   }  
 }  

You don't need to download and install Scala before using it with TomEE. Maven takes care of everything. With Maven, the war generation is as simple as "mvn clean install". The resulting war will have only two jars: scala-library-2.9.2.jar and boxdata-web-1.0-SNAPSHOT.jar.

See what "boxdata-web-1.0-SNAPSHOT.jar" contains:
   128 Tue Apr 16 14:34:24 EDT 2013 META-INF/MANIFEST.MF  
   663 Tue Apr 16 14:34:12 EDT 2013 META-INF/ejb-jar.xml  
   827 Tue Apr 16 14:34:12 EDT 2013 META-INF/beans.xml  
  2727 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/DiskUsageDto.class  
  4145 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/ThreadDto.class  
  6451 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/SystemLoadDto.class  
  2518 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/DeviceUsageDto.class  
  1915 Tue Apr 16 14:34:24 EDT 2013 boxdata/data/dto/FileUsageDto.class  
  1977 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/SystemLoad.class  
  1464 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/DeviceUsage.class  
  2006 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/SystemThreads.class  
  1915 Tue Apr 16 14:34:24 EDT 2013 boxdata/rest/ApplicationConfig.class  
  3298 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SystemThreadsEjb.class  
  12153 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SystemThreadsEjb$$anonfun$getThreadsInfo$1.class  
  2308 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/DeviceUsageEjb.class  
  1877 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/FileUsageEjb$$anonfun$buildUsage$1.class  
  5115 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SystemLoadEjb.class  
  2736 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/SchedulerEjb.class  
  12043 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/DiskUsageEjb$$anonfun$getUsage$1.class  
  4070 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/FileUsageEjb.class  
  2395 Tue Apr 16 14:34:24 EDT 2013 boxdata/ejb/DiskUsageEjb.class  
  3823 Tue Apr 16 14:34:24 EDT 2013 boxdata/cdi/util/DtoBuilder.class  
  2346 Sat Apr 13 09:17:58 EDT 2013 META-INF/maven/boxdata/boxdata-web/pom.xml  
   110 Tue Apr 16 14:34:26 EDT 2013 META-INF/maven/boxdata/boxdata-web/pom.properties  

For more details about how to build a JavaEE application with Scala, nothing better than checking a real code. Go ahead and clone the https://github.com/tveronezi/boxdata. This project is a starting point for your own applications with TomEE and Scala. For those who love Javascript, the example also shows how to integrate ExtJS and Highcharts.

Application output
Intellij with Scala code (plugin website)

If you are a windows user, you will need to download the bleeding-edge version of TomEE in order to run this application (See how to build and run it with Intellij in this blog post). If you are a Unix-like OS user, you can execute the "make start-tomee" makefile target (check the README file).

Have fun with JavaEE and TomEE!

Ps.: Check out the other JavaEE Scala application -> http://buildnplay.blogspot.ca/2013/06/ejb-mdb-rest-jpa-cdi-all-javaee-fun.html

[]s

Thursday, March 7, 2013

System integration via JavaMail and MDBs with Apache TomEE

Attention: This is old stuff. New build instructions here

In the previous post we moved the authentication logic from one application to another. We are trying to split tasks between applications and eventually between servers. That means that the PhotoDB application development team can concentrate their effort on how to manage pictures by leaving the user management to another group of geeks. For now, the FaceID geeks have only implemented the user management via regular database tables, but they could easily integrate other authentication methods (think LDAP, Google, Facebook etc)  in a transparent fashion.

Now imagine that our PhotoDB application should go on-line for everyone to use it. How would the PhotoDB administrator create all the users accounts? Manually creating one by one would make it the worst job in the world. Giving the potential customers free access to the FaceID application so they can add themselves as users of the system is not a good idea; The FaceID application is supposed to be used by system admins only. The usual solution is to allow the customers to create their user account from the target application itself. In our case, it would be a page like the example bellow.


Wait a minute! We have just created a whole application that is supposed to manage the users for us and now we are planing to move the user management back to the PhotoDB application? Not really. The PhotoDB application will request a new user from FaceID but in a decoupled way. The glue in between the two will be regular emails, MDBs and Message Brokers. We also don't want to delegate the email manipulation to any of our existing applications. It is time to add a new player to the team: Msglnk.

Msglnk is the central repository of all the email information of our solution. The PhotoDB and the FaceID applications don't need to keep any email configuration. They may want to send an email, but they do it by delegating the task to the Msglnk app. The app reads and sends emails with JavaMail. It communicates with the other two applications via JMS in the following way:

  • When a new email arrives, it sends a javax.jms.TextMessage through the IncomingEmailQueue javax.jms.Queue.
  • It has a MDB that listens the SendEmailQueue. Messages coming from this queue are requests from other application to send an email in their behaviour. The Msglnk formats the email and sends it through one of the pre-defined sessions. 
  • It checks for new emails every 5 minutes or when the user clicks the  "Read emails from remote server" button.

Check what the application looks like:

  
Out of the box, this application will not read or send any emails. You should first define the default mail session. The "Email session configuration" field has the skeleton of a JavaMail session configuration. It is designed to be used with a gmail account, but you can change it to fit your provider configurations. If you want to use a gmail account, make sure that your account has the pop access available, change the "your user here" and "your password here" strings by the real values and click "Save session configuration". Now you can click the red button in order to read your emails.

Tip: check the 2-step verification settings. You way want to use an application-specific password to improve security.

You can test your configuration with the "Send email" tab.


If "foo@bar.com" receives the email, the configuration is correct.

Ok, let's test it. Copy and paste this code (open the link) into a terminal. It will perform the following steps:
  • create a <HOME_DIR>/posts/solution-demo directory
  • clone all the three applications under the created directory
  • download a specially created Makefile that builds all the three apps
  • execute the "start-all" target of the downloaded Makefile
In a few seconds you should see an output like this...

 .  
 .  
 .  
 7-Mar-2013 6:43:18 AM org.apache.openejb.cdi.OpenEJBLifecycle startApplication  
 INFO: OpenWebBeans Container is starting...  
 7-Mar-2013 6:43:18 AM org.apache.webbeans.plugins.PluginLoader startUp  
 INFO: Adding OpenWebBeansPlugin : [CdiPlugin]  
 7-Mar-2013 6:43:18 AM org.apache.webbeans.plugins.PluginLoader startUp  
 INFO: Adding OpenWebBeansPlugin : [OpenWebBeansJsfPlugin]  
 7-Mar-2013 6:43:18 AM org.apache.openejb.cdi.BeansDeployer validateInjectionPoints  
 INFO: All injection points are validated successfully.  
 7-Mar-2013 6:43:18 AM org.apache.openejb.cdi.OpenEJBLifecycle startApplication  
 INFO: OpenWebBeans Container has started, it took 2 ms.  
 7-Mar-2013 6:43:18 AM org.apache.openejb.assembler.classic.Assembler createApplication  
 INFO: Deployed Application(path=/home/tveronezi/TOMEE/tomee-runtime/webapps/docs)  
 7-Mar-2013 6:43:18 AM org.apache.coyote.AbstractProtocol start  
 INFO: Starting ProtocolHandler ["http-bio-8080"]  
 7-Mar-2013 6:43:18 AM org.apache.coyote.AbstractProtocol start  
 INFO: Starting ProtocolHandler ["ajp-bio-8009"]  
 7-Mar-2013 6:43:18 AM org.apache.catalina.startup.Catalina start  
 INFO: Server startup in 4648 ms  

Windows users should check the Makefile source code and manually do what it does.

Now you should have all the three applications up and running. Go to http://localhost:8080/msglnk/ and set up the default email session with a gmail account. Go to http://localhost:8080/photodb/, inform a valid email address, set a password and click the "Create new user" button. You should see a page like this...


Go to http://localhost:8080/faceid/ and check if your user is already there. Check if you can login with this new user.


When you hit the "Ok" button, you should see this in the log...


Now check your email box. You should have a new email waiting for you.


Reply this email and wait up to 5 minutes to get your account activated, or go to http://localhost:8080/msglnk/ to and click the "Read emails from remote server" button to manually trigger the email reader.

Once your user is activated, try to login again.



Side note

The Msglnk GUI is implemented with YUI 3. Go check the code to see how to use this JS framework. We have now three different ways to implement JS applications:



Did you notice that the last thing we need to worry about is TomEE? :O)
Give it a try and have fun with Apache TomEE!

Tuesday, February 19, 2013

JAAS between two EE applications with TomEE, WS-RS and Javascript

Attention: This is old stuff. New build instructions here

PhotoDB no longer contains user data. Now its users credentials are remotely managed by FaceID. PhotoDB binds itself to it via via ws-rs from loginscript.js, and the JAAS does the rest.

While JAX-RS 2.0 and its very handy Client API are not yet official, our JAAS needs to use a regular java.net.URL connection. This way we avoid to get tied to any proprietary library. The loginscript.js opens an http connection to the remote application and authenticates the provided user credentials against what FaceID have stored. The resulting output is either a csv string with a list of all the groups in which the user is part, or a "Bad user or password" exception.

The applications now share the same TomEE installation, but you don't need to worry about it. We have a new Makefile task: make up-war-restart. So, let's build it!

Clone the projects and open two terminal windows.



First run make clean-start && make tail for the PhotoDB in order to build the application and tail the catalina.out file.



You should be able to use the PhotoDB application with the user "michael" and password "test".
 Now run make up-war-restart for the FaceID.


You should see some action in the catalina.out file.



Open http://localhost:8080/faceid/ and http://localhost:8080/photodb/


The FaceID application has only one user. The user name is admin and its password is admin. The PhotoDB application has no user yet. So, let's create one.


The "Groups" field is a comma-separated value like "groupA, groupB, groupC". For now let's use "photo-user".


Now go to PhotoDB and login with your newly created user.



Side note: Did you notice how fast TomEE starts up two applications?
Join the fun with TomEE! http://tomee.apache.org/

PhotoDB: https://github.com/tveronezi/photodb
FaceID: https://github.com/tveronezi/faceid

[]s