001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.rice;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.kuali.core.datadictionary.ValidationCompletionUtils;
029: import org.kuali.core.inquiry.Inquirable;
030: import org.kuali.core.lookup.LookupResultsService;
031: import org.kuali.core.lookup.Lookupable;
032: import org.kuali.core.question.Question;
033: import org.kuali.core.service.AttachmentService;
034: import org.kuali.core.service.AuthorizationService;
035: import org.kuali.core.service.BusinessObjectDictionaryService;
036: import org.kuali.core.service.BusinessObjectMetaDataService;
037: import org.kuali.core.service.BusinessObjectService;
038: import org.kuali.core.service.DataDictionaryService;
039: import org.kuali.core.service.DateTimeService;
040: import org.kuali.core.service.DictionaryValidationService;
041: import org.kuali.core.service.DocumentAuthorizationService;
042: import org.kuali.core.service.DocumentService;
043: import org.kuali.core.service.DocumentTypeService;
044: import org.kuali.core.service.EncryptionService;
045: import org.kuali.core.service.KeyValuesService;
046: import org.kuali.core.service.KualiConfigurationService;
047: import org.kuali.core.service.KualiGroupService;
048: import org.kuali.core.service.KualiModuleService;
049: import org.kuali.core.service.KualiModuleUserPropertyService;
050: import org.kuali.core.service.KualiRuleService;
051: import org.kuali.core.service.LookupService;
052: import org.kuali.core.service.MailService;
053: import org.kuali.core.service.MaintenanceDocumentDictionaryService;
054: import org.kuali.core.service.MaintenanceDocumentService;
055: import org.kuali.core.service.NoteService;
056: import org.kuali.core.service.PersistenceService;
057: import org.kuali.core.service.PersistenceStructureService;
058: import org.kuali.core.service.PostProcessorService;
059: import org.kuali.core.service.SequenceAccessorService;
060: import org.kuali.core.service.TransactionalDocumentDictionaryService;
061: import org.kuali.core.service.UniversalUserService;
062: import org.kuali.core.service.WebAuthenticationService;
063: import org.kuali.core.service.XmlObjectSerializerService;
064: import org.kuali.core.util.OjbCollectionHelper;
065: import org.kuali.core.util.cache.MethodCacheInterceptor;
066: import org.kuali.core.util.spring.NamedOrderedListBean;
067: import org.kuali.core.workflow.service.KualiWorkflowInfo;
068: import org.kuali.core.workflow.service.WorkflowDocumentService;
069: import org.kuali.core.workflow.service.WorkflowGroupService;
070: import org.kuali.rice.kns.config.KNSResourceLoaderFactory;
071: import org.kuali.rice.resourceloader.GlobalResourceLoader;
072: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
073: import org.springframework.transaction.PlatformTransactionManager;
074: import org.springframework.transaction.support.TransactionTemplate;
075:
076: import com.opensymphony.oscache.general.GeneralCacheAdministrator;
077:
078: public class KNSServiceLocator<T extends Object> {
079:
080: public static final String VALIDATION_COMPLETION_UTILS = "validationCompletionUtils";
081:
082: public static Object getService(String serviceName) {
083: return GlobalResourceLoader.getService(serviceName);
084: }
085:
086: public static <T> T getBean(Class<T> type) {
087: Collection<T> beansOfType = getBeansOfType(type).values();
088: if (beansOfType.isEmpty()) {
089: throw new NoSuchBeanDefinitionException(
090: "No beans of this type in the KNS application context: "
091: + type.getName());
092: }
093: if (beansOfType.size() > 1) {
094: return getBean(type, type.getSimpleName().substring(0, 1)
095: .toLowerCase()
096: + type.getSimpleName().substring(1));
097: }
098: return beansOfType.iterator().next();
099: }
100:
101: @SuppressWarnings("unchecked")
102: public static <T> T getBean(Class<T> type, String name) {
103: return (T) KNSResourceLoaderFactory.getSpringResourceLoader()
104: .getContext().getBean(name);
105: }
106:
107: @SuppressWarnings("unchecked")
108: public static <T> Map<String, T> getBeansOfType(Class<T> type) {
109: return new HashMap((Map) KNSResourceLoaderFactory
110: .getSpringResourceLoader().getContext().getBeansOfType(
111: type));
112: }
113:
114: public static String[] getBeanNames() {
115: return KNSResourceLoaderFactory.getSpringResourceLoader()
116: .getContext().getBeanDefinitionNames();
117: }
118:
119: public static Set<String> getSingletonNames() {
120: Set<String> singletonNames = new HashSet<String>();
121: Collections.addAll(singletonNames, KNSResourceLoaderFactory
122: .getSpringResourceLoader().getContext()
123: .getBeanFactory().getSingletonNames());
124: return singletonNames;
125: }
126:
127: public static Set<Class> getSingletonTypes() {
128: Set<Class> singletonTypes = new HashSet<Class>();
129: for (String singletonName : getSingletonNames()) {
130: singletonTypes.add(KNSResourceLoaderFactory
131: .getSpringResourceLoader().getContext()
132: .getBeanFactory().getType(singletonName));
133: }
134: return singletonTypes;
135: }
136:
137: public static List<NamedOrderedListBean> getNamedOrderedListBeans(
138: String listName) {
139: List<NamedOrderedListBean> namedOrderedListBeans = new ArrayList<NamedOrderedListBean>();
140: for (Object namedOrderedListBean : KNSResourceLoaderFactory
141: .getSpringResourceLoader().getContext().getBeansOfType(
142: NamedOrderedListBean.class).values()) {
143: if (((NamedOrderedListBean) namedOrderedListBean).getName()
144: .equals(listName)) {
145: namedOrderedListBeans
146: .add((NamedOrderedListBean) namedOrderedListBean);
147: }
148: }
149: return namedOrderedListBeans;
150: }
151:
152: public static final ValidationCompletionUtils getValidationCompletionUtils() {
153: return (ValidationCompletionUtils) getService(VALIDATION_COMPLETION_UTILS);
154: }
155:
156: public static final String ENCRYPTION_SERVICE = "encryptionService";
157:
158: public static final EncryptionService getEncryptionService() {
159: return (EncryptionService) getService(ENCRYPTION_SERVICE);
160: }
161:
162: public static final String MAIL_SERVICE = "mailService";
163:
164: public static final MailService getMailService() {
165: return (MailService) getService(MAIL_SERVICE);
166: }
167:
168: public static final String METHOD_CACHE_INTERCEPTOR = "methodCacheInterceptor";
169:
170: public static MethodCacheInterceptor getMethodCacheInterceptor() {
171: return (MethodCacheInterceptor) getService(METHOD_CACHE_INTERCEPTOR);
172: }
173:
174: public static final String XML_OBJECT_SERIALIZER_SERVICE = "xmlObjectSerializerService";
175:
176: public static XmlObjectSerializerService getXmlObjectSerializerService() {
177: return (XmlObjectSerializerService) getService(XML_OBJECT_SERIALIZER_SERVICE);
178: }
179:
180: public static final String DOCUMENT_SERVICE = "documentService";
181:
182: public static DocumentService getDocumentService() {
183: return (DocumentService) getService(DOCUMENT_SERVICE);
184: }
185:
186: public static final String POST_PROCESSOR_SERVICE = "postProcessorService";
187:
188: public static PostProcessorService getPostProcessorService() {
189: return (PostProcessorService) getService(POST_PROCESSOR_SERVICE);
190: }
191:
192: public static final String WEB_AUTHENTICATION_SERVICE = "webAuthenticationService";
193:
194: public static WebAuthenticationService getWebAuthenticationService() {
195: return (WebAuthenticationService) getService(WEB_AUTHENTICATION_SERVICE);
196: }
197:
198: public static final String DATETIME_SERVICE = "dateTimeService";
199:
200: public static DateTimeService getDateTimeService() {
201: return (DateTimeService) getService(DATETIME_SERVICE);
202: }
203:
204: public static final String WORKFLOW_GROUP_SERVICE = "workflowGroupService";
205:
206: public static WorkflowGroupService getWorkflowGroupService() {
207: return (WorkflowGroupService) getService(WORKFLOW_GROUP_SERVICE);
208: }
209:
210: public static final String DOCUMENT_TYPE_SERVICE = "documentTypeService";
211:
212: public static DocumentTypeService getDocumentTypeService() {
213: return (DocumentTypeService) getService(DOCUMENT_TYPE_SERVICE);
214: }
215:
216: public static final String LOOKUP_SERVICE = "lookupService";
217:
218: public static LookupService getLookupService() {
219: return (LookupService) getService(LOOKUP_SERVICE);
220: }
221:
222: public static final String LOOKUP_RESULTS_SERVICE = "lookupResultsService";
223:
224: public static LookupResultsService getLookupResultsService() {
225: return (LookupResultsService) getService(LOOKUP_RESULTS_SERVICE);
226: }
227:
228: public static final String UNIVERSAL_USER_SERVICE = "universalUserService";
229:
230: public static UniversalUserService getUniversalUserService() {
231: return (UniversalUserService) getService(UNIVERSAL_USER_SERVICE);
232: }
233:
234: public static final String KUALI_MODULE_SERVICE = "kualiModuleService";
235:
236: public static KualiModuleService getKualiModuleService() {
237: return (KualiModuleService) getService(KUALI_MODULE_SERVICE);
238: }
239:
240: public static final String MODULE_USER_PROPERTY_SERVICE = "kualiModuleUserPropertyService";
241:
242: public static KualiModuleUserPropertyService getKualiModuleUserPropertyService() {
243: return (KualiModuleUserPropertyService) getService(MODULE_USER_PROPERTY_SERVICE);
244: }
245:
246: public static final String KUALI_GROUP_SERVICE = "kualiGroupService";
247:
248: public static KualiGroupService getKualiGroupService() {
249: return (KualiGroupService) getService(KUALI_GROUP_SERVICE);
250: }
251:
252: public static final String WORKFLOW_DOCUMENT_SERVICE = "workflowDocumentService";
253:
254: public static WorkflowDocumentService getWorkflowDocumentService() {
255: return (WorkflowDocumentService) getService(WORKFLOW_DOCUMENT_SERVICE);
256: }
257:
258: public static final String WORKFLOW_INFO_SERVICE = "workflowInfoService";
259:
260: public static KualiWorkflowInfo getWorkflowInfoService() {
261: return (KualiWorkflowInfo) getService(WORKFLOW_INFO_SERVICE);
262: }
263:
264: public static final String KUALI_CONFIGURATION_SERVICE = "kualiConfigurationService";
265:
266: public static KualiConfigurationService getKualiConfigurationService() {
267: return (KualiConfigurationService) getService(KUALI_CONFIGURATION_SERVICE);
268: }
269:
270: public static final String BUSINESS_OBJECT_DICTIONARY_SERVICE = "businessObjectDictionaryService";
271:
272: public static BusinessObjectDictionaryService getBusinessObjectDictionaryService() {
273: return (BusinessObjectDictionaryService) getService(BUSINESS_OBJECT_DICTIONARY_SERVICE);
274: }
275:
276: public static final String BUSINESS_OBJECT_METADATA_SERVICE = "businessObjectMetaDataService";
277:
278: public static BusinessObjectMetaDataService getBusinessObjectMetaDataService() {
279: return (BusinessObjectMetaDataService) getService(BUSINESS_OBJECT_METADATA_SERVICE);
280: }
281:
282: public static final String TRANSACTIONAL_DOCUMENT_DICTIONARY_SERVICE = "transactionalDocumentDictionaryService";
283:
284: public static TransactionalDocumentDictionaryService getTransactionalDocumentDictionaryService() {
285: return (TransactionalDocumentDictionaryService) getService(TRANSACTIONAL_DOCUMENT_DICTIONARY_SERVICE);
286: }
287:
288: public static final String MAINTENANCE_DOCUMENT_DICTIONARY_SERVICE = "maintenanceDocumentDictionaryService";
289:
290: public static MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
291: return (MaintenanceDocumentDictionaryService) getService(MAINTENANCE_DOCUMENT_DICTIONARY_SERVICE);
292: }
293:
294: public static final String DATA_DICTIONARY_SERVICE = "dataDictionaryService";
295:
296: public static DataDictionaryService getDataDictionaryService() {
297: return (DataDictionaryService) getService(DATA_DICTIONARY_SERVICE);
298: }
299:
300: public static final String MAINTENANCE_DOCUMENT_SERVICE = "maintenanceDocumentService";
301:
302: public static MaintenanceDocumentService getMaintenanceDocumentService() {
303: return (MaintenanceDocumentService) getService(MAINTENANCE_DOCUMENT_SERVICE);
304: }
305:
306: public static final String NOTE_SERVICE = "noteService";
307:
308: public static NoteService getNoteService() {
309: return (NoteService) getService(NOTE_SERVICE);
310: }
311:
312: public static final String PERSISTENCE_SERVICE = "persistenceService";
313:
314: public static PersistenceService getPersistenceService() {
315: return (PersistenceService) getService(PERSISTENCE_SERVICE);
316: }
317:
318: public static final String PERSISTENCE_STRUCTURE_SERVICE = "persistenceStructureService";
319:
320: public static PersistenceStructureService getPersistenceStructureService() {
321: return (PersistenceStructureService) getService(PERSISTENCE_STRUCTURE_SERVICE);
322: }
323:
324: public static final String KUALI_RULE_SERVICE = "kualiRuleService";
325:
326: public static KualiRuleService getKualiRuleService() {
327: return (KualiRuleService) getService(KUALI_RULE_SERVICE);
328: }
329:
330: public static final String BUSINESS_OBJECT_SERVICE = "businessObjectService";
331:
332: public static BusinessObjectService getBusinessObjectService() {
333: return (BusinessObjectService) getService(BUSINESS_OBJECT_SERVICE);
334: }
335:
336: // special ones for Inquirable and Lookupable
337: public static final String KUALI_INQUIRABLE = "kualiInquirable";
338:
339: public static Inquirable getKualiInquirable() {
340: return (Inquirable) getService(KUALI_INQUIRABLE);
341: }
342:
343: public static final String KUALI_LOOKUPABLE = "kualiLookupable";
344:
345: public static Lookupable getKualiLookupable() {
346: return (Lookupable) getService(KUALI_LOOKUPABLE);
347: }
348:
349: public static final String GL_LOOKUPABLE = "glLookupable";
350:
351: public static Lookupable getGLLookupable() {
352: return (Lookupable) getService(GL_LOOKUPABLE);
353: }
354:
355: public static Lookupable getLookupable(String lookupableName) {
356: return (Lookupable) getService(lookupableName);
357: }
358:
359: // special one for QuestionPrompt
360: public static Question getQuestion(String questionName) {
361: return (Question) getService(questionName);
362: }
363:
364: // DictionaryValidationService
365: public static final String DICTIONARY_VALIDATION_SERVICE = "dictionaryValidationService";
366:
367: public static DictionaryValidationService getDictionaryValidationService() {
368: return (DictionaryValidationService) getService(DICTIONARY_VALIDATION_SERVICE);
369: }
370:
371: // AuthorizationService
372: public static final String AUTHORIZATION_SERVICE = "authorizationService";
373:
374: public static AuthorizationService getAuthorizationService() {
375: return (AuthorizationService) getService(AUTHORIZATION_SERVICE);
376: }
377:
378: // AttachmentService
379: public static final String ATTACHMENT_SERVICE = "attachmentService";
380:
381: public static AttachmentService getAttachmentService() {
382: return (AttachmentService) getService(ATTACHMENT_SERVICE);
383: }
384:
385: // DocumentAuthorizationService
386: public static final String DOCUMENT_AUTHORIZATION_SERVICE = "documentAuthorizationService";
387:
388: public static DocumentAuthorizationService getDocumentAuthorizationService() {
389: return (DocumentAuthorizationService) getService(DOCUMENT_AUTHORIZATION_SERVICE);
390: }
391:
392: // SequenceAccessorService
393: public static final String SEQUENCE_ACCESSOR_SERVICE = "sequenceAccessorService";
394:
395: public static SequenceAccessorService getSequenceAccessorService() {
396: return (SequenceAccessorService) getService(SEQUENCE_ACCESSOR_SERVICE);
397: }
398:
399: // KeyValuesService
400: public static final String KEY_VALUES_SERVICE = "keyValuesService";
401:
402: public static KeyValuesService getKeyValuesService() {
403: return (KeyValuesService) getService(KEY_VALUES_SERVICE);
404: }
405:
406: public static final String OJB_COLLECTION_HELPER = "ojbCollectionHelper";
407:
408: public static OjbCollectionHelper getOjbCollectionHelper() {
409: return (OjbCollectionHelper) getService(OJB_COLLECTION_HELPER);
410: }
411:
412: public static final String PERSISTENCE_CACHE_ADMINISTRATOR = "persistenceCacheAdministrator";
413:
414: public static final GeneralCacheAdministrator getPersistenceCacheAdministrator() {
415: return (GeneralCacheAdministrator) getService(PERSISTENCE_CACHE_ADMINISTRATOR);
416: }
417:
418: public static final String TRANSACTION_MANAGER = "transactionManager";
419:
420: public static PlatformTransactionManager getTransactionManager() {
421: return (PlatformTransactionManager) getService(TRANSACTION_MANAGER);
422: }
423:
424: public static final String TRANSACTION_TEMPLATE = "transactionTemplate";
425:
426: public static TransactionTemplate getTransactionTemplate() {
427: return (TransactionTemplate) getService(TRANSACTION_TEMPLATE);
428: }
429:
430: }
|