1 /*
2 * Replica is published under the terms
3 * of the Apache Software License.
4 */
5 package replica.server.commands;
6
7 import java.util.StringTokenizer;
8
9 import org.hsqldb.Server;
10 import org.springframework.jdbc.datasource.DriverManagerDataSource;
11
12 import replica.core.ApplicationMessage;
13
14 /***
15 *
16 * @author Pedro Costa
17 * @author Helder Silva
18 * @since 31/Jan/2004
19 */
20 public class StartupDatabaseCommand extends LoggedInCommand {
21
22 int port = 9001;
23 ShutdownDatabaseCommand shutdownDatabaseCommand;
24 DriverManagerDataSource dataSource;
25 String name = "mydb";
26
27 /***
28 *
29 */
30 public StartupDatabaseCommand() {
31 super();
32 }
33
34 /* (non-Javadoc)
35 * @see replica.server.commands.LoggedInCommand#internalExecute(replica.core.ApplicationMessage)
36 */
37 protected Object internalExecute(ApplicationMessage arg) {
38
39 String c = arg.getBody().toString().trim();
40
41 if( c.endsWith(";") )
42 c = c.substring(0,c.length()-1);
43
44 StringTokenizer tokenizer = new StringTokenizer(c);
45 tokenizer.nextToken(); // ignore
46
47 String sec = tokenizer.nextToken();
48
49 if( sec.equalsIgnoreCase("database") ) // ignore
50 sec = tokenizer.nextToken();
51
52 int port = getPort();
53
54 if( sec.equalsIgnoreCase("ON") ){
55 String portStr = tokenizer.nextToken();
56 if( portStr.equalsIgnoreCase("PORT") ) //ignore
57 portStr = tokenizer.nextToken();
58 try{
59 port = Integer.parseInt(portStr);
60 }
61 catch(NumberFormatException e){
62 return "Invalid port number : [" + portStr + "].";
63 }
64 }
65
66 String name = getName();
67
68 if( tokenizer.hasMoreTokens() ){
69 String nToken = tokenizer.nextToken();
70 if( nToken.equalsIgnoreCase("as") )
71 name = tokenizer.nextToken();
72 }
73
74 Server server = new Server();
75 server.setPort(port);
76
77 if( name != null && name.trim().length() > 0 ){
78 server.putPropertiesFromString("database.0="+name);
79 server.setDatabaseName(0, name);
80 }
81
82 server.start();
83
84 getDataSource().setDriverClassName("org.hsqldb.jdbcDriver");
85 getDataSource().setUrl("jdbc:hsqldb:hsql://localhost:"+port+
86 (name==null?"":"/"+name) );
87
88 getShutdownDatabaseCommand().setServer(server);
89
90 return "Database started on port " + port + ".";
91 }
92
93 /***
94 * @return
95 */
96 public int getPort() {
97 return port;
98 }
99
100 /***
101 * @param i
102 */
103 public void setPort(int i) {
104 port = i;
105 }
106
107 /***
108 * @return
109 */
110 public ShutdownDatabaseCommand getShutdownDatabaseCommand() {
111 return shutdownDatabaseCommand;
112 }
113
114 /***
115 * @param command
116 */
117 public void setShutdownDatabaseCommand(ShutdownDatabaseCommand command) {
118 shutdownDatabaseCommand = command;
119 }
120
121 /***
122 * @return
123 */
124 public DriverManagerDataSource getDataSource() {
125 return dataSource;
126 }
127
128 /***
129 * @param source
130 */
131 public void setDataSource(DriverManagerDataSource source) {
132 dataSource = source;
133 }
134
135 /***
136 * @return
137 */
138 public String getName() {
139 return name;
140 }
141
142 /***
143 * @param string
144 */
145 public void setName(String string) {
146 name = string;
147 }
148
149 }
This page was automatically generated by Maven