001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.farm.deployment; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 import org.apache.geronimo.farm.config.ClusterInfo; 025 import org.apache.geronimo.farm.config.NodeInfo; 026 import org.apache.geronimo.gbean.GBeanInfo; 027 import org.apache.geronimo.gbean.GBeanInfoBuilder; 028 import org.apache.geronimo.gbean.GBeanLifecycle; 029 import org.apache.geronimo.kernel.Kernel; 030 import org.apache.geronimo.kernel.config.ConfigurationManager; 031 import org.apache.geronimo.kernel.config.ConfigurationUtil; 032 import org.apache.geronimo.kernel.repository.Artifact; 033 034 /** 035 * 036 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 037 */ 038 public class BasicClusterConfigurationController implements GBeanLifecycle, ClusterConfigurationController { 039 private static final Log log = LogFactory.getLog(BasicClusterConfigurationController.class); 040 041 private final ClusterInfo clusterInfo; 042 private final String nodeName; 043 private final Artifact artifact; 044 private boolean startConfigurationUponStart; 045 private boolean ignoreStartConfigurationFailureUponStart; 046 047 public BasicClusterConfigurationController(ClusterInfo clusterInfo, 048 String nodeName, 049 Artifact artifact, 050 boolean startConfigurationUponStart, 051 boolean ignoreStartConfigurationFailureUponStart) { 052 if (null == clusterInfo) { 053 throw new IllegalArgumentException("clusterInfo is required"); 054 } else if (null == nodeName) { 055 throw new IllegalArgumentException("nodeName is required"); 056 } else if (null == artifact) { 057 throw new IllegalArgumentException("artifact is required"); 058 } 059 this.clusterInfo = clusterInfo; 060 this.nodeName = nodeName; 061 this.artifact = artifact; 062 this.startConfigurationUponStart = startConfigurationUponStart; 063 this.ignoreStartConfigurationFailureUponStart = ignoreStartConfigurationFailureUponStart; 064 } 065 066 public void doStart() throws Exception { 067 if (startConfigurationUponStart) { 068 try { 069 startConfiguration(); 070 } catch (Exception e) { 071 if (ignoreStartConfigurationFailureUponStart) { 072 log.info("Exception while starting configuration [" + artifact + "] on [" + nodeName 073 + "]. Ignoring.", e); 074 } else { 075 log.error("Exception while starting configuration [" + artifact + "] on [" + nodeName + "].", e); 076 throw e; 077 } 078 } 079 } 080 } 081 082 public void doFail() { 083 try { 084 stopConfiguration(); 085 } catch (Exception e) { 086 log.error("Exception while stopping configuration [" + artifact + "] on [" + nodeName + "].", e); 087 } 088 } 089 090 public void doStop() throws Exception { 091 try { 092 stopConfiguration(); 093 } catch (Exception e) { 094 log.error("Exception while stopping configuration [" + artifact + "] on [" + nodeName + "].", e); 095 throw e; 096 } 097 } 098 099 public void startConfiguration() throws Exception { 100 for (NodeInfo nodeInfo : clusterInfo.getNodeInfos()) { 101 if (!nodeInfo.getName().equals(nodeName)) { 102 continue; 103 } 104 105 Kernel kernel = nodeInfo.newKernel(); 106 107 ConfigurationManager configurationManager = newConfigurationManager(kernel); 108 if (!configurationManager.isLoaded(artifact)) { 109 configurationManager.loadConfiguration(artifact); 110 } 111 configurationManager.startConfiguration(artifact); 112 } 113 } 114 115 public void stopConfiguration() throws Exception { 116 for (NodeInfo nodeInfo : clusterInfo.getNodeInfos()) { 117 if (!nodeInfo.getName().equals(nodeName)) { 118 continue; 119 } 120 121 Kernel kernel = nodeInfo.newKernel(); 122 123 ConfigurationManager configurationManager = newConfigurationManager(kernel); 124 configurationManager.stopConfiguration(artifact); 125 } 126 } 127 128 protected ConfigurationManager newConfigurationManager(Kernel kernel) { 129 return ConfigurationUtil.getConfigurationManager(kernel); 130 } 131 132 public static final GBeanInfo GBEAN_INFO; 133 134 public static final String GBEAN_J2EE_TYPE = "ClusterConfigurationController"; 135 public static final String GBEAN_ATTR_NODE_NAME = "nodeName"; 136 public static final String GBEAN_ATTR_ARTIFACT = "artifact"; 137 public static final String GBEAN_ATTR_START_CONF_UPON_START= "startConfigurationUponStart"; 138 public static final String GBEAN_ATTR_IGNORE_START_CONF_FAIL_UPON_START= "ignoreStartConfigurationFailureUponStart"; 139 public static final String GBEAN_REF_CLUSTER_INFO = "ClusterInfo"; 140 141 static { 142 GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(BasicClusterConfigurationController.class, GBEAN_J2EE_TYPE); 143 144 builder.addAttribute(GBEAN_ATTR_NODE_NAME, String.class, true); 145 builder.addAttribute(GBEAN_ATTR_ARTIFACT, Artifact.class, true); 146 builder.addAttribute(GBEAN_ATTR_START_CONF_UPON_START, boolean.class, true); 147 builder.addAttribute(GBEAN_ATTR_IGNORE_START_CONF_FAIL_UPON_START, boolean.class, true); 148 149 builder.addReference(GBEAN_REF_CLUSTER_INFO, ClusterInfo.class); 150 151 builder.addInterface(ClusterConfigurationController.class); 152 153 builder.setConstructor(new String[] {GBEAN_REF_CLUSTER_INFO, 154 GBEAN_ATTR_NODE_NAME, 155 GBEAN_ATTR_ARTIFACT, 156 GBEAN_ATTR_START_CONF_UPON_START, 157 GBEAN_ATTR_IGNORE_START_CONF_FAIL_UPON_START}); 158 159 GBEAN_INFO = builder.getBeanInfo(); 160 } 161 162 public static GBeanInfo getGBeanInfo() { 163 return GBEAN_INFO; 164 } 165 166 }