001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.clustering.wadi;
017:
018: import java.net.URI;
019: import java.util.Collections;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.geronimo.clustering.LocalNode;
024: import org.apache.geronimo.clustering.Node;
025: import org.apache.geronimo.gbean.GBeanInfo;
026: import org.apache.geronimo.gbean.GBeanInfoBuilder;
027: import org.apache.geronimo.gbean.GBeanLifecycle;
028: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
029: import org.codehaus.wadi.core.reflect.base.DeclaredMemberFilter;
030: import org.codehaus.wadi.core.reflect.jdk.JDKClassIndexerRegistry;
031: import org.codehaus.wadi.core.util.SimpleStreamer;
032: import org.codehaus.wadi.group.Dispatcher;
033: import org.codehaus.wadi.group.DispatcherRegistry;
034: import org.codehaus.wadi.group.MessageExchangeException;
035: import org.codehaus.wadi.group.StaticDispatcherRegistry;
036: import org.codehaus.wadi.servicespace.admin.AdminServiceSpace;
037: import org.codehaus.wadi.tribes.TribesDispatcher;
038: import org.codehaus.wadi.web.impl.URIEndPoint;
039:
040: /**
041: *
042: * @version $Rev$ $Date$
043: */
044: public class TribesDispatcherHolder implements GBeanLifecycle,
045: DispatcherHolder {
046: private static final Log log = LogFactory
047: .getLog(TribesDispatcherHolder.class);
048:
049: private final URI endPointURI;
050: private final String clusterName;
051: private final LocalNode node;
052: private final ClassLoader cl;
053: private final DispatcherRegistry dispatcherRegistry;
054:
055: private TribesDispatcher dispatcher;
056: private AdminServiceSpace adminServiceSpace;
057:
058: public TribesDispatcherHolder(ClassLoader cl, URI endPointURI,
059: String clusterName, LocalNode node) {
060: if (null == endPointURI) {
061: throw new IllegalArgumentException(
062: "endPointURI is required");
063: } else if (null == clusterName) {
064: throw new IllegalArgumentException(
065: "clusterName is required");
066: } else if (null == node) {
067: throw new IllegalArgumentException("node is required");
068: } else if (null == cl) {
069: throw new IllegalArgumentException("cl is required");
070: }
071: this .endPointURI = endPointURI;
072: this .clusterName = clusterName;
073: this .node = node;
074: this .cl = cl;
075:
076: dispatcherRegistry = new StaticDispatcherRegistry();
077: }
078:
079: public void doStart() throws Exception {
080: dispatcher = new TribesDispatcher(clusterName, node.getName(),
081: new URIEndPoint(endPointURI), Collections.EMPTY_SET);
082: dispatcher.start();
083:
084: adminServiceSpace = new AdminServiceSpace(
085: dispatcher,
086: new JDKClassIndexerRegistry(new DeclaredMemberFilter()),
087: new SimpleStreamer(cl));
088:
089: registerCustomAdminServices();
090:
091: adminServiceSpace.start();
092:
093: dispatcherRegistry.register(dispatcher);
094: }
095:
096: public void doStop() throws Exception {
097: adminServiceSpace.stop();
098: dispatcherRegistry.unregister(dispatcher);
099: dispatcher.stop();
100: }
101:
102: public void doFail() {
103: if (null != adminServiceSpace) {
104: try {
105: adminServiceSpace.stop();
106: } catch (Exception e) {
107: log.error("see nested", e);
108: }
109: }
110:
111: if (null != dispatcher) {
112: dispatcherRegistry.unregister(dispatcher);
113: try {
114: dispatcher.stop();
115: } catch (MessageExchangeException e) {
116: log.error("see nested", e);
117: }
118: }
119: }
120:
121: public Dispatcher getDispatcher() {
122: return dispatcher;
123: }
124:
125: public Node getNode() {
126: return node;
127: }
128:
129: protected void registerCustomAdminServices() {
130: NodeServiceHelper nodeServiceHelper = new NodeServiceHelper(
131: adminServiceSpace);
132: nodeServiceHelper
133: .registerNodeService(new BasicNodeService(node));
134: }
135:
136: public static final GBeanInfo GBEAN_INFO;
137:
138: public static final String GBEAN_ATTR_END_POINT_URI = "endPointURI";
139: public static final String GBEAN_ATTR_CLUSTER_NAME = "clusterName";
140: public static final String GBEAN_ATTR_CLUSTER_URI = "clusterUri";
141:
142: public static final String GBEAN_REF_NODE = "Node";
143:
144: static {
145: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
146: TribesDispatcherHolder.class,
147: NameFactory.GERONIMO_SERVICE);
148:
149: infoBuilder.addAttribute("classLoader", ClassLoader.class,
150: false);
151: infoBuilder.addAttribute(GBEAN_ATTR_END_POINT_URI, URI.class,
152: true);
153: infoBuilder.addAttribute(GBEAN_ATTR_CLUSTER_NAME, String.class,
154: true);
155:
156: infoBuilder.addReference(GBEAN_REF_NODE, LocalNode.class,
157: NameFactory.GERONIMO_SERVICE);
158:
159: infoBuilder.addInterface(DispatcherHolder.class);
160:
161: infoBuilder.setConstructor(new String[] { "classLoader",
162: GBEAN_ATTR_END_POINT_URI, GBEAN_ATTR_CLUSTER_NAME,
163: GBEAN_REF_NODE });
164:
165: GBEAN_INFO = infoBuilder.getBeanInfo();
166: }
167:
168: public static GBeanInfo getGBeanInfo() {
169: return GBEAN_INFO;
170: }
171: }
|