2 Ekim 2015 Cuma

What is the CSS feature - "box-sizing"?

For W3C, width and height values contain only content area. Padding and border values are not contained by these values. Most browsers use this standart.

However, IE6 and previous versions of IE6 accept such a rule that width and height values contain not only content area, but also padding and border values.

Because of this, W3C creates a feature in css to give a chance to user for selection of box-sizing type;

Structure : box-sizing: <value>
Values : content-box | border-box
Default value: content-box
Applicable elements: All elements
Inheritence: No

References: 
1) https://css-tricks.com/box-sizing/
2) http://fatihhayrioglu.com/css3-box-sizing-ozelligi/
3) http://www.w3schools.com/cssref/css3_pr_box-sizing.asp

10 Nisan 2015 Cuma

Object.wait() & Object.notifyAll() methods in Synchronization Block

Example Code:
public class MyClass {
  private List temp = new ArrayList();

  public void addItem(V obj) {
    synchronized (temp) {
      while (temp.size() >= maxSize) {
        temp.wait();
      }
      temp.add(obj);
    }
  }

  public void clear() {
    synchronized (temp) {
      temp.clear();
      temp.notifyAll();
    }
  }
}
At the first look, some questions come to my mind such as;

1) If "temp"(monitor object) is locked in sychronized block of addItem method, how can processor pass over "synchronized (temp)" in clear method and run "temp.notifyAll()" to wake up monitor object?

Answer: Object.wait() releases the monitor object! Hence, processor can get into synchronized code block in clear method and notify all threads that are put waiting.

2) Is it possible that Object.wait() is used without synchronized block?

Answer: Maybe not in this example above, but the condition in "while" may be set by a separate thread. Hence, synchronization code block is required to have this work correctly.

2 Nisan 2015 Perşembe

How to update the page when some events are occurred at the server on JSF

In simple JSF project, client request the server dynamically to get javascript and HTML codes. But, how can we see changes on HTML page when some events are occurred at the server-side?

Standart JSF 2.0 components does not supply this feature, that's why third party libraries are used to push HTML page, such as; Primefaces(<p:push>), RichFaces(<a4j:push>) and IceFaces(IcePush). However, while applying these features of these libraries, we may get in trouble with the server that we use. It may take time to configure the server and reach the solution!

Hence, I recommend <p:poll> of Primefaces. In polling mechanism, client sends ajax request to the server every 'X' seconds that we can configure. Then, server can update HTML page within necessities.

 It is very easy to apply it on JSF projects;
<p:poll interval="10" listener="#{bean.checkNotification}" oncomplete="prepareUI()" update=":form:growl :form:isUpdated" />

Specification of Port Number on Socket Programming

In applications that provide communication via socket, it is important that port number should not be between 1 and 1024. Ports between these numbers are used by root for other specific processes. If we choose a number between 1 and 1024 to specify port, we meet "Bind Permission Error" in our program!

7 Ekim 2014 Salı

How to make background (out of popup) inactive in jQuery UI, when popup appears?

There is a nice pop-up (dialog) in jQuery UI. It is easy to implement and control. The dialog window can be moved, resized and closed with the 'x' icon. 
There is a small example below;

***************************
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful<br/> for displaying information.</p></div>----------------------------------------
var commDialog = $("#dialog").dialog({ autoOpen : false, resizable : false, height : 520, width : 550, show : { effect : "fade", duration : 1000 }, hide : { effect : "fade", duration : 500 } });

commDialog.dialog('open');
commDialog.dialog('close');
***************************

*** Sometimes, when a pop-up appears, an user is forced not to use any other element out of pop-up element. It can be provided with disabling background behind the pop-up. There are two solution to disable background.

a) First solution is classic masking;

***************************
<div class="mask"></div>
----------------------------------------
.dialog {
position:absolute; z-index: 1; }

.mask { position: fixed; top: 0; left: 0; background: #000; opacity: 0.8; z-index: 2; height: 100%; width: 100%; }
----------------------------------------
$(".mask").fadeIn('slow');
$(".mask").fadeOut('slow');
***************************

b) Second is gained by using a property of JQuery UI Dialog;
 >> Related property is "modal". When "modal" is true, it means that masking is supplied on the background.

***************************
var commDialog = $("#dialog").dialog({
autoOpen : false, resizable : false, modal: true, height : 520, width : 550, show : { effect : "fade", duration : 1000 }, hide : { effect : "fade", duration : 500 } });
***************************

12 Mayıs 2014 Pazartesi

Container Managed Authentication

Authentication and authorization is not so easy work-load for programmers who are not expert on JSF framework. Understanding different solutions and choosing the right one based on requirements is very critic in this context. Solutions I mention can be categorized such as;

A - Java EE Container Managed Authentication
B - Homegrown a Servlet Filter
C - 3rd Party Java EE Authentication Frameworks

While I was searching the right solution, I worked on small examples on JSF 2.x, Tomcat 7.0 and Hibernate. One of them was developed based on container managed authentication. I think that this solution is the easiest to implement on Web applications. There are five steps to adapt this solution into your application;

 1) Configure server.xml  on the Tomcat directory;
<Realm className="org.apache.catalina.realm.JDBCRealm"
       driverName="com.mysql.jdbc.Driver"
       connectionURL="jdbc:mysql://localhost:3306/authentication_db"
       connectionName="..." connectionPassword="..."
       userTable="user" userNameCol="USER_NAME" userCredCol="PASSWORD" 
       userRoleTable="usergroup" roleNameCol="USER_GROUP_NAME" />

2) Create entities (User and User Group) based on JDBCRealm implementation on the link - https://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#Standard_Realm_Implementations

3) Not to forget updating hibernate mapping files of entities based on changes on entities after Step-2. JDBCRealm Implementation needs many-to-many relationship between User and User Group tables, that's why related tags on mapping files should be configured properly.

4) Configure web.xml file;
    <security-constraint>
        <display-name>Restricted</display-name>
        <web-resource-collection>
            <web-resource-name>Restricted Area</web-resource-name>
            <url-pattern>/authorized/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/login.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <role-name>user</role-name>
    </security-role>

5) Lastly, create your login function;
public String login(){

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        try {
            //Login via the Servlet Context
            request.login(getLoginName(), getLoginPass());


            return "success";
        } catch (ServletException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Login", null));
            e.printStackTrace();
        }
        return "failure";
}

Optimization can be made based on reqirements of an application, but the development-process will be similar I present above for container managed authentication.

20 Aralık 2013 Cuma

Hibernate - JSF Project Error : JavaReflectionManager cannot be cast to MetadataProviderInjector

During working on Hibernate-JSF project on Eclipse, I got such errors;
java.lang.ClassCastException: org.hibernate.annotations.common.reflection.java.JavaReflectionManager cannot be cast to org.hibernate.annotations.common.reflection.MetadataProviderInjector
java.lang.NoSuchFieldError: INSTANCE
For this situation, two annotation modules are detected in library files that are already added into the project for Hibernate processes. It causes conflict. Since "hibernateX.jar - hibernate core library" can perform annotation task without other dependency, you should delete both "hibernate-annotations.jar" and "hibernate-commons-annotations.jar" files to solve these kinds of problems.