src/net/fortuna/ical4j/model/TimeZoneRegistryImpl.java

Tue, 10 Feb 2015 19:58:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 19:58:00 +0100
changeset 4
45d57ecba757
parent 0
fb9019fb1bf7
child 5
e0e108e77052
permissions
-rw-r--r--

Upgrade the upgraded ical4j component to use org.apache.commons.lang3.

michael@0 1 /**
michael@0 2 * Copyright (c) 2012, Ben Fortuna
michael@0 3 * All rights reserved.
michael@0 4 *
michael@0 5 * Redistribution and use in source and binary forms, with or without
michael@0 6 * modification, are permitted provided that the following conditions
michael@0 7 * are met:
michael@0 8 *
michael@0 9 * o Redistributions of source code must retain the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer.
michael@0 11 *
michael@0 12 * o Redistributions in binary form must reproduce the above copyright
michael@0 13 * notice, this list of conditions and the following disclaimer in the
michael@0 14 * documentation and/or other materials provided with the distribution.
michael@0 15 *
michael@0 16 * o Neither the name of Ben Fortuna nor the names of any other contributors
michael@0 17 * may be used to endorse or promote products derived from this software
michael@0 18 * without specific prior written permission.
michael@0 19 *
michael@0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
michael@0 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32 package net.fortuna.ical4j.model;
michael@0 33
michael@0 34 import java.io.IOException;
michael@0 35 import java.net.URL;
michael@0 36 import java.util.Map;
michael@0 37 import java.util.Properties;
michael@0 38 import java.util.regex.Matcher;
michael@0 39 import java.util.regex.Pattern;
michael@0 40
michael@0 41 import net.fortuna.ical4j.data.CalendarBuilder;
michael@0 42 import net.fortuna.ical4j.data.ParserException;
michael@0 43 import net.fortuna.ical4j.model.component.VTimeZone;
michael@0 44 import net.fortuna.ical4j.model.property.TzUrl;
michael@0 45 import net.fortuna.ical4j.util.CompatibilityHints;
michael@0 46 import net.fortuna.ical4j.util.Configurator;
michael@0 47 import net.fortuna.ical4j.util.ResourceLoader;
michael@0 48
michael@0 49 import org.apache.commons.logging.Log;
michael@0 50 import org.apache.commons.logging.LogFactory;
michael@0 51
michael@0 52 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
michael@0 53
michael@0 54 /**
michael@0 55 * $Id$
michael@0 56 *
michael@0 57 * Created on 18/09/2005
michael@0 58 *
michael@0 59 * The default implementation of a <code>TimeZoneRegistry</code>. This implementation will search the classpath for
michael@0 60 * applicable VTimeZone definitions used to back the provided TimeZone instances.
michael@0 61 * @author Ben Fortuna
michael@0 62 */
michael@0 63 public class TimeZoneRegistryImpl implements TimeZoneRegistry {
michael@0 64
michael@0 65 private static final String DEFAULT_RESOURCE_PREFIX = "zoneinfo/";
michael@0 66
michael@0 67 private static final Pattern TZ_ID_SUFFIX = Pattern.compile("(?<=/)[^/]*/[^/]*$");
michael@0 68
michael@0 69 private static final String UPDATE_ENABLED = "net.fortuna.ical4j.timezone.update.enabled";
michael@0 70
michael@0 71 private static final Map DEFAULT_TIMEZONES = new ConcurrentHashMap();
michael@0 72
michael@0 73 private static final Properties ALIASES = new Properties();
michael@0 74 static {
michael@0 75 try {
michael@3 76 ALIASES.load(ResourceLoader.getResourceAsStream("net/fortuna/ical4j/model/tz.alias"));
michael@3 77 }
michael@0 78 catch (IOException ioe) {
michael@0 79 LogFactory.getLog(TimeZoneRegistryImpl.class).warn(
michael@0 80 "Error loading timezone aliases: " + ioe.getMessage());
michael@0 81 }
michael@3 82 try {
michael@3 83 ALIASES.load(ResourceLoader.getResourceAsStream("tz.alias"));
michael@3 84 }
michael@3 85 catch (Exception e) {
michael@3 86 LogFactory.getLog(TimeZoneRegistryImpl.class).debug(
michael@3 87 "Error loading custom timezone aliases: " + e.getMessage());
michael@3 88 }
michael@0 89 }
michael@0 90
michael@0 91 private Map timezones;
michael@0 92
michael@0 93 private String resourcePrefix;
michael@0 94
michael@0 95 /**
michael@0 96 * Default constructor.
michael@0 97 */
michael@0 98 public TimeZoneRegistryImpl() {
michael@0 99 this(DEFAULT_RESOURCE_PREFIX);
michael@0 100 }
michael@0 101
michael@0 102 /**
michael@0 103 * Creates a new instance using the specified resource prefix.
michael@0 104 * @param resourcePrefix a prefix prepended to classpath resource lookups for default timezones
michael@0 105 */
michael@0 106 public TimeZoneRegistryImpl(final String resourcePrefix) {
michael@0 107 this.resourcePrefix = resourcePrefix;
michael@0 108 timezones = new ConcurrentHashMap();
michael@0 109 }
michael@0 110
michael@0 111 /**
michael@0 112 * {@inheritDoc}
michael@0 113 */
michael@0 114 public final void register(final TimeZone timezone) {
michael@0 115 // for now we only apply updates to included definitions by default..
michael@0 116 register(timezone, false);
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * {@inheritDoc}
michael@0 121 */
michael@0 122 public final void register(final TimeZone timezone, boolean update) {
michael@0 123 if (update) {
michael@0 124 // load any available updates for the timezone..
michael@0 125 timezones.put(timezone.getID(), new TimeZone(updateDefinition(timezone.getVTimeZone())));
michael@0 126 }
michael@0 127 else {
michael@0 128 timezones.put(timezone.getID(), timezone);
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 /**
michael@0 133 * {@inheritDoc}
michael@0 134 */
michael@0 135 public final void clear() {
michael@0 136 timezones.clear();
michael@0 137 }
michael@0 138
michael@0 139 /**
michael@0 140 * {@inheritDoc}
michael@0 141 */
michael@0 142 public final TimeZone getTimeZone(final String id) {
michael@0 143 TimeZone timezone = (TimeZone) timezones.get(id);
michael@0 144 if (timezone == null) {
michael@0 145 timezone = (TimeZone) DEFAULT_TIMEZONES.get(id);
michael@0 146 if (timezone == null) {
michael@0 147 // if timezone not found with identifier, try loading an alias..
michael@0 148 final String alias = ALIASES.getProperty(id);
michael@0 149 if (alias != null) {
michael@0 150 return getTimeZone(alias);
michael@0 151 }
michael@0 152 else {
michael@0 153 synchronized (DEFAULT_TIMEZONES) {
michael@0 154 // check again as it may be loaded now..
michael@0 155 timezone = (TimeZone) DEFAULT_TIMEZONES.get(id);
michael@0 156 if (timezone == null) {
michael@0 157 try {
michael@0 158 final VTimeZone vTimeZone = loadVTimeZone(id);
michael@0 159 if (vTimeZone != null) {
michael@0 160 // XXX: temporary kludge..
michael@0 161 // ((TzId) vTimeZone.getProperties().getProperty(Property.TZID)).setValue(id);
michael@0 162 timezone = new TimeZone(vTimeZone);
michael@0 163 DEFAULT_TIMEZONES.put(timezone.getID(), timezone);
michael@0 164 }
michael@0 165 else if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
michael@0 166 // strip global part of id and match on default tz..
michael@0 167 Matcher matcher = TZ_ID_SUFFIX.matcher(id);
michael@0 168 if (matcher.find()) {
michael@0 169 return getTimeZone(matcher.group());
michael@0 170 }
michael@0 171 }
michael@0 172 }
michael@0 173 catch (Exception e) {
michael@0 174 Log log = LogFactory.getLog(TimeZoneRegistryImpl.class);
michael@0 175 log.warn("Error occurred loading VTimeZone", e);
michael@0 176 }
michael@0 177 }
michael@0 178 }
michael@0 179 }
michael@0 180 }
michael@0 181 }
michael@0 182 return timezone;
michael@0 183 }
michael@0 184
michael@0 185 /**
michael@0 186 * Loads an existing VTimeZone from the classpath corresponding to the specified Java timezone.
michael@0 187 */
michael@0 188 private VTimeZone loadVTimeZone(final String id) throws IOException, ParserException {
michael@3 189 final URL resource = ResourceLoader.getResource(resourcePrefix + id + ".ics");
michael@0 190 if (resource != null) {
michael@0 191 final CalendarBuilder builder = new CalendarBuilder();
michael@0 192 final Calendar calendar = builder.build(resource.openStream());
michael@0 193 final VTimeZone vTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE);
michael@0 194 // load any available updates for the timezone.. can be explicility disabled via configuration
michael@0 195 if (!"false".equals(Configurator.getProperty(UPDATE_ENABLED))) {
michael@0 196 return updateDefinition(vTimeZone);
michael@0 197 }
michael@0 198 return vTimeZone;
michael@0 199 }
michael@0 200 return null;
michael@0 201 }
michael@0 202
michael@0 203 /**
michael@0 204 * @param vTimeZone
michael@0 205 * @return
michael@0 206 */
michael@0 207 private VTimeZone updateDefinition(VTimeZone vTimeZone) {
michael@0 208 final TzUrl tzUrl = vTimeZone.getTimeZoneUrl();
michael@0 209 if (tzUrl != null) {
michael@0 210 try {
michael@0 211 final CalendarBuilder builder = new CalendarBuilder();
michael@0 212 final Calendar calendar = builder.build(tzUrl.getUri().toURL().openStream());
michael@0 213 final VTimeZone updatedVTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE);
michael@0 214 if (updatedVTimeZone != null) {
michael@0 215 return updatedVTimeZone;
michael@0 216 }
michael@0 217 }
michael@0 218 catch (Exception e) {
michael@0 219 Log log = LogFactory.getLog(TimeZoneRegistryImpl.class);
michael@0 220 log.warn("Unable to retrieve updates for timezone: " + vTimeZone.getTimeZoneId().getValue(), e);
michael@0 221 }
michael@0 222 }
michael@0 223 return vTimeZone;
michael@0 224 }
michael@0 225 }

mercurial