⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resourcemanagerimpl.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
package naplet.resource;

import naplet.*;
import naplet.resource.ServiceProxy;
import naplet.serviceChannel.*;
import naplet.server.ServiceInstallException;

import java.io.*;
import java.util.*;
import java.lang.reflect.*;

// import java.security.PrivilegedAction;

public class ResourceManagerImpl
		implements ResourceManager, Runnable
{

	private static ResourceManagerImpl instance = null;

	/**
	 * List of handlers to non-priviledged application serivces
	 */
	private Map appServiceHandlerList = null;

	/**
	 * List of <code>PrivilegedService</code> objects or local
	 * application services.
	 * They are declared as threads and started by
	 * <code>ResourceManagerImpl</code>.
	 * They wait for signals from Naplets to
	 * proceeds. Naplets and PrivilegedServices exchange data through
	 * <code>ServiceChannel</code>.
	 */
	private ArrayList pvgServiceList = null;

	/**
	 * List of <code>ServiceChannel</code> objects, providing each
	 * application service with a pair of input/output buffers.
	 */
	private Map pvgServiceChannelList;

	protected ResourceManagerImpl( Map config ) throws ServiceInstallException
	{
		register( config );
	}

	public static ResourceManagerImpl getInstance( Map config ) 
		throws ServiceInstallException 
	{
		if ( instance == null )
		{
			instance = new ResourceManagerImpl( config );
		}
		return instance;
	}

	public ServiceProxy getServiceProxy( NapletID nid )
	{
		return new ServiceProxy( pvgServiceChannelList, appServiceHandlerList );
	}

	public void run()
	{
		if ( pvgServiceList != null )
		{
			int num = pvgServiceList.size();
			Thread[] thr = new Thread[num];
			for ( int i = 0; i < num; i++ )
			{
				PrivilegedService as
						= ( PrivilegedService ) pvgServiceList.get( i );
				thr[i] = new Thread( as );
				thr[i].start();

				System.out.println( "Privileged Service comes on-line" );
			}
		}
	}

	private void register( Map config )
			throws ServiceInstallException
	{

		// Nonpriviledged application services provided in the naplet server
		if ( config.containsKey( "AppService" ) )
		{
			String ser = ( String ) config.get( "AppService" );
			StringTokenizer tokens = new StringTokenizer( ser );

			int numService = tokens.countTokens();
			appServiceHandlerList = new HashMap( numService );

			while ( tokens.hasMoreTokens() )
			{
				String name = tokens.nextToken();
				System.out.println( "Nonpriviledged Service Name = " + name );

				try
				{
					Class sc = Class.forName( name );
					AppService as = ( AppService ) sc.newInstance();

					appServiceHandlerList.put( name, as );

				}
				catch ( ClassNotFoundException cnfe )
				{
					throw new ServiceInstallException(
							"Service " + name + " not Found!" );
				}
				catch ( IllegalAccessException iae )
				{
					throw new ServiceInstallException(
							"Illegal Access to Service " + name );
				}
				catch ( Exception e )
				{
					e.printStackTrace();
					throw new ServiceInstallException(
							"Configuration Exception with Service " + name );
				}
			}
		}

		// Privileged app- services associated with the naplet server
		if ( config.containsKey( "PrivilegedService" ) )
		{
			String ser = ( String ) config.get( "PrivilegedService" );
			String delimiter = " ,\t";
			StringTokenizer tokens = new StringTokenizer( ser, delimiter );

			int numService = tokens.countTokens();
			pvgServiceList = new ArrayList( numService );

			pvgServiceChannelList = new HashMap();

			while ( tokens.hasMoreTokens() )
			{
				String name = tokens.nextToken();

				System.out.println( "Service Name = " + name );

				try
				{
					Class sc = Class.forName( name );
					PrivilegedService as = ( PrivilegedService ) sc.newInstance();

					// Create a pair of I/O stream to and from naplets
					ServiceChannel ch = new ServiceChannel();
					pvgServiceChannelList.put( name, ch );

					// Register the I/O streams with the app service
					as.setIOStream( ch.getServiceInputStream( ),
									ch.getServiceOutputStream( ) );

					as.setIO( ch.getServiceReader( ), ch.getServiceWriter( ) );

					pvgServiceList.add( as );
				}
				catch ( ClassNotFoundException cnfe )
				{
					throw new ServiceInstallException(
							"Service " + name + " not Found!" );
				}
				catch ( Exception e )
				{
					throw new ServiceInstallException(
							"Configuration Exception with Service " + name );
				}
			}
		}
		else
		{
			pvgServiceList = null;
			pvgServiceChannelList = null;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -