001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.jci.examples.configuration; 019 020 import java.io.File; 021 import java.io.FileInputStream; 022 import java.io.IOException; 023 import java.io.InputStream; 024 import java.util.ArrayList; 025 import java.util.Collection; 026 import java.util.Properties; 027 028 import org.apache.commons.jci.listeners.FileChangeListener; 029 import org.apache.commons.jci.monitor.FilesystemAlterationListener; 030 import org.apache.commons.jci.monitor.FilesystemAlterationMonitor; 031 import org.apache.commons.jci.monitor.FilesystemAlterationObserver; 032 033 /** 034 * 035 * @author tcurdt 036 */ 037 public final class ConfigurationReloading { 038 039 private final FilesystemAlterationMonitor fam = new FilesystemAlterationMonitor(); 040 041 private void run(String[] args) { 042 043 final File configFile = new File("some.properties"); 044 045 System.out.println("Watching " + configFile.getAbsolutePath()); 046 047 final Collection<Configurable> configurables = new ArrayList<Configurable>(); 048 049 final FilesystemAlterationListener listener = new FileChangeListener() { 050 public void onStop(FilesystemAlterationObserver pObserver) { 051 super.onStop(pObserver); 052 053 if (hasChanged()) { 054 System.out.println("Configuration change detected " + configFile); 055 056 final Properties props = new Properties(); 057 InputStream is = null; 058 try { 059 is = new FileInputStream(configFile); 060 props.load(is); 061 062 System.out.println("Notifying about configuration change " + configFile); 063 064 for (Configurable configurable : configurables) { 065 configurable.configure(props); 066 } 067 068 } catch (Exception e) { 069 System.err.println("Failed to load configuration " + configFile); 070 } finally { 071 try { 072 is.close(); 073 } catch (IOException e) { 074 } 075 } 076 077 } 078 } 079 }; 080 081 fam.addListener(configFile, listener); 082 fam.start(); 083 084 configurables.add(new Something()); 085 086 while(true) { 087 try { 088 Thread.sleep(1000); 089 } catch (InterruptedException e) { 090 } 091 } 092 } 093 094 public static void main(String[] args) { 095 new ConfigurationReloading().run(args); 096 } 097 }