bandt
bandt

Reputation: 41

Application Wrapper using Java

Is it possible to implement a wrapper application for other (Java) applications using Java?

The purpose is to enforce usage policies for documents independent of the application used to work with a specific document.

E.G. I have an encrypted file that needs to be decrypted and opened in some kind of editor. So the wrapper application would decrypt the file and start the editor within itself to enforce an read-only policy by denying the write-access to the application, for example. Therefore the Runtime.getRuntime().exec(<command>) method doesn't fit well :)

There are also some ways to intercept method invocations within the same application but none that would wrap a whole other application.

I've also read about altering the JVM itself to intercept the file access. That sounds pretty good. But I need to dynamically change the policy depending on a user. That might not work as far as I know by now.

I guess there might not be any way to do this using Java code, but I'd appreciate any kind of hints and help.

Upvotes: 4

Views: 267

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168845

I've also read about altering the JVM itself to intercept the file access. That sounds pretty good. But i need to dynamically change the policy depending on a user.

Set a custom SecurityManager that overrides checkWrite(String) to throw an exception.

Here is a simple example that prevents child frames from exiting the VM (checkExit(int)).

import java.awt.GridLayout;
import java.awt.event.*;
import java.security.Permission;
import javax.swing.*;

/** NoExit demonstrates how to prevent 'child' applications from 
 * ending the VM with a call to System.exit(0). */
public class NoExit extends JFrame implements ActionListener {

    JButton frameLaunch = new JButton("Frame");
    JButton exitLaunch = new JButton("Exit");

    /** Stores a reference to the original security manager. */
    ExitManager sm;

    public NoExit() {
        super("Launcher Application");

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        sm = new ExitManager( System.getSecurityManager() );
        System.setSecurityManager(sm);

        setLayout(new GridLayout(0,1));

        frameLaunch.addActionListener(this);
        exitLaunch.addActionListener(this);

        add( frameLaunch );
        add( exitLaunch );

        pack();
        setSize( getPreferredSize() );
        setLocationByPlatform(true);
    }

    public void actionPerformed(ActionEvent ae) {
        if ( ae.getSource()==frameLaunch ) {
            TargetFrame tf = new TargetFrame();
        } else {
            // change back to the standard SM that allows exit.
            System.setSecurityManager(
                    sm.getOriginalSecurityManager() );
            // exit the VM when *we* want
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        NoExit ne = new NoExit();
        ne.setVisible(true);
    }
}

/** Our custom ExitManager does not allow the VM to exit, but does 
 * allow itself to be replaced by the original security manager. */
class ExitManager extends SecurityManager {

    SecurityManager original;

    ExitManager(SecurityManager original) {
        this.original = original;
    }

    /** Deny permission to exit the VM. */
    public void checkExit(int status) {
        throw( new SecurityException() );
    }

    /** Allow this security manager to be replaced,
  if fact, allow pretty much everything. */
    public void checkPermission(Permission perm) {
    }

    public SecurityManager getOriginalSecurityManager() {
        return original;
    }
}

/** This example frame attempts to System.exit(0) on closing, we must 
 * prevent it from doing so. */
class TargetFrame extends JFrame {

    TargetFrame() {
        super("Close Me!");
        add(new JLabel("Hi!"));

        addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.out.println("Bye!");
                System.exit(0);
            }
        });

        pack();
        setSize( getPreferredSize() );
        setLocationByPlatform(true);
        setVisible(true);
    }
}

Upvotes: 2

aglassman
aglassman

Reputation: 2653

The Eclipse RPC may be a good option to look at. It provides editor views which can easily be changed to enable / disable save, and other functionality at run time. Since Eclipse is written in Java, most Java code you already have will play nice with the framework.

Upvotes: 0

Related Questions