View Javadoc

1   /**
2    * Copyright 2005-2013 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.impl.bus;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.config.CoreConfigHelper;
20  import org.kuali.rice.core.api.config.property.ConfigContext;
21  import org.kuali.rice.core.api.util.RiceUtilities;
22  import org.kuali.rice.ksb.util.KSBConstants;
23  import org.springframework.beans.factory.config.AbstractFactoryBean;
24  
25  /**
26   * A factory bean which is used to produce an instance id for the service bus.
27   * 
28   * <p>The value for the instance id is determined in the following order of preferences:
29   * 
30   * <ol>
31   *   <li>If {@code instanceId} is set on this factory bean, that value will be used.</li> 
32   *   <li>If {@link KSBConstants.Config#INSTANCE_ID} has been set in the configuration context, that value will be used.</li>
33   *   <li>If none of the above, the instance id will be a combination of this application's namespace plus ip address.</li>
34   * </ol>
35   * 
36   * <p>In the case that the instance id is generated, the application id will be pulled
37   * from the configuration context using the {@link KSBConstants.Config#INSTANCE_ID} configuration parameter which
38   * should always have a value.
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  public class InstanceIdFactoryBean extends AbstractFactoryBean<String> {
44  
45  	private String instanceId;
46  	
47  	public void setInstanceId(String instanceId) {
48  		this.instanceId = instanceId;
49  	}
50  	
51  	@Override
52  	protected String createInstance() throws Exception {
53  		if (StringUtils.isBlank(instanceId)) {
54  			this.instanceId = loadInstanceIdFromConfig();
55  		}
56  		if (StringUtils.isBlank(instanceId)) {
57  			String applicationId = CoreConfigHelper.getApplicationId();
58  			String ipNumber = RiceUtilities.getIpNumber();
59  			this.instanceId = applicationId + "-" + ipNumber;
60  		}
61          ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.INSTANCE_ID, this.instanceId);
62  		return this.instanceId;
63  	}
64  	
65  	protected String loadInstanceIdFromConfig() {
66  		return ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.INSTANCE_ID);
67  	}
68  
69  	@Override
70  	public Class<String> getObjectType() {
71  		return String.class;
72  	}
73  
74  	
75  	
76  }