001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Rep.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.rep;
009:
010: import com.uwyn.rife.ioc.HierarchicalProperties;
011: import com.uwyn.rife.resources.ResourceFinder;
012: import java.util.Collection;
013:
014: /**
015: * This static abstract class provides easy application-wide access to the
016: * default <code>Repository</code>.
017: * <p>It's main purpose is to be able to quickle retrieve its
018: * <code>Participant</code>s.
019: * <p>It's possible to retrieve and replace the default
020: * <code>Repository</code> with the {@link #setDefaultRepository(Repository)
021: * setDefaultRepository} and {@link #getDefaultRepository()
022: * getDefaultRepository} methods.
023: *
024: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
025: * @version $Revision: 3634 $
026: * @see Participant
027: * @see Repository
028: * @since 1.0
029: */
030: public abstract class Rep {
031: private static Repository sDefaultRepository = null;
032: private static HierarchicalProperties sDummyProperties = null;
033:
034: /**
035: * Initializes the default repository by creating an instance of
036: * <code>BlockingRepository</code> and initializing it with the provided
037: * XML path, a default resource finder and no context.
038: *
039: * @see #initialize(String, ResourceFinder, Object)
040: * @since 1.0
041: */
042: public static void initialize(String repXmlPath) {
043: initialize(repXmlPath, null, null);
044: }
045:
046: /**
047: * Initializes the default repository by creating an instance of
048: * <code>BlockingRepository</code> and initializing it with the provided
049: * properties and context.
050: *
051: * @see BlockingRepository
052: * @see BlockingRepository#initialize(String, ResourceFinder)
053: * @since 1.0
054: */
055: public static void initialize(String repXmlPath,
056: ResourceFinder resourcefinder, Object context) {
057: BlockingRepository repository = new BlockingRepository(context);
058: sDefaultRepository = repository;
059: repository.initialize(repXmlPath, resourcefinder);
060: }
061:
062: /**
063: * Replaces the default repository.
064: *
065: * @param repository An instance of <code>Repository</code> that will
066: * afterwards become the application-wide default repository.
067: * @see Repository
068: * @since 1.0
069: */
070: public static void setDefaultRepository(Repository repository) {
071: sDefaultRepository = repository;
072: }
073:
074: /**
075: * Retrieves the current default repository.
076: *
077: * @return An instance of <code>Repository</code> that is currently the
078: * application-wide default repository.
079: * @see Repository
080: * @since 1.0
081: */
082: public static Repository getDefaultRepository() {
083: return sDefaultRepository;
084: }
085:
086: /**
087: * Convenience method to quickly check if a participant with a certain
088: * name is available in the default repository.
089: *
090: * @see Repository#hasParticipant(String)
091: * @since 1.0
092: */
093: public static boolean hasParticipant(String name) {
094: if (null == sDefaultRepository) {
095: return false;
096: }
097:
098: return sDefaultRepository.hasParticipant(name);
099: }
100:
101: /**
102: * Convenience method to quickly retrieve the first participant with a
103: * certain name from the default repository.
104: *
105: * @see Repository#getParticipant(String)
106: * @since 1.0
107: */
108: public static Participant getParticipant(String name) {
109: if (null == sDefaultRepository) {
110: return null;
111: }
112:
113: return sDefaultRepository.getParticipant(name);
114: }
115:
116: /**
117: * Convenience method to quickly retrieve all the participants with a
118: * certain name from the default repository.
119: *
120: * @see Repository#getParticipants(String)
121: * @since 1.0
122: */
123: public static Collection<? extends Participant> getParticipants(
124: String name) {
125: if (null == sDefaultRepository) {
126: return null;
127: }
128:
129: return sDefaultRepository.getParticipants(name);
130: }
131:
132: /**
133: * Convenience method to quickly retrieve the properties from the default
134: * repository. If no default repository has been configured, an empty instance
135: * of <code>HierarchicalProperties</code> is returned.
136: *
137: * @see Repository#getProperties()
138: * @since 1.1
139: */
140: public static HierarchicalProperties getProperties() {
141: if (null == sDefaultRepository) {
142: if (null == sDummyProperties) {
143: HierarchicalProperties system_properties = new HierarchicalProperties()
144: .putAll(System.getProperties());
145: sDummyProperties = new HierarchicalProperties()
146: .parent(system_properties);
147: }
148: return sDummyProperties;
149: }
150:
151: return sDefaultRepository.getProperties();
152: }
153:
154: /**
155: * Cleans up the default repository if it exists. This is typically done at
156: * application shutdown.
157: *
158: * @since 1.0
159: */
160: public static void cleanup() {
161: if (null == sDefaultRepository) {
162: return;
163: }
164:
165: sDefaultRepository.cleanup();
166: }
167: }
|