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

net.anotheria.portalkit.services.subscription.generated.SubscriptionServer Maven / Gradle / Ivy

The newest version!
package net.anotheria.portalkit.services.subscription.generated;
//BEGIN GENERATED CODE

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import java.rmi.server.UnicastRemoteObject;
import java.security.Permission;
import java.rmi.registry.Registry;
import org.distributeme.core.RegistryUtil;
import org.distributeme.core.ServiceDescriptor;
import org.distributeme.core.util.LocalServiceDescriptorStore;
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;
import org.distributeme.core.routing.RoutingAware;

@SuppressWarnings("PMD")
public class SubscriptionServer{

	private static Logger log;
	private static Marker FATAL = MarkerFactory.getMarker("FATAL");
	private static long serverStartTime = System.currentTimeMillis();

	public static void main(String a[]) throws Exception{
		// the security manager decision and setting is now done by server side utils
		org.distributeme.core.util.ServerSideUtils.setSecurityManagerIfRequired();

		try {
			init();
			// Log current server PID (Process Id)
			PidTools.logPid();
			// force verbosity to configure itself
			Verbosity.logServerSideExceptions();
			createSupportServicesAndRegisterLocally();
			createServiceAndRegisterLocally();
			startService();
			notifyListenersAboutStart();
		} catch (Throwable e) {
			log.error(FATAL, "Unhandled exception caught", e);
			System.err.println(e.getMessage());
			System.exit(-4);
		}
	} //...main

	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 = LoggerFactory.getLogger(SubscriptionServer.class);
		// // CUSTOM CODE STARTED
		net.anotheria.portalkit.services.subscription.SubscriptionServiceSpringConfigurator.configure();
		// // CUSTOM CODE ENDED
	} //...init

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

	public static void createServiceAndRegisterLocally() throws Exception{
		// Use default port, which is -1
		createServiceAndRegisterLocally(-1);
	}

	public static void createServiceAndRegisterLocally(int customRegistryPort) throws Exception{
		// creating impl
		// No factory specified
		// init code not empty, assuming it contains factory Element
		net.anotheria.portalkit.services.subscription.SubscriptionService impl = null;
		try{
			impl = MetaFactory.get(net.anotheria.portalkit.services.subscription.SubscriptionService.class, Extension.LOCAL);
		}catch (FactoryNotFoundException factoryNotFound){
			throw new AssertionError("Un- or mis-configured, can't instantiate service instance for net.anotheria.portalkit.services.subscription.SubscriptionService tried initcode, submitted factory, autoguessed factory (net.anotheria.portalkit.services.subscription.SubscriptionServiceFactory) and impl class (net.anotheria.portalkit.services.subscription.SubscriptionServiceImpl)");
		}
		skeleton = new RemoteSubscriptionServiceSkeleton(impl);
		rmiServant = (RemoteSubscriptionService) UnicastRemoteObject.exportObject(skeleton, SystemProperties.SERVICE_BINDING_PORT.getAsInt());
		serviceId = SubscriptionServiceConstants.getServiceId();
		// Save original serviceId for later RoutingAware call
		String definedServiceId = serviceId;

		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(customRegistryPort);
		}catch(RemoteException e){
			log.error(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.error(FATAL, "Couldn't rebind myself at the local registry", e);
			System.err.println("Couldn't rebind myself at the local registry");
			e.printStackTrace();
			System.exit(-2);
		} //...local registry bind.

		if (impl instanceof RoutingAware){
			((RoutingAware)impl).notifyServiceId(definedServiceId, serviceId, null, null) ;
		} //.../if impl RoutingAware
		LifecycleComponentImpl.INSTANCE.registerPublicService(serviceId, skeleton);
	}

	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);
			LocalServiceDescriptorStore.getInstance().addServiceDescriptor(descriptor);

			if (!RegistryUtil.bind(descriptor)){
				log.error(FATAL, "Couldn't bind myself to the central registry at "+RegistryUtil.describeRegistry());
				System.err.println("Couldn'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 (in "+(System.currentTimeMillis()-serverStartTime)+" ms).");
		Runtime.getRuntime().addShutdownHook(new org.distributeme.core.listener.ServerLifecycleListenerShutdownHook(serverListeners));
	} //...startService

	public static void createSupportServicesAndRegisterLocally() throws Exception{
		org.distributeme.support.lifecycle.generated.LifecycleSupportServer.init();
		org.distributeme.support.lifecycle.generated.LifecycleSupportServer.createServiceAndRegisterLocally();
		org.distributeme.support.eventservice.generated.EventServiceRMIBridgeServer.init();
		org.distributeme.support.eventservice.generated.EventServiceRMIBridgeServer.createServiceAndRegisterLocally();
		org.distributeme.agents.transporter.generated.TransporterServer.init();
		org.distributeme.agents.transporter.generated.TransporterServer.createServiceAndRegisterLocally();
	} //...createSupportServicesAndRegisterLocally

}
//END GENERATED CODE




© 2015 - 2024 Weber Informatics LLC | Privacy Policy