我有一個函數(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();
}
}
}}
您提供的代碼不完整。 –
我現在提供所有的代碼,你能幫助我嗎?謝謝 – Ahmed