【问题标题】:Application Wrapper using Java使用 Java 的应用程序包装器
【发布时间】:2012-06-11 13:39:18
【问题描述】:

是否可以使用 Java 为其他 (Java) 应用程序实现包装应用程序?

其目的是强制执行独立于用于处理特定文档的应用程序的文档的使用策略。

例如我有一个加密文件,需要在某种编辑器中解密和打开。因此,包装应用程序将解密文件并在其内部启动编辑器,以通过拒绝对应用程序的写访问来强制执行只读策略。因此Runtime.getRuntime().exec(<command>) 方法不太适合:)

还有一些方法可以拦截同一应用程序中的方法调用,但没有一种方法可以封装整个其他应用程序。

我还阅读了有关更改 JVM 本身以拦截文件访问的信息。这听起来很不错。但我需要根据用户动态更改策略。据我目前所知,这可能行不通。

我想可能没有任何方法可以使用 Java 代码来做到这一点,但我希望能得到任何提示和帮助。

【问题讨论】:

  • 为什么应用程序需要是“包装器”应用程序?我认为您可以使用 eclipse RPC 来实现这种类型的需求。您只需要通过禁用 Eclipse RPC 编辑器提供的任何特定保存功能来禁用保存代码的功能。
  • 我在考虑一个包装应用程序,因为我想到了一种更通用的方法来解决这个问题。我想使用 Eclipse RPC 需要编写我自己的编辑器。还是我弄错了?这并不是我首先要寻找的。抱歉,如果我之前没有弄清楚。最好将编辑器的乐趣留给用户,例如 .txt 文件,但将控制权留给对我的应用程序的文件访问。
  • Eclipse RPC 有一个内置的编辑器,也有内置的文件浏览器。我认为它很适合您的项目。
  • 我确实理解您将编辑器的选择权留给用户的观点,但可能比 IMO 更麻烦。
  • 麻烦了。 :) 但这就是重点。 ;) 我正在开发 DLP 系统的原型,并且希望任何类型的文档都得到权限管理的支持。顺便说一下,谢谢你的 cmets。

标签: java security policy file-access securitymanager


【解决方案1】:

我还阅读了有关更改 JVM 本身以拦截文件访问的信息。这听起来很不错。但我需要根据用户动态更改策略。

设置自定义SecurityManager 覆盖checkWrite(String) 以引发异常。

这是一个防止子框架退出 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);
    }
}

【讨论】:

    【解决方案2】:

    Eclipse RPC 可能是一个不错的选择。它提供了编辑器视图,可以在运行时轻松更改以启用/禁用保存和其他功能。由于 Eclipse 是用 Java 编写的,因此您已经拥有的大多数 Java 代码都可以很好地与该框架配合使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 2011-01-02
      • 2011-01-29
      • 2010-09-21
      相关资源
      最近更新 更多