001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.ksb.impl.bus;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.rice.core.api.config.CoreConfigHelper;
020 import org.kuali.rice.core.api.config.property.ConfigContext;
021 import org.kuali.rice.core.api.util.RiceUtilities;
022 import org.kuali.rice.ksb.util.KSBConstants;
023 import org.springframework.beans.factory.config.AbstractFactoryBean;
024
025 /**
026 * A factory bean which is used to produce an instance id for the service bus.
027 *
028 * <p>The value for the instance id is determined in the following order of preferences:
029 *
030 * <ol>
031 * <li>If {@code instanceId} is set on this factory bean, that value will be used.</li>
032 * <li>If {@link KSBConstants.Config#INSTANCE_ID} has been set in the configuration context, that value will be used.</li>
033 * <li>If none of the above, the instance id will be a combination of this application's namespace plus ip address.</li>
034 * </ol>
035 *
036 * <p>In the case that the instance id is generated, the application id will be pulled
037 * from the configuration context using the {@link KSBConstants.Config#INSTANCE_ID} configuration parameter which
038 * should always have a value.
039 *
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 *
042 */
043 public class InstanceIdFactoryBean extends AbstractFactoryBean<String> {
044
045 private String instanceId;
046
047 public void setInstanceId(String instanceId) {
048 this.instanceId = instanceId;
049 }
050
051 @Override
052 protected String createInstance() throws Exception {
053 if (StringUtils.isBlank(instanceId)) {
054 this.instanceId = loadInstanceIdFromConfig();
055 }
056 if (StringUtils.isBlank(instanceId)) {
057 String applicationId = CoreConfigHelper.getApplicationId();
058 String ipNumber = RiceUtilities.getIpNumber();
059 this.instanceId = applicationId + "-" + ipNumber;
060 }
061 ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.INSTANCE_ID, this.instanceId);
062 return this.instanceId;
063 }
064
065 protected String loadInstanceIdFromConfig() {
066 return ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.INSTANCE_ID);
067 }
068
069 @Override
070 public Class<String> getObjectType() {
071 return String.class;
072 }
073
074
075
076 }