A basic JSF application to manage Freeradius-2

Now it is time to examine xhtml files. First of all, you need to have a welcome file list. Please add a figure like following;

<welcome-file-list>
    <welcome-file>SOMETHING-COMES-HERE.XHTML</welcome-file>
</welcome-file-list>

You can find my web.xml file in jsfapplication/src/main/webapps/webinf.

Webapp folder is where you add your xhtml files.

-Right click to the webapps folder.

-Click new

-Click HTML File

-Choose xhml file format then enter the name of the xhtml file. This will be where you write your html code with Primefaces themes.

We must be careful when we write tags. We must put h before tag names else the themes will not be seen. Such as;

<h:head>
    <title>ADD USER</title>
</h:head>

In my xhtml files, i used panels, datatables, outputlabels, inputtexts, command buttons and select check boxes. You can find my xhtml files in jsfapplication/main/webapp.

Now let’s examine the necessary tables for our database. As i mentioned in my first article, there is a one-to-many relationship between a user and role. That is why i created a cross table that has the id of a user and the id of a role. The structure is

CREATE TABLE 
(
    userId UNSIGNED_INT NOT NULL,
    roleId NOT NULL
);

And we need the role table like following;

CREATE TABLE Roles(
roleId INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
roleName VARCHAR(100)
);

And for the users, we are going to use radcheck table.

For this relationship, i created an extra dao to manage the actions more efficiently.The queries for the cross table( the insertions and deletions) are done in this dao. And we use this table to get the roles of all the users. To prevent the dublicates, i used maps and sets in the dao of user.

Finally, i will explain the unit tests. First of all my reference book was junit recipes by rainsberger, a very straightforward textbook that explains how to write test classes of a jsf project.

First, you need to add Groovy plugin and dependencies.Add the dependency like following;

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.3.0</version>
</dependency>

Add the plugin like following;

Go to Files-Settings-Plugins and if there is no groovy, please press + button to add Groovy.

After that you should be getting a package under your base package called groovy. That package will be the place for your test classes.

You can write test classes or methods for everything in your project. For constructors, functions, queries, objects, getters and setters, etc. The main idea is that you should test what is not supposed to happen. If it does, the test class is working; which means there is something wrong with your testcase.

The structure is easy. When you right click to your class and say create test, it will automatically be opened. Your test class should be extended to GroovyTestCase . And the return types of your functions will be in the format of

assertEquals(<expected parameter>,<actual parameter>)

You can examine my test classes that i have written for possible errors in jsfapplication/main/java/Groovy.

That will be all for my project. If you have any concerns,Β  please reach out to me i would be happy to get the critizations from you. Enjoy your coding!

Advertisements

A basic JSF application to manage Freeradius-1

Hi again. In this article i will give a detailed explanation about my application. The code for the project will be citated to my github account in some paragraphs(https://github.com/aktasrozerin/jsfapplication). It is something that one can manage the radius server users and clients. It is just the beginning part i am still working around it, when i make changes, i will upload them immediately.

Let’s start from stratch. What we need now is to create a maven project. By the way i will be using intellij-idea but it is similar to Eclipse and NetBeans anyway. After creating a maven project, the rest is about fill the blanks πŸ™‚

Now we need to create our base package. You can name it as you wish. That package will be where you put your objects, beans, data access objects, services, html pages, etc. You need to add the base package information into application-context.xml file like

<context:component-scan base-package="radius"/>

pom.xml is where we put our plugins, repositories and dependencies. You can copy my pom.xml file into yours or you can use other features according to your application. You can find my pom.xml file in the comments below the page.

For now my project has user feature but soon i will also upload client feature. For user feature, i have one adapter class called UserAdapter. You can find this class in jsfapplication/src/main/java/radius/adapters/UserAdapter.

The users also have some roles. Those roles can be added from the web application interface. It is also in jsfapplication/src/main/java/radius/adapters/RoleAdapter. A user may have more than one role, so between a user and role, there is a one-to-many relationship. If you have freeradius installed in your computer, that means you have a database. It’s name doesn’t matter but you will have to add a few tables more for that reason. First of all please add MySQL to your project. At the rightmost of the screen there is a bar called “database“.

-Click on it and press Alt+Insert to choose a database.

-Click data source then MySQL.

-Enter your freeradius database name into database name part. Also add your MySQL username and password and check connection.

Now it is time to introduce the beans. The beans are the objects for html pages. Every html function and data must be defined in those beans. You can define as many beans as you like. As long as it has service connection with @ManagedProperty. You can see my beans in jsfapplication/src/main/java/radius/Beans.

Next, you are going to need converters. If you don’t have converters, you will get errors when you pass bean parameters in your html pages. They will be understood as strings instead of objects. You will pass those converter classes in your html files to prevent that error. You can also find my converters in radius package.

After that we now have data access objects(daos). For now i have three daos but it will be more. In those daos i defined data sources(connections with MySQL), jdbctemplates(objects that executes queries) and methods that will be done in web application such as insert,update and print. See DAO package for daos and their interfaces in radius package.

You have to include every single dao into your application-context.xml file. This is a Spring Config file please don’t get mixed. It’s name can be something else as long as it is included in web.xml. One example for addition is like following;

<bean id="JDBCUserDao" class="radius.DAO.DaoImp.JDBCUserDaoImp">
    <property name="dataSource" ref="dataSource" />
</bean>

And also please add your MySQL connection informations like following after your beans in application-context.xml file.

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/radius"/>
       <property name="username" value="root"/>   //you don't have to be a root
       <property name="password" value=""/>   //type your password in here
</bean>

In the next article i will explain xhtml files, other tables and unit tests. Hope to see you there too πŸ™‚

Deploying web applications with Tomcat Web Application Manager

In the previous article, i mentioned about deploying web applications with docker. In this one, i will be talking about working in localhost. Tomcat has an interface in which one can manage all the configurations, applications and setting easily. All we have to do is to create users. But of course to do that, our application must be using tomcat server.

Let’s start configuring our web application. If you have created a maven project, please right click to pom.xml and open settings.xml. It will be containing something like this;

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">


</settings>

You should add a proxy and a server for your web application. Please write a code like the following before </settings>

<servers>
    <server>
        <id>tomcat7</id>  //my tomcat is tomcat7
        <username>rozerin</username>
        <password>PASSWORD</password> //type a password in here
    </server>
</servers>
<proxies>
    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>rozerin</username>
        <password>PASSWORD</password> //type a password in here 
        <host>proxy.xxx.com</host>
        <port>8080</port>
        <nonProxyHosts>xxx.com</nonProxyHosts>
    </proxy>
</proxies>

For that part you are done. Now it is time to make arrangements for tomcat server.

Please open tomcat folder. You will see a conf folder. Open that too. You will see a tomcat-users.xml file. We must edit that file. Open the command line and enter into that file typing;

sudo nano <path_to_your_tomcat-users.xml_file>

Screenshot from 2015-10-07 17:00:41

Make sure that you have defined a manager-script role. In the password part, please write your password you have written in settings.xml file. And also make sure that your usernames are the same.

Now it is time to test our configurations. Open your web browser and type

localhost:8080/manager

or

127.0.0.1:8080/manager

You will be getting and authentication screen. Type your username and password. If there is no error you will get the interface for tomcat like following;

Screenshot from 2015-10-07 17:04:12

If you see this, you are done. The topic of my previous article was to deploy applications. At the bottom of the page, one can upload the war files easily. If you don’t, please check your tomcat-users.xml and settings.xml files. Probably the mistake is somewhere in those files or check your catalina logs.

That is all for now. Hope to see you in my next article πŸ™‚

Deploying JSF applications with Docker

Hello again. In this article i will explain how to run an application on a docker container. Docker is an open-source project that automates the deployment of applications inside software containers build from images. This gives the opportunity of flexibility and portability for submitted applications. The management is extremely easy. It is managed on linux command line. For more information and more commands, please see Docker official page https://www.docker.com/

To deploy an application with docker, one has to use docker containers. Docker containers can be pulled by docker run or docker pull command. If the image is not installed for that container, Docker will install it automatically. Or the images can be created manually according to the necessary software.

Before we start, i feel like i need to give some pre-informations about the usage of docker. The relation between a container and an image can be taught as a sculpture and mold. The container is sculpture and the image is mold. Which means a container is made of an image and to run an application on docker, one needs to create a container from an image. When docker creates the container automatically, that is pretty much the way it achieves that goal. More than one container can run at the same time. If you want to see your containers, please type on the command line

docker ps

You will see your running containers. If nothing is listed, this means that there is no container running at the current moment. Please type

docker ps -a

If you haven’t created any container, this list will be empty too. Don’t worry, when we create our image, we also will create our container. Now it is time to create our image.

As i said, the images can be created manually. This is what we will do. If you want to create an image, you have to be familiar with dockerfiles. A dockerfile is a file that keeps the necessary information about associated image and container. And it has a specific language for its own. For example, there is a RUN command that runs the application or file that is typed. There are many commands for dockerfile but we will be dealing with only a few of them.

In our example, i will try to run a JSF application which is configured with a tomcat server from a docker container. Now we need the .war file of our application. You can use ant build to get your file or you can create it from the command line of the compiler or you can execute a goal for building it. My compiler is intellij idea but that is pretty the same for the other Java compilers. If you are using idea, please type

execute maven goal

in the search button. Make sure the checkbox that says “include non-project items” is selected. Select it. Make sure that your project path is typed in the first line. In second line, type

war:war

After the execution, go to your target class. You will see a war file. If that is not created, try to see if there is any error by typing

mvn build tomcat7

or

Look at the rightest side of the window. You will see “Maven Projects“. There are options like clean, install and compile. Try to find the error by double clicking those options. In the command line or in the logs, you can find the errors easily.

Now we need to copy that war file into tomcat files. Please open your tomcat folder. It is in the location where you installed it. In that folder, try to find webapps. This is the folder where you will copy your war file. But from the interface that is not possible you have to do this operation from command line. Please type in the command line

cp <your .war file location> <your webapps location>

Before you ensure that the war file is there, please do not continue. This is a must.

Now it is time to create our image. Please type in the command line

sudo nano Dockerfile

there will an empty page appear if you haven’t created a dockerfile before. Below you can find my dockerfile.

Screenshot from 2015-10-07 16:12:54

FROM command is to include the base image from public repositories(since docker is an open-source software).This command is a must.

RUN command is to run the necessary commands for the container. In my file, i am trying to run tomcat and the war file. Just like writing in the command line.

EXPOSE is to allocate the necessary port. Since we are running a web application, it is 8080.

ENV is for assignments. Sets the http port to 8080.

WORKDIR is to give an appropriate path to application.

CMD is to execute the container. There can only one cmd command in a dockerfile. If you insert more than one, the last one will be executed(tomcat is run from catalina.sh).

Now it is time to create our image from that dockerfile. Please type

docker build -t <name_of_your_image> .

Please don’t forget to put a dot at the end of the command. You must eventually see “Succesfully build <id_of_the_image>”

Next, we need to create our container. Please type

docker run -p <a_port_number>:8080 -d <name_of_your_image>

Instead of <a_port_number> we could have wrote 8080, but since it is allocated by tomcat, we need to assign a different port. Now type docker ps -a to see your container. It must be something like in the following picture;

Screenshot from 2015-10-07 16:31:58

Look at the top-most container. It is the port that i am running. For now we are done.

Without docker, we can run our application with localhost:8080. When it is docker, we need to use docker’s ip address.

Please type

ifconfig

in the command line. At the top-most, you must be seeing a docker part. You can see docker ip.

Open your web browser now. Please type

<your_docker_ip>:8080/<name_of_your_application>

You should be seeing your application there.My output is something like that ( It is an application that gets the users for radius server as input and lists them. You can see that application inΒ https://github.com/aktasrozerin/jsfapplication ) Yours should be seen as following;

Screenshot from 2015-10-07 17:51:23

If there is any error, i would be happy to help. That is the way we deploy our applications in a better and efficient way with docker.

That is all for this article, hope to meet you again and enjoy your coding!