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:$ $Date:$
037: */
038: public class BasicClusterConfigurationController implements
039: GBeanLifecycle, ClusterConfigurationController {
040: private static final Log log = LogFactory
041: .getLog(BasicClusterConfigurationController.class);
042:
043: private final ClusterInfo clusterInfo;
044: private final String nodeName;
045: private final Artifact artifact;
046: private boolean startConfigurationUponStart;
047: private boolean ignoreStartConfigurationFailureUponStart;
048:
049: public BasicClusterConfigurationController(ClusterInfo clusterInfo,
050: String nodeName, Artifact artifact,
051: boolean startConfigurationUponStart,
052: boolean ignoreStartConfigurationFailureUponStart) {
053: if (null == clusterInfo) {
054: throw new IllegalArgumentException(
055: "clusterInfo is required");
056: } else if (null == nodeName) {
057: throw new IllegalArgumentException("nodeName is required");
058: } else if (null == artifact) {
059: throw new IllegalArgumentException("artifact is required");
060: }
061: this .clusterInfo = clusterInfo;
062: this .nodeName = nodeName;
063: this .artifact = artifact;
064: this .startConfigurationUponStart = startConfigurationUponStart;
065: this .ignoreStartConfigurationFailureUponStart = ignoreStartConfigurationFailureUponStart;
066: }
067:
068: public void doStart() throws Exception {
069: if (startConfigurationUponStart) {
070: try {
071: startConfiguration();
072: } catch (Exception e) {
073: if (ignoreStartConfigurationFailureUponStart) {
074: log.info("Exception while starting configuration ["
075: + artifact + "] on [" + nodeName
076: + "]. Ignoring.", e);
077: } else {
078: log.error(
079: "Exception while starting configuration ["
080: + artifact + "] on [" + nodeName
081: + "].", e);
082: throw e;
083: }
084: }
085: }
086: }
087:
088: public void doFail() {
089: try {
090: stopConfiguration();
091: } catch (Exception e) {
092: log.error("Exception while stopping configuration ["
093: + artifact + "] on [" + nodeName + "].", e);
094: }
095: }
096:
097: public void doStop() throws Exception {
098: try {
099: stopConfiguration();
100: } catch (Exception e) {
101: log.error("Exception while stopping configuration ["
102: + artifact + "] on [" + nodeName + "].", e);
103: throw e;
104: }
105: }
106:
107: public void startConfiguration() throws Exception {
108: for (NodeInfo nodeInfo : clusterInfo.getNodeInfos()) {
109: if (!nodeInfo.getName().equals(nodeName)) {
110: continue;
111: }
112:
113: Kernel kernel = nodeInfo.newKernel();
114:
115: ConfigurationManager configurationManager = newConfigurationManager(kernel);
116: if (!configurationManager.isLoaded(artifact)) {
117: configurationManager.loadConfiguration(artifact);
118: }
119: configurationManager.startConfiguration(artifact);
120: }
121: }
122:
123: public void stopConfiguration() throws Exception {
124: for (NodeInfo nodeInfo : clusterInfo.getNodeInfos()) {
125: if (!nodeInfo.getName().equals(nodeName)) {
126: continue;
127: }
128:
129: Kernel kernel = nodeInfo.newKernel();
130:
131: ConfigurationManager configurationManager = newConfigurationManager(kernel);
132: configurationManager.stopConfiguration(artifact);
133: }
134: }
135:
136: protected ConfigurationManager newConfigurationManager(Kernel kernel) {
137: return ConfigurationUtil.getConfigurationManager(kernel);
138: }
139:
140: public static final GBeanInfo GBEAN_INFO;
141:
142: public static final String GBEAN_J2EE_TYPE = "ClusterConfigurationController";
143: public static final String GBEAN_ATTR_NODE_NAME = "nodeName";
144: public static final String GBEAN_ATTR_ARTIFACT = "artifact";
145: public static final String GBEAN_ATTR_START_CONF_UPON_START = "startConfigurationUponStart";
146: public static final String GBEAN_ATTR_IGNORE_START_CONF_FAIL_UPON_START = "ignoreStartConfigurationFailureUponStart";
147: public static final String GBEAN_REF_CLUSTER_INFO = "ClusterInfo";
148:
149: static {
150: GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(
151: BasicClusterConfigurationController.class,
152: GBEAN_J2EE_TYPE);
153:
154: builder.addAttribute(GBEAN_ATTR_NODE_NAME, String.class, true);
155: builder.addAttribute(GBEAN_ATTR_ARTIFACT, Artifact.class, true);
156: builder.addAttribute(GBEAN_ATTR_START_CONF_UPON_START,
157: boolean.class, true);
158: builder.addAttribute(
159: GBEAN_ATTR_IGNORE_START_CONF_FAIL_UPON_START,
160: boolean.class, true);
161:
162: builder.addReference(GBEAN_REF_CLUSTER_INFO, ClusterInfo.class);
163:
164: builder.addInterface(ClusterConfigurationController.class);
165:
166: builder.setConstructor(new String[] { GBEAN_REF_CLUSTER_INFO,
167: GBEAN_ATTR_NODE_NAME, GBEAN_ATTR_ARTIFACT,
168: GBEAN_ATTR_START_CONF_UPON_START,
169: GBEAN_ATTR_IGNORE_START_CONF_FAIL_UPON_START });
170:
171: GBEAN_INFO = builder.getBeanInfo();
172: }
173:
174: public static GBeanInfo getGBeanInfo() {
175: return GBEAN_INFO;
176: }
177:
178: }
|