/*******************************************************************************
 * Copyright (c) 1998, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
*     pvijayaratnam - cache coordination test implementation
 ******************************************************************************/  
package org.eclipse.persistence.testing.models.jpa.sessionbean;

import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.internal.sessions.DatabaseSessionImpl;

import org.eclipse.persistence.sessions.coordination.RemoteCommandManager;
import org.eclipse.persistence.sessions.coordination.TransportManager;
import org.eclipse.persistence.sessions.coordination.jms.JMSTopicTransportManager;

public class CacheCoordinationSessionCustomizer implements SessionCustomizer {

	
    public void customize(Session session) {

	    String cachecoordinationProtocol = session.getProperty("cachecoordination.protocol").toString();
	    
	    RemoteCommandManager rcm = new RemoteCommandManager((DatabaseSessionImpl)session);
	    
    //Cachecoordination configuration JMS-specific properties 	    
     if(cachecoordinationProtocol.equalsIgnoreCase("jms")){
		  
            JMSTopicTransportManager tm = new JMSTopicTransportManager(rcm);
	    tm.setTopicHostUrl(session.getProperty("rcm.wls.jms.topichost.url").toString());
	    tm.setTopicName(session.getProperty("rcm.wls.jms.topicname").toString());
	    tm.setTopicConnectionFactoryName(session.getProperty("rcm.wls.jms.topic.connectionfactory.name").toString());	
	    rcm.setTransportManager(tm);
    }
    //Cachecoordination configuration RMI-specific properties 
     else if(cachecoordinationProtocol.equalsIgnoreCase("rmi")){

	    ((DatabaseSessionImpl)session).getCommandManager().getDiscoveryManager().setAnnouncementDelay(Integer.parseInt(session.getProperty("announcement.delay").toString()));
            ((DatabaseSessionImpl)session).getCommandManager().getDiscoveryManager().setMulticastGroupAddress(session.getProperty("rcm.multicastgroup").toString());
            ((DatabaseSessionImpl)session).getCommandManager().getDiscoveryManager().setMulticastPort(Integer.parseInt(session.getProperty("rcm.multicastport").toString()));
            ((DatabaseSessionImpl)session).getCommandManager().getDiscoveryManager().setPacketTimeToLive(Integer.parseInt(session.getProperty("rcm.packet.timetolive").toString()));

	    rcm.setUrl(session.getProperty("current.server.url").toString());
     }

            rcm.getTransportManager().setUserName(session.getProperty("server.user").toString());
	    rcm.getTransportManager().setPassword(session.getProperty("server.pwd").toString());    
     	    rcm.setShouldPropagateAsynchronously(false);
	    rcm.getTransportManager().setNamingServiceType(TransportManager.JNDI_NAMING_SERVICE);            
            rcm.getTransportManager().setInitialContextFactoryName("weblogic.jndi.WLInitialContextFactory");
	    rcm.getTransportManager().setShouldRemoveConnectionOnError(true);
            rcm.setServerPlatform(((org.eclipse.persistence.sessions.DatabaseSession)session).getServerPlatform());
	    ((DatabaseSessionImpl)session).setCommandManager(rcm);
	    ((DatabaseSessionImpl)session).setShouldPropagateChanges(true);
            rcm.initialize();
	    
	    // Sleep to allow RCM to startup and find each session.
            try {
                  Thread.sleep(2000);
            } catch (Exception ignore) {
            }

        }


}
