/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.muse.ws.notification.impl;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.notification.Filter;
import org.apache.muse.ws.notification.WsnConstants;
import org.apache.muse.ws.notification.faults.InvalidFilterFault;
import org.apache.muse.ws.resource.basefaults.BaseFault;
/**
*
* FilterFactory is a component for creating WS-Notification filters from XML
* fragments that are compliant with the WS-N v1.3 schema. It allows for the
* creation of topic, message, and properties filters. It is a stateless parser,
* so it is represented as a singleton.
*
* @author Dan Jemiolo (danj)
*
*/
public class FilterFactory
{
//
// Used to look up all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(FilterFactory.class);
private static final FilterFactory _SINGLETON = new FilterFactory();
private Collection _handlers = new LinkedList();
private FilterFactory()
{
_handlers.add(new TopicFilterHandler());
_handlers.add(new ProducerPropertiesFilterHandler());
_handlers.add(new MessagePatternFilterHandler());
}
public void addHandler(FilterFactoryHandler handler)
{
_handlers.add(handler);
}
/**
*
* @return The singleton instance of the factory component.
*
*/
public static FilterFactory getInstance()
{
return _SINGLETON;
}
/**
*
*
* @param xml
* An XML fragment representing the WS-N filter, or null.
*
* @return A filter based on topics, message patterns, or property values.
* If the first parameter was null, the method returns an instance
* of PublishAllMessagesFilter.
*
*/
public Filter newInstance(Element xml)
throws BaseFault
{
//
// no filter? publish all messages
//
if (xml == null)
return PublishAllMessagesFilter.getInstance();
Element[] children = XmlUtils.getAllElements(xml);
//
// <Filter/> element, but not child elements? publish all messages
//
if (children.length == 0)
return PublishAllMessagesFilter.getInstance();
FilterCollection filters = new FilterCollection();
for (int n = 0; n < children.length; ++n)
{
QName name = XmlUtils.getElementQName(children[n]);
String dialect = children[n].getAttribute(WsnConstants.DIALECT);
Filter filter = null;
//
// iterate over all the handlers to see which one can handle our XML
//
Iterator i = _handlers.iterator();
while (i.hasNext() && filter == null)
{
FilterFactoryHandler handler = (FilterFactoryHandler)i.next();
if (handler.accepts(name, dialect))
filter = handler.newInstance(children[n]);
}
//
// no match for known filter types - try and tell the user what
// was found and why it's not valid
//
if (filter == null)
{
Object[] filler = { XmlUtils.getFirstElement(xml) };
throw new InvalidFilterFault(_MESSAGES.get("InvalidFilterType", filler));
}
filters.addFilter(filter);
}
return filters;
}
}