2016-03-03 25 views
3

我有一個函數(MonitorLog),它包含一個無限循環;這是必要的,因爲我想監視文件並在修改時執行操作。然而,然後部分在Drools不想開始(「WS更新」不出現)。請幫忙嗎?如何在包含無限循環的函數(監視器)時觸發Drools中的規則?

package smartWssec ; 
dialect "mvel" 
rule "SmartWssec" 
when 
$log :PolicyUpdateWssec(flag == true ,changelog == true) 
then 
$log.UpdateXml("D:\\Mod\\policy.xml")// consequence 
System.out.println("Ws-Policy Updated") 
end 

/////////////////////// ///////////

package smartWssec; 


import java.io.IOException; 

import org.kie.api.KieServices; 
import org.kie.api.runtime.KieContainer; 
import org.kie.api.runtime.KieSession; 



    public class MainPolicyUpdateWssec { 


    public static void main(String[] args) throws IOException, InterruptedException { 
       //create a Session 
    KieServices ks = KieServices.Factory.get(); 
    KieContainer kContainer = ks.getKieClasspathContainer(); 
    KieSession kSession =  kContainer.newKieSession("ksession-rules"); 
      try { 

       //create fact 
       PolicyUpdateWssec LogModSecurity = new PolicyUpdateWssec(); 
       LogModSecurity.MonitorLog("D:\\Mod"); 
       LogModSecurity.ReadLog("D:\\Mod\\Modsecurity.txt"); 
       //insert fact and fire Rules 
       kSession.insert(LogModSecurity); 
       kSession.fireAllRules(); 

      } 
      finally { 
      kSession.dispose(); 
      } 
      } 

//////////////////////////////// /////////////////////////

public class PolicyUpdateWssec { 
private boolean flag; 
private boolean changelog; 



public boolean getchangelog(){ 
    return changelog; 
} 
public void setChangelog(boolean changelog){ 
    this.changelog=changelog; 
} 

public boolean getflag(){ 
    return flag; 
    } 

public void setflag(boolean flag){ 
    this.flag=flag; 
} 


public void Update(String filename){ 

    try (PrintStream ps = new PrintStream("D:\\Mod\\Wspolicyupdate.txt"); 
      Stream<String> stream = Files.lines(Paths.get(filename))) { 
      stream.map(line -> line.replaceAll("</wsp:Policy>","<wssp:Integrity>\r\n\r\n<wssp:SignatureAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>\r\n\r\n<wssp:CanonicalizationAlgorithm URI=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>\r\n\r\n<!-- Require the Timestamp header to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://www.bea.com/wls90/security/policy/wsee#part\">\r\n\r\nwls:SecurityHeader(wsu:Timestamp)\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n<!-- Require the message body to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://schemas.xmlsoap.org/2002/12/wsse#part\">\r\n\r\nwsp:Body()\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n</wssp:Integrity>\r\n\r\n</wsp:Policy>")).forEach(ps::println); 
      System.out.println("Ws-Policy updated"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }} 

public void ReadLog(String filename) throws IOException{ 

String line =null; 
boolean flag=false; 

// FileReader reads text files in the default encoding 

FileReader fileReader = new FileReader(filename); 

// Always wrap FileReader in BufferedReader 

BufferedReader bufferedReader = new BufferedReader(fileReader); 

    while((line = bufferedReader.readLine()) != null) { 


    flag=line.matches("(.*)sql_injection_attacks(.*)"); 
    if(flag==true) setflag(flag); 

    } 

    // Always close files 
    bufferedReader.close();   
} 

public void MonitorLog(String path) throws InterruptedException{ 

boolean changelog=false; 

    try { 
     WatchService watcher = FileSystems.getDefault().newWatchService(); 
     Path dir = Paths.get(path); 
     dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); 

     System.out.println("Watch Service registered for : " + dir.getFileName()); 

      WatchKey key = watcher.poll(10, TimeUnit.SECONDS); 

      try { 

      // wait for a key to be available 

       key = watcher.take(); 
      } catch (InterruptedException ex) { 
       return; 
      } 

      for (WatchEvent<?> event : key.pollEvents()) { 
       // get event type 
       WatchEvent.Kind<?> kind = event.kind(); 

       // get file name 
       @SuppressWarnings("unchecked") 
       WatchEvent<Path> ev = (WatchEvent<Path>) event; 
       Path fileName = ev.context(); 

       System.out.println(kind.name() + ": " + fileName); 

       if (kind == ENTRY_MODIFY) { 
        changelog=true; 
        setChangelog(changelog); 
        System.out.println("WS-Policy is being Updated ......."); 


       } 

      } 



     watcher.close();    
    } 
    catch (IOException ex) { 
     System.err.println(ex); 
    } 


    } 




public void UpdateXml(String filename) { 

ArrayList<String> lines = new ArrayList<String>(); 
String line = null; 

File f1=null; 
FileReader fr=null; 

BufferedReader br=null; 
FileWriter fw=null; 
BufferedWriter out=null; 
try { 
    f1 = new File(filename); 
    fr = new FileReader(f1); 
    br = new BufferedReader(fr); 
    while ((line = br.readLine()) != null) { 
     if (line.contains("</wsp:Policy>")) 
     line = line.replace("</wsp:Policy>", "<wssp:Integrity>\r\n\r\n<wssp:SignatureAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>\r\n\r\n<wssp:CanonicalizationAlgorithm URI=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>\r\n\r\n<!-- Require the Timestamp header to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://www.bea.com/wls90/security/policy/wsee#part\">\r\n\r\nwls:SecurityHeader(wsu:Timestamp)\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n<!-- Require the message body to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://schemas.xmlsoap.org/2002/12/wsse#part\">\r\n\r\nwsp:Body()\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n</wssp:Integrity>\r\n\r\n</wsp:Policy>"); 
     lines.add(line); 
    } 

    fw = new FileWriter(f1); 
    out = new BufferedWriter(fw); 
    for (String s : lines) 
    out.write(s); 
    out.flush(); 

} catch (Exception ex) { 
    ex.printStackTrace(); 
} finally { 
    try{ 
    fr.close(); 
    br.close(); 
    out.close(); 
    }catch(IOException ioe) 

    { 
     ioe.printStackTrace(); 
    } 

} 
}} 
+0

您提供的代碼不完整。 –

+0

我現在提供所有的代碼,你能幫助我嗎?謝謝 – Ahmed

回答

1

將類成員更改爲觸發規則是不夠的。您必須通知Drools引擎才能實現此目的。修改您的代碼,以便您有插入的事實,這個事實手柄:

FactHandle fh = kSession.insert(LogModSecurity); 

,然後在修改之後調用update:

setChangelog(changelog); // assuming this is code in PolicyUpdateWssec 
kSession.update(fh, this); 

我不能告訴你,你這是如何因整合,缺乏大部分代碼。

編輯MonitorLog中的代碼似乎稍微等待某個目錄中的某個文件的修改,然後嘗試對該修改的文件執行某些操作。 (WatchService方法輪詢並同時從這組鍵中刪除某些東西 - 您在那裏丟失了一個鍵。)如果什麼都沒有發生,輪詢將返回空值,因此預計NPE。然後,如果它是MODIFY,則更改日誌設置爲true。然後調用fireAllRules。但規則不會觸發,因爲flag從未設置爲true。

就我所知,沒有必要制定規則。您希望對文件進行更改:等待它,然後執行任何需要完成的操作。該規則由於該更改而觸發(並且可能某個文件必須包含一些特定的文本)。這裏沒有什麼值得使用生產規則系統的。

+0

我把我所有的代碼都放在了一起,你現在可以幫我整合你說的嗎?非常感謝你 – Ahmed

+0

感謝您的回覆laune – Ahmed