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.synapse.config.xml.endpoints;
021:
022: import org.apache.synapse.endpoints.Endpoint;
023: import org.apache.synapse.endpoints.FailoverEndpoint;
024: import org.apache.synapse.SynapseException;
025: import org.apache.synapse.SynapseConstants;
026: import org.apache.axiom.om.OMElement;
027: import org.apache.axiom.om.OMNode;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import javax.xml.namespace.QName;
032: import java.util.ArrayList;
033: import java.util.Iterator;
034:
035: /**
036: * Creates FailoverEndpoint using a XML configuration.
037: *
038: * <endpoint [name="name"]>
039: * <failover>
040: * <endpoint>+
041: * </failover>
042: * </endpoint>
043: */
044: public class FailoverEndpointFactory implements EndpointFactory {
045:
046: private static Log log = LogFactory
047: .getLog(FailoverEndpointFactory.class);
048:
049: private static FailoverEndpointFactory instance = new FailoverEndpointFactory();
050:
051: private FailoverEndpointFactory() {
052: }
053:
054: public static FailoverEndpointFactory getInstance() {
055: return instance;
056: }
057:
058: public Endpoint createEndpoint(OMElement epConfig,
059: boolean anonymousEndpoint) {
060:
061: OMElement failoverElement = epConfig
062: .getFirstChildWithName(new QName(
063: SynapseConstants.SYNAPSE_NAMESPACE, "failover"));
064: if (failoverElement != null) {
065:
066: FailoverEndpoint failoverEndpoint = new FailoverEndpoint();
067:
068: // set endpoint name
069: String name = epConfig.getAttributeValue(new QName("name"));
070: if (name != null) {
071: failoverEndpoint.setName(name);
072: }
073:
074: // set endpoints
075: ArrayList endpoints = getEndpoints(failoverElement,
076: failoverEndpoint);
077: failoverEndpoint.setEndpoints(endpoints);
078:
079: return failoverEndpoint;
080: }
081:
082: return null;
083: }
084:
085: public Object getObjectFromOMNode(OMNode om) {
086: if (om instanceof OMElement) {
087: return createEndpoint((OMElement) om, false);
088: } else {
089: handleException("Invalid XML configuration for an Endpoint. OMElement expected");
090: }
091: return null;
092: }
093:
094: private ArrayList getEndpoints(OMElement failoverElement,
095: Endpoint parent) {
096:
097: ArrayList endpoints = new ArrayList();
098: Iterator iter = failoverElement
099: .getChildrenWithName(org.apache.synapse.config.xml.XMLConfigConstants.ENDPOINT_ELT);
100: while (iter.hasNext()) {
101:
102: OMElement endptElem = (OMElement) iter.next();
103:
104: EndpointFactory epFac = EndpointAbstractFactory
105: .getEndpointFactroy(endptElem);
106: Endpoint endpoint = epFac.createEndpoint(endptElem, true);
107: endpoint.setParentEndpoint(parent);
108: endpoints.add(endpoint);
109: }
110:
111: return endpoints;
112: }
113:
114: private static void handleException(String msg) {
115: log.error(msg);
116: throw new SynapseException(msg);
117: }
118:
119: private static void handleException(String msg, Exception e) {
120: log.error(msg, e);
121: throw new SynapseException(msg, e);
122: }
123: }
|