michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.model; michael@0: michael@0: import java.io.IOException; michael@0: import java.net.URL; michael@0: import java.util.Map; michael@0: import java.util.Properties; michael@0: import java.util.regex.Matcher; michael@0: import java.util.regex.Pattern; michael@0: michael@0: import net.fortuna.ical4j.data.CalendarBuilder; michael@0: import net.fortuna.ical4j.data.ParserException; michael@0: import net.fortuna.ical4j.model.component.VTimeZone; michael@0: import net.fortuna.ical4j.model.property.TzUrl; michael@0: import net.fortuna.ical4j.util.CompatibilityHints; michael@0: import net.fortuna.ical4j.util.Configurator; michael@0: import net.fortuna.ical4j.util.ResourceLoader; michael@0: michael@0: import org.apache.commons.logging.Log; michael@0: import org.apache.commons.logging.LogFactory; michael@0: michael@0: import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; michael@0: michael@0: /** michael@0: * $Id$ michael@0: * michael@0: * Created on 18/09/2005 michael@0: * michael@0: * The default implementation of a TimeZoneRegistry. This implementation will search the classpath for michael@0: * applicable VTimeZone definitions used to back the provided TimeZone instances. michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class TimeZoneRegistryImpl implements TimeZoneRegistry { michael@0: michael@0: private static final String DEFAULT_RESOURCE_PREFIX = "zoneinfo/"; michael@0: michael@0: private static final Pattern TZ_ID_SUFFIX = Pattern.compile("(?<=/)[^/]*/[^/]*$"); michael@0: michael@0: private static final String UPDATE_ENABLED = "net.fortuna.ical4j.timezone.update.enabled"; michael@0: michael@0: private static final Map DEFAULT_TIMEZONES = new ConcurrentHashMap(); michael@0: michael@0: private static final Properties ALIASES = new Properties(); michael@0: static { michael@0: try { michael@3: ALIASES.load(ResourceLoader.getResourceAsStream("net/fortuna/ical4j/model/tz.alias")); michael@3: } michael@0: catch (IOException ioe) { michael@0: LogFactory.getLog(TimeZoneRegistryImpl.class).warn( michael@0: "Error loading timezone aliases: " + ioe.getMessage()); michael@0: } michael@3: try { michael@3: ALIASES.load(ResourceLoader.getResourceAsStream("tz.alias")); michael@3: } michael@3: catch (Exception e) { michael@3: LogFactory.getLog(TimeZoneRegistryImpl.class).debug( michael@3: "Error loading custom timezone aliases: " + e.getMessage()); michael@3: } michael@0: } michael@0: michael@0: private Map timezones; michael@0: michael@0: private String resourcePrefix; michael@0: michael@0: /** michael@0: * Default constructor. michael@0: */ michael@0: public TimeZoneRegistryImpl() { michael@0: this(DEFAULT_RESOURCE_PREFIX); michael@0: } michael@0: michael@0: /** michael@0: * Creates a new instance using the specified resource prefix. michael@0: * @param resourcePrefix a prefix prepended to classpath resource lookups for default timezones michael@0: */ michael@0: public TimeZoneRegistryImpl(final String resourcePrefix) { michael@0: this.resourcePrefix = resourcePrefix; michael@0: timezones = new ConcurrentHashMap(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void register(final TimeZone timezone) { michael@0: // for now we only apply updates to included definitions by default.. michael@0: register(timezone, false); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void register(final TimeZone timezone, boolean update) { michael@0: if (update) { michael@0: // load any available updates for the timezone.. michael@0: timezones.put(timezone.getID(), new TimeZone(updateDefinition(timezone.getVTimeZone()))); michael@0: } michael@0: else { michael@0: timezones.put(timezone.getID(), timezone); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void clear() { michael@0: timezones.clear(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final TimeZone getTimeZone(final String id) { michael@0: TimeZone timezone = (TimeZone) timezones.get(id); michael@0: if (timezone == null) { michael@0: timezone = (TimeZone) DEFAULT_TIMEZONES.get(id); michael@0: if (timezone == null) { michael@0: // if timezone not found with identifier, try loading an alias.. michael@0: final String alias = ALIASES.getProperty(id); michael@0: if (alias != null) { michael@0: return getTimeZone(alias); michael@0: } michael@0: else { michael@0: synchronized (DEFAULT_TIMEZONES) { michael@0: // check again as it may be loaded now.. michael@0: timezone = (TimeZone) DEFAULT_TIMEZONES.get(id); michael@0: if (timezone == null) { michael@0: try { michael@0: final VTimeZone vTimeZone = loadVTimeZone(id); michael@0: if (vTimeZone != null) { michael@0: // XXX: temporary kludge.. michael@0: // ((TzId) vTimeZone.getProperties().getProperty(Property.TZID)).setValue(id); michael@0: timezone = new TimeZone(vTimeZone); michael@0: DEFAULT_TIMEZONES.put(timezone.getID(), timezone); michael@0: } michael@0: else if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) { michael@0: // strip global part of id and match on default tz.. michael@0: Matcher matcher = TZ_ID_SUFFIX.matcher(id); michael@0: if (matcher.find()) { michael@0: return getTimeZone(matcher.group()); michael@0: } michael@0: } michael@0: } michael@0: catch (Exception e) { michael@0: Log log = LogFactory.getLog(TimeZoneRegistryImpl.class); michael@0: log.warn("Error occurred loading VTimeZone", e); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return timezone; michael@0: } michael@0: michael@0: /** michael@0: * Loads an existing VTimeZone from the classpath corresponding to the specified Java timezone. michael@0: */ michael@0: private VTimeZone loadVTimeZone(final String id) throws IOException, ParserException { michael@3: final URL resource = ResourceLoader.getResource(resourcePrefix + id + ".ics"); michael@0: if (resource != null) { michael@0: final CalendarBuilder builder = new CalendarBuilder(); michael@0: final Calendar calendar = builder.build(resource.openStream()); michael@0: final VTimeZone vTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE); michael@0: // load any available updates for the timezone.. can be explicility disabled via configuration michael@0: if (!"false".equals(Configurator.getProperty(UPDATE_ENABLED))) { michael@0: return updateDefinition(vTimeZone); michael@0: } michael@0: return vTimeZone; michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: /** michael@0: * @param vTimeZone michael@0: * @return michael@0: */ michael@0: private VTimeZone updateDefinition(VTimeZone vTimeZone) { michael@0: final TzUrl tzUrl = vTimeZone.getTimeZoneUrl(); michael@0: if (tzUrl != null) { michael@0: try { michael@0: final CalendarBuilder builder = new CalendarBuilder(); michael@0: final Calendar calendar = builder.build(tzUrl.getUri().toURL().openStream()); michael@0: final VTimeZone updatedVTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE); michael@0: if (updatedVTimeZone != null) { michael@0: return updatedVTimeZone; michael@0: } michael@0: } michael@0: catch (Exception e) { michael@0: Log log = LogFactory.getLog(TimeZoneRegistryImpl.class); michael@0: log.warn("Unable to retrieve updates for timezone: " + vTimeZone.getTimeZoneId().getValue(), e); michael@0: } michael@0: } michael@0: return vTimeZone; michael@0: } michael@0: }