Importing SVN archives - Trunk
This commit is contained in:
parent
fc4c6bd340
commit
ff53af6668
507 changed files with 8866 additions and 2450 deletions
legacyworlds-server/legacyworlds-server-main/src/main
|
@ -52,7 +52,7 @@ public class ImportBuildables
|
|||
|
||||
public CostData cost;
|
||||
|
||||
public TechData tech;
|
||||
public String tech;
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
|
@ -70,18 +70,6 @@ public class ImportBuildables
|
|||
public int work;
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "tech" )
|
||||
public static class TechData
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
public String name;
|
||||
|
||||
@XStreamAsAttribute
|
||||
public int level;
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "building" )
|
||||
public static class BuildingData
|
||||
|
@ -196,7 +184,6 @@ public class ImportBuildables
|
|||
this.uocBuildingDep.addParameter( "output_type" , "building_output_type" );
|
||||
this.uocBuildingDep.addParameter( "output" , Types.INTEGER );
|
||||
this.uocBuildingDep.addParameter( "dep_name" , Types.VARCHAR );
|
||||
this.uocBuildingDep.addParameter( "dep_level" , Types.INTEGER );
|
||||
|
||||
this.uocShipNoDep = new StoredProc( dataSource , "tech" , "uoc_ship" );
|
||||
this.uocShipNoDep.addParameter( "name" , Types.VARCHAR );
|
||||
|
@ -216,7 +203,6 @@ public class ImportBuildables
|
|||
this.uocShipDep.addParameter( "power" , Types.INTEGER );
|
||||
this.uocShipDep.addParameter( "flight_time" , Types.INTEGER );
|
||||
this.uocShipDep.addParameter( "dep_name" , Types.VARCHAR );
|
||||
this.uocShipDep.addParameter( "dep_level" , Types.INTEGER );
|
||||
}
|
||||
|
||||
|
||||
|
@ -241,7 +227,7 @@ public class ImportBuildables
|
|||
ship.cost.upkeep , ship.power , ship.time );
|
||||
} else {
|
||||
this.uocShipDep.execute( ship.name , ship.description , ship.cost.build , ship.cost.work ,
|
||||
ship.cost.upkeep , ship.power , ship.time , ship.tech.name , ship.tech.level );
|
||||
ship.cost.upkeep , ship.power , ship.time , ship.tech );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -256,7 +242,7 @@ public class ImportBuildables
|
|||
} else {
|
||||
this.uocBuildingDep.execute( building.name , building.description , building.cost.build ,
|
||||
building.cost.work , building.cost.upkeep , building.workers , building.type.toString( ) ,
|
||||
building.output , building.tech.name , building.tech.level );
|
||||
building.output , building.tech );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,17 +31,26 @@ public class ImportTechs
|
|||
|
||||
private final Logger logger = Logger.getLogger( ImportTechs.class );
|
||||
|
||||
private static class ImportError
|
||||
extends RuntimeException
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
|
||||
@XStreamAlias( "technologies" )
|
||||
@SuppressWarnings( "serial" )
|
||||
public static class Techs
|
||||
public static class Technologies
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamImplicit( itemFieldName = "tech-line" )
|
||||
public List< TechLine > lines;
|
||||
@XStreamImplicit( itemFieldName = "category" )
|
||||
public List< Category > categories;
|
||||
|
||||
@XStreamImplicit( itemFieldName = "technology" )
|
||||
public List< Technology > technologies;
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
public static class TechLine
|
||||
public static class Category
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
|
@ -49,18 +58,18 @@ public class ImportTechs
|
|||
|
||||
@XStreamAsAttribute
|
||||
public String description;
|
||||
|
||||
@XStreamImplicit( itemFieldName = "level" )
|
||||
public List< TechLevel > levels;
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
public static class TechLevel
|
||||
public static class Technology
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
public String name;
|
||||
|
||||
@XStreamAsAttribute
|
||||
public String category;
|
||||
|
||||
@XStreamAsAttribute
|
||||
public String description;
|
||||
|
||||
|
@ -69,23 +78,57 @@ public class ImportTechs
|
|||
|
||||
@XStreamAsAttribute
|
||||
public int cost;
|
||||
|
||||
@XStreamImplicit( itemFieldName = "depends" )
|
||||
public List< Dependency > dependencies;
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
public static class Dependency
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAlias( "on" )
|
||||
@XStreamAsAttribute
|
||||
public String dependsOn;
|
||||
}
|
||||
|
||||
/** The file from which technology definitions will be loaded */
|
||||
private File file;
|
||||
|
||||
/** The transaction template used to execute the import transaction */
|
||||
private TransactionTemplate tTemplate;
|
||||
private StoredProc uocLine;
|
||||
private StoredProc uocLevel;
|
||||
|
||||
/** Wrapper for the stored procedure which updates or creates technology categories */
|
||||
private StoredProc uocCategory;
|
||||
|
||||
/** Wrapper for the stored procedure which updates or creates technology definitions */
|
||||
private StoredProc uocTechnology;
|
||||
|
||||
/** Wrapper for the stored procedure which adds a dependency to a technology */
|
||||
private StoredProc addDependency;
|
||||
|
||||
|
||||
/**
|
||||
* Initialise an XStream instance using the annotations on the {@link Technologies} class and
|
||||
* its various component classes.
|
||||
*
|
||||
* @return the initialised XStream instance
|
||||
*/
|
||||
private XStream initXStream( )
|
||||
{
|
||||
XStream xstream = new XStream( );
|
||||
xstream.processAnnotations( Techs.class );
|
||||
xstream.processAnnotations( Technologies.class );
|
||||
return xstream;
|
||||
}
|
||||
|
||||
|
||||
private Techs loadData( )
|
||||
/**
|
||||
* Load technology definitions from an XML data file, deserialising it as a {@link Technologies}
|
||||
* instance through XStream.
|
||||
*
|
||||
* @return the technology definition instance or <code>null</code> if an error occurs.
|
||||
*/
|
||||
private Technologies loadData( )
|
||||
{
|
||||
FileInputStream fis;
|
||||
try {
|
||||
|
@ -96,7 +139,7 @@ public class ImportTechs
|
|||
|
||||
try {
|
||||
XStream xstream = this.initXStream( );
|
||||
return (Techs) xstream.fromXML( fis );
|
||||
return (Technologies) xstream.fromXML( fis );
|
||||
} catch ( Exception e ) {
|
||||
e.printStackTrace( );
|
||||
return null;
|
||||
|
@ -110,9 +153,15 @@ public class ImportTechs
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a basic Spring context containing the components that may be used to connect to the
|
||||
* database.
|
||||
*
|
||||
* @return the initialised Spring context
|
||||
*/
|
||||
private ClassPathXmlApplicationContext createContext( )
|
||||
{
|
||||
// Load data source and Hibernate properties
|
||||
// Load data source properties
|
||||
String[] dataConfig = {
|
||||
this.getDataSource( )
|
||||
};
|
||||
|
@ -127,44 +176,159 @@ public class ImportTechs
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create the {@link #tTemplate} transaction template from the transaction manager in the
|
||||
* context. In addition, initialise stored procedure wrappers {@link #uocCategory},
|
||||
* {@link #uocTechnology}.
|
||||
*
|
||||
* @param ctx
|
||||
* the Spring context
|
||||
*/
|
||||
private void getBeans( ApplicationContext ctx )
|
||||
{
|
||||
PlatformTransactionManager tManager = ctx.getBean( PlatformTransactionManager.class );
|
||||
this.tTemplate = new TransactionTemplate( tManager );
|
||||
|
||||
DataSource dataSource = ctx.getBean( DataSource.class );
|
||||
this.uocLine = new StoredProc( dataSource , "tech" , "uoc_line" );
|
||||
this.uocLine.addParameter( "tln" , Types.VARCHAR );
|
||||
this.uocLine.addParameter( "tld" , Types.VARCHAR );
|
||||
|
||||
this.uocLevel = new StoredProc( dataSource , "tech" , "uoc_level" );
|
||||
this.uocLevel.addParameter( "tech_line" , Types.VARCHAR );
|
||||
this.uocLevel.addParameter( "level" , Types.INTEGER );
|
||||
this.uocLevel.addParameter( "name" , Types.VARCHAR );
|
||||
this.uocLevel.addParameter( "desc" , Types.VARCHAR );
|
||||
this.uocLevel.addParameter( "points" , Types.INTEGER );
|
||||
this.uocLevel.addParameter( "cost" , Types.INTEGER );
|
||||
this.uocCategory = new StoredProc( dataSource , "tech" , "uoc_category" );
|
||||
this.uocCategory.addOutput( "rv" , Types.INTEGER );
|
||||
this.uocCategory.addParameter( "cat_name" , Types.VARCHAR );
|
||||
this.uocCategory.addParameter( "cat_desc" , Types.VARCHAR );
|
||||
|
||||
this.uocTechnology = new StoredProc( dataSource , "tech" , "uoc_technology" );
|
||||
this.uocTechnology.addOutput( "rv" , Types.INTEGER );
|
||||
this.uocTechnology.addParameter( "nt_name" , Types.VARCHAR );
|
||||
this.uocTechnology.addParameter( "nt_cat" , Types.VARCHAR );
|
||||
this.uocTechnology.addParameter( "nt_desc" , Types.VARCHAR );
|
||||
this.uocTechnology.addParameter( "nt_points" , Types.INTEGER );
|
||||
this.uocTechnology.addParameter( "nt_cost" , Types.INTEGER );
|
||||
|
||||
this.addDependency = new StoredProc( dataSource , "tech" , "add_dependency" );
|
||||
this.addDependency.addOutput( "rv" , Types.INTEGER );
|
||||
this.addDependency.addParameter( "nd_name" , Types.VARCHAR );
|
||||
this.addDependency.addParameter( "nd_dep" , Types.VARCHAR );
|
||||
}
|
||||
|
||||
|
||||
private void importTechnologies( Techs data )
|
||||
/**
|
||||
* Import the technology data stored as a {@link Technologies} instance.
|
||||
*
|
||||
* @param data
|
||||
* the technology categories and technology definitions to import
|
||||
*
|
||||
* @throws ImportError
|
||||
* if the definitions are erroneous
|
||||
*/
|
||||
private void importTechnologies( Technologies data )
|
||||
{
|
||||
for ( TechLine line : data.lines ) {
|
||||
this.uocLine.execute( line.name , line.description );
|
||||
this.importCategories( data.categories );
|
||||
for ( Technology tech : data.technologies ) {
|
||||
this.importTechnology( tech );
|
||||
}
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
for ( TechLevel level : line.levels ) {
|
||||
this.uocLevel.execute( line.name , i , level.name , level.description , level.points , level.cost );
|
||||
i++;
|
||||
|
||||
/**
|
||||
* Import a single technology definition, along with its dependencies.
|
||||
*
|
||||
* @param technology
|
||||
* the technology definition to import
|
||||
*
|
||||
* @throws ImportError
|
||||
* if the definition is incorrect
|
||||
*/
|
||||
private void importTechnology( Technology technology )
|
||||
{
|
||||
this.logger.debug( "Importing technology " + technology.name );
|
||||
|
||||
int result = (Integer) this.uocTechnology.execute( technology.name , technology.category ,
|
||||
technology.description , technology.points , technology.cost ).get( "rv" );
|
||||
switch ( result ) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
this.logger.error( "Technology " + technology.name + ": name string not found" );
|
||||
throw new ImportError( );
|
||||
case 2:
|
||||
this.logger.error( "Technology " + technology.name + ": category '" + technology.category
|
||||
+ "' not found" );
|
||||
throw new ImportError( );
|
||||
case 3:
|
||||
this.logger.error( "Technology " + technology.name + ": description string '" + technology.description
|
||||
+ "' not found" );
|
||||
throw new ImportError( );
|
||||
case 4:
|
||||
this.logger.error( "Technology " + technology.name + ": invalid points and/or cost" );
|
||||
throw new ImportError( );
|
||||
}
|
||||
|
||||
if ( technology.dependencies == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
for ( Dependency dep : technology.dependencies ) {
|
||||
result = (Integer) this.addDependency.execute( technology.name , dep.dependsOn ).get( "rv" );
|
||||
switch ( result ) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
this.logger.error( "Technology " + technology.name + ": not found while adding dependency" );
|
||||
throw new ImportError( );
|
||||
case 2:
|
||||
this.logger.error( "Technology " + technology.name + ": dependency '" + dep.dependsOn
|
||||
+ "' not found" );
|
||||
throw new ImportError( );
|
||||
case 3:
|
||||
this.logger.error( "Technology " + technology.name + ": duplicate dependency '" + dep.dependsOn
|
||||
+ "'" );
|
||||
throw new ImportError( );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Import the list of categories into the database using the <em>tech.uoc_category</em> stored
|
||||
* procedure.
|
||||
*
|
||||
* @param categories
|
||||
* the list of categories
|
||||
*
|
||||
* @throws ImportError
|
||||
* if the stored procedure returns a failure code
|
||||
*/
|
||||
private void importCategories( List< Category > categories )
|
||||
{
|
||||
for ( Category category : categories ) {
|
||||
this.logger.debug( "Importing category " + category.name );
|
||||
|
||||
int result = (Integer) this.uocCategory.execute( category.name , category.description ).get( "rv" );
|
||||
switch ( result ) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
this.logger.error( "Category " + category.name + ": name string not found" );
|
||||
throw new ImportError( );
|
||||
case 2:
|
||||
this.logger.error( "Category " + category.name + ": description string '" + category.description
|
||||
+ "' not found" );
|
||||
throw new ImportError( );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load the technologies and related data from the file specified on the command line, then
|
||||
* start a database transaction during which categories, technologies and dependencies will be
|
||||
* created. If anything should go wrong during the import, rollback the transaction.
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void run( )
|
||||
{
|
||||
final Techs data = this.loadData( );
|
||||
final Technologies data = this.loadData( );
|
||||
if ( data == null ) {
|
||||
System.err.println( "could not read data" );
|
||||
return;
|
||||
|
@ -181,8 +345,10 @@ public class ImportTechs
|
|||
try {
|
||||
importTechnologies( data );
|
||||
rv = true;
|
||||
} catch ( ImportError e ) {
|
||||
rv = false;
|
||||
} catch ( RuntimeException e ) {
|
||||
logger.error( e.getMessage( ) );
|
||||
logger.error( "error during import" , e );
|
||||
rv = false;
|
||||
}
|
||||
if ( !rv ) {
|
||||
|
@ -202,6 +368,15 @@ public class ImportTechs
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Make sure that the command-line parameters of this tool consist in a single file name, that
|
||||
* the file exists and is readable, setting {@link #file} accordingly.
|
||||
*
|
||||
* @param options
|
||||
* the array of command line parameters
|
||||
*
|
||||
* @return <code>true</code> if the parameters are ok, <code>false</code> otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean setOptions( String... options )
|
||||
{
|
||||
|
|
|
@ -2,8 +2,7 @@ package com.deepclone.lw.cli;
|
|||
|
||||
|
||||
import java.io.*;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
@ -19,10 +18,8 @@ import org.springframework.transaction.TransactionStatus;
|
|||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
import com.deepclone.lw.cli.i18n.LoadableText;
|
||||
import com.deepclone.lw.cli.i18n.Loader;
|
||||
|
||||
|
||||
|
||||
|
@ -32,125 +29,12 @@ public class ImportText
|
|||
|
||||
private final Logger logger = Logger.getLogger( ImportText.class );
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
public abstract static class StringData
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
public String id;
|
||||
|
||||
|
||||
public abstract String getString( );
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "inline-string" )
|
||||
public static class InlineString
|
||||
extends StringData
|
||||
{
|
||||
public String value;
|
||||
|
||||
|
||||
@Override
|
||||
public String getString( )
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "from-file" )
|
||||
public static class FileString
|
||||
extends StringData
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
public String source;
|
||||
|
||||
|
||||
@Override
|
||||
public String getString( )
|
||||
{
|
||||
StringBuilder sBuilder = new StringBuilder( );
|
||||
try {
|
||||
BufferedReader in = new BufferedReader( new FileReader( source ) );
|
||||
String str;
|
||||
while ( ( str = in.readLine( ) ) != null ) {
|
||||
sBuilder.append( str );
|
||||
sBuilder.append( "\n" );
|
||||
}
|
||||
in.close( );
|
||||
} catch ( IOException e ) {
|
||||
throw new RuntimeException( "Could not read " + source );
|
||||
}
|
||||
|
||||
return sBuilder.toString( );
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
public static class LanguageData
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
public String id;
|
||||
|
||||
@XStreamAsAttribute
|
||||
public String name;
|
||||
|
||||
@XStreamImplicit
|
||||
public List< StringData > strings = new LinkedList< StringData >( );
|
||||
}
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "lw-text-data" )
|
||||
public static class TextData
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamImplicit( itemFieldName = "language" )
|
||||
public List< LanguageData > languages = new LinkedList< LanguageData >( );
|
||||
}
|
||||
|
||||
private File file;
|
||||
private TransactionTemplate tTemplate;
|
||||
private SimpleJdbcCall uocTranslation;
|
||||
private SimpleJdbcCall uocLanguage;
|
||||
|
||||
|
||||
private XStream initXStream( )
|
||||
{
|
||||
XStream xstream = new XStream( );
|
||||
xstream.processAnnotations( TextData.class );
|
||||
xstream.processAnnotations( InlineString.class );
|
||||
xstream.processAnnotations( FileString.class );
|
||||
return xstream;
|
||||
}
|
||||
|
||||
|
||||
private TextData loadData( )
|
||||
{
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream( this.file );
|
||||
} catch ( FileNotFoundException e ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
XStream xstream = this.initXStream( );
|
||||
return (TextData) xstream.fromXML( fis );
|
||||
} catch ( Exception e ) {
|
||||
e.printStackTrace( );
|
||||
return null;
|
||||
} finally {
|
||||
try {
|
||||
fis.close( );
|
||||
} catch ( IOException e ) {
|
||||
// EMPTY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ClassPathXmlApplicationContext createContext( )
|
||||
{
|
||||
// Load data source and Hibernate properties
|
||||
|
@ -188,26 +72,23 @@ public class ImportText
|
|||
}
|
||||
|
||||
|
||||
private void importText( TextData data )
|
||||
private void importText( LoadableText data )
|
||||
{
|
||||
for ( LanguageData ld : data.languages ) {
|
||||
this.importLanguage( ld );
|
||||
for ( String lId : data.getLanguages( ) ) {
|
||||
this.importLanguage( data , lId );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void importLanguage( LanguageData ld )
|
||||
private void importLanguage( LoadableText data , String lId )
|
||||
{
|
||||
if ( ld.strings == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Try creating or updating the language
|
||||
this.uocLanguage.execute( ld.id , ld.name );
|
||||
this.uocLanguage.execute( lId , data.getLanguageName( lId ) );
|
||||
|
||||
// Import translations
|
||||
for ( StringData sd : ld.strings ) {
|
||||
this.uocTranslation.execute( ld.id , sd.id , sd.getString( ) );
|
||||
for ( Map.Entry< String , String > string : data.getStrings( lId ) ) {
|
||||
System.out.println( "Language " + lId + " string " + string.getKey( ) );
|
||||
this.uocTranslation.execute( lId , string.getKey( ) , string.getValue( ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,11 +96,8 @@ public class ImportText
|
|||
@Override
|
||||
public void run( )
|
||||
{
|
||||
final TextData data = this.loadData( );
|
||||
if ( data == null ) {
|
||||
System.err.println( "could not read data" );
|
||||
return;
|
||||
}
|
||||
Loader textLoader = new Loader( this.file );
|
||||
final LoadableText data = textLoader.load( );
|
||||
|
||||
AbstractApplicationContext ctx = this.createContext( );
|
||||
this.createTemplates( ctx );
|
||||
|
@ -245,7 +123,7 @@ public class ImportText
|
|||
} );
|
||||
|
||||
if ( rv ) {
|
||||
this.logger.info( "Text import successful" );
|
||||
System.out.println( "Text import successful" );
|
||||
}
|
||||
|
||||
ToolBase.destroyContext( ctx );
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.deepclone.lw.cli.i18n;
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "from-file" )
|
||||
public class FileString
|
||||
extends StringData
|
||||
{
|
||||
private File sourceFile;
|
||||
|
||||
@XStreamAsAttribute
|
||||
public String source;
|
||||
|
||||
|
||||
@Override
|
||||
public String getString( )
|
||||
{
|
||||
StringBuilder sBuilder = new StringBuilder( );
|
||||
System.out.println( "Loading text from " + this.sourceFile.getAbsolutePath( ) );
|
||||
try {
|
||||
BufferedReader in = new BufferedReader( new FileReader( this.sourceFile ) );
|
||||
String str;
|
||||
while ( ( str = in.readLine( ) ) != null ) {
|
||||
sBuilder.append( str );
|
||||
sBuilder.append( "\n" );
|
||||
}
|
||||
in.close( );
|
||||
} catch ( IOException e ) {
|
||||
throw new RuntimeException( "Could not read " + this.sourceFile.getAbsolutePath( ) );
|
||||
}
|
||||
|
||||
return sBuilder.toString( );
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setLoader( Loader loader )
|
||||
{
|
||||
this.sourceFile = new File( loader.getDirectory( ) , this.source );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.deepclone.lw.cli.i18n;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "inline-string" )
|
||||
public class InlineString
|
||||
extends StringData
|
||||
{
|
||||
public String value;
|
||||
|
||||
|
||||
@Override
|
||||
public String getString( )
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setLoader( Loader loader )
|
||||
{
|
||||
// EMPTY
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.deepclone.lw.cli.i18n;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
public class LanguageData
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
public String id;
|
||||
|
||||
@XStreamAsAttribute
|
||||
public String name;
|
||||
|
||||
@XStreamImplicit
|
||||
public List< StringData > strings = new LinkedList< StringData >( );
|
||||
|
||||
|
||||
public void setLoader( Loader loader )
|
||||
{
|
||||
for ( StringData sd : this.strings ) {
|
||||
sd.setLoader( loader );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.deepclone.lw.cli.i18n;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
|
||||
public class LoadableText
|
||||
{
|
||||
|
||||
private final Map< String , String > languageNames;
|
||||
private final Map< String , Map< String , String >> text;
|
||||
|
||||
|
||||
public LoadableText( TextData data )
|
||||
{
|
||||
this.languageNames = new HashMap< String , String >( );
|
||||
this.text = new HashMap< String , Map< String , String > >( );
|
||||
|
||||
if ( data.languages == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
for ( LanguageData language : data.languages ) {
|
||||
this.languageNames.put( language.id , language.name );
|
||||
Map< String , String > strings = new HashMap< String , String >( );
|
||||
this.text.put( language.id , strings );
|
||||
|
||||
for ( StringData string : language.strings ) {
|
||||
strings.put( string.id , string.getString( ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void merge( LoadableText text )
|
||||
{
|
||||
for ( Map.Entry< String , String > entry : text.languageNames.entrySet( ) ) {
|
||||
Map< String , String > lStrings = this.text.get( entry.getKey( ) );
|
||||
if ( lStrings == null ) {
|
||||
this.languageNames.put( entry.getKey( ) , entry.getValue( ) );
|
||||
lStrings = new HashMap< String , String >( );
|
||||
this.text.put( entry.getKey( ) , lStrings );
|
||||
}
|
||||
|
||||
for ( Map.Entry< String , String > string : text.text.get( entry.getKey( ) ).entrySet( ) ) {
|
||||
if ( lStrings.put( string.getKey( ) , string.getValue( ) ) != null ) {
|
||||
throw new RuntimeException( "String '" + string.getKey( ) + "' defined more than once" );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Set< String > getLanguages( )
|
||||
{
|
||||
return this.languageNames.keySet( );
|
||||
}
|
||||
|
||||
|
||||
public String getLanguageName( String lId )
|
||||
{
|
||||
return this.languageNames.get( lId );
|
||||
}
|
||||
|
||||
|
||||
public Set< Map.Entry< String , String >> getStrings( String lId )
|
||||
{
|
||||
return this.text.get( lId ).entrySet( );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package com.deepclone.lw.cli.i18n;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
|
||||
|
||||
public class Loader
|
||||
{
|
||||
|
||||
private final Set< String > included;
|
||||
private final XStream xstream;
|
||||
private final File file;
|
||||
private final String directory;
|
||||
|
||||
|
||||
public Loader( File file )
|
||||
{
|
||||
this.included = new HashSet< String >( );
|
||||
this.xstream = new XStream( );
|
||||
this.xstream.processAnnotations( TextData.class );
|
||||
this.xstream.processAnnotations( InlineString.class );
|
||||
this.xstream.processAnnotations( FileString.class );
|
||||
this.file = file;
|
||||
this.directory = this.file.getAbsoluteFile( ).getParent( );
|
||||
}
|
||||
|
||||
|
||||
public Loader( Loader parent , String included )
|
||||
{
|
||||
this.included = parent.included;
|
||||
this.xstream = parent.xstream;
|
||||
if ( included.charAt( 0 ) == '/' ) {
|
||||
this.file = new File( included );
|
||||
} else {
|
||||
this.file = new File( parent.directory , included );
|
||||
}
|
||||
this.directory = this.file.getAbsoluteFile( ).getParent( );
|
||||
}
|
||||
|
||||
|
||||
public LoadableText load( )
|
||||
{
|
||||
System.out.println( "Loading data file " + this.file.getAbsolutePath( ) );
|
||||
|
||||
TextData data = this.loadFile( );
|
||||
data.setLoader( this );
|
||||
|
||||
LoadableText result = new LoadableText( data );
|
||||
if ( data.includes != null ) {
|
||||
for ( String file : data.includes ) {
|
||||
Loader child = new Loader( this , file );
|
||||
if ( this.included.contains( child.file.getAbsolutePath( ) ) ) {
|
||||
continue;
|
||||
}
|
||||
this.included.add( child.file.getAbsolutePath( ) );
|
||||
result.merge( child.load( ) );
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private TextData loadFile( )
|
||||
{
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream( this.file );
|
||||
} catch ( FileNotFoundException e ) {
|
||||
throw new RuntimeException( e );
|
||||
}
|
||||
|
||||
try {
|
||||
return (TextData) this.xstream.fromXML( fis );
|
||||
} catch ( Exception e ) {
|
||||
throw new RuntimeException( e );
|
||||
} finally {
|
||||
try {
|
||||
fis.close( );
|
||||
} catch ( IOException e ) {
|
||||
// EMPTY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getDirectory( )
|
||||
{
|
||||
return this.directory;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.deepclone.lw.cli.i18n;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
public abstract class StringData
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamAsAttribute
|
||||
public String id;
|
||||
|
||||
|
||||
public abstract String getString( );
|
||||
|
||||
|
||||
public abstract void setLoader( Loader loader );
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.deepclone.lw.cli.i18n;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings( "serial" )
|
||||
@XStreamAlias( "lw-text-data" )
|
||||
public class TextData
|
||||
implements Serializable
|
||||
{
|
||||
@XStreamImplicit( itemFieldName = "include" )
|
||||
public List< String > includes = new LinkedList< String >( );
|
||||
|
||||
@XStreamImplicit( itemFieldName = "language" )
|
||||
public List< LanguageData > languages = new LinkedList< LanguageData >( );
|
||||
|
||||
|
||||
public void setLoader( Loader loader )
|
||||
{
|
||||
for ( LanguageData l : this.languages ) {
|
||||
l.setLoader( loader );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -38,9 +38,10 @@ public class Server
|
|||
builder.setScope( BeanDefinition.SCOPE_SINGLETON );
|
||||
builder.addPropertyValue( "serviceName" , name );
|
||||
builder.addPropertyValue( "service" , bean );
|
||||
builder.addPropertyValue( "ServiceInterface" , iface.getCanonicalName( ) );
|
||||
builder.addPropertyValue( "serviceInterface" , iface.getCanonicalName( ) );
|
||||
builder.addPropertyValue( "registryPort" , String.valueOf( this.getRmiPort( ) ) );
|
||||
builder.addPropertyValue( "servicePort" , String.valueOf( sPort ) );
|
||||
builder.addPropertyValue( "registryHost" , "localhost" );
|
||||
ctx.registerBeanDefinition( name , builder.getBeanDefinition( ) );
|
||||
}
|
||||
|
||||
|
@ -48,17 +49,31 @@ public class Server
|
|||
private AbstractApplicationContext makeRMIContext( ApplicationContext parent )
|
||||
{
|
||||
GenericApplicationContext context = new GenericApplicationContext( parent );
|
||||
this.addRMIService( context , "termSrv" , parent.getBean( "terminator" ) , ServerTerminator.class , this.getTerminationPort( ) );
|
||||
this.addRMIService( context , "sessionSrv" , parent.getBean( "sessionManager" ) , SessionManager.class , this.getServicePort( ) );
|
||||
this.addRMIRegistry( context );
|
||||
this.addRMIService( context , "termSrv" , parent.getBean( "terminator" ) , ServerTerminator.class , this
|
||||
.getTerminationPort( ) );
|
||||
this.addRMIService( context , "sessionSrv" , parent.getBean( "sessionManager" ) , SessionManager.class , this
|
||||
.getServicePort( ) );
|
||||
context.refresh( );
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
private void addRMIRegistry( GenericApplicationContext context )
|
||||
{
|
||||
BeanDefinitionBuilder builder;
|
||||
builder = BeanDefinitionBuilder.rootBeanDefinition( "org.springframework.remoting.rmi.RmiRegistryFactoryBean" );
|
||||
builder.setScope( BeanDefinition.SCOPE_SINGLETON );
|
||||
builder.addPropertyValue( "port" , String.valueOf( this.getRmiPort( ) ) );
|
||||
builder.addPropertyValue( "alwaysCreate" , "true" );
|
||||
context.registerBeanDefinition( "rmiRegistry" , builder.getBeanDefinition( ) );
|
||||
}
|
||||
|
||||
|
||||
private ApplicationContext makeDataConfigContext( )
|
||||
{
|
||||
String[] dSource = {
|
||||
this.getDataSource( ) ,
|
||||
this.getDataSource( ) ,
|
||||
};
|
||||
FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext( dSource );
|
||||
ctx.refresh( );
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
<import resource="configuration/naming-beans.xml" />
|
||||
<import resource="configuration/simple-beans.xml" />
|
||||
<import resource="configuration/system-beans.xml" />
|
||||
<import resource="configuration/techs-beans.xml" />
|
||||
<import resource="configuration/updates-beans.xml" />
|
||||
<import resource="configuration/user-beans.xml" />
|
||||
|
||||
</beans>
|
||||
|
|
Reference in a new issue