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

📄 persistencecontroller.java

📁 Java mulitplayer strategy game. Adaptation of KDE Galaxy Conquest. (rules are changed - but still th
💻 JAVA
字号:
/*
 * PersistenceController.java
 *
 * Created on 7 czerwiec 2005, 19:48
 */

package net.sf.jawp.gf.persistence;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import net.sf.jawp.util.Log;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.Transaction;

/**
 * wrapper for prevayler
 * 
 * @author jarek
 * @param <SYSTEM> the type of persistens object (prevalent system)
 * @param <UNMODIFIABLE> the type of unmodifiable view of system
 */
public class PersistenceController<SYSTEM extends PersistentSystem<UNMODIFIABLE>, UNMODIFIABLE>
{
	private static final Log LOG = Log.getLog(PersistenceController.class);

	// private SYSTEM controlledSystem;

	private Prevayler prevayler;
	
	/**
	 * stores folder 
	 */
	private String uniqueName;

	/** Creates a new instance of PersistenceController */
	public PersistenceController()
	{
	}
	
	@SuppressWarnings("unchecked")
	private SYSTEM getStoredObject()
	{
		return (SYSTEM) this.prevayler.prevalentSystem();
	}

	public final UNMODIFIABLE getUnmodifiableObject()
	{
		return getStoredObject().getUnmodifiable();
	}

	public final SYSTEM getSystemObject()
	{
		return this.getStoredObject();
	}
	
	private Date getDate()
	{
		return this.prevayler.clock().time();
	}

	public final <RETURN_TYPE> RETURN_TYPE query(
			final QueryCommand<? extends RETURN_TYPE, SYSTEM, UNMODIFIABLE> query)
	{
		if (query.isSensitive())
		{
			throw new UnsupportedOperationException();
			// return this.prevayler.execute( query );
		}
		else
		{
			return query.perform(getStoredObject(), getDate());
		}
	}
	
	@SuppressWarnings("unchecked")
	public final <RETURN_TYPE> RETURN_TYPE runCommand(
			final Command<RETURN_TYPE, SYSTEM , UNMODIFIABLE > cmd )
	{
		try
		{
			return (RETURN_TYPE)this.prevayler.execute( new TransactionWithQueryWrapper<RETURN_TYPE, SYSTEM, UNMODIFIABLE>(cmd)  );
		}
		catch (final Exception e)
		{
			throw new RuntimeException(e);
		}
	}

	public final void loadSystem(final String uniqueNameArg)
	{
		handleInitExceptions(
				new InitFunction()
				{
					public final void run() throws IOException, ClassNotFoundException 
					{
						createPrevayler(new Fake(), uniqueNameArg);
					}
				}
							);
	}

	
	/**
	 * wrapper for handling prevayler initializations exceptions
	 * @param function
	 */
	private void handleInitExceptions( final InitFunction function)
	{
		try
		{
			function.run();
			
		}
		catch (final IOException ioe)
		{
			LOG.error(ioe, ioe);
			throw new RuntimeException(ioe);
		}
		catch (final ClassNotFoundException cnfe)
		{
			LOG.error(cnfe, cnfe);
			throw new RuntimeException(cnfe);
		}
	}
	
	public final void initSystem(final SYSTEM system, final String uniqueNameArg)
	{
		handleInitExceptions(
				new InitFunction()
				{
					public final void run() throws IOException, ClassNotFoundException 
					{
						final Prevayler prev = createPrevayler(system, uniqueNameArg);
						prev.execute(new FakeTransaction());
						prev.takeSnapshot();
						prev.execute(new FakeTransaction());
						prev.takeSnapshot();
					}
				}
							);

	}

	private Prevayler createPrevayler(final Serializable system, final String uniqueNameArg) throws IOException, ClassNotFoundException
	{
		final Prevayler prev = PrevaylerFactory.createPrevayler(system,
				uniqueNameArg);

		if (prev != null)
		{
			this.prevayler = prev;
		}
		this.uniqueName = uniqueNameArg;
		return prev;
	}

	public final void storeSystem()
	{
		try
		{
			this.prevayler.takeSnapshot();
		}
		catch (final IOException ioe)
		{
			LOG.error(ioe, ioe);
			throw new RuntimeException(ioe);
		}
	}

	/**
	 * closes the system
	 *
	 */
	public final void closeSystem()
	{
		storeSystem();
		
		closePrevayler();
	}

	private void closePrevayler()
	{
		try
		{
			this.prevayler.close();
		}
		catch (final IOException ioe)
		{
			LOG.error(ioe, ioe);
			throw new RuntimeException(ioe);
		}
		this.prevayler = null;
	}
	
	/**
	 * removes/deletes whole system
	 *
	 */
	public final void disposeSystem()
	{
		if ( this.prevayler != null)
		{
			closePrevayler();
		}
		deleteFolder();
		
	}
	
	public final void disposeSystem(final String uniqueNameArg)
	{
		this.uniqueName = uniqueNameArg;
		disposeSystem();
	}
	

	private void deleteFolder()
	{
		final File f = getFolderForName(this.uniqueName);
		if ( f.exists())
		{
			PersistenceController.deleteFileRecursive( f);
		}
	}
	
	

	public static File getFolderForName( final String storeName)
	{
		return new File(storeName);
	}
	
	public static boolean isStoredSystemAvailable( final String storeName)
	{
		final File f = getFolderForName(storeName);
		final String[] logs = f.list( new PrevaylerLogsSelector() );
		return logs != null && logs.length > 0;
		
	}
	
	private static class PrevaylerLogsSelector implements FilenameFilter
	{
		public boolean accept(final File file, final String str)
		{
			return str.contains(".transactionLog" ) || str.contains(".snapshot");
		}
		
	}
	
	private static void deleteFileRecursive(final File f)
	{
		if ( f.isDirectory() )
		{
			for ( final File child : f.listFiles())
			{
				deleteFileRecursive( child);
			}
		}
		else
		{
			if ( !f.delete() )
			{
				throw new RuntimeException("problem with deleting file:" + f.getAbsolutePath());
			}
		}
	}
	
	private static final class Fake implements Serializable
	{
		private static final long serialVersionUID = 101010L;
	}

	/**
	 *  this fake transaction fixes very subtle Prevayler bug
	 *  -> if there is no transaction called on newly created system 
	 *  it is not properly loaded after first snapshot
	 * 
	 * @author jarek
	 * @version $Revision: 1.22 $
	 *
	 */
	private static final class FakeTransaction implements Transaction
	{
		private static final long serialVersionUID = 101010L;

		public void executeOn(final Object obj, final Date date)
		{
			// nichts
		}

	}
	
	/**
	 * simple function for wrapping prevayler init calls 
	 * @author jarek
	 * @version $Revision: 1.22 $
	 *
	 */
	private static interface InitFunction 
	{
		void run() throws IOException, ClassNotFoundException;
	}
}

⌨️ 快捷键说明

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