View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.kuali.rice.core.config.ConfigContext;
24  import org.kuali.rice.core.exception.RiceRuntimeException;
25  import org.kuali.rice.core.util.DataAccessUtils;
26  import org.kuali.rice.kns.util.ObjectUtils;
27  import org.kuali.rice.ksb.messaging.FlattenedServiceDefinition;
28  import org.kuali.rice.ksb.messaging.ServiceInfo;
29  import org.kuali.rice.ksb.messaging.dao.ServiceInfoDAO;
30  import org.kuali.rice.ksb.messaging.service.ServiceRegistry;
31  import org.kuali.rice.ksb.service.KSBContextServiceLocator;
32  import org.kuali.rice.ksb.service.KSBServiceLocator;
33  
34  
35  public class ServiceRegistryImpl implements ServiceRegistry {
36  
37  	private ServiceInfoDAO dao;
38  	protected KSBContextServiceLocator serviceLocator;
39  	
40  	/**
41  	 * @return the serviceLocator
42  	 */
43  	public KSBContextServiceLocator getServiceLocator() {
44  		return this.serviceLocator;
45  	}
46  
47  	/**
48  	 * @param serviceLocator the serviceLocator to set
49  	 */
50  	public void setServiceLocator(KSBContextServiceLocator serviceLocator) {
51  		this.serviceLocator = serviceLocator;
52  	}
53  
54  	public void saveEntry(ServiceInfo entry) {
55  		try {
56  			if (ObjectUtils.isNull(entry.getSerializedServiceNamespace())) {
57  				entry.setSerializedServiceNamespace(new FlattenedServiceDefinition(serviceLocator==null ?
58  						KSBServiceLocator.getMessageHelper().serializeObject(entry.getServiceDefinition()) :
59  								serviceLocator.getMessageHelper().serializeObject(entry.getServiceDefinition())));
60  			} else {
61  				entry.getSerializedServiceNamespace().setFlattenedServiceDefinitionData(serviceLocator==null ?
62  						KSBServiceLocator.getMessageHelper().serializeObject(entry.getServiceDefinition()) :
63  								serviceLocator.getMessageHelper().serializeObject(entry.getServiceDefinition()));
64  			}
65  		} catch (Exception e) {
66  			throw new RiceRuntimeException(e);
67  		}
68  		getDao().addEntry(entry);
69  	}
70  
71  	public List<ServiceInfo> fetchAll() {
72  		return getDao().fetchAll();
73  	}
74  
75  	public List<ServiceInfo> fetchAllActive() {
76  	    return dao.fetchAllActive();
77  	}
78  
79  	public List<ServiceInfo> fetchActiveByNameLocalPart(String localPart) {
80  		return getDao().fetchActiveByNameLocalPart(localPart);
81  	}
82  	
83  	public List<ServiceInfo> fetchActiveByName(QName serviceName) {
84  		return getDao().fetchActiveByName(serviceName);		
85  	}
86  	
87  	public List<ServiceInfo> fetchActiveByNamespace(String serviceNamespace) {
88  		return getDao().fetchActiveByNamespace(serviceNamespace);
89  	}
90  	
91  	public List<ServiceInfo> findLocallyPublishedServices(String ipNumber, String serviceNamespace) {
92  		if (ConfigContext.getCurrentContextConfig().getDevMode()) {
93  			return new ArrayList<ServiceInfo>();
94  		}
95  		return getDao().findLocallyPublishedServices(ipNumber, serviceNamespace);
96  	}
97  
98  	public void removeEntry(ServiceInfo entry) {
99  		getDao().removeEntry(entry);
100 	}
101 
102 	public void removeLocallyPublishedServices(String ipNumber, String serviceNamespace) {
103 		getDao().removeLocallyPublishedServices(ipNumber, serviceNamespace);
104 	}
105 
106 	public ServiceInfoDAO getDao() {
107 		return this.dao;
108 	}
109 
110 	public void setDao(ServiceInfoDAO dao) {
111 		this.dao = dao;
112 	}
113 
114 	public void removeEntries(List<ServiceInfo> serviceEntries) {
115 		for (ServiceInfo info : serviceEntries) {
116 			removeEntry(info);
117 		}
118 	}
119 
120 	public void saveEntries(List<ServiceInfo> serviceEntries) {
121 		for (ServiceInfo info : serviceEntries) {
122 			saveEntry(info);
123 		}
124 	}
125 
126 	public void markServicesDead(List<ServiceInfo> serviceEntries) {
127 		for (ServiceInfo info : serviceEntries) {
128 			// there is contention on these records from multiple nodes and odds
129 			// are the
130 			// one we have in memory is stale. refetch and mork dead.
131 			ServiceInfo currentInfo = getDao().findServiceInfo(info.getMessageEntryId());
132 			currentInfo.setAlive(false);
133 			try {
134 				saveEntry(currentInfo);
135 			} catch (Exception e) {
136 				boolean isOptimisticLockExp = DataAccessUtils.isOptimisticLockFailure(e);
137 				// suppress optimistic lock exceptions, it's collision with
138 				// other nodes
139 				if (!isOptimisticLockExp) {
140 					throw (RuntimeException) e;
141 				}
142 			}
143 
144 		}
145 	}
146 
147 	public FlattenedServiceDefinition getFlattenedServiceDefinition(Long flattenedServiceDefinitionId) {
148 		return getDao().findFlattenedServiceDefinition(flattenedServiceDefinitionId);
149 	}
150 }