Wednesday, October 2, 2013

Configuring JBoss AS 7 Data Source with Oracle


1. Create directory for the oracle driver deployment

 $ cd $JBOSS_HOME/modules  
 $ mkdir -p com/oracle/ojdbc6/main  
 $ cd $JBOSS_HOME/modules/com/oracle/ojdbc6/main  
 $ vi module.xml  


2. Add following to the module.xml

 <module xmlns="urn:jboss:module:1.0" name="com.oracle.ojdbc6">  
  <resources>  
   <resource-root path="ojdbc6.jar"/>  
  </resources>  
  <dependencies>  
   <module name="javax.api"/>  
  </dependencies>  
 </module>  

3. Copy the downloaded ojdbc6.jar to the  $JBOSS_HOME/modules/com/oracle/ojdbc6/main
4. Edit following file

 $ vi $JBOSS_HOME/standalone/configuration/standalone.xml  

5. Look for following in the above file

 <subsystem xmlns="urn:jboss:domain:datasources:1.0">  

6. Insert following driver config in the drivers section

 <driver name="oracle" module="com.oracle.ojdbc6">  
   <xa-datasource-class>oracle.jdbc.OracleDriver</xa-datasource-class>  
 </driver>  

7. Insert following data source under the data sources section

 <datasource jta="true" jndi-name="java:/myAppDS" pool-name="OracleDS" enabled="true" use-java-context="true" use-ccm="true">  
   <connection-url>[--connection url--]</connection-url>  
   <driver>oracle</driver>  
   <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>  
   <pool>  
     <prefill>true</prefill>  
     <use-strict-min>false</use-strict-min>  
     <flush-strategy>FailingConnectionOnly</flush-strategy>  
   </pool>  
   <security>  
     <user-name>[--user--]</user-name>  
     <password>[--password--]</password>  
   </security>  
 </datasource>  

Multiple Instances with JBoss 7 and Issue the build with Maven


You might require to run multiple JBoss instances in the same JBoss server by applying different ports to them. Following are the steps to follow in JBoss 7.

1. Go to JBoss directory and copy the standalone directory and rename it to a preferred name. Lets say it is demo.

2. Then go to demo/configuration directory and open the standalone.xml file and find the element of "socket-binding-group".You can observe the default as follows.

 <socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">  

4. Change the "port-offset" attribute of that element to another number to shift the ports of the new instance. Lets say we changed it to the 100 as follows.

 <socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:100}">  

5. Then move to the bin directory and execute the following command to start the server

 ./standalone.sh -Djboss.server.base.dir=../demo  

6. You are done with another instance within the same JBoss and access in the browser with the port 8180 as we set the offset to 100 in 4th step.

7. If you are using Maven as the build tool and jboss-as plugin to deploy, you can use the following command to deploy your application

 mvn clean package jboss-as:deploy -Djboss-as.port=10099  

Monday, September 23, 2013

Compose sorted map using an unsorted map's values in Java

You might have unsorted map and sometimes you might need to sort them in some requirements using it's values, not the keys. You can have a util method like this to do it. Make sure the value's class must implement the Comparable interface to mention the sort criteria.

 public static Map sortMap(Map unsortedMap) {  
           if (unsortedMap != null && unsortedMap.size() > 1) {  
   
                List list = new LinkedList(unsortedMap.entrySet());  
   
                Collections.sort(list, new Comparator() {  
                     public int compare(Object o1, Object o2) {  
                          return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());  
                     }  
                });  
   
                Map sortedMap = new LinkedHashMap();  
                for (Iterator it = list.iterator(); it.hasNext();) {  
                     Map.Entry entry = (Map.Entry) it.next();  
                     sortedMap.put(entry.getKey(), entry.getValue());  
                }  
                return sortedMap;  
           } else {  
                return unsortedMap;  
           }  
      }  

Disabling all the click events under a div element using jQuery

You might have a requirement to disable all the click events under a particular div. Following is a way to disable click events.

 var disable = function() {  
             var divElement = $("#myDiv");  
                var disableMask = $("<div></div>");  
                disableMask.attr("id", divElement.prop("id") + "_mask");  
                disableMask.css({  
                     "position" : "absolute",  
                     "width" : "100%",  
                     "height" : "100%",  
                     "zIndex" : "100"  
                });  
                divElement.append(disableMask);  
 };  
   
 var enable = function() {  
       var divElement = $("#myDiv");  
       var disableMask = $("#" + divElement.prop("id") + "_mask");  
       if(disableMask != null){  
                  $("#" + divElement.prop("id") + "_mask").remove();  
       }  
 };  

Absolute and Relative HTML div positioning using jQuery

Lets say you have multiple HTML div elements to be absolute positioned under another div element, then you need to make parent div positioned as relative and then the child div elements to be positioned as absolute. You can change the other css attributes as well using jQuery on child div elements.

 var parentDiv = $("#parent");  
 parentDiv.css({  
   "position" : "relative",  
   "height" : "100px",  
   "width" : "200px"   
 });  
   
 var childDiv = $("<div></div>").appendTo(parentDiv);  
 childDiv.prop("id","child");  
 childDiv.css({  
   "position" : "absolute",  
   "height" : "50px",  
   "width" : "50px",  
   "top" : "10px",  
   "left" : "10px"  
 });  

Now the child div element is placed under absolutely within the parent div according to the css properties of top, left, right, bottom. You can have any number of child div elements positioned like this  and change the positioning properties accordingly.