/*=============================================================================*
* 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.topics.util;
import org.apache.ws.notification.topics.ResourcePropertyValueChangeTopic;
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.impl.ResourcePropertyValueChangeTopicImpl;
import org.apache.ws.notification.topics.impl.ResourceTerminationTopicImpl;
import org.apache.ws.notification.topics.impl.TopicSpaceImpl;
import org.apache.ws.resource.Resource;
import org.apache.ws.resource.lifetime.ResourceTerminationListener;
import org.apache.ws.resource.properties.NamespaceVersionHolder;
import org.apache.ws.resource.properties.ResourceProperty;
import org.apache.ws.resource.properties.ResourcePropertySet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* A utility class containing static methods used as helpers for working with WSN Topics.
*
* @author Sal Campana
*/
public class TopicUtils
{
/**
* Adds a topic for the specified property to the specified topic set.
*
* @param prop
* @param topicSpaceSet
* @return
*/
public static Topic addResourcePropertyValueChangeTopic( ResourceProperty prop,
TopicSpaceSet topicSpaceSet )
throws Exception
{
if ( prop.getMetaData( ).isReadOnly( ) )
{
throw new IllegalArgumentException( "Property " + prop.getMetaData( ).getName( ) + " is readonly!" );
}
ResourcePropertyValueChangeTopic valueChangeTopic = new ResourcePropertyValueChangeTopicImpl( prop );
prop.addChangeListener( valueChangeTopic );
String topicNsURI = prop.getMetaData( ).getName( ).getNamespaceURI( );
TopicSpace topicSpace = topicSpaceSet.getTopicSpace( topicNsURI );
if ( topicSpace == null )
{
topicSpace = new TopicSpaceImpl( topicNsURI );
topicSpaceSet.addTopicSpace( topicSpace );
}
topicSpace.addTopic( valueChangeTopic );
return valueChangeTopic;
}
/**
* Adds topics for all non-readonly properties from the specified property set to the specified topic set.
*
* @param propSet
* @param topicSpaceSet
* @return
*/
public static Topic[] addResourcePropertyValueChangeTopics( ResourcePropertySet propSet,
TopicSpaceSet topicSpaceSet )
throws Exception
{
List topicList = new ArrayList( );
Iterator propIter = propSet.iterator( );
while ( propIter.hasNext( ) )
{
ResourceProperty prop = (ResourceProperty) propIter.next( );
if ( !prop.getMetaData( ).isReadOnly( ) )
{
Topic topic = addResourcePropertyValueChangeTopic( prop, topicSpaceSet );
topicList.add( topic );
}
}
return (Topic[]) topicList.toArray( new Topic[0] );
}
/**
* Adds the topic for ResourceTermination....there should be only one of these!
*
* @param topicSpaceSet
* @return
*/
public static Topic addResourceTerminationTopic( TopicSpaceSet topicSpaceSet,
Resource resource,
NamespaceVersionHolder namespaces )
throws Exception
{
//there can be only 1 !
String namespace = namespaces.getLifetimeXsdNamespace( );
TopicSpace topicSpace = topicSpaceSet.getTopicSpace( namespace );
if ( topicSpace == null )
{
topicSpace = new TopicSpaceImpl( namespace );
topicSpaceSet.addTopicSpace( topicSpace );
}
Topic resourceTerminationTopic = null;
while ( topicSpace.topicIterator( ).hasNext( ) )
{
Topic topic = (Topic) topicSpace.topicIterator( ).next( );
if ( topic.getName( ).equals( ResourceTerminationTopicImpl.TOPIC_NAME ) )
{
resourceTerminationTopic = topic;
break;
}
}
if ( resourceTerminationTopic == null )
{
resourceTerminationTopic = new ResourceTerminationTopicImpl( namespaces );
resource.addTerminationListener( (ResourceTerminationListener) resourceTerminationTopic );
topicSpace.addTopic( resourceTerminationTopic );
}
return resourceTerminationTopic;
}
}