Integrating JDeveloper into build systems with OJServer

OJServer is a headless instance of JDeveloper.

You might ask what good is an IDE without a UI. Well if you think about it, an IDE is a collection of tools within a UI, but sometimes you want to use a tool without spending too much time interacting with its UI elements. Or you may want to use the same tool on multiple projects or files which would be cumbersome to open and close within an IDE.

Typically, most tools used within an IDE (like the java compiler) can also be run outside of it independently, but you may already have configured your IDE environment in a certain way so that the compiler is launched with the correct options, and your preferences and settings may be configured to your liking. Although these can be duplicated or copied or even referenced in some cases, its not quite the same context. Some tools, like the Refactoring tool, may not have a command-line equivalent, but you may still want to use some of these tools in your headless build environment.

OJServer can help integrate the capabilities of JDeveloper with other build systems. OJServer starts JDeveloper in headless mode and also starts up an RMI listener that listens for service requests. Each service is a distinct unit of work that can be triggered by a remote client. The service gets a JDeveloper context and can execute any arbitrary API available within the IDE (excluding View APIs of course).

By default, OJServer comes with two services pre-defined, a simple "Ping" service and a "Deploy" service. Here's how the Ping service is written:

Ping Service

package oracle.example.ojserver;

import oracle.jdeveloper.ojserver.spi.Server;
import oracle.jdeveloper.ojserver.spi.Service;
import oracle.jdeveloper.ojserver.spi.ServiceContext;

public class PingService implements Service {


    @Override
    public void start(Server server) {
        // Setup code goes here...
        
        server.getLogger().info("Ping service started");
    }

    @Override
    public void execute(ServiceContext serviceContext) {
        
        String greeting = (String) serviceContext.getProperty("greeting");
        serviceContext.getServer().getLogger().info("Server pinged! Text is " + serviceContext.getProperty("greeting"));
        serviceContext.setResult(new Date());
    }

    @Override
    public void stop(Server server) {
        // Release resources..
        
        server.getLogger().info("Ping service stopped");
    }
}
Create a JDeveloper extension with this service and plug it in using a trigger hook.

Registering the Service

META-INF/extension.xml


 <ojserver-hook xmlns="http://xmlns.oracle.com/ide/extension">
   <service name="ping"
            class="oracle.jdevimpl.ojserver.PingService"/>
 </ojserver-hook>

</triggers>

The Ping client

package oracle.example.client;

import java.net.MalformedURLException;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import oracle.jdeveloper.ojserver.OjClient;
import oracle.jdeveloper.ojserver.rmi.DefaultClientContext;


public class OJPing {
    public static void main(String[] args) {
        // Accept greeting  from args
        if ( args.length == 0 ) {
            printUsage();
            System.exit(1);
        }
        // Optionally also accept OJServer host and port from args
        // For now, we assume defaults.
        
        
        String greeting = args[0];
        
        
        OjClient client = new OjClient("localhost", 2010);
        DefaultClientContext context = new DefaultClientContext();
        
        context.setProperty("greeting", greeting);


        try {
            client.invoke("PingService", context);
            
            Object result = context.getResult();
            System.out.println("OJServer returned " + result);
            
            System.exit(0);
            
        } catch (NotBoundException e) {
            System.out.println("Invalid service name");
            System.exit(1);
        } catch (MalformedURLException e) {
            System.out.println("Invalid server URL");
            System.exit(1);
        } catch (RemoteException e) {
            System.out.println("Exception on Server: " + e.getMessage());
            System.exit(1);
        }
        
    }

    private static void printUsage() {
        System.out.println("OJPing <greeting> ");
    }
}
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Techniques on extending JDeveloper

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today