001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.services;
007:
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.Map;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.jasig.portal.EntityTypes;
015: import org.jasig.portal.groups.GroupsException;
016: import org.jasig.portal.groups.IEntityNameFinder;
017: import org.jasig.portal.groups.IEntityNameFinderFactory;
018: import org.jasig.portal.properties.PropertiesManager;
019:
020: /**
021: * @author Dan Ellentuck
022: * @version $Revision: 35418 $
023: */
024: public class EntityNameFinderService {
025:
026: private static final Log log = LogFactory
027: .getLog(EntityNameFinderService.class);
028:
029: private static EntityNameFinderService m_instance;
030: private Map nameFinders = null;
031: private static boolean initialized = false;
032:
033: /**
034: *
035: */
036: private EntityNameFinderService() {
037: super ();
038: initialize();
039: }
040:
041: /**
042: * @return org.jasig.portal.groups.IEntityNameFinder
043: */
044: public IEntityNameFinder getNameFinder(Class type)
045: throws GroupsException {
046: IEntityNameFinder finder = (IEntityNameFinder) (getNameFinders()
047: .get(type));
048: if (finder == null) {
049: throw new GroupsException("Name finder for "
050: + type.getName() + " could not be located.");
051: }
052: return finder;
053: }
054:
055: /**
056: * @return java.util.Map
057: */
058: private Map getNameFinders() {
059: if (nameFinders == null) {
060: nameFinders = new HashMap(10);
061: }
062: return nameFinders;
063: }
064:
065: /**
066: * Gets all the entity types and tries to instantiate and cache a finder for each
067: * one. There needn't be a finder for every entity type, so if there's no entry
068: * in the portal.properties, we just log the fact and continue.
069: */
070: private synchronized void initialize() {
071: Iterator types = EntityTypes.singleton().getAllEntityTypes();
072: String factoryName = null;
073:
074: while (types.hasNext()) {
075: Class type = (Class) types.next();
076: if (type != Object.class) {
077: String factoryKey = "org.jasig.portal.services.EntityNameFinderService.NameFinderFactory.implementation_"
078: + type.getName();
079: try {
080: factoryName = PropertiesManager
081: .getProperty(factoryKey);
082: } catch (Exception runtime) {
083: String dMsg = "EntityNameFinderService.initialize(): "
084: + "could not find property for "
085: + type.getName() + " factory.";
086: log.debug(dMsg);
087: }
088: if (factoryName != null) {
089: try {
090: IEntityNameFinderFactory factory = (IEntityNameFinderFactory) Class
091: .forName(factoryName).newInstance();
092: getNameFinders().put(type, factory.newFinder());
093: } catch (Exception e) {
094: String eMsg = "EntityNameFinderService.initialize(): "
095: + "Could not instantiate finder for "
096: + type.getName() + ": ";
097: log.error(eMsg, e);
098: }
099: }
100: }
101: }
102: setInitialized(true);
103: }
104:
105: /**
106: * @return EntityNameFinderService
107: */
108: public final static synchronized EntityNameFinderService instance() {
109: if (m_instance == null) {
110: m_instance = new EntityNameFinderService();
111: }
112: return m_instance;
113: }
114:
115: /**
116: * @return boolean
117: */
118: private static boolean isInitialized() {
119: return initialized;
120: }
121:
122: /**
123: * @param newInitialized boolean
124: */
125: static void setInitialized(boolean newInitialized) {
126: initialized = newInitialized;
127: }
128:
129: /**
130: */
131: public static void start() {
132: if (!isInitialized()) {
133: instance();
134: }
135: }
136: }
|