001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/query/virtualmetainfo/VirtualMetaPropertiesRegistrySingleton.java,v 1.1.1.1 2004/03/25 12:08:37 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:37 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruņa
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruņa nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040: package es.udc.mypersonalizer.kernel.model.query.virtualmetainfo;
041:
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.net.URL;
045: import java.util.HashMap;
046: import java.util.Map;
047:
048: import org.xml.sax.SAXException;
049:
050: import es.udc.mypersonalizer.kernel.log.Log;
051: import es.udc.mypersonalizer.kernel.log.LogManager;
052: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
053: import es.udc.mypersonalizer.kernel.model.metainfo.MetaProperty;
054: import es.udc.mypersonalizer.kernel.model.xmlconverters.VirtualMetaPropertiesXMLConverter;
055: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
056: import es.udc.mypersonalizer.kernel.util.xml.LogWarningsHandler;
057:
058: /**
059: * Singleton class providing simple access by unique name to the configured
060: * virtual metaproperties.
061: *
062: * @author Abel Muinho
063: * @since 1.0
064: */
065: public class VirtualMetaPropertiesRegistrySingleton {
066: private static final Log log = LogManager
067: .getLog(LogNamingConventions.MYPERSONALIZER);
068:
069: /** Map of known virtual metaproperties. */
070: private Map virtualMetaProperties = null;
071:
072: /** The single instance of this class. */
073: private static VirtualMetaPropertiesRegistrySingleton instance = null;
074:
075: /** Name of the configuration resource where virtual metaproperties are
076: * defined.
077: */
078: private static final String CONFIG_RESOURCE = "myper-virtual-metaproperties-config.xml";
079:
080: /** Hidden constructor. */
081: private VirtualMetaPropertiesRegistrySingleton() {
082: }
083:
084: /**
085: * Obtains the single instance of this class, with the registry data
086: * already loaded.
087: * @return the single instance.
088: */
089: public static VirtualMetaPropertiesRegistrySingleton getInstance() {
090:
091: if (instance == null) {
092: instance = new VirtualMetaPropertiesRegistrySingleton();
093: try {
094: instance.refresh();
095: } catch (InternalErrorException e) {
096: /* Configuration error. As the virtual metaproperties are
097: * optional, continue as if no configuration file had been
098: * found.
099: */
100: instance.virtualMetaProperties = new HashMap();
101: }
102: }
103: return instance;
104: }
105:
106: /**
107: * Loads configuration data, replacing any previous data in the registry.
108: * @throws InternalErrorException if the configuration data can't be used.
109: */
110: public synchronized void refresh() throws InternalErrorException {
111: ClassLoader loader = VirtualMetaPropertiesRegistrySingleton.class
112: .getClassLoader();
113: URL resourceURL = loader.getResource(CONFIG_RESOURCE);
114: if (resourceURL == null) {
115: /* No configuration file. That's ok, use an empty map. */
116: virtualMetaProperties = new HashMap(0);
117: } else {
118: try {
119: InputStream is = resourceURL.openStream();
120: virtualMetaProperties = VirtualMetaPropertiesXMLConverter
121: .fromXML(is, new LogWarningsHandler(log, this
122: .getClass()));
123: } catch (IOException e) {
124: log.write("Error accessing to the configuration file.",
125: e, this .getClass());
126: throw new InternalErrorException(e);
127: } catch (SAXException e) {
128: log.write("Bad XML configuration file.", e, this
129: .getClass());
130: throw new InternalErrorException(e);
131: }
132: }
133: }
134:
135: /**
136: * Returns the virtual metaproperty with the given unique name.
137: * @param uniqueName the unique name of the metaproperty to look up.
138: * @return the metaproperty with the given unique name, or
139: * <code>null</code> if no metaproperty is known with that name.
140: */
141: public MetaProperty getMetaProperty(String uniqueName) {
142: return (MetaProperty) virtualMetaProperties.get(uniqueName);
143: }
144:
145: /**
146: * Package-visible method for testing, which adds a metaproperty to the
147: * registry.
148: * @param uniqueName the unique name of the metaproperty.
149: * @param metaProperty the metaproperty to add.
150: */
151: void addMetaProperty(String uniqueName, MetaProperty metaProperty) {
152: virtualMetaProperties.put(uniqueName, metaProperty);
153: }
154: }
|