/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* Licensed 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.ws.notification.base;
import org.apache.ws.notification.base.v2004_06.porttype.NotificationProducerPortType;
import org.apache.ws.notification.topics.Topic;
import org.apache.ws.notification.topics.TopicSpace;
import org.apache.ws.notification.topics.TopicSpaceSet;
import org.apache.ws.notification.topics.v2004_06.TopicsConstants;
import org.apache.ws.resource.properties.ResourceProperty;
import org.apache.ws.resource.properties.ResourcePropertyCallback;
import org.apache.ws.resource.properties.impl.CallbackFailedException;
import org.apache.ws.util.XmlBeanUtils;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicDocument;
import org.oasisOpen.docs.wsn.x2004.x06.wsnWSBaseNotification12Draft01.TopicExpressionType;
import javax.xml.namespace.QName;
import java.util.Iterator;
/**
* A callback for the Topic resource property from the WS-BaseNotification
* NotificationProducer portType.
*/
public class TopicResourcePropertyCallback
implements ResourcePropertyCallback
{
private TopicSpaceSet m_topicSet;
/**
* Creates a new {@link TopicResourcePropertyCallback} for the specified topic set.
*
* @param topicSet the producer's topic set
*/
public TopicResourcePropertyCallback( TopicSpaceSet topicSet )
{
m_topicSet = topicSet;
}
/**
* Refreshes the value of the wsnt:Topic resource property so that it
* reflects the topics contained in the producer resource's topic set.
*
* @param prop the wsnt:Topic resource property
*
* @return the wsnt:Topic resource property
*
* @throws org.apache.ws.resource.properties.impl.CallbackFailedException
*/
public ResourceProperty refreshProperty( ResourceProperty prop )
throws CallbackFailedException
{
QName propName = prop.getMetaData( ).getName( );
if ( !propName.equals( NotificationProducerPortType.PROP_QNAME_TOPIC ) )
{
throw new IllegalArgumentException( "Unsupported property: " + propName );
}
prop.clear( );
TopicSpace[] topicSpaces = m_topicSet.getTopicSpaces( );
for ( int i = 0; i < topicSpaces.length; i++ )
{
TopicSpace topicSpace = topicSpaces[i];
String targetNamespace = topicSpace.getTargetNamespace( );
Iterator iterator = topicSpace.topicIterator( );
while ( iterator.hasNext( ) )
{
Topic topic = (Topic) iterator.next( );
String topicName = topic.getName( );
QName rootTopicQName = new QName( targetNamespace, topicName );
if ( topic.isVisible( ) )
{
addSimpleTopicExpression( prop, rootTopicQName );
}
Iterator subTopics = topic.topicIterator( );
while ( subTopics.hasNext( ) )
{
Topic subTopic = (Topic) subTopics.next( );
addConcreteTopicExpressions( prop, rootTopicQName, subTopic );
}
}
}
return prop;
}
/**
* Recursively adds Concrete topic expressions to the Topic resource property.
*
* @param topicProp The ResourceProperty associated with the Topic being added to.
* @param rootTopicName The QName of the parent topic.
* @param subTopic The current topic being added.
*/
private static void addConcreteTopicExpressions( ResourceProperty topicProp,
QName rootTopicName,
Topic subTopic )
{
TopicDocument topicDocument = TopicDocument.Factory.newInstance( );
String topicName = subTopic.getName( );
QName concreteTopicName =
new QName( rootTopicName.getNamespaceURI( ), rootTopicName.getLocalPart( ) + "/" + topicName );
if ( subTopic.isVisible( ) )
{
TopicExpressionType topicExpressionType = topicDocument.addNewTopic( );
topicExpressionType.setDialect( TopicsConstants.TOPIC_EXPR_DIALECT_CONCRETE );
XmlBeanUtils.setValueAsQName( topicExpressionType, concreteTopicName );
topicProp.add( topicDocument );
}
Iterator iterator = subTopic.topicIterator( );
while ( iterator.hasNext( ) )
{
Topic topic = (Topic) iterator.next( );
addConcreteTopicExpressions( topicProp, concreteTopicName, topic );
}
}
private static void addSimpleTopicExpression( ResourceProperty topicProp,
QName rootTopicName )
{
TopicDocument topicDocument = TopicDocument.Factory.newInstance( );
TopicExpressionType topicExpressionType = topicDocument.addNewTopic( );
topicExpressionType.setDialect( TopicsConstants.TOPIC_EXPR_DIALECT_SIMPLE );
XmlBeanUtils.setValueAsQName( topicExpressionType, rootTopicName );
topicProp.add( topicDocument );
}
}