/*=============================================================================*
* 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.eventing;
import edu.berkeley.cs.db.yfilter.filter.EXfilterBasic;
import edu.berkeley.cs.db.yfilter.filter.SystemGlobals;
import edu.berkeley.cs.db.yfilterplus.queryparser.Query;
import edu.berkeley.cs.db.yfilterplus.queryparser.XPQuery;
import edu.berkeley.cs.db.yfilterplus.xmltree.XMLTree;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.addressing.EndpointReference;
import org.apache.ws.notification.base.CustomBaseNotificationConstants;
import org.apache.ws.notification.base.v2004_06.impl.WsnNamespaceVersionHolderImpl;
import org.apache.ws.pubsub.Filter;
import org.apache.ws.resource.JndiConstants;
import org.apache.ws.resource.NamespaceVersionHolder;
import org.apache.ws.resource.ResourceException;
import org.apache.ws.resource.ResourceUnknownException;
import org.apache.ws.resource.impl.AbstractResourceHome;
import javax.xml.namespace.QName;
import java.util.Calendar;
import java.util.Map;
/**
* DOCUMENT_ME
*
* @version $Revision: 1.8 $
* @author $author$
*/
public class SubscriptionHome
extends AbstractResourceHome
{
private static final Log LOG = LogFactory.getLog( SubscriptionHome.class.getName( ) );
/** The WSDL Port name associated with the resource. This is useful for building EPR's. **/
public static final String PORT_NAME = "SubscriptionManager";
/** DOCUMENT_ME */
public static final String HOME_LOCATION =
JndiConstants.CONTEXT_NAME_SERVICES + "/" + CustomBaseNotificationConstants.SERVICE_NAME.getLocalPart( )
+ "/" + JndiConstants.ATOMIC_NAME_HOME;
private static final NamespaceVersionHolder SPEC_NAMESPACE_SET = new WsnNamespaceVersionHolderImpl( );
/**
* Map containing all FilesystemResource instances - this map <em>must</em> be static for
* compatibility with certain JNDI providers.
*/
private static Map s_resources;
/** DOCUMENT_ME */
public int c = 0;
// private java.util.HashMap subs;
private java.util.HashMap queriesBack;
private String[] queries;
/** DOCUMENT_ME */
EXfilterBasic yfilter;
/** Creates a new instance of ResourceHome */
public SubscriptionHome( )
{
yfilter = new EXfilterBasic( );
// subs= new java.util.HashMap();
queriesBack = new java.util.HashMap( );
queries = new String[1000000];
LOG.info( "ResourceHome" + this.toString( ) );
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public NamespaceVersionHolder getNamespaceSet( )
{
return SPEC_NAMESPACE_SET;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public QName getPortType( )
{
return CustomBaseNotificationConstants.PORT_TYPE;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public QName getResourceKeyNameQName( )
{
return CustomBaseNotificationConstants.RESOURCE_KEY_NAME;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public QName getServiceName( )
{
return CustomBaseNotificationConstants.SERVICE_NAME;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public String getServicePortName( )
{
return PORT_NAME;
}
/**
* DOCUMENT_ME
*
* @param filter DOCUMENT_ME
* @param expires DOCUMENT_ME
* @param delivery DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public Subscription create( Filter filter,
Calendar expires,
java.net.URI delivery )
{
Subscription rs = new Subscription( filter, expires, delivery );
rs.setResourceHome( this );
EndpointReference subscriptionEPR = getEndpointReference( rs.getID( ) );
rs.setEpr( subscriptionEPR );
addSubscription( rs );
return rs;
}
/**
* DOCUMENT_ME
*
* @param message DOCUMENT_ME
*/
public void notify( Object message )
{
//first check dialect
try
{
org.apache.axis.message.SOAPEnvelope env = (org.apache.axis.message.SOAPEnvelope) message;
String xml =
org.apache.axis.utils.XMLUtils.ElementToString( env.getBody( ) );
LOG.info( "Notify: " );
LOG.info( "--" + xml + "--" );
//copy
//first parse and drive YFilter,
XMLTree tree = new XMLTree( new java.io.StringReader( xml ) );
yfilter.setEventSequence( tree.getEvents( ) );
yfilter.startParsing( );
// print the matched queries //
if ( SystemGlobals.hasQueries )
{
yfilter.printQueryResults( System.out );
}
else
{
LOG.info( "no match" );
}
//get matches
java.util.Iterator it = yfilter.getMatchedQueries( ).iterator( );
while ( it.hasNext( ) )
{
Integer qid = (Integer) it.next( );
String sid = queries[qid.intValue( )];
Subscription s = (Subscription) super.find( sid );
//notify matches
LOG.info( "notify : " + sid );
s.getNotificationConsumer( ).notify( s,
env.getBody( ) );
}
yfilter.clear( );
}
catch ( Exception e )
{
e.printStackTrace( );
}
}
/*
public Resource find( ResourceKey key ) throws ResourceException {
LOG.info("find "+ key.getValue()+" have "+subs.size());
if(subs.containsKey(key.getValue())){
RemoteSubscription s = (RemoteSubscription)subs.get(key.getValue());
return s;
}else
throw new ResourceUnknownException();
}
*/
/**
* Removes a resource.
*
* @throws ResourceUnknownException if no resource exists with the given key
* @throws ResourceException if any other error occurs.
*/
public void remove( Object id )
throws ResourceException
{
LOG.info( "removeSubscription : " + id );
Query q = (Query) queriesBack.get( id );
yfilter.deleteQuery( q,
q.getQueryId( ) );
//TODO remove from Hashmap
super.remove( id );
}
/**
* Returns a map of all FilesystemResource instances. Used by the {@link AbstractResourceHome}
* superclass.
*/
protected final synchronized Map getResourceMap( )
{
if ( s_resources == null )
{
s_resources = AbstractResourceHome.createResourceMap( m_resourceIsPersistent );
}
return s_resources;
}
/*
*-------------------------------------------------------------------
*/
private void addSubscription( Subscription s )
{
try
{
c++;
Query query = XPQuery.parseQuery( (String) s.getFilter( ).getExpression( ), c );
LOG.info( "addSubscription " + s.getID( ) + " query :" + query );
yfilter.addQuery( query );
add( s );
queriesBack.put( s.getID( ),
query );
queries[c] = (String) s.getID( );
}
catch ( Exception e )
{
e.printStackTrace( );
}
}
}