Showing posts with label build. Show all posts
Showing posts with label build. Show all posts

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

Sunday, December 2, 2012

Ubuntu, openjdk-7-jdk, ant, subversion, git, chromium and the command line

Attention: This is old stuff. New build instructions here

The objective today is to build the photodb application and the trunk version of the TomEE/OpenEJB server from scratch with a brand new Ubuntu box. For this demo, I will use a virtual machine with 2GB of RAM.

Installing the OS
Install all the tools we need. Copy and paste these lines to a terminal.

sudo apt-get install -y openjdk-7-jdk ant subversion git chromium-browser && \
mkdir -p ~/dev/bin && \
cd ~/dev/bin && \
wget http://www.apache.org/dist/maven/maven-3/3.0.4/binaries/\
apache-maven-3.0.4-bin.tar.gz && \
tar -xvzf apache-maven-3.0.4-bin.tar.gz && \
sed -i '$ a\' ~/.bashrc && \
sed -i '$ a\#photodb export' ~/.bashrc && \
sed -i '$ a\export M2_HOME=$HOME/dev/bin/apache-maven-3.0.4' \
~/.bashrc && \
sed -i '$ a\export PATH=$M2_HOME/bin:$PATH' ~/.bashrc && \
export M2_HOME=$HOME/dev/bin/apache-maven-3.0.4 && \
export PATH=$M2_HOME/bin:$PATH && \
mkdir -p ~/dev/ws && \
cd ~/dev/ws && git clone https://github.com/tveronezi/\
photodb.git && \
cd photodb && \
git checkout b319d23aba888aa3059f953c5c551e0dda1bd346 && \
cd ~/dev/ws/photodb && \
make start-tomee && \
sleep 20 && \
chromium-browser http://localhost:8080/photodb/

The commands above will:
  1. Install all the binaries we need;
  2. Download and install Maven 3;
  3. Clone the photodb source code from git;
  4. Start the TomEE server.

Sit back and enjoy the output lines going by. It will take some time to complete the execution of all these commands (About one hour in my VM), but this is just one time thing. Maven downloads about 1GB of binaries for us. It also builds our application and the Apache TomEE/OpenEJB server locally.

See the screenshots of this process bellow.

Open a terminal

Copy and paste the commands

Installing the binaries

Installing Apache Maven

"svn co" of the Apache TomEE/OpenEJB source code

Building the TomEE server

Building our application

Starting the server.

The script will open the "index.html" page
We have some handy commands to start, stop and update your application.

  • "make kill-tomee" kills the TomEE server instance.
  • "make start-tomee" kills any running instance of TomEE, runs "mvn clean install -DskipTests=true" (20 seconds in my mv) and restarts the TomEE server (5 seconds in my vm).
  • "make up-static" replaces the JS code in the server.
  • "make run-jasmine" runs the JS unit tests.
  • "make clean" runs "mvn clean" and updates the TomEE/OpenEJB source code. If you run "make start-tomee" after "make clean", the script will rebuild the TomEE/OpenEJB server (12 minutes in my vm).
Lets test our application. Copy and paste these commands to a terminal.

cd ~/Pictures/ && \
wget https://dl.dropbox.com/u/1459144/posts/pele.jpg && \
nautilus pele.jpg 


Drag the picture to the browser

Photo uploaded!

Do you want to join the fun?
Learn more about the TomEE/OpenEJB server: http://tomee.apache.org/
Clone the photodb source-code: https://github.com/tveronezi/photodb