use of com.github.sardine.impl.SardineException in project openhab1-addons by openhab.
the class EventReloaderJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
final String config = context.getJobDetail().getJobDataMap().getString(KEY_CONFIG);
CalendarRuntime eventRuntime = EventStorage.getInstance().getEventCache().get(config);
log.debug("running EventReloaderJob for config : {}", config);
// reload cached events (if necessary)
if (!cachedEventsLoaded.containsKey(config)) {
try {
log.debug("reload cached events for config: {}", eventRuntime.getConfig().getKey());
for (File fileCalendarKeys : new File(CalDavLoaderImpl.CACHE_PATH).listFiles()) {
if (!eventRuntime.getConfig().getKey().equals(Util.getFilename(fileCalendarKeys.getName()))) {
log.trace("not our config : {}", Util.getFilename(fileCalendarKeys.getName()));
continue;
}
log.trace("found our config : {}", Util.getFilename(fileCalendarKeys.getName()));
final Collection<File> icsFiles = FileUtils.listFiles(fileCalendarKeys, new String[] { "ics" }, false);
for (File icsFile : icsFiles) {
try {
FileInputStream fis = new FileInputStream(icsFile);
log.debug("loading events from file : {}", icsFile);
loadEvents(Util.getFilename(icsFile.getAbsolutePath()), new org.joda.time.DateTime(icsFile.lastModified()), fis, eventRuntime.getConfig(), new ArrayList<String>(), true);
} catch (IOException e) {
log.error("cannot load events for file: " + icsFile, e);
} catch (ParserException e) {
log.error("cannot load events for file: " + icsFile, e);
}
}
break;
}
} catch (Throwable e) {
log.error("cannot load events", e);
} finally {
cachedEventsLoaded.put(config, true);
}
}
try {
log.debug("loading events for config: " + config);
List<String> oldEventIds = new ArrayList<String>();
for (EventContainer eventContainer : eventRuntime.getEventMap().values()) {
oldEventIds.add(eventContainer.getFilename());
log.debug("old eventcontainer -- id : {} -- filename : {} -- calcuntil : {} -- lastchanged : {} -- ishistoric : {}", eventContainer.getEventId(), eventContainer.getFilename(), eventContainer.getCalculatedUntil(), eventContainer.getLastChanged(), eventContainer.isHistoricEvent());
if (log.isDebugEnabled()) {
for (int i = 0; i < eventContainer.getEventList().size(); i++) {
CalDavEvent elem = eventContainer.getEventList().get(i);
log.debug("old eventlist contient l'evenement : {} -- deb : {} -- fin : {} -- lastchang {}", elem.getName(), elem.getStart(), elem.getEnd(), elem.getLastChanged());
}
}
}
loadEvents(eventRuntime, oldEventIds);
// stop all events in oldMap
removeDeletedEvents(config, oldEventIds);
for (EventNotifier notifier : CalDavLoaderImpl.instance.getEventListenerList()) {
try {
notifier.calendarReloaded(config);
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
// print All scheduled jobs :
if (log.isDebugEnabled()) {
log.debug("jobs scheduled : ");
Scheduler scheduler = CalDavLoaderImpl.instance.getScheduler();
for (String groupName : CalDavLoaderImpl.instance.getScheduler().getJobGroupNames()) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
String jobName = jobKey.getName();
String jobGroup = jobKey.getGroup();
// get job's trigger
List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
Date nextFireTime = triggers.get(0).getNextFireTime();
log.debug("[job] : {} - [groupName] : {} - {}", jobName, jobGroup, nextFireTime);
}
}
}
} catch (SardineException e) {
log.error("error while loading calendar entries: {} ({} - {} )", e.getMessage(), e.getStatusCode(), e.getResponsePhrase(), e);
throw new JobExecutionException("error while loading calendar entries", e, false);
} catch (Exception e) {
log.error("error while loading calendar entries: {}", e.getMessage(), e);
throw new JobExecutionException("error while loading calendar entries", e, false);
}
}
use of com.github.sardine.impl.SardineException in project openhab1-addons by openhab.
the class EventReloaderJob method loadEvents.
/**
* all events which are available must be removed from the oldEventIds list
*
* @param calendarRuntime
* @param oldEventIds
* @throws IOException
* @throws ParserException
*/
public synchronized void loadEvents(final CalendarRuntime calendarRuntime, final List<String> oldEventIds) throws IOException, ParserException {
CalDavConfig config = calendarRuntime.getConfig();
Sardine sardine = Util.getConnection(config);
List<DavResource> list = sardine.list(config.getUrl(), 1, false);
log.trace("before load events : oldeventsid contains : {}", oldEventIds.toString());
for (DavResource resource : list) {
final String filename = Util.getFilename(resource.getName());
try {
if (resource.isDirectory()) {
continue;
}
// an ics file can contain multiple events
// ==> multiple eventcontainers could have the same filename (and different eventid),
// ==>we must not have one of them remaining in oldEventIds var (bad chosen name, cause it's a list of
// oldEventContainers's filename, so with doubles possible)
// or the remaining jobs with this filename will get unscheduled on the "removeDeletedEvents(config,
// oldEventIds)" call (line 136)
oldEventIds.removeAll(Arrays.asList(filename));
// must not be loaded
EventContainer eventContainer = calendarRuntime.getEventContainerByFilename(filename);
final org.joda.time.DateTime lastResourceChangeFS = new org.joda.time.DateTime(resource.getModified());
log.trace("eventContainer found: {}", eventContainer != null);
log.trace("last resource modification: {}", lastResourceChangeFS);
log.trace("last change of already loaded event: {}", eventContainer != null ? eventContainer.getLastChanged() : null);
if (config.isLastModifiedFileTimeStampValid()) {
if (eventContainer != null && !lastResourceChangeFS.isAfter(eventContainer.getLastChanged())) {
// to be created
if (eventContainer.getCalculatedUntil() != null && eventContainer.getCalculatedUntil().isAfter(org.joda.time.DateTime.now().plusMinutes(config.getReloadMinutes()))) {
// the event is calculated as long as the next reload
// interval can handle this
log.trace("skipping resource {}, not changed (calculated until: {})", resource.getName(), eventContainer.getCalculatedUntil());
continue;
}
if (eventContainer.isHistoricEvent()) {
// no more upcoming events, do nothing
log.trace("skipping resource {}, not changed (historic)", resource.getName());
continue;
}
File icsFile = Util.getCacheFile(config.getKey(), filename);
if (icsFile != null && icsFile.exists()) {
FileInputStream fis = new FileInputStream(icsFile);
this.loadEvents(filename, lastResourceChangeFS, fis, config, oldEventIds, false);
fis.close();
continue;
}
}
}
log.debug("loading resource: {} (FSchangedTS not valid)", resource);
// prepare resource url
URL url = new URL(config.getUrl());
String resourcePath = resource.getPath();
String escapedResource = resource.getName().replaceAll("/", "%2F");
resourcePath = resourcePath.replace(resource.getName(), escapedResource);
url = new URL(url.getProtocol(), url.getHost(), url.getPort(), resourcePath);
InputStream inputStream = sardine.get(url.toString().replaceAll(" ", "%20"));
this.loadEvents(filename, lastResourceChangeFS, inputStream, config, oldEventIds, false);
} catch (ParserException e) {
log.error("error parsing ics file: " + filename, e);
} catch (SardineException e) {
log.error("error reading ics file: " + filename, e);
}
}
log.trace("after load events : oldeventsid contains : {}", oldEventIds.toString());
}
Aggregations