|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DataManager.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Server - Virtual Reality Object Oriented MUD Server
|
|
4 |
Copyright (C) 2001 - 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.server.data;
|
|
28 |
|
|
29 |
import java.io.BufferedReader;
|
|
30 |
import java.io.File;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.PrintWriter;
|
|
33 |
|
|
34 |
import org.vrmoo.common.exception.VRMooParseException;
|
|
35 |
import org.vrmoo.common.util.FileUtility;
|
|
36 |
import org.vrmoo.common.util.ParserUtility;
|
|
37 |
|
|
38 |
import org.vrmoo.server.command.GetNextIDCommand;
|
|
39 |
import org.vrmoo.server.data.update.Update;
|
|
40 |
import org.vrmoo.server.data.update.UpdateFactory;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* This class manages the server data directory. If the server starts up and
|
|
44 |
* the data directory does not exist, copy the "default-data" directory into
|
|
45 |
* the "data" directory. This class also manages version information for the
|
|
46 |
* server data directory. If the data directory is using an old version, this
|
|
47 |
* class will update it to the current version.
|
|
48 |
*
|
|
49 |
* @author Jeff Weston
|
|
50 |
*/
|
|
51 |
public class DataManager |
|
52 |
{ |
|
53 |
/**
|
|
54 |
* The current version of the VRMoo Server data directory.
|
|
55 |
*/
|
|
56 |
private static final int CUR_VERSION = 3; |
|
57 |
|
|
58 |
/**
|
|
59 |
* This class is not meant to be instantiated, so keep the default
|
|
60 |
* constructor private.
|
|
61 |
*/
|
|
62 | 0 |
private DataManager( )
|
63 |
{ |
|
64 |
} |
|
65 |
|
|
66 |
/**
|
|
67 |
* Initialize the data directory for use. Checks for the existence of the
|
|
68 |
* data directory. If it doesn't exist, use the default data directory.
|
|
69 |
* Also checks the version of the data directory. If the data directory is
|
|
70 |
* using an old version, update it. Once done, this method instructs other
|
|
71 |
* classes that rely on the data directory to load their data.
|
|
72 |
*/
|
|
73 | 0 |
public static synchronized void init( ) |
74 |
{ |
|
75 | 0 |
try
|
76 |
{ |
|
77 | 0 |
File dataDir = new File( "data" ); |
78 | 0 |
if ( ! dataDir.exists( ) )
|
79 |
{ |
|
80 | 0 |
System.out.println( "Data directory not found." );
|
81 | 0 |
System.out.println( "Using default data directory." );
|
82 | 0 |
FileUtility.copyDirectory( "default-data", "data" ); |
83 |
} |
|
84 |
} |
|
85 |
catch ( Exception e )
|
|
86 |
{ |
|
87 | 0 |
e.printStackTrace( ); |
88 |
} |
|
89 |
|
|
90 | 0 |
handleUpdates( ); |
91 | 0 |
WorldManager.init( ); |
92 | 0 |
GetNextIDCommand.getInstance( ).loadData( ); |
93 |
} |
|
94 |
|
|
95 |
/**
|
|
96 |
* Instructs all of the classes that have data in the data directory to
|
|
97 |
* save their data to disk. Once that is complete, this method writes out
|
|
98 |
* a data file indicating the version of the data directory.
|
|
99 |
*/
|
|
100 | 0 |
public static synchronized void saveData( ) |
101 |
{ |
|
102 | 0 |
WorldManager.saveData( ); |
103 | 0 |
GetNextIDCommand.getInstance( ).saveData( ); |
104 |
|
|
105 | 0 |
try
|
106 |
{ |
|
107 | 0 |
PrintWriter printWriter = FileUtility.getPrintWriter( |
108 |
"data/version.dat" );
|
|
109 | 0 |
printWriter.println( CUR_VERSION ); |
110 | 0 |
printWriter.close( ); |
111 |
} |
|
112 |
catch ( Exception e )
|
|
113 |
{ |
|
114 | 0 |
e.printStackTrace( ); |
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
/**
|
|
119 |
* Deal with older versions of the data directory that need to be updated.
|
|
120 |
*/
|
|
121 | 0 |
private static void handleUpdates( ) |
122 |
{ |
|
123 | 0 |
try
|
124 |
{ |
|
125 | 0 |
int version = getCurrentVersion( );
|
126 | 0 |
while ( version != CUR_VERSION )
|
127 |
{ |
|
128 | 0 |
Update update = UpdateFactory.createUpdate( version ); |
129 | 0 |
update.update( ); |
130 | 0 |
version = getCurrentVersion( ); |
131 |
} |
|
132 |
} |
|
133 |
catch ( Exception e )
|
|
134 |
{ |
|
135 | 0 |
e.printStackTrace( ); |
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
/**
|
|
140 |
* Get the current version of the VRMoo Server data directory.
|
|
141 |
*
|
|
142 |
* @return the current version of the VRMoo Server data directory
|
|
143 |
*
|
|
144 |
* @throws IOException for any IO errors that occur
|
|
145 |
* @throws VRMooParseException for any parsing errors that occur
|
|
146 |
*/
|
|
147 | 0 |
private static int getCurrentVersion( ) |
148 |
throws IOException, VRMooParseException
|
|
149 |
{ |
|
150 | 0 |
BufferedReader reader = FileUtility.getBufferedReader( |
151 |
"data/version.dat" );
|
|
152 | 0 |
String line = reader.readLine( ); |
153 | 0 |
reader.close( ); |
154 |
|
|
155 | 0 |
return ParserUtility.parseInt( line );
|
156 |
} |
|
157 |
} |
|
158 |
|
|