2012-06-07 63 views
4

是否可以使用Java爲其他(Java)應用程序實現包裝器應用程序?使用Java的應用程序封裝器

其目的是爲獨立於用於處理特定文檔的應用程序的文檔實施使用策略。

E.G.我有一個需要解密的加密文件,並在某種編輯器中打開。因此,例如,包裝應用程序將解密文件並啓動其自身的編輯器,以通過拒絕對應用程序的寫入訪問來強制實施只讀策略。因此,Runtime.getRuntime().exec(<command>)方法不適合:)

也有一些方法可以在同一個應用程序中攔截方法調用,但不會包裝整個其他應用程序。

我也讀過關於改變JVM本身來攔截文件訪問。這聽起來很不錯。但我需要根據用戶動態更改策略。據我所知,這可能不會奏效。

我想可能沒有辦法使用Java代碼來做到這一點,但我會很感激任何提示和幫助。

+0

爲什麼應用程序需要是'包裝器'應用程序?我認爲你可以使用eclipse RPC來實現這種類型的需求。您只需禁用Eclipse RPC編輯器提供的任何特定保存功能即可禁用保存代碼的功能。 – aglassman

+0

我正在考慮一個包裝應用程序,因爲我想到了一個更通用的方法來解決這個問題。我想使用Eclipse RPC將需要編寫我自己的編輯器。或者我弄錯了?這不是我正在尋找的第一位。對不起,如果我以前沒有清楚。將編輯器的一個例子轉換爲.txt文件給用戶,但將控制權留給我的應用程序的文件訪問權限會很好。 – bandt

+0

Eclipse RPC具有內置編輯器,並且也內置在文件瀏覽器中。我認爲它會很好地適合你的項目。 – aglassman

回答

0

Eclipse RPC可能是一個很好的選擇。它提供了編輯器視圖,可以在運行時輕鬆更改以啓用/禁用保存以及其他功能。由於Eclipse是用Java編寫的,所以你已經擁有的大多數Java代碼都會在框架中表現出色。

2

我也讀過關於改變JVM本身攔截文件訪問。這聽起來很不錯。但我需要根據用戶動態更改策略。

設置自定義SecurityManager覆蓋checkWrite(String)以引發異常。

這是一個簡單的例子,可以防止子框架退出虛擬機(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); 
    } 
} 
相關問題