001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.datadictionary;
017
018 import org.apache.commons.logging.Log;
019 import org.apache.commons.logging.LogFactory;
020
021 import java.io.IOException;
022 import java.net.URL;
023 import java.util.LinkedList;
024 import java.util.Map;
025 import java.util.concurrent.ConcurrentHashMap;
026 import java.util.concurrent.Executors;
027 import java.util.concurrent.ScheduledExecutorService;
028 import java.util.concurrent.TimeUnit;
029 import java.util.zip.CRC32;
030 import java.util.zip.CheckedInputStream;
031
032 /**
033 * This is a description of what this class does - gilesp don't forget to fill this in.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038 public class URLMonitor {
039 private static final Log LOG = LogFactory.getLog(URLMonitor.class);
040
041 private final LinkedList<URLContentChangedListener> listeners = new LinkedList<URLContentChangedListener>();
042 private final Map<URL, Long> resourceMap = new ConcurrentHashMap();
043 private final int reloadIntervalMilliseconds;
044 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
045
046
047 public URLMonitor(int reloadIntervalMilliseconds) {
048 this.reloadIntervalMilliseconds = reloadIntervalMilliseconds;
049 }
050
051 public synchronized void addListener(URLContentChangedListener listener) {
052
053 listeners.add(listener);
054
055 if (listeners.size() == 1) {
056 scheduler.scheduleAtFixedRate(urlPoller,
057 reloadIntervalMilliseconds, reloadIntervalMilliseconds, TimeUnit.MILLISECONDS);
058 }
059 }
060
061 public void addURI(URL zipUrl) {
062
063 resourceMap.put(zipUrl, getCRC(zipUrl));
064 }
065
066 private Long getCRC(URL zipUrl) {
067 Long result = -1l;
068 try {
069 CRC32 crc = new CRC32();
070 CheckedInputStream cis = new CheckedInputStream(zipUrl.openStream(), crc);
071
072 byte[] buffer = new byte[1024];
073 int length;
074
075 //read the entry from zip file and extract it to disk
076 while( (length = cis.read(buffer)) > 0);
077
078 cis.close();
079
080 result = crc.getValue();
081 } catch (IOException e) {
082 LOG.warn("Unable to calculate CRC, resource doesn't exist?", e);
083 }
084 return result;
085 }
086
087 private final Runnable urlPoller = new Runnable() {
088
089 @Override
090 public void run() {
091 for (Map.Entry<URL, Long> entry : resourceMap.entrySet()) {
092 Long crc = getCRC(entry.getKey());
093
094 if (!entry.getValue().equals(crc)) {
095 entry.setValue(crc);
096 for (URLContentChangedListener listener : listeners) {
097 listener.urlContentChanged(entry.getKey());
098 }
099 }
100 }
101 }
102 };
103
104 public static interface URLContentChangedListener {
105 public void urlContentChanged(URL url);
106 }
107 }