Saturday, 24 March 2012

JNotify - Java library that allows java application to listen to file system events

JNotify works on both Windows (Windows 2000, XP, Vista) and Linux with INotify support (Kernel 2.6.14 and above).

JNotify is a java library that allow java application to listen to file system events, such as:
file created.
file modified.
file renamed.
file deleted.
This property can be used in a number of scenarios.I have used it to reload resource bundles
whenever there is a modiofication in it.
Reference:
http://jnotify.sourceforge.net/ 


1)When this program is not working properly,
2)you should check some files are there or not in your computer
3)jnotify.dll and jnotify_64bit.dll are mandatory for working these program
4)you should copy and paste these two dll files in your system32 folder or jdk lib folder
 


1:  package org.life.java.lkr.questions;  
2:  import java.io.File;  
3:  import net.contentobjects.jnotify.JNotify;  
4:  import net.contentobjects.jnotify.JNotifyListener;  
5:  import org.apache.log4j.Logger;  
6:  /**  
7:   *   
8:   * @author Konda Reddy. L
9:   */  
10:  public class JNotifyDemo {  
11:       private static Logger logger = Logger.getLogger(JNotifyDemo.class);  
12:       public void sample() throws Exception {  
13:            // path to watch  
14:            // String path = System.getProperty("user.home");  
15:            String path = System.getProperty("user.dir");  
16:            //System.out.println(path);  
17:            logger.info(path);  
18:            // watch mask, specify events you care about,  
19:            // or JNotify.FILE_ANY for all events.  
20:            int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED  
21:                      | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;  
22:            // watch subtree?  
23:            boolean watchSubtree = true;  
24:            // add actual watch  
25:            int watchID = JNotify  
26:                      .addWatch(path, mask, watchSubtree, new Listener());  
27:            // sleep a little, the application will exit if you  
28:            // don't (watching is asynchronous), depending on your  
29:            // application, this may not be required  
30:            Thread.sleep(1000000);  
31:            // to remove watch the watch  
32:            boolean res = JNotify.removeWatch(watchID);  
33:            if (!res) {  
34:                 // invalid watch ID specified.  
35:            }  
36:       }  
37:       class Listener implements JNotifyListener {  
38:            public void fileRenamed(int wd, String rootPath, String oldName,  
39:                      String newName) {  
40:                 System.out.println("fileRenamed");  
41:                 //print("renamed " + rootPath + " : " + oldName + " -> " + newName);  
42:                 logger.info("renamed"+rootPath+":"+oldName+"->"+newName);  
43:            }  
44:            public void fileModified(int wd, String rootPath, String name) {  
45:                 //System.out.println("modified");  
46:                 logger.info("modified");  
47:                 logger.info("name");
                      File csvfile = new File(name);
   logger.info("csvfile::" + csvfile);
   try {
    FileProccessor.loadDatabaseData(csvfile);
   } catch (Exception e) {
    e.printStackTrace();
   }  
48:                 //System.out.println(name);  
49:  //print("modified " + rootPath + " : " + name);  
50:                 logger.info("modified"+rootPath+":"+name);  
51:            }  
52:            public void fileDeleted(int wd, String rootPath, String name) {  
53:                 //print("deleted " + rootPath + " : " + name);  
54:                 logger.info("fileDeleted"+rootPath+":"+name);  
55:            }  
56:            public void fileCreated(int wd, String rootPath, String name) {  
57:                 //System.out.println("fileCreated");  
58:                 //print("created " + rootPath + " : " + name);  
59:                 logger.info("fileCreated"+rootPath+":"+name);  
60:                 logger.info("fileCreated");  
61:            }  
62:            void print(String msg) {  
63:                 System.err.println(msg);  
64:            }  
65:       }  
66:       public static void main(String[] args) throws Exception {  
67:            new JNotifyDemo().sample();  
68:       }  
69:  }  
1:  package org.life.java.lkr.questions;   
2:  import java.io.BufferedReader;  
3:  import java.io.File;  
4:  import java.io.FileReader;  
5:  import java.io.FileWriter;  
6:  import java.io.FilenameFilter;  
7:  import org.apache.log4j.Logger;  
8:  import org.hibernate.Session;  
9:  import org.hibernate.Transaction;  
10:  import org.hibernate.cfg.Configuration;  
11:  public class FileProccessor {  
12:       private static Logger logger = Logger.getLogger(FileProccessor.class);  
13:       private static Session session = new Configuration().configure()  
14:                 .buildSessionFactory().openSession();  
15:       private static Integer age;  
16:       private static String name;  
17:       /**  
18:        *   
19:        * This method assumes that Hibernate is set up already and can be loaded  
20:        *   
21:        * with data contained in the CSV file.  
22:        *   
23:        *   
24:        *   
25:        * @param csvFile  
26:        *   
27:        * @throws Exception  
28:        */  
29:       public static void loadDatabaseData(File csvFile) throws Exception {  
30:            String filename = csvFile.getName();  
31:            logger.info("Reading contents of :" + filename);  
32:            BufferedReader in = new BufferedReader(new FileReader(csvFile));  
33:            logger.info("csv" + csvFile);  
34:            String values = in.readLine();  
35:            if (values != null) {  
36:                 do {  
37:                      logger.info("before replace all" + values);  
38:                      String str = values;  
39:                      String[] temp;  
40:                      /* delimiter */  
41:                      String delimiter = ",";  
42:                      /*  
43:                       * given string will be split by the argument delimiter  
44:                       * provided.  
45:                       */  
46:                      temp = str.split(delimiter);  
47:                      /* print substrings */  
48:                      for (int i = 0; i < temp.length; i++) {  
49:                           logger.info("temp Values:::" + temp[i]);  
50:                           age = Integer.parseInt(temp[i]);  
51:                           name = temp[++i];  
52:                           logger.info("name is:" + name);  
53:                      }  
54:                      // inserting data into database  
55:                      insertIntoDatabase(age, name);  
56:                      values = in.readLine();  
57:                 } while (values != null);  
58:                 try {  
59:                      FileWriter fwriter = new FileWriter(csvFile);  
60:                      fwriter.close();  
61:                 } catch (Exception e) {  
62:                      e.printStackTrace();  
63:                 }  
64:            }  
65:       }  
66:       /**  
67:        * Calls hibernate and inserts the datainto the database  
68:        *   
69:        *   
70:        * @param age2  
71:        * @param name2  
72:        */  
73:       private static void insertIntoDatabase(Integer age2, String name2) {  
74:            Transaction transaction = session.beginTransaction();  
75:            Gdata emp = new Gdata();  
76:            emp.setAge(age2);  
77:            emp.setName(name2);  
78:            session.save(emp);  
79:            transaction.commit();  
80:            logger.info("inserting done");  
81:            logger.info("SQL SUCCEEDED");  
82:       }  
83:       /**  
84:        *   
85:        * Private class to allow only CSV files to be read. Could have been in a  
86:        *   
87:        * different class but thought it would be more portable this way.  
88:        *   
89:        *   
90:        *   
91:        * @author Konda Reddy  
92:        *   
93:        *   
94:        */  
95:       private static class CsvFilter implements FilenameFilter {  
96:            public CsvFilter() {  
97:            }  
98:            public boolean accept(File dir, String name) {  
99:                 if (name.toLowerCase().endsWith(".csv")) {  
100:                      return true;  
101:                 } else {  
102:                      return false;  
103:                 }  
104:            }  
105:       }  
106:  }  
1:  package org.life.java.lkr.questions;  
2:  public class Gdata {  
3:       private Integer id;  
4:       private Integer age;  
5:       private String name;  
6:       /**  
7:        * @return the age  
8:        */  
9:       public Integer getAge() {  
10:            return age;  
11:       }  
12:       /**  
13:        * @param age the age to set  
14:        */  
15:       public void setAge(Integer age) {  
16:            this.age = age;  
17:       }  
18:       /**  
19:        * @return the name  
20:        */  
21:       public String getName() {  
22:            return name;  
23:       }  
24:       /**  
25:        * @param name the name to set  
26:        */  
27:       public void setName(String name) {  
28:            this.name = name;  
29:       }  
30:       /**  
31:        * @return the id  
32:        */  
33:       public Integer getId() {  
34:            return id;  
35:       }  
36:       /**  
37:        * @param id the id to set  
38:        */  
39:       public void setId(Integer id) {  
40:            this.id = id;  
41:       }  
42:  }  
1:  <?xml version="1.0"?>  
2:  <!DOCTYPE hibernate-mapping PUBLIC   
3:       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
4:       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
5:  <hibernate-mapping>  
6:  <class name="org.life.java.lkr.questions.Gdata" table="test">  
7:       <id name="id" column="idtest">  
8:       <generator class="increment"/>  
9:       </id>  
10:       <property name="age" column="age"/>  
11:       <property name="name" column="name"/>  
12:  </class>  
13:  </hibernate-mapping>  
1:  <!DOCTYPE hibernate-configuration PUBLIC  
2:       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
3:       "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
4:  <hibernate-configuration>  
5:  <session-factory>  
6:  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
7:  <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>  
8:  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>  
9:  <!-- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property> -->  
10:  <property name="hibernate.connection.username">root</property>  
11:  <property name="hibernate.connection.password">root</property>  
12:  <mapping resource="Gadget.hbm.xml"/>  
13:  </session-factory>  
14:  </hibernate-configuration>  

1 comment:

  1. Casino Bonus Code 2021 - JWTHub
    › cbet/bonus › cbet/bonus Get $10 free to use at 경주 출장마사지 Casino 화성 출장마사지 Bonus Code: TNFBETS. 군산 출장안마 Claim 시흥 출장샵 the casino bonus code TNFBETS and take advantage 과천 출장샵 of up to $50 in casino credits at the

    ReplyDelete