/*=============================================================================*
* Copyright 2005 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.muse.example.resourceadmin;
import org.apache.ws.addressing.EndpointReference;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.muse.example.application.ApplicationResource;
import org.apache.ws.muse.example.businessprocesstype.BusinessprocesstypeResource;
import org.apache.ws.muse.example.host.HostResource;
import org.apache.ws.muse.example.integrationserver.IntegrationserverResource;
import org.apache.ws.muse.example.resourceadmin.properties.DiscoveryDocument;
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.xmlbeans.XmlObject;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;
import javax.naming.InitialContext;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* @author Kinga Dziembowski
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DiscoveryCallback
implements ResourcePropertyCallback
{
private ResourceadminResource m_resource;
/**
* Creates a new {@link DiscoveryCallback} object.
*
* @param resource DOCUMENT_ME
*/
public DiscoveryCallback( ResourceadminResource resource )
{
m_resource = resource;
}
/* (non-Javadoc)
* @see org.apache.ws.resource.properties.ResourcePropertyCallback#refreshProperty(org.apache.ws.resource.properties.ResourceProperty)
*/
public ResourceProperty refreshProperty( ResourceProperty prop )
throws CallbackFailedException
{
try
{
DiscoveryDocument disc = DiscoveryDocument.Factory.newInstance( );
EndpointReference epr = null;
InitialContext ctx = new InitialContext( );
prop.clear( );
ResourceadminHome aHome = (ResourceadminHome) ( ctx.lookup( ResourceadminHome.HOME_LOCATION ) );
Map resources = aHome.getResources( );
Collection values = resources.values( );
Iterator iter = values.iterator( );
int size = values.size( );
// If there are the Resources registered to ResourceAdmin build array of their EPRs
if ( size > 0 )
{
EndpointReferenceType[] eprs = new EndpointReferenceType[size];
int i = 0;
while ( iter.hasNext( ) )
{
Object resource = iter.next( );
if ( resource instanceof ApplicationResource )
{
ApplicationResource appResource = (ApplicationResource) resource;
epr = appResource.getEndpointReference( );
}
else if ( resource instanceof HostResource )
{
HostResource hostResource = (HostResource) resource;
epr = hostResource.getEndpointReference( );
}
else if ( resource instanceof IntegrationserverResource )
{
IntegrationserverResource integrationResource = (IntegrationserverResource) resource;
epr = integrationResource.getEndpointReference( );
}
else if ( resource instanceof BusinessprocesstypeResource )
{
BusinessprocesstypeResource bpResource = (BusinessprocesstypeResource) resource;
epr = bpResource.getEndpointReference( );
}
else
{
throw new Exception( " Unknown resource type" );
}
if ( ( epr != null ) && epr instanceof XmlBeansEndpointReference )
{
XmlBeansEndpointReference eprType = (XmlBeansEndpointReference) epr;
// we need to make sure the returned EPR is in 2004 namespace
XmlObject obj = eprType.getXmlObject( org.apache.ws.addressing.v2004_08_10.AddressingConstants.NSURI_ADDRESSING_SCHEMA );
if ( obj instanceof EndpointReferenceType )
{
eprs[i] = (EndpointReferenceType) obj;
i++;
}
else
{
throw new Exception( "Expected type - EndpointReferenceType" + resource.toString( ) );
}
}
else
{
throw new Exception( "Expected type - XmlBeansEndpointReference" );
}
}
disc.addNewDiscovery( ).setEndpointReferenceArray( eprs );
prop.add( disc );
}
else
{
EndpointReferenceType[] eprs = new EndpointReferenceType[0];
disc.addNewDiscovery( ).setEndpointReferenceArray( eprs );
}
}
catch ( Exception re )
{
re.printStackTrace( );
throw new CallbackFailedException( re );
}
return prop;
}
}