Thursday, July 4, 2013

How to excute a java code inside a P/L SQL

I will demonstrate this with a simple HelloWorld java program.
1.    1.    First write HelloWorld program in java.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author MiFoLK
 */
public class HelloWorld {
   
       
    public static String hello(){
        return "hello world !!";
    }
}

2.       Then we have to load the java class in Oracle database using loadjava tool. So we have to copy the HelloWorld.java to the bin folder of the oracle. We have to give username and password.


Eg:   loadjava -u user_name/password -v -resolve HelloWorld.java

Figure 1




Further we can verify the compilation and load with a simple query against user objects. If it is done correctly the status is showing as valid.

Figure 2


3.    Publish the java class in Oracle database.
Now I have used P/L Sql Develper to write the function where the java class is called.
Basically it is done in this way.

CREATE OR REPLACE FUNCTION hello_func(varchar2 pubkey) 
-- Declare a data base function called hello_func which takes a varchar2 param and returns ---------varchar 2
RETURN varchar2
-- This function should be declared according java function
AS LANGUAGE JAVA
NAME 'HelloWorld.hello(java.lang.String) return String';
--Map the java function with database function.

This is how I applied it into my simple example.

Figure 3


3.1   Open a new SQL Window and type this Query.
3.2   Then execute the query. 
3.3   Go to Objects window and refresh the functions folder.
3.4   Then  HELLO_FUNC function will appear. 

1.                    4. Calling P/L SQL procedure.
4.1   Right click on HELLO_FUNC function and click test.
4.2   Then you can see a test script.
Figure 4

4.3   Once you execute it you can see the result of the program. 

Figure 5

In this manner you can execute java programs via oracle p/l sql query. :)





Sunday, June 23, 2013

How to create war component from command prompt

Creating war file is a simple if you use an IDE when you are programming in J2EE applications.
Now Let's see how to create this war component using command prompt.

1. First create a folder in a directory you wish to create the .war file. I named it as HelloWar.

2. Prepare these two components inside HelloWar folder.
           1. WEB-INF folder
           2. index.html or index.jsp

3. Inside the WEB-INF folder, place the followings.
           1. lib folder
           2. classes folder
           3. jboss-web.xml
           4. web.xml.
Figure 1- jboss-web.xml

Figure 2- web.xml

4. Now it's time to go :)
So first go the folder using command prompt and then you can check the folder structure of the application.


5. Then now we are going to create .war using  jar -cvf HelloWar.war * command.

6. With the above command you add .war to the application folder. Again you can verify this.




How to call ejb from mbean


As this ejb and mbean both going to be deployed in Jboss server I have implemented in a way that both are implemented in same EAR project, but ejb and mbean is made keeping their differences of implementation.


First Let's implement EJB part. 
1.
 Implementation of Adder Interface
Figure 1- Implementation of Adder Interface
2.
Implementation of AdderBean
Figure 2- Implementation of AdderBean


3. Then clean and build the project EJBDemo3

building the project EJBDemo3
Figure 3-  building the project EJBDemo3


4. Then go to dist folder in EJB and find that EJBDemo3.ear file and deploy in Jboss server in your working instance. When you deploy it you will see this kind of output in the command prompt.
 Command prompt view of the output
Figure 4 - Command prompt view of the output



Now we are going to create mbean


5. When creating mbean service there are some jars you should have to add as libraries. Those are
            Jakarta Commons IO,
            Jakarta Commons Logging,
            jboss-common.jar,
            jboss-jmx.jar ,
            jboss-system.jar,
            jboss-xml-binding.jar

And in this case we need another Jar called  org.jboss.annotation.ejb.ServiceJ 



6. AdderService implementation

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.mbean;

import org.jboss.system.ServiceMBeanSupport;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import org.jboss.annotation.ejb.Service;

import demo.ejb.Adder;

/**
 *
 * @author Minoshini
 */
@Service (objectName = "demo.mbean:service=Adder")
public class AdderService extends ServiceMBeanSupport implements AdderServiceMBean{
   
   private int a=2;
   private int b=2;
  
   public int getA() {
        return a;
    }

     public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }
   
    public int add(){
       try {
            InitialContext ctx = new InitialContext();
            Object ref = ctx.lookup("EJBDemo3/AdderBean/local");
            Adder adder = (Adder) PortableRemoteObject.narrow(ref,Adder.class);
            return adder.add(a, b);
        } catch (NamingException e) {
            e.printStackTrace();
            return 0;
        }
   }
   // The lifecycle
   protected void startService() throws Exception
   {
      log.info("Starting of AdderService");
   }
   protected void stopService() throws Exception
   {
      log.info("Stopping of AdderService");
   }
}




7. AdderServiceMBean


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.mbean;

/**
 *
 * @author Minoshini
 */
public interface AdderServiceMBean {
   int getA();
   void setA(int a);
   int getB();
   void setB(int b);
  
   public int add();
}



8. After that again clean and build the EJBDemo3 Project


9. Then go the build project like this E:\java\EJBDemo3\EJBDemo3-ejb\build
In the META-INF folder there is a jboss.xml and rename it as jboss-service.xml and edit it like this.

 jboss-service.xml
Figure 5-  jboss-service.xml 



10. Then we are going to create the .SAR folder structure to deploy in jboss server. It should follow this order.

adder-service.sar
adder-service.sar/META-INF/jboss-server.xml
adder-service.sar/demo/mbean/AdderService.class
adder-service.sar/demo/mbean/AdderServiceMBean.class


11. Then deploy this adder-service.sar folder in deploy folder of Jboss instance you are working with.


12. After that go to the http://localhost:8080/jmx-console/ and see mbean.demo and service Adder.

Service Adder
Figure 6- Service Adder


JMX MBean view
Figure 7- JMX MBean view 


From the mbean you can enter inputs also. When you call the add it will direct to ejb and retur the result.
 Output of the EJB via JMX-Console
Figure 8- Output of the EJB via JMX-Console

Wednesday, May 22, 2013

Object-Oriented JavaScript

JavaScript Today

JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers, PCs, laptops, tablets, smart phones, and more .

JavaScript is a prototype-based scripting language that is dynamic, weakly typed, and has first-class functions. Its syntax was influenced by the language C. JavaScript copies many names and naming conventions from Java, but the two languages are otherwise unrelated and have very different semantics. The key design principles within JavaScript are taken from the Self and Scheme programming languages.It is a multi-paradigm language, supporting object-oriented,imperative, and functional programming styles.

JavaScript's use in applications outside of web pages—for example, in PDF documents, site-specific browsers, and desktop widgets—is also significant. Newer and faster JavaScript VMs and frameworks built upon them (notably Node.js) have also increased the popularity of JavaScript for server-side web applications.
JavaScript was formalized in the ECMAScript language standard and is primarily used as part of a web browser (client-side JavaScript). This enables programmatic access to computational objects within a host environment.

Object Orientation 

Thursday, May 16, 2013

HTML5 Graph Drawing with rgraph


RGraph is a HTML5 charts library that uses the HTML5 canvas tag to draw and supports over twenty different types of charts. Using the new tag, RGraph creates these "HTML charts" inside the web browser using JavaScript, meaning quicker pages and less web server load. This leads to smaller page sizes, lower costs and faster websites.

Forget Flash or Silverlight - JavaScript and now HTML5 canvas are built in to all modern browsers and allow for quick and easy 2D drawing. Using RGraph to produce your charts makes the process a breeze and enables you to quickly get up and running. HTML5 Canvas is supported by all modern browsers and mobile devices meaning your charts and graphs will be seen by the widest possible number of users. You can read more about the canvas tag here as well as more about JavaScript charts here.

Some Example of the Types of Graphs that can be drawn.


Bar charts


Line charts





Gauge charts





http://www.rgraph.net/examples/gauge.html