Using Wrapper Using Java

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

The goal is to apply usage policies for documents that are independent of the application used to work with a specific document.

eg. I have an encrypted file that needs to be decrypted and opened in some editor. Thus, the shell application decrypts the file and launches the editor within itself to ensure that the read-only policy is enforced, for example, by denying write access to the application. Therefore, the Runtime.getRuntime().exec(<command>) method does not fit well :)

There are also some ways to intercept method calls in one application, but none of them will wrap the entire other application.

I also read about changing the JVM itself to intercept file access. That sounds good. But I need to dynamically change the policy depending on the user. Perhaps this will not work as far as I know.

I suppose there can be no way to do this using Java code, but I would appreciate any hints and help.

+4
source share
2 answers

I also read about changing the JVM itself to intercept file access. That sounds good. But I need to dynamically change the policy depending on the user.

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

Here is a simple example that prevents children from exiting the virtual machine ( 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); } } 
+2
source

Eclipse RPC may be a good option to view. It provides editor views that you can easily modify to enable / disable saving and other functions at runtime. Since Eclipse is written in Java, most of the Java code that you already have will work well with the framework.

0
source

Source: https://habr.com/ru/post/1416582/


All Articles