View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci.examples.configuration;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Iterator;
27  import java.util.Properties;
28  
29  import org.apache.commons.jci.listeners.FileChangeListener;
30  import org.apache.commons.jci.monitor.FilesystemAlterationListener;
31  import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
32  import org.apache.commons.jci.monitor.FilesystemAlterationObserver;
33  
34  /**
35   * 
36   * @author tcurdt
37   */
38  public final class ConfigurationReloading {
39  
40      private final FilesystemAlterationMonitor fam = new FilesystemAlterationMonitor();
41  
42      private void run(String[] args) {
43  
44          final File configFile = new File("some.properties");
45  
46          System.out.println("Watching " + configFile.getAbsolutePath());
47  
48          final Collection configurables = new ArrayList();
49  
50          final FilesystemAlterationListener listener = new FileChangeListener() {
51              public void onStop(FilesystemAlterationObserver pObserver) {
52                  super.onStop(pObserver);
53  
54                  if (hasChanged()) {
55                      System.out.println("Configuration change detected " + configFile);
56  
57                      final Properties props = new Properties();
58                      InputStream is = null; 
59                      try {
60                      	is = new FileInputStream(configFile);
61                          props.load(is);
62  
63                          System.out.println("Notifying about configuration change " + configFile);
64  
65                          for (Iterator it = configurables.iterator(); it.hasNext();) {
66                              final Configurable configurable = (Configurable) it.next();
67                              configurable.configure(props);
68                          }
69  
70                      } catch (Exception e) {
71                          System.err.println("Failed to load configuration " + configFile);
72                      } finally {
73                      	try {
74  							is.close();
75  						} catch (IOException e) {
76  						}
77                      }
78  
79                  }
80              }
81          };
82  
83          fam.addListener(configFile, listener);
84  		fam.start();
85  
86  		configurables.add(new Something());
87  
88          while(true) {
89              try {
90                  Thread.sleep(1000);
91              } catch (InterruptedException e) {
92              }
93          }
94      }
95  
96      public static void main(String[] args) {
97          new ConfigurationReloading().run(args);
98      }
99  }