All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.distributeme.agents.transporter.generated.TransporterServer Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
package org.distributeme.agents.transporter.generated;
//CHECKSTYLE:OFF

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java.rmi.server.UnicastRemoteObject;
import java.security.Permission;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import org.distributeme.core.RegistryUtil;
import org.distributeme.core.RegistryLocation;
import java.rmi.server.ExportException;
import org.distributeme.core.ServiceDescriptor;
import org.distributeme.core.ServiceDescriptor.Protocol;
import net.anotheria.anoprise.metafactory.MetaFactory;
import net.anotheria.anoprise.metafactory.FactoryNotFoundException;
import net.anotheria.anoprise.metafactory.Extension;
import net.anotheria.util.PidTools;
import net.anotheria.util.IdCodeGenerator;
import org.distributeme.core.RMIRegistryUtil;
import java.rmi.RemoteException;
import net.anotheria.anoprise.metafactory.ServiceFactory;
import org.distributeme.core.lifecycle.LifecycleComponentImpl;
import org.distributeme.core.Verbosity;
import org.distributeme.core.SystemPropertyNames;
import org.distributeme.core.ServerShutdownHook;
import org.distributeme.core.conventions.SystemProperties;
import java.util.List;

public class TransporterServer{

	private static Logger log;

	// Support service have no main method
	private static final List serverListeners = new java.util.ArrayList(0);
	private static void notifyListenersAboutStart(){
		// configured listeners
		List configuredListeners = org.distributeme.core.listener.ListenerRegistry.getInstance().getServerLifecycleListeners();
		if (configuredListeners!=null && configuredListeners.size()>0){
			for (org.distributeme.core.listener.ServerLifecycleListener listener : configuredListeners){
				try{
					listener.afterStart();
				}catch(Exception e){
					log.error("Couldn't call afterStart on  listener " + listener, e);
				}
			} //...for
		} //...if
	} //...notifyListenersAboutStart

	public static void init() throws Exception{
		log = Logger.getLogger(TransporterServer.class);
		// // CUSTOM CODE STARTED
		// // CUSTOM CODE ENDED
	} //...init

	// Have to keep local reference to the rmiServant and skeleton to prevent gc removal
	private static RemoteTransporterService skeleton = null;
	private static RemoteTransporterService rmiServant = null;
	private static String serviceId = null;

	public static void createServiceAndRegisterLocally() throws Exception{
		// creating impl
		// Registering factory
		MetaFactory.addFactoryClass(org.distributeme.agents.transporter.TransporterService.class, Extension.LOCAL, org.distributeme.agents.transporter.TransporterServiceFactory.class);
		org.distributeme.agents.transporter.TransporterService impl = null;
		try{
			impl = MetaFactory.get(org.distributeme.agents.transporter.TransporterService.class, Extension.LOCAL);
		}catch (FactoryNotFoundException factoryNotFound){
			throw new AssertionError("Un- or mis-configured, can't instantiate service instance for org.distributeme.agents.transporter.TransporterService tried initcode, submitted factory, autoguessed factory (org.distributeme.agents.transporter.TransporterServiceFactory) and impl class (org.distributeme.agents.transporter.TransporterServiceImpl)");
		}
		skeleton = new RemoteTransporterServiceSkeleton(impl);
		rmiServant = (RemoteTransporterService) UnicastRemoteObject.exportObject(skeleton, 0);
		serviceId = TransporterServiceConstants.getServiceId();

		String regNameProviderClass = System.getProperty(SystemPropertyNames.REGISTRATION_NAME_PROVIDER);
		if (regNameProviderClass!=null){
			org.distributeme.core.routing.RegistrationNameProvider suppliedNameProvider = (org.distributeme.core.routing.RegistrationNameProvider)Class.forName(regNameProviderClass).newInstance();
			serviceId = suppliedNameProvider.getRegistrationName(serviceId);
		} //...if (regNameProviderClass!=null)

		log.info("Getting local registry");
		Registry registry = null;
		try{
			registry = RMIRegistryUtil.findOrCreateRegistry();
		}catch(RemoteException e){
			log.fatal("Couldn't obtain free port for a local rmi registry", e);
			System.err.println("Couldn't obtain a free port for local rmi registry");
			System.exit(-1);
		}

		log.info("Registering "+serviceId+" locally.");

		try{
			registry.rebind(serviceId, rmiServant);
		}catch(Exception e){;
			log.fatal("Coulnd't rebind myself at the local registry", e);
			System.err.println("Coulnd't rebind myself at the local registry");
			e.printStackTrace();
			System.exit(-2);
		} //...local registry bind.

	}

	public static ServiceDescriptor createDescriptor(String instanceId) throws Exception{
		return RegistryUtil.createLocalServiceDescription(Protocol.RMI,  serviceId, instanceId, RMIRegistryUtil.getRmiRegistryPort());
	}
	public static void startService() throws Exception{
		String instanceId = IdCodeGenerator.generateCode(10);
		boolean registerCentrally = !SystemProperties.SKIP_CENTRAL_REGISTRY.getAsBoolean();
		if (registerCentrally){
			ServiceDescriptor descriptor = createDescriptor(instanceId);

			if (!RegistryUtil.bind(descriptor)){
				log.fatal("Coulnd't bind myself to the central registry at "+RegistryUtil.describeRegistry());
				System.err.println("Coulnd't bind myself at the central registry at "+RegistryUtil.describeRegistry());
				System.exit(-3);
			} //...central registry bind
			Runtime.getRuntime().addShutdownHook(new ServerShutdownHook(descriptor));

		}else{
			System.out.println("skipping registration for "+serviceId);
		}
		System.out.println("Server "+serviceId+" is up and ready.");
		Runtime.getRuntime().addShutdownHook(new org.distributeme.core.listener.ServerLifecycleListenerShutdownHook(serverListeners));
	} //...startService
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy